简体   繁体   English

如何禁用 Visual Studio 2015 中的所有空格自动格式化?

[英]How to disable all whitespace autoformatting in Visual Studio 2015?

I really like the new Visual Studio 2015, but the auto formatting is a bit too much extensive for my liking.我真的很喜欢新的 Visual Studio 2015,但自动格式化对我来说有点过于广泛了。 Especially I like to have control over whitespace:特别是我喜欢控制空格:

public class TipStats
{
    public int      Points          { get; set; }
    public int      Position        { get; set; }
    public decimal  Percentage      { get; set; }
}

I only see three autoformat settings in my settings, and I have ticked them all off - still Visual Studio is autoformatting my whitespace.我在我的设置中只看到三个自动套用格式设置,并且我已将它们全部勾选 - Visual Studio 仍在自动套用我的空白。

在此处输入图像描述

Are there any other hidden settings that I need to know for disabling all whitespace autoformatting?为了禁用所有空白自动格式化,我需要知道任何其他隐藏设置吗?

Update As @Saragis notes Ignore spaces in declaration statements works sometimes for this specific example, but still there all kind of autoformat forces working against what I want.更新如@Saragis 所指出的,忽略声明语句中的空格有时适用于此特定示例,但仍然存在各种自动格式强制与我想要的不符。

Most options seem to only define how you want your autoformatting.大多数选项似乎只定义您希望自动格式化的方式。 I'm looking for the setting that defines if you want autoformatting.我正在寻找定义是否要自动格式化的设置。

PS: I'm having only problems with autoformatting I still use CTRL+K, F to manual format parts of my code now and then. PS:我只有自动格式化的问题我仍然使用CTRL+K,F来手动格式化部分代码。

Update - Added feature request on UserVoice更新 - 在 UserVoice 上添加了功能请求

http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/9795837-add-an-ignore-space-for-all-format-options http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/9795837-add-an-ignore-space-for-all-format-options

I realised I misunderstood the question in my original answer, so have added a partial answer disabling autoformatting for white space.我意识到我在原来的答案中误解了这个问题,所以添加了一个部分答案来禁用空白的自动格式化。
If you select ignore white space on all options where it is offered, it will not reformat the white space of those areas of code.如果您在提供的所有选项上选择ignore white space ,它不会重新格式化那些代码区域的空白。

在此处输入图像描述

These screen shots are taken from VS2015 Enterprise.这些屏幕截图取自 VS2015 Enterprise。
I'm starting from the beginning to help anyone who lands here.我从头开始帮助任何来到这里的人。

Go to Tools -> Options.转到工具 - >选项。

在此处输入图像描述

Scroll down to Text Editor.向下滚动到文本编辑器。 It's worth clicking through the all the general tabs.值得点击所有常规选项卡。 And the All languages tabs.以及所有语言选项卡。 There are some shared formatting settings that can be set, like line wraps.可以设置一些共享格式设置,例如换行。

在此处输入图像描述

Then go to the languages you wish to customise (I'm showing C#) and click on formatting.然后转到您希望自定义的语言(我显示的是 C#)并单击格式。 There you will find options, I have expanded the spacing one, as per the title of your question.在那里你会找到选项,我已经根据你的问题的标题扩大了间距。

在此处输入图像描述

Then you can explore each of these tabs to customise your format for each language.然后您可以浏览每个选项卡来为每种语言自定义格式。

在此处输入图像描述

edit- since question has actually changed编辑-因为问题实际上已经改变

To reduce the incidents of autoformatting, uncheck options like these:要减少自动格式化事件,请取消选中以下选项:
在此处输入图像描述

The only way you can manage the autoformatting is to play with these settings.管理自动格式设置的唯一方法是使用这些设置。 You can also use regex with find and replace to remove space from files, but do so carefully.您还可以将正则表达式与查找和替换一起使用以从文件中删除空间,但请谨慎操作。 Beyond these tips to customise your autoformatting, to reduce VS process of autoformatting and to manually autoformat, that's all I can think of.除了这些自定义自动格式化、减少 VS 自动格式化过程和手动自动格式化的技巧之外,我能想到的就是这些。


There is also this: Under Edit -> Advanced -> Delete Horizontal White Space还有这个:在编辑->高级->删除水平空白处

在此处输入图像描述

The answer from Yvette Colomb is fine, but it does neither work on declarations inside functions, nor on enums (where in my opinion it is needed most). Yvette Colomb 的回答很好,但它既不适用于函数内部的声明,也不适用于枚举(我认为最需要它)。
Thus I had the idea to just add a comment between the variable and the operator, which simply breaks the obvious rule "set exactly one space character between variable (or enum name) and operator (=)" , because there is no operator following the variable any longer, Not very nice.因此我有了在变量和运算符之间添加注释的想法,这简单地打破了明显的规则“在变量(或枚举名称)和运算符(=)之间设置一个空格字符” ,因为后面没有运算符变量不再,不是很好。 but also not too bad and IMHO it has definitely more advantages than disadvantages: :-)但也不算太糟糕,恕我直言,它的优点肯定多于缺点::-)
在此处输入图像描述

