Geeks With Blogs
Brian Schroer Don't Call Me Mort!

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 | Back to top


Comments on this post: Visual Studio "Clear Regions" macro

# re: Visual Studio "Clear Regions" macro
Requesting Gravatar...
Sweet! Nice addition.
Left by Kyle Baley on May 13, 2008 10:22 PM

# re: Visual Studio "Clear Regions" macro
Requesting Gravatar...
Nice AddOn, I liked using it in VS2008, unfortunately it doesn't work anymore in VS2010. As I am not too much into VS macro programming I don't know why. Do you have any ideas what to change to make it working?

One other thing. Often there are empty lines in front and behind a #region line. If you remove the region, you end up with 2 free lines. Could be cool if the macro also could determine this situation and remove one of the 2 free lines. I guess it shouldn't be too difficult, probably you just look for two "\n" in a row and replace it by one. Unfortunately I can't test it, because it's not running in VS2010 at all.
Left by Stefan Barthel on May 19, 2010 6:29 AM

# re: Visual Studio "Clear Regions" macro
Requesting Gravatar...
Looks like you can't replace with nothing anymore in VS2010, that's why it does not work.

I tried also the replacing \n\n with \n, but it doesn't work as expected, as it also removes single empty lines sometimes. Neither works \n\n\n -> \n\n, there again some other chars seem to be in between...
Left by Stefan Barthel on May 19, 2010 7:31 AM

# re: Visual Studio "Clear Regions" macro
Requesting Gravatar...
Finally... So I changed your script to replace with Environment.NewLine instead of "" and added three times the following code. Not most elegant but it mostly does the job... (doesn't remove the very first and the very last empty line, didn't find a matching expression for that...):

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics

Public Module RegionTools
' 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
Dim saveFindWhat = .FindWhat

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

.Execute()

.Action = vsFindAction.vsFindActionReplaceAll
.FindWhat = Environment.NewLine + Environment.NewLine + Environment.NewLine
.ReplaceWith = Environment.NewLine + Environment.NewLine
.Target = vsFindTarget.vsFindTargetCurrentDocument
.MatchCase = False
.MatchWholeWord = False
.MatchInHiddenText = True
.ResultsLocation = vsFindResultsLocation.vsFindResultsNone
.Action = vsFindAction.vsFindActionReplaceAll

.Execute()

.Action = vsFindAction.vsFindActionReplaceAll
.FindWhat = Environment.NewLine + Environment.NewLine + Environment.NewLine
.ReplaceWith = Environment.NewLine + Environment.NewLine
.Target = vsFindTarget.vsFindTargetCurrentDocument
.MatchCase = False
.MatchWholeWord = False
.MatchInHiddenText = True
.ResultsLocation = vsFindResultsLocation.vsFindResultsNone
.Action = vsFindAction.vsFindActionReplaceAll

.Execute()

.Action = vsFindAction.vsFindActionReplaceAll
.FindWhat = Environment.NewLine + Environment.NewLine + Environment.NewLine
.ReplaceWith = Environment.NewLine + Environment.NewLine
.Target = vsFindTarget.vsFindTargetCurrentDocument
.MatchCase = False
.MatchWholeWord = False
.MatchInHiddenText = True
.ResultsLocation = vsFindResultsLocation.vsFindResultsNone
.Action = vsFindAction.vsFindActionReplaceAll

.Execute()

.Target = saveTarget
.MatchCase = saveMatchCase
.MatchWholeWord = saveMatchWholeWord
.MatchInHiddenText = saveMatchInHiddenText
.PatternSyntax = savePatternSyntax
.FindWhat = saveFindWhat
End With
End Sub
End Module
Left by Stefan Barthel on May 19, 2010 8:06 AM

# re: Visual Studio "Clear Regions" macro
Requesting Gravatar...
parameters don't need to be set if they have already been set once and you don't need to change them. After the first Execute(), you could delete any more occurences of these:

.Action = vsFindAction.vsFindActionReplaceAll
.Target = vsFindTarget.vsFindTargetCurrentDocument
.MatchCase = False
.MatchWholeWord = False
.MatchInHiddenText = True
.ResultsLocation = vsFindResultsLocation.vsFindResultsNone
.Action = vsFindAction.vsFindActionReplaceAll

Left by Tom Morris on Jan 05, 2011 3:52 PM

Your comment:
 (will show your gravatar)


Copyright © Brian Schroer | Powered by: GeeksWithBlogs.net