2

Does anyone know, in Visual Studio 2019 or later, if there's a way to collapse all text but only at the class level? I have many classes within a single document and I simply want to collapse all of the classes at the very top level of the class so that I can then expand the one class I need to work on. Collapsing everything is way too much and makes more work for me than collapsing the code manually.

For example, if I have 5 classes in my file I'd like to collapse the text down to just 5 lines WITHOUT collapsing anything within the classes so that when I manually expand one of the classes I immediately see all the code (uncollapsed) within that class. At the same time I still need to be able to manually collapse at the code level so I cannot disable the "Show outlining for code level constructs" option.

I know about the following key combinations so please don't waste your time suggesting them as they do not do what I am looking for:

Ctrl + M, Ctrl + O -> this is great and I use it frequently by it isn't perfect which is why I'm asking this quesiton.

Ctrl + M, Ctrl + M -> this is a complete waste and collapses everything selected even code sections such as for loops.

Ctrl + M, Ctrl + L -> this is a complete waste without having to select what you want to collapse, it also collapses everything including code sections and creates more work than it is worth.

nucode
  • 51
  • 5

1 Answers1

1

You can use #region and #endregion.

First add #region and #endregion above and below the class, and you can add comments after #region, then the comments you added will be displayed after folding this part of the code. as follows:

 #region class1
 class Class1
 {
     private void test()
     {

     }
 }
 #endregion

Then you can fold and expand according to the +- on the left.

enter image description here

Of course, if you want to try using shortcut keys, you can try the following methods. The first method is simple but it will also fold the methods in the class. The second method is cumbersome to operate but it is very convenient to use shortcut keys.

Method 1:

Follow the steps below:

Tools=>Options=>Text Editor=>C#=>Advanced=>Check Collapse #regions when collapsing to definitions

Then you can achieve batch operations through ctrl+m, ctrl+o, but other content will be collapsed.

Method 2:

Use some plug-ins, for example, you can refer to Collapse all #regions only(!) in C# (Visual Studio)

wenbingeng-MSFT
  • 1,546
  • 1
  • 1
  • 8
  • This is an interesting idea and it would work so to speak but it would also add yet more lines of code to my existing mountain of code. I really don't want to add more lines of code to get around this problem because I'd have to do this for each and every file. That's just not worth it. I briefly looked for plugins but didn't find any which were clear solutions. – nucode May 13 '23 at 01:44
  • @nucode Hello, it seems that only #region can meet your needs at present. But if you want to quickly add #region you can refer to [Automatically create #region with same name at #endregion](https://stackoverflow.com/questions/18122131/automatically-create-region-with-same-name-at-endregion) – wenbingeng-MSFT May 15 '23 at 02:19