2

In VisualStudio, when you write a function it can collapse to outlines, everyone knows it. Also you can use #pragma region and #pragma endregion to manually specify a code region so that it can collapse. However I found a problem that, if any collapsed code blocks are within the #pragma region, they cannot collapse anymore. Is there any way to solve it?

<code>funcA()</code>,<code>funcB()</code>, <code>funcC()</code> can be collapsed

After I add <code>#pragma region</code>, they cannot collapse independently.

C. Wang
  • 2,516
  • 5
  • 29
  • 46

2 Answers2

9

There are several other also better and flexible ways than #pragma region you can do to collapse code in VS:

Method 1: Use {...} instead which natively supports code collapsing in VS.

  1. Enable option: Tools->Text Editor->C/C++->Formatting->OutLine Statement Blocks->True.

  2. Put your in different scopes {...}, then it will collapse the code in different scopes:

    enter image description here


Method 2: use keyboard shortcuts to collapse code you want:

  • CTRL + M + O will collapse all.

  • CTRL + M + L will expand all.

  • CTRL + M + P will expand all and disable outlining.

  • CTRL + M + M will collapse/expand the current section.


It is worthy noting that method #1 works better for plain code snippets. For functions, because similar to that we cannot have functions inside functions in C++, we cannot put functions in scope defined by {...}. Fortunately, we can still use method #1 for functions by creating namespaces for each scope. Or simply, use method #2.

Community
  • 1
  • 1
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
  • I was just wondering, how few functions can resides under { } ? Don't it screw other formatting? – Digital_Reality Jan 09 '14 at 11:03
  • @Digital_Reality This method works better for plain code snippets, not for functions, similar for that [we cannot have functions inside functions in C++](http://stackoverflow.com/questions/4324763/c-can-we-have-functions-inside-functions). What do you mean by `screw other formatting`? – herohuyongtao Jan 09 '14 at 11:20
  • Well, OPs question was specific to functions inside his region. So I guess does not suits to his needs. But good to know, Thanks! – Digital_Reality Jan 09 '14 at 11:24
  • @Digital_Reality For functions, better to use method 2. – herohuyongtao Jan 09 '14 at 11:28
  • @Digital_Reality Updated by adding how to use method #1 for functions. – herohuyongtao Jan 09 '14 at 13:11
0

Yeah that problem exist

One workaround which sometimes works is to use outlining -> collapse to definitions, then re-expand each block.

Digital_Reality
  • 4,488
  • 1
  • 29
  • 31