Collapsing and Expanding Regions in Visual Studio

Whilst there is still ongoing debate about the merit of using regions, I have to admit that I have no strong leaning towards any side of the argument. However, I do need the ability to hide and show all of the regions in a class quickly and easily.

A trick that I first picked up when using VS2003 still works for me today and can quickly push you towards indifference in the debate as well. 

This article written by Roland Weigelt (in 2003!) lists the code for some macros that can be simply assigned to a keyboard shortcut to help manage any regions you come across. Because, regardless of your stance on the issue, you will always come across regions somewhere.

Below I have assembled a short guide to installing and using the macros -

1. Copy all of the macro source code from the original article but only the module code itself (i.e ignoring the import statements and module declaration - these are added by VS automatically when you create a new module). The code below has been stolen from the original article and reproduced here for convenience

' Toggles the current region surrounding the cursor
    
Sub ToggleParentRegion()
        
Dim objSelection As TextSelection
        objSelection 
DTE.ActiveDocument.Selection
        
Dim objPosition As EnvDTE.TextPoint
        objPosition 
objSelection.AnchorPoint
        objSelection.SelectLine()
        
If (InStr(objSelection.Text.ToLower(), "#region") <> 0Then  ' Updated 14.08.2003
            
objSelection.MoveToPoint(objPosition)
            DTE.ExecuteCommand(
"Edit.ToggleOutliningExpansion")
            DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)
        
ElseIf (objSelection.FindText("#region", vsFindOptions.vsFindOptionsBackwards)) Then
            
DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)
            DTE.ExecuteCommand(
"Edit.ToggleOutliningExpansion")
            DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)
        
Else
            
objSelection.MoveToPoint(objPosition)
        
End If
    End Sub

    
' Expands all regions in the current document
    
Sub ExpandAllRegions()
        DTE.ExecuteCommand(
"Edit.StopOutlining")
        DTE.ExecuteCommand(
"Edit.StartAutomaticOutlining")
    
End Sub

    
' Collapses all regions in the current document
    
Sub CollapseAllRegions()
        ExpandAllRegions()
        
Dim objSelection As TextSelection
        objSelection 
DTE.ActiveDocument.Selection
        objSelection.StartOfDocument()
        
While (objSelection.FindText("#region"))
            objSelection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)
            DTE.ExecuteCommand(
"Edit.ToggleOutliningExpansion")
            objSelection.StartOfDocument()
        
End While
        
DTE.ActiveDocument.Selection.StartOfDocument()
    
End Sub

  
2. In Visual Studio (VS2008 in this example) open the Macros IDE (Alt-F11 or under Tools -> Macros)

3. In the Macros IDE, add a new macros module called RegionTools by right-clicking on MyMacros and select Add -> Add Module

4. Paste the code copied from above inside the module definition

5. Save and the close the Macros IDE

6. Now assign your keyboard shortcuts. Open the Options menu by opening Tools -> Options in Visual Studio

7. Under Environment in the tree menu (left hand side of the Options dialog), select Keyboard

8. Filter all of the available commands by typing RegionTools in the  filter box




9. You should now see the 3 new macros that you added to the Macros IDE. And now you can assign them keyboard shortcuts at your whim. Personally I only use CollapseAllRegions (mapped to Ctrl+Shift+Num -) and ExpandAllRegions (mapped to Ctrl+Shift+Num +). But they are both of enormous benefit.

 Now all that is left is for someone to tell me this has been built in to VS2008!

August 6, 2008 13:34 by steven.burman
E-mail | Permalink | Comments (2) | Comment RSSRSS comment feed

Related posts

Comments

August 23. 2008 21:41

Paul

These Keyboard mapping are already set up for me:
Edit.ToggleOutliningExpansion: CTRL+M CTRL+M
Edit.ToggleAllOutlining: CTRL+M CTRL+L

I didn't use the macros defined in the article, but I assume they do the same thing.

Paul

November 3. 2008 00:40

Pure Krome

They have been built into VS since 2005 at least. I've remapped them to F7 (for no real reason). I can't live without them.

Paul is correct, above. that's the stuff Smile

no need for macro's.

Pure Krome

Comments are closed