2

As you know, Delphi 2010 has built-in code formatting. However, formatting unfolds all folded code blocks. Any ideas how to fix it? This "feature" is particularly annoying for me and I was really astonished when I couldn't find any mention of this problem. What I've tried:

  • Searching for alternative, e.g., GExperts have code formatter plugin, but it also unfolds code blocks.
  • Tried to write a macro for GExperts: Ctrl+D(format code) -> various code folding/unfolding shortcuts, but couldn't manage to make it respect all folded/unfolded block structures.
  • Write a CnWizards script, but could't find a code folding example.

I would be really grateful for any idea/hint.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467

2 Answers2

3

One way to work around it is to not format the entire file at once, but to select a portion, and format that.

As long as you don't select a region, the expand-state of all regions remains untouched.

Formatting an entire file at once is usually not really a good idea anyway, because the formatter doesn't "understand" what it's doing. I always inspect all changes that it makes, and that's easier when there are no changes outside of the area that's visible.

Wouter van Nifterick
  • 23,603
  • 7
  • 78
  • 122
  • Many thanks for a reply. However, selecting a portion of text and then formatting it still unfolds other blocks of code in Delphi 2010. The same for GExperts code formatter. Which version of Delphi does it work for? – Standard User Oct 31 '13 at 10:22
1

Well, this question is really old, but as it was mentioned here:

How to preserve code folding when formatting source

and there is no answer, I thought I could answer both, here is the answer I posted there:

What you can do is create regions and disable code folding, format code and then reenable the code folding.

To create code regions do:

{$REGION 'Optional text that appears when the code block is folded'} 
// code 
{$ENDREGION}

To toggle code folding option, press Ctrl+Shift K+O.

so, put your code into regions, fold what you want, press Ctrl+Shift K+O to disable the folding, format by pressing Ctrl+D then press Ctrl+Shift K+O again to re enable the folding.

When you re enable the folding, what was folded with a region is going to stay folded.

information source: http://docwiki.embarcadero.com/RADStudio/XE6/en/Using_Code_Folding

I hope this helps you.

Community
  • 1
  • 1