简体   繁体   English

在Visual Studio中隐藏区域

[英]Hiding the regions in Visual Studio

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: 我知道VS代码折叠问题是一个老板栗,但我在我浏览的所有其他讨论中都找不到这个:

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. 我们有一个C#团队,一些爱区和其他人讨厌他们,我们似乎没有多少中间立场可以合作。

Is there a plug- or add-in for VS that will just 'hide' the regions? 是否有VS的插件或插件才会“隐藏”这些区域? 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. 那些想要它们的人会看到它们正常,但安装加载项的人和查看.cs文件的区域就不存在,好像它们不存在一样。

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... 在移动代码时我可以看到这可能是一个问题,它可能会导致某些方法出现在错误区域之内或之外,但这可能是团队满意的权衡...

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 : 我终于写了一篇名为I Hate #Regions的文章

Make #regions suck less (for free): 让#regions减少(免费):

http://visualstudiogallery.msdn.microsoft.com/0ca60d35-1e02-43b7-bf59-ac7deb9afbca 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) 为#region / #end区域线提供更小,更轻的背景,使它们不那么明显(也是一个选项)
  • Works in C# and VB 适用于C#和VB

区域工具截图

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 请参阅代码折叠问题

就个人而言,我编写了一个VS宏(工具>宏)来扩展文件打开的所有区域。

I don't know of any plugin like that, honestly. 老实说,我不知道这样的插件。 However, with VSTO , it's very easy to write one yourself. 但是,使用VSTO ,自己编写一个很容易。

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. 您不会看到其他预处理器关键字。

For VS 2008 users, you can stop outlining and restart it programatically (via a macro). 对于VS 2008用户,您可以以编程方式(通过宏)停止概述和重新启动它。 I found this code at http://weblogs.asp.net/rweigelt/archive/2003/07/06/9741.aspx 我在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; 以上适用于C#IDE; for some reason, VB's IDE doesn't remove outlining with regions when explicitly told to. 出于某种原因,VB的IDE在明确告知时不会删除区域的大纲。 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. 这是一个适用于Visual Studio 2008中的Visual Basic .NET。它有点蛮力,但想法是评论所有#region标记,扩展轮廓。 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). 下一步取消对它们的注释,将它们恢复到原始状态(严格来说不是真的......可能在代码的其他部分使用#region的流氓)。 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. 在VS2008中,可能还有一些其他方法可以实现VB。 I'll post as I find them. 我会发现它们发布它们。

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 我非常喜欢Regions,我写了一个名为Regionizer的程序,它是一个位于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). 尝试查找没有此工具(或区域)的代码就是我所说的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. 我一直在寻找一种简单的方法来折叠和扩展所有区域,并且MS告诉我们这些区域是.suo文件的一部分(与项目或解决方案同名)并且没有一种简单的方法来获取句柄在这些地区反对他们自己。

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. 我已经搜索了整个DTE并且我没有找到折叠所有区域或扩展所有区域的方法,我将尝试宏代码并查看它是否有效。

Will post an update shortly. 将很快发布更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM