Hiding the regions in Visual Studio
Asked Answered
T

7

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...

Thickening answered 13/5, 2009 at 14:0 Comment(10)
So you want to hide the lines with the preprocessor directives #region and #endregion?Pardoner
Yes - not remove them, just make them invisible.Thickening
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?Vito
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.Hatchett
They're not bad if their contents are clearly labelled. Like freezer bags.Tannertannery
@person-b: Until someone ignores the label and shoves peas in with the carrots.Hatchett
@LucasJones I was thinking more like the bags in a morgue.Donn
@YuriyFaktorovich: I can see how that could lead to some unfortunate circumstances.Tannertannery
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.Briefless
@Alex: regions are not a "tool"! They are a waste of code linesStatesmanship
H
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

Hirsutism answered 30/5, 2011 at 18:50 Comment(18)
@NotDan, ah, my apologies. In fact, just tried it out and this does exactly what I want. Very nice!Xuthus
@NotDan, OOOooooOOOooOoooOOO! Very cool! Finally, I can stop bitching to my coworkers who think regions are the greatest thing since sliced bread. :-)Andrien
@NotDan: +1: Any chance you've a version of this for VS 2008?Horselaugh
@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 :(Hirsutism
Thanks anyway mate, we're upgrading to VS2010 in the next few weeks anyway :)Horselaugh
Fantastic. Now if I could just find an extension to hide the auto generated comments /// <param name="instance">The instance.</param> void Insert(Control instance);Lumber
@Lumber 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.Hirsutism
can you post the source for this?Corwun
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.Rudiger
Would be very interested in the source for this as well :)Diametrically
Please open source it so to have it for VS 2013 :)Sapiential
If you press Ctrl+M+L, regions still get collapsed... I wish you could reopen them after this hotkey was pressed =3Erotica
I hate the regions feature too! Thanks for your Visual Studio extension.Pinkiepinkish
@Hirsutism 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.Photostat
@NotDan: I also miss your extension in VS 2017. Please open source your project or release a VS 2017 versionStatesmanship
VS 2017 : Open the .visx file with 7zip, Edit the manifest file and add <VisualStudio Version="15.0"> <Edition>Pro</Edition> </VisualStudio> ---Now install it and ignore the warning...VS2017 compatible :DJoachim
here is a version working with the VS2019 v16.6.0 (using some deprecated API, but still working) marketplace.visualstudio.com/…Intermediary
An alternative for VS 2019 is here: github.com/fsdsabel/ExpandRegions/releasesPellagra
F
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

Federica answered 13/5, 2009 at 15:43 Comment(1)
i constantly use these shortcuts as wellCorwun
W
1

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

Woolsey answered 13/5, 2009 at 14:4 Comment(1)
Could you please post your macro here? Have no idea how to write them but regions drive me crazy already.Heptastich
R
0

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

Rockrose answered 13/5, 2009 at 14:3 Comment(0)
C
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.

Cohdwell answered 13/5, 2009 at 14:9 Comment(1)
This doesn't help if the region is collapsed - in fact, it could make it worse.Hatchett
D
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.

Dray answered 10/2, 2012 at 13:27 Comment(0)
C
-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.

Chericheria answered 1/4, 2012 at 18:35 Comment(1)
Not sure if that's an april fools comment or not... There are better tools for this : red-gate.com/products/dotnet-development/smartassemblyEr

© 2022 - 2025 — McMap. All rights reserved.