Toggle all regions in Visual Studio

Visual Studio already has some keystrokes to collapse and expand the regions in your code. The problem is, none of them do the work only with the regions.

Again, a Macro will help us to improve Visual Studio environment.

Open the Macros IDE choosing Tools -> Macros -> Macros IDE…
Create a new Module and replace all the code pasting the one below:

Imports EnvDTE
Imports System.Diagnostics
' Macros for improving keyboard support for "#region ... #endregion"
Public Module RegionTools
    ' Expands all regions in the current document
    Sub ExpandAllRegions()

        Dim objSelection As TextSelection ' Our selection object

        DTE.SuppressUI = True ' Disable UI while we do this
        objSelection = DTE.ActiveDocument.Selection() ' Hook up to the ActiveDocument's selection
        objSelection.StartOfDocument() ' Shoot to the start of the document

        ' Loop through the document finding all instances of #region. This action has the side benefit
        ' of actually zooming us to the text in question when it is found and ALSO expanding it since it
        ' is an outline.
        Do While objSelection.FindText("#region", vsFindOptions.vsFindOptionsMatchInHiddenText)
            ' This next command would be what we would normally do *IF* the find operation didn't do it for us.
            'DTE.ExecuteCommand("Edit.ToggleOutliningExpansion")
        Loop
        objSelection.StartOfDocument() ' Shoot us back to the start of the document
        DTE.SuppressUI = False ' Reenable the UI

        objSelection = Nothing ' Release our object

    End Sub

    ' Collapses all regions in the current document
    Sub CollapseAllRegions()

        Dim objSelection As TextSelection ' Our selection object

        ExpandAllRegions() ' Force the expansion of all regions

        DTE.SuppressUI = True ' Disable UI while we do this
        objSelection = DTE.ActiveDocument.Selection() ' Hook up to the ActiveDocument's selection
        objSelection.EndOfDocument() ' Shoot to the end of the document

        ' Find the first occurence of #region from the end of the document to the start of the document. Note:
        ' Note: Once a #region is "collapsed" .FindText only sees it's "textual descriptor" unless
        ' vsFindOptions.vsFindOptionsMatchInHiddenText is specified. So when a #region "My Class" is collapsed,
        ' .FindText would subsequently see the text 'My Class' instead of '#region "My Class"' for the subsequent
        ' passes and skip any regions already collapsed.
        Do While (objSelection.FindText("#region", vsFindOptions.vsFindOptionsBackwards))
            DTE.ExecuteCommand("Edit.ToggleOutliningExpansion") ' Collapse this #region
            objSelection.EndOfDocument() ' Shoot back to the end of the document for
            ' another pass.
        Loop
        objSelection.StartOfDocument() ' All done, head back to the start of the doc
        DTE.SuppressUI = False ' Reenable the UI

        objSelection = Nothing ' Release our object

    End Sub
End Module

Cleaning extra white spaces in Visual Studio

Sometimes, the Visual Studio’s text editor doesn’t do a perfect job formatting your code.

Fortunately, with its built Macros system we can help it to help us. By creating a simple Macro, we can eliminate those annoying extra spaces that insist showing off.

First, let’s open the Macro IDE, found at Tools -> Macros -> Macros IDE…
Open the EnviromentEvents file and paste the code below anywhere before the End Module:

    Private saved As Boolean = False    

    Private Sub DocumentEvents_DocumentSaved(ByVal document As EnvDTE.Document) _
         Handles DocumentEvents.DocumentSaved
        If Not saved And Not DTE.ActiveDocument.Name.ToLower().EndsWith("aspx") Then
            Try
                ' Remove all the trailing whitespaces.
                DTE.Find.FindReplace(vsFindAction.vsFindActionReplaceAll, _
                  ":Zs#$", _
                  vsFindOptions.vsFindOptionsRegularExpression, _
                  String.Empty, _
                  vsFindTarget.vsFindTargetCurrentDocument, , , _
                  vsFindResultsLocation.vsFindResultsNone)
                saved = True
                document.Save()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Trim White Space exception")
            End Try
        Else
            saved = False
        End If
    End Sub

This code won’t affect your aspx files. When you run the macro on that kind of file, the colors highlighting get screwed.