Visual Studio C# 语句折叠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/285422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 21:39:20  来源:igfitidea点击:

Visual Studio C# statement collapsing

c#visual-studio-2008idetext-editoroutlining

提问by Grank

When editing really long code blocks (which should definitely be refactored anyway, but that's beyond the scope of this question), I often long for the ability to collapse statement blocks like one can collapse function blocks. That is to say, it would be great if the minus icon appeared on the code outline for everything enclosed in braces. It seems to appear for functions, classes, regions, namespaces, usings, but not for conditional or iterative blocks. It would be fantastic if I could collapse things like ifs, switches, foreaches, that kind of thing!

在编辑非常长的代码块时(无论如何都应该重构,但这超出了本问题的范围),我经常渴望能够像折叠功能块那样折叠语句块。也就是说,如果括号中的所有内容的代码大纲上都出现减号图标,那就太好了。它似乎适用于函数、类、区域、命名空间、使用,但不适用于条件块或迭代块。如果我能折叠诸如 ifs、switchs、foreaches 之类的东西,那就太棒了!

Googling into that a bit, I discovered that apparently C++ outlining in VS allows this but C# outlining in VS does not. I don't really get why. Even notepad++ will so these collapses if I select the C# formatting, so I don't get why Visual Studio doesn't.

谷歌搜索了一下,我发现 VS 中的 C++ 大纲显然允许这样做,但 VS 中的 C# 大纲不允许。我真的不明白为什么。如果我选择 C# 格式,即使 notepad++ 也会使这些崩溃,所以我不明白为什么 Visual Studio 没有。

Does anyone know of a VS2008 add-in that will enable this behavior? Or some sort of hidden setting for it?

有谁知道可以启用此行为的 VS2008 加载项?或者它的某种隐藏设置?

Edited to add: inserting regions is of course an option and it did already occur to me, but quite frankly, I shouldn't have to wrap things in a region that are already wrapped in braces... if I was going to edit the existing code, I would just refactor it to have better separation of concern anyway. ("wrapping" with new methods instead of regions ;)

编辑添加:插入区域当然是一个选项,我确实已经想到了,但坦率地说,我不应该将东西包装在已经用大括号包裹的区域中......如果我要编辑现有代码,我只会重构它以更好地分离关注点。(用新方法而不是区域“包装”;)

采纳答案by Skyblade

Starting with Visual Studio 2017, statement collapsing is built-in.

从 Visual Studio 2017 开始,语句折叠是内置的。

There are several extensions that perform this task for pre-2017 versions of VS, starting with VS 2010 version:

从 VS 2010 版本开始,有几个扩展可以为 2017 之前版本的 VS 执行此任务:

The last extension supports only VS 2015 and VS 2017, but it's the most powerful one.
It supports syntax coloring inside collapsed blocks, it is more fault-tolerant and optimized.

最后一个扩展仅支持 VS 2015 和 VS 2017,但它是最强大的。
它支持折叠块内的语法着色,更容错和优化。

If the extension doesn't seem to install after you used a browser to download it, try using the built-in Visual Studio extension manager.

如果使用浏览器下载扩展后似乎没有安装它,请尝试使用内置的 Visual Studio 扩展管理器。

回答by Kon

I'm not aware of add-ins, but you mentioned regions and I see nothing wrong with doing something like this...

我不知道加载项,但你提到了地区,我认为做这样的事情没有错......

foreach (Item i in Items)
{
  #region something big happening here
  ...
  #endregion

  #region something big happening here too
  ...
  #endregion

  #region something big happening here also
  ...
  #endregion
}

EDIT: In response to the question's EDIT: You're right, sticking a bunch of regions everywhere isn't ideal and refactoring is probably the way to go. But it seems that you're looking for something magical that will "organize" the code for you, and I don't think that exists.

编辑:回应问题的编辑:你说得对,在任何地方都粘贴一堆区域并不理想,重构可能是要走的路。但似乎您正在寻找可以为您“组织”代码的神奇东西,而我认为这不存在。

回答by Chris Marisic

Visual Studio 2008 supports regions inside of functions as long as you keep them in the same code hierarchical level

Visual Studio 2008 支持函数内部的区域,只要您将它们保持在相同的代码层次结构级别

#region Won't work
for(int i = 0; i<Count; i++)
{
//do something
#endregion
}

for(int i=0; i<Count; i++)
{
#region Works fine
//do lots of stuff
#endregion
}

回答by Matt Brunell

You can collapse specific blocks of text within visual studio, but you have to turn off automatic outlining.

您可以在 Visual Studio 中折叠特定的文本块,但必须关闭自动大纲。

Right click in your code window and select (Outlining | Stop Outlining)

右键单击代码窗口并选择(大纲 | 停止大纲)

Then, select some text, right click and select (Outlining | Hide Selection)

然后,选择一些文本,右键单击并选择(大纲|隐藏选择)

When you turn on automatic outlining again, your custom "Regions" will no longer collapse.

当您再次打开自动大纲时,您的自定义“区域”将不再折叠。

回答by Vin

I will add here that in VS 2010 Microsoft has added WPF adorner capabilities using Managed Extensibility Framework (MEF), this will allow us to extend the source code editor to organize them in a much better way to make it more readable and accessible.

我将在这里补充一点,在 VS 2010 中,Microsoft 使用托管可扩展性框架 (MEF) 添加了 WPF 装饰器功能,这将允许我们扩展源代码编辑器,以更好的方式组织它们,使其更具可读性和可访问性。

For instance the Summary Comments visualizer that Scott Gu demoed at PDC 2008.

例如,Scott Gu 在 PDC 2008 上演示的 Summary Comments 可视化工具。

So look forward to a better tomorrow for developers :)

所以期待开发者的明天更美好:)

回答by Vin

# region ,#endregion is the smart option.

# region ,#endregion 是智能选项。

回答by Sam Harwell

This feature has been added to Visual Studio 2010's C# editor. I can't find the source verifying it was actually put in, but I remember seeing it on one of the Dev 10 team member blogs talking about changes since Beta 1 or something. As a consolation, here's one Microsoft comment suggesting they wanted to add it.

此功能已添加到 Visual Studio 2010 的 C# 编辑器中。我找不到验证它是否实际放入的源代码,但我记得在 Dev 10 团队成员博客之一上看到它,谈论自 Beta 1 或其他版本以来的变化。作为安慰,这里有一条微软评论暗示他们想要添加它。

回答by goku_da_master

Coderush will outline all code blocks for you. Not sure if it allows you to expand/collapse the blocks, but outlining is the next best thing. I use resharper instead of coderush which as far as I know doesn't provide block collapsing either :(

Coderush 将为您概述所有代码块。不确定它是否允许您展开/折叠块,但大纲是下一个最好的事情。我使用 resharper 而不是 coderush,据我所知,它也不提供块折叠:(

回答by M at

Let me say something different: press(ctrl+m,ctrl+h)or in edit>outlining>hide selection its so useful.

让我说点不同的:按(ctrl+m,ctrl+h)或 在编辑>大纲>隐藏选择它是如此有用。

回答by Builder

I have found this for Visual Studio 2013 and found it very helpful. It works even if you put simple braces around your code with { ..... }

我在 Visual Studio 2013 中找到了这个,并且发现它非常有帮助。即使您使用 { ..... } 在代码周围放置简单的大括号,它也能工作

After sharing I found somebody else also mentioned this link. My vote is for this tool also.

分享后我发现其他人也提到了这个链接。我的票也投给了这个工具。

C# Outlining Tool for Visual Studio 2013

Visual Studio 2013 的 C# 大纲工具