可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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...
回答1:
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
回答2:
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
回答3:
Personally, I write a VS macro (Tools > Macro) to expand all regions on file open.
回答4:
I don't know of any plugin like that, honestly. However, with VSTO, it's very easy to write one yourself.
回答5:
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.
回答6:
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.
回答7:
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.