0

See also this question: How do I avoid expanding folded regions when I invoke the code formatter?

This is not a dupe, because I'm interested in solving the problem using the ToolsApi.

When I press CTRL + D the code formats nicely (mostly).
But all my folded code sections get unfolded.
Is there a way to keep these sections folded.

If not, is it possible to save the code folding info prior to formatting, so I can restore it later?
I'm thinking of writing IDE-addin using the Open Tools api.
I'm using XE7, but this problem exists in all versions that have source formatting.

Possible scenario's involve:

  • Record and replay code foldings (hook elide calls).
  • Only allow formatting to work on the current block (redefine the CTRL + D action).
Community
  • 1
  • 1
Johan
  • 74,508
  • 24
  • 191
  • 319
  • I'm guessing this will involve hooking the `elide` calls to built a list of the folded blocks and than walking the source in the editor to restore those blocks. – Johan Apr 10 '15 at 16:20
  • http://stackoverflow.com/q/19691231/62576 – Ken White Apr 10 '15 at 16:21
  • Also consider what happens if the formatting changes line breaks within the code. So just saving the positions/lengths of folded code is not enough, if you don't take into account that they might change value during formatting. – Remy Lebeau Apr 10 '15 at 18:53

1 Answers1

0

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.