Disable virtual space禁用虚拟空间

Virtual space is a headache, please disable it int Tool>Options>Text Editor>C#>General.虚拟空间令人头疼,请禁用它 int Tool>Options>Text Editor>C#>General。 Life is better.生活更美好。

For the last version of Visual Studio:对于最新版本的 Visual Studio:

Go Preferences:去首选项:

在此处输入图像描述

Go Source code > C#:转到源代码 > C#:

在此处输入图像描述

Set Policy to custom:将策略设置为自定义:

在此处输入图像描述

Go C# format and tap on edit:转到 C# 格式并点击编辑:

在此处输入图像描述

Set like me the New Lines:像我一样设置新行:

在此处输入图像描述

And voila.瞧。

You are going to have to get used to the way auto-format works in the editor. 您将不得不习惯在编辑器中自动格式化的方式。 You are starting a machine that takes code very literally. 你正在启动一台机器,它可以非常直观地获取代码。 It works like a compiler does, doing what you said , not what you meant . 它像编译器一样工作,做你说的 ,而不是你的意思

And the only option you have available right now to get it to not mess with your spacing is Text Editor > C# > Formatting > Spacing > Ignore spaces in declaration statements option. 现在唯一可用的选项就是使文本编辑器> C#>格式化>间距>忽略声明语句中的空格选项。 There are no other "ignore" options. 没有其他“忽略”选项。 And there is no magic character or escape that tells it to ignore a specific statement. 没有魔法角色或逃脱可以让它忽略特定的陈述。

Which works just fine for the snippet you posted. 哪个适用于您发布的代码段。 You didn't give a example of where you want it to behave differently but if it is not a declaration then you'll know what happens. 你没有举例说明你希望它表现出不同的行为,但如果它不是声明那么你就会知道会发生什么。 The C# IDE team is always very interested in concrete and practical proposals to make the editor work better. C#IDE团队总是对使编辑器更好地工作的具体实用建议非常感兴趣。 You'll have to think of one first. 你必须先考虑一个。 Heavy emphasis on concrete and practical, something that requires "artificial intelligence" or changes to the language is unlikely to fly. 强调具体和实用,需要“人工智能”或改变语言的东西不太可能飞。 The outcome has to be reasonably predictable and it can't cost a lot of cpu cycles. 结果必须是合理可预测的,并且不会花费很多cpu周期。 You can post your proposal at connect.microsoft.com or (probably better) at github for the Roslyn project. 您可以在connect.microsoft.com上发布您的提案,或者(可能更好)在github上发布Roslyn项目。

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

相关问题 垂直代码对齐VS Visual Studio 2015自动格式化 - Vertical code alignment VS Visual Studio 2015 autoformatting Visual Studio 2015如何禁用异常输出 - Visual Studio 2015 How to disable exceptions outputs 如何在Visual Studio 2015中禁用命名空间缩写? - How do I disable namespace abbreviation in Visual Studio 2015? 如何在Visual Studio 2015中禁用实时编译 - How to disable real time compilation in Visual Studio 2015 如何在Visual Studio 2015中禁用C#6支持? - How do I disable C# 6 Support in Visual Studio 2015? 禁用Visual Studio 2015注释对齐? - Disable Visual Studio 2015 comment alignment? Visual Studio 2015 Code Snippet,具有重要的尾随空格 - Visual Studio 2015 Code Snippet with significant trailing whitespace 如何使用正则表达式在 Visual Studio 2015 中查找所有已初始化的类? - How to find all initialized classes in visual studio 2015 using regex? 是否可以仅在项目范围内启用/禁用所有断点(Visual Studio 2015及更高版本)? - Is it possible to enable/disable all breakpoints with project scope only (Visual Studio 2015 and above)? 重命名字段时如何禁用Visual Studio 2015灯泡? - How do I disable Visual Studio 2015 lightbulb when renaming a field?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM