Hiding the regions in Visual Studio

后端 未结 7 465
-上瘾入骨i
-上瘾入骨i 2020-12-08 10:19

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# gu

相关标签:
7条回答
  • 2020-12-08 11:01

    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

    0 讨论(0)
  • 2020-12-08 11:07

    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.

    0 讨论(0)
  • 2020-12-08 11:20

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

    0 讨论(0)
  • 2020-12-08 11:21

    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

    0 讨论(0)
  • 2020-12-08 11:21

    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.

    0 讨论(0)
  • 2020-12-08 11:23

    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.

    0 讨论(0)
提交回复
热议问题