How to preserve code folding when formatting source

自作多情 提交于 2019-12-13 03:59:49

问题


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.



来源:https://stackoverflow.com/questions/29566156/how-to-preserve-code-folding-when-formatting-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!