Brian Schroer

Don't Call Me Mort!

  Home  |   Contact  |   Syndication    |   Login
  30 Posts | 0 Stories | 20 Comments | 10 Trackbacks

News

Tag Cloud


Archives

Post Categories

Links

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

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

Feedback

# re: Visual Studio "Clear Regions" macro 5/13/2008 10:22 PM Kyle Baley
Sweet! Nice addition.

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 6 and 2 and type the answer here: