In a couple of recent posts, I've all but declared war on #regions. I'm not going to regurgitate my reasons but for those of you who go into "you won't like me when I'm angry" mode when they see a 800-line class condensed down to five, I submit to you not one, but TWO methods for getting rid of them.

Method 1: The VBA way

In this method, I used my newfound knowledge of using regular expressions in Visual Studio's Find and Replace and recorded a macro. Did some clean-up of the code and voila:

    Sub ClearRegions()
        DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
        DTE.Find.FindWhat = "^:b*#region.*n"
        DTE.Find.ReplaceWith = ""
        DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
        DTE.Find.MatchCase = False
        DTE.Find.MatchWholeWord = False
        DTE.Find.MatchInHiddenText = True
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxRegExpr
        DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResultsNone
        DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
        DTE.Find.Execute()

        DTE.Find.FindWhat = "^:b*#endregion.*n"
        DTE.Find.ReplaceWith = ""
        DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
        DTE.Find.MatchCase = False
        DTE.Find.MatchWholeWord = False
        DTE.Find.MatchInHiddenText = True
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxRegExpr
        DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResultsNone
        DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
        DTE.Find.Execute()
    End Sub

To bind a key combination to a macro

  • Click Tools | Options
  • Select Keyboard from the Environment tree node
  • In the search box, type ClearRegions to find the macro
  • Select it from the list
  • Click in the "Press shortcut keys" textbox
  • You can probably take it from here

So you throw that into a Macro Module, bind it to Ctrl+Shift+3, for example, and henceforth, you can use that shortcut to be free of #regions forever.

If I wanted to be *really* anal about it, I'd extract the duplicate code into a separate method. That way, I could re-use it for a macro to, say, remove triple-slash comments (regular expression: "^:b*///.*n" ).

Method 2: The ReSharper Way

This method comes courtesy of Wade Grandoni, a non-blogger-who-should from whom I plan to steal blog post ideas until he takes the hint. When we're done here, removing regions becomes part of the ReSharper reformatting process. I.E. Ctrl+Alt+F (or Ctrl+Alt+Shift+F) and your regions are gone. If it isn't obvious yet, this method requires ReSharper

To achieve this little bit of magic:

  • Go to ReSharper | Options
  • Under Language Options, expand C#, then Formatting Style
  • Select Type Members Layout
  • Uncheck "Use Default Patterns" and DO NOT BE ALARMED AT ALL THE TEXT THAT APPEARS

What you see at this point is, I think, some default patterns that get applied to your classes when you reformat. And the default is to ignore regions for the most part. To change this find default pattern about halfway down. It's the one that looks like this:

    <!--Default pattern-->

    <Pattern>

and change it to this:

    <!--Default pattern-->

    <Pattern RemoveAllRegions="true">

Henceforth, whenever you reformat your code, regions will be removed. N.B.: This works only if you have selected the option to "Reorder type members" in the Reformat dialog:

If you select this option (as well as any others) the first time, it will be remembered every time you Ctrl+Alt+F or Ctrl+Alt+Shift+F from then on.

If you're a little confused as to what the rest of that XML is doing in the default patterns, you could delete the entire thing and replace it with the following:

<?xml version="1.0" encoding="utf-8" ?>

<Patterns xmlns="urn:shemas-jetbrains-com:member-reordering-patterns">

  <Pattern RemoveAllRegions="true">

     <Entry />

  </Pattern>

</Patterns>

This is the bare minimum you need to remove regions from your code when you reformat. What the rest of that XML does requires more reading than I'm willing to put in at 6:30 in the morning.

This method has the advantage of less friction while you're coding in that you don't need to perform any extra steps to remove regions (assuming you reformat before you check-in, which, of course, you do). On the other hand, Method 1 can be adapted to remove other patterns (like triple-slash comments) and, of course, can be used for the ReSharper-less in the crowd. And neither method will allow you to keep any regions that are, in fact, useful.

Kyle the Regional