Skip to content

Fold up Regions Macro for Visual Studio

February 9, 2009

So we wrap everything in regions.  I know some coders scoff at that but we think it keeps the code tidy.  One of the downsides is that there isn’t a keyboard shortcut to collapse all regions without collapsing everything else too.  One of my wonderful co-workers found one.

Here is what you need to do.  Go to Tools/Macros/Macro IDE in Visual Studio.

croppercapture1

Create a new macro and name it RegionTools. (This is important as it has to match the module name.

Drop the following code into it.

Imports EnvDTE
Imports System.Diagnostics
Public Module RegionTools

    Sub ExpandAllRegions()

        Dim objSelection As TextSelection

        DTE.SuppressUI = True
        objSelection = DTE.ActiveDocument.Selection()
        objSelection.StartOfDocument()
        Do While objSelection.FindText(“#region”, vsFindOptions.vsFindOptionsMatchInHiddenText)
           
        Loop
        objSelection.StartOfDocument()
        DTE.SuppressUI = False

        objSelection = Nothing

    End Sub
    Sub CollapseAllRegions()

        Dim objSelection As TextSelection

        ExpandAllRegions()

        DTE.SuppressUI = True
        objSelection = DTE.ActiveDocument.Selection()
        objSelection.EndOfDocument()

        Do While (objSelection.FindText(“#region”, vsFindOptions.vsFindOptionsBackwards))
            DTE.ExecuteCommand(“Edit.ToggleOutliningExpansion”)
            objSelection.EndOfDocument()
        Loop
        objSelection.StartOfDocument()
        DTE.SuppressUI = False
        objSelection = Nothing

    End Sub
End Module

 

 
That done you can map the macro to a proper shortcut as I’ve done using Ctl-Shift–

croppercapture2

Enjoy.

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: