visual-studioblockshortcutbracescode-folding

Collapsing all inner braces in Visual Studio 2019


Suppose we have something like this:

{                      // 1
    A();
    {                  // 1.1
        B();
        {              // 1.1.1
            {          // 1.1.1.1
                D();
            }
            X();
            {          // 1.1.1.2
                E();
            }
        }
        C();
        {              // 1.1.2
            F();
        }
    }
}

And we'd like to collapse block 1.1.1 along with all the blocks in it to get:

{                      // 1
    A();
    {                  // 1.1
        B();
        {...}          // 1.1.1
        //  {...}         1.1.1.1 also collapsed in 1.1.1
        //  X();
        //  {...}         1.1.1.2 also collapsed in 1.1.2
        C();
        {              // 1.1.2
            F();
        }
    }
}

Is there a way to do it quickly (esp. in case of multiple layers of sub-blocks)?

I know of CTRL+M+L, which I tend to use quite often, and would love to learn more.

Have a good day.


Solution

  • The feauture I was looking for was in Edit.Outlining.Collapse All in:

    It seems to not have a shortcut assigned by default, so

    1. I went to Tools.Options...

    2. Then in the left panel select Environment > Keyboard

    3. Find Edit.CollapseAllincurrentblock on the right

    4. In editbox "Press shortcut keys", pressed CTRL+M, CTRL+B in order

    5. In "Use new shortcut in:", I used Global

    6. Click Assign

    7. Done.

    Now, if you're in a block, clicking CTRL+M followed by CTRL+B should collapse all blocks in current block. The only missing part is that the current block itself remains expanded, but that can be fixed with CTRL+M, CTRL+S (collapse current region, which collapses the current block, but not inner blocks).