How to preserve code folding when formatting sourc

2019-09-11 05:12发布

问题:

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).

回答1:

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.