Visual Studio "Clear Regions" macro

A while ago, Kyle Baley shared a Visual Studio macro to remove #regions.

I've very much enjoyed using it to clean up overly-regioned code, but it did have one problem: The macro macro used regular expressions to find and replace, and the next time you use the Visual Studio "Find" dialog after running the macro, the "use regular expressions" box is still checked, which can mess up some searches.

I made an update to save the search options, and restore them after the find/replace is complete so that doesn't happen:

' Get rid of regions (but not the enclosed code)
Sub ClearRegions()
    With DTE.Find
        Dim saveTarget As vsFindTarget = .Target
        Dim saveMatchCase As Boolean = .MatchCase
        Dim saveMatchWholeWord As Boolean = .MatchWholeWord
        Dim saveMatchInHiddenText As Boolean = .MatchInHiddenText
        Dim savePatternSyntax As vsFindPatternSyntax = .PatternSyntax

        .Action = vsFindAction.vsFindActionReplaceAll
        .FindWhat = "^:b*\#region.*\n"
        .ReplaceWith = ""
        .Target = vsFindTarget.vsFindTargetCurrentDocument
        .MatchCase = False
        .MatchWholeWord = False
        .MatchInHiddenText = True
        .PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxRegExpr
        .ResultsLocation = vsFindResultsLocation.vsFindResultsNone
        .Action = vsFindAction.vsFindActionReplaceAll

        .Execute()

        .FindWhat = "^:b*\#endregion.*\n"
        .ReplaceWith = ""
        .Target = vsFindTarget.vsFindTargetCurrentDocument
        .MatchCase = False
        .MatchWholeWord = False
        .MatchInHiddenText = True
        .PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxRegExpr
        .ResultsLocation = vsFindResultsLocation.vsFindResultsNone
        .Action = vsFindAction.vsFindActionReplaceAll

        .Execute()

        .Target = saveTarget
        .MatchCase = saveMatchCase
        .MatchWholeWord = saveMatchWholeWord
        .MatchInHiddenText = saveMatchInHiddenText
        .PatternSyntax = savePatternSyntax
    End With
End Sub

Print | posted on Sunday, May 11, 2008 11:16 PM

Feedback

# re: Visual Studio "Clear Regions" macro

left by Kyle Baley at 5/13/2008 10:22 PM Gravatar
Sweet! Nice addition.
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: