26

I know VS code folding issues are an old chestnut, but I haven't been able to find this in all the other discussions I have browsed through:

We have a team of C# guys, some love regions and others hate them and we don't seem to have much middle ground to work with.

Is there a plug- or add-in for VS that will just 'hide' the regions? So that those that want them will see them as normal, but the people that install the add-in and view a .cs file the regions just aren't there, as if they don't exist.

I can see this might be an issue when moving code around that it might cause issues of certain methods being in or outside of the wrong region, but that might be a tradeoff the team is happy with...

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
KevinT
  • 3,094
  • 4
  • 26
  • 29
  • So you want to hide the lines with the preprocessor directives #region and #endregion? – Merus May 13 '09 at 14:04
  • 1
    Yes - not remove them, just make them invisible. – KevinT May 13 '09 at 14:06
  • 52
    Ugh, regions. Kings of code obfuscation. Deans of dire design. Oh how thou hast scorned me with thine presence. But for a moment to brush thee aside, leave the code open wide, for a browse or debuggery, yet with you, skullduggery. Ugh, regions. Foul captains of construction. Proud champions of chance. With thine secrets you hide, and all your insides. Hath you properties, say you? Yet a method or two, are well hidden within, where should I begin. Merchants of misuse. Avatars of abuse. Regions of reclus-ive code are thee. Ugh, regions, may your use be sparse and your number, few. Ugh, regions. – Jeff Yates May 13 '09 at 14:41
  • 3
    They're not bad if their contents are clearly labelled. Like freezer bags. – Lucas Jones May 13 '09 at 19:20
  • 6
    @person-b: Until someone ignores the label and shoves peas in with the carrots. – Jeff Yates May 20 '09 at 18:13
  • 3
    @LucasJones I was thinking more like the bags in a morgue. – Yuriy Faktorovich Jul 03 '12 at 18:29
  • 1
    @YuriyFaktorovich: I can see how that could lead to some unfortunate circumstances. – Lucas Jones Jul 04 '12 at 16:03
  • 3
    Honestly though, regions are a tool, and you can use it or misuse it. Don't hate the hammer just because your team is using it to open tin cans. – Alex May 24 '15 at 12:55
  • I used to like and use regions, but now I've seen the light! If you find yourself wanting to hide code, then surely it belongs in another class, or needs reorganising in some way? – Mitch Wheat May 13 '09 at 14:08
  • @Alex: regions are not a "tool"! They are a waste of code lines – Alexxus Sep 28 '18 at 04:56

7 Answers7

63

I hate regions (my team loves them) and was surprised to find that nobody has written an extension to make them better. I finally wrote one myself called I Hate #Regions:

Make #regions suck less (for free):

http://visualstudiogallery.msdn.microsoft.com/0ca60d35-1e02-43b7-bf59-ac7deb9afbca

  • Auto Expand regions when a file is opened
  • Optionally prevent regions from being collapsed (but still be able to collapse other code)
  • Give the #region / #end region lines a smaller, lighter background so they are less noticeable (also an option)
  • Works in C# and VB

Region Tool Screenshot

Tor
  • 1,522
  • 3
  • 16
  • 26
NotDan
  • 31,709
  • 36
  • 116
  • 156
  • @NotDan, ah, my apologies. In fact, just tried it out and this does exactly what I want. Very nice! – Kirk Woll May 30 '11 at 20:40
  • @NotDan, OOOooooOOOooOoooOOO! Very cool! Finally, I can stop bitching to my coworkers who think regions are the greatest thing since sliced bread. :-) – Nick Spreitzer Jun 01 '11 at 15:49
  • @NotDan: +1: Any chance you've a version of this for VS 2008? – Binary Worrier Jun 07 '11 at 12:43
  • 1
    @Binary Worrier: Unfortunately not, most of the code uses the new VS2010 extensibility APIs. I'm sure it could be done for VS2008 but it would be completely different code and not an easy port :( – NotDan Jun 07 '11 at 18:35
  • Thanks anyway mate, we're upgrading to VS2010 in the next few weeks anyway :) – Binary Worrier Jun 07 '11 at 20:05
  • Fantastic. Now if I could just find an extension to hide the auto generated comments /// The instance. void Insert(Control instance); – mcintyre321 Apr 10 '12 at 15:31
  • @mcintyre321 ohhhhhh... good idea! We don't have too much of that so I don't have to deal with it often.. but I do hate those too! I'll keep that in mind for future enhancement. – NotDan Apr 10 '12 at 16:31
  • 2
    can you post the source for this? – Jake Berger Apr 27 '12 at 22:04
  • Can't a #region-line collapse to a one-pixel high line? That case, you still see there is a region. And if you move your mouse near, you get some sort of handle, so you toggle the display. – doekman Dec 03 '12 at 14:27
  • Would be very interested in the source for this as well :) – David Cumps Feb 01 '13 at 12:16
  • 1
    Please open source it so to have it for VS 2013 :) – marcob Apr 11 '14 at 09:58
  • If you press Ctrl+M+L, regions still get collapsed... I wish you could reopen them after this hotkey was pressed =3 – Ray Oct 13 '15 at 09:55
  • 1
    I hate the regions feature too! Thanks for your Visual Studio extension. – Colonel Panic Jan 29 '16 at 12:02
  • 5
    @NotDan I know this is getting old, but can you make an updated extension for VS 2017 or make it possible for other to create a pull request to make that happen? I still hate the way regions collapse to hide the code on me. I want to see all that code even the ugly bits so it can't hide from my eyes. – Jeremy Jul 11 '17 at 14:37
  • 1
    @NotDan: I also miss your extension in VS 2017. Please open source your project or release a VS 2017 version – Alexxus Sep 28 '18 at 05:00
  • 1
    VS 2017 : Open the .visx file with 7zip, Edit the manifest file and add Pro ---Now install it and ignore the warning...VS2017 compatible :D – Sushant VS Nov 20 '18 at 14:38
  • here is a version working with the **VS2019** v16.6.0 (using some deprecated API, but still working) https://marketplace.visualstudio.com/items?itemName=Gweltaz27.IHateRegions – chviLadislav Oct 09 '20 at 13:46
  • An alternative for **VS 2019** is here: https://github.com/fsdsabel/ExpandRegions/releases – rustyx Mar 31 '21 at 15:13
16

There are shortcut keys to deal with them:

Ctrl+M, Ctrl+M  Collapse or expand the block you're currently in.
Ctrl+M, Ctrl+O  Collapse all blocks in the file
Ctrl+M, Ctrl+L  Expand all blocks in the file
Ctrl+M, Ctrl+P  Stop outlining mode. (Ctrl+M, Ctrl+O resumes) 

See The Problem With Code Folding

Cœur
  • 37,241
  • 25
  • 195
  • 267
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
1

Personally, I write a VS macro (Tools > Macro) to expand all regions on file open.

Alexxus
  • 893
  • 1
  • 11
  • 25
x0n
  • 51,312
  • 7
  • 89
  • 111
  • 3
    Could you please post your macro here? Have no idea how to write them but regions drive me crazy already. – User Jul 06 '09 at 14:47
0

I don't know of any plugin like that, honestly. However, with VSTO, it's very easy to write one yourself.

Mike Caron
  • 5,674
  • 4
  • 48
  • 71
0

This is somewhat... sleazy, and may have side-effects but:

Tools-->Options-->Environment-->Fonts and Colors-->Preprocessor Keyword Change the foreground and background colours to white (or whatever your default background is).

You wont see other preprocessor keywords though.

BrianH
  • 1,082
  • 1
  • 16
  • 23
0

For VS 2008 users, you can stop outlining and restart it programatically (via a macro). I found this code at http://weblogs.asp.net/rweigelt/archive/2003/07/06/9741.aspx

Imports EnvDTE

' Expands all regions in the current document    
Sub ExpandAllRegions()        
    DTE.ExecuteCommand("Edit.StopOutlining")        
    DTE.ExecuteCommand("Edit.StartAutomaticOutlining")    
End Sub

The above works for C# IDE; for some reason, VB's IDE doesn't remove outlining with regions when explicitly told to. Hope this helps!

-- appended edit:

Here's one that does work for Visual Basic .NET in Visual Studio 2008. It's a bit of brute force, but the idea is to comment all of the #region markers, expanding the outline. The next step uncomments them, returning them to their original state (not strictly true... could have rogue uses of #region in other parts of the code). This code doesn't return the Find & Replace dialog back to it's original state, either. With those caveats, this is the macro module that will perform this chore:

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module MacroMod01
    Sub ExpandAllRegions()
    ' comment out all #region occurances
        DTE.ExecuteCommand("Edit.Replace")
        DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
        DTE.Find.FindWhat = "#region"
        DTE.Find.ReplaceWith = "'#region"
        DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
        DTE.Find.MatchCase = False
        DTE.Find.MatchWholeWord = False
        DTE.Find.MatchInHiddenText = True
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
        DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResultsNone
        DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
        DTE.Find.Execute()

    ' uncomment all #region occurances
        DTE.ExecuteCommand("Edit.Replace")
        DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
        DTE.Find.FindWhat = "'#region"
        DTE.Find.ReplaceWith = "#region"
        DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
        DTE.Find.MatchCase = False
        DTE.Find.MatchWholeWord = False
        DTE.Find.MatchInHiddenText = True
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
        DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResultsNone
        DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
        DTE.Find.Execute()

    'close the find 'n replace dialog
        DTE.Windows.Item("{CF2DDC32-8CAD-11D2-9302-005345000000}").Close()  

    End Sub

End Module

There may be a few other methods to accomplish this for VB in VS2008. I'll post as I find them.

sscheider
  • 522
  • 5
  • 14
-5

I think it is funny anyone that hates Regions. I love Regions so much I wrote a program called Regionizer, which is an open source project located at http://regionizer.codeplex.com

I have had bosses and some team members who hate them, but my tool organizes all code alphabetically, so methods are alphabetically sorted in the Methods Region, Properties are sorted in the Properties Region, and Events are sorted in the Events region.

Try to find code without this tool (or Regions) is what I call Spaghetti Code).

I have been searching for an easy way to collapse and expand all regions and have been told by MS that the regions are part of the .suo file (same name as the project or solution) and there is not an easy way to get a handle on the regions object them selves.

I have searched all through the DTE and I haven't found a way to collapse all or Expand all regions, I am going to try the Macro code and see if it works.

Will post an update shortly.

user1054326
  • 318
  • 1
  • 3
  • 2
    Not sure if that's an april fools comment or not... There are better tools for this : http://www.red-gate.com/products/dotnet-development/smartassembly/ – Stéphane Jun 29 '12 at 09:32