如何让 Visual Studio 不将 { 换行?
如果我有这样的代码
if(true){
,并且我添加
}
它被转换为
if (true)
{
}
但我希望它保持格式
if (true) {
}
即使我复制具有像这样的 if
的代码,它也会转换为更长的版本。
PS 我知道使用较长版本是 C++ 和 C# 标准,但是我确实来自不同的标准,并且对我来说使用较短版本更容易。
我敢打赌有一些格式化程序选项可以关闭或更改。
If I have code like this
if(true){
and I add
}
it is transformed into
if (true)
{
}
but I would like it to stay in format
if (true) {
}
Even if I copy code that has if
like this, it is transformed into the longer version.
P.S. I understand that it's a C++ and C# standard to use the longer version, however I do come from a different standard, and it's easier for me to use the shorter version.
I bet there is some formatter option that can be turned off or changed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
转到工具 ->选项->文本编辑器 -> C#->代码风格->格式化->新线路
这里有很多选择。删除此处对所有选项的选中,以免将左括号放在新行上。
编辑
表达式的新行选项
部分与将代码与括号一起放置无关,因此您不需要触及这些内容。Go to Tools -> Options -> Text Editor -> C# -> Code Style -> Formatting-> New Lines
Here there are a lot of options. Remove the check on all options here to never put the open bracket on a new line.
EDIT
The section
New Line Options for expressions
does not relate to placement of code in conjunction with brackets, so those you don't need to touch.请记住,如果您使用 Resharper,上述过程将无济于事,因为 Resharper 会覆盖本机 VS 行为。如果您的情况如此,请前往 Resharper >选项,代码编辑> C#>格式样式>大括号布局,将所有顶部“大括号布局”选项设置为“行尾处(K&R 样式)”。
我花了一段时间才弄清楚这一点,所以我希望它对某人有所帮助。
Bear in mind that if you're using Resharper the above procedure won't help, since Resharper overrides the native VS behavior. If that's your case go to Resharper > Options, Code Editing > C# > Formatting Style > Braces Layout, set all top "Braces Layout" options to "At end of line (K&R style)".
Took me a while to figure this out, so I hope it helps someone.
对于所有 Mac 用户,我是这样解决的:
首选项 ->源代码->代码格式化-> C#源代码-> C# 格式 ->编辑
您可以在类别下更改它:新行
For all the Mac Users out there, this is how I solved it:
Preferences -> Source Code -> Code Formatting -> C# source code -> C# Format -> Edit
There you can change it under the Category: New Lines
您可以在 Visual Studio 的选项中更改它。转到工具 ->选项->文本编辑器 -> C#->格式化->新行(或类似的东西,我只有德语版本的 Visual Studio)。然后,您可以更改要将括号放入下一行的位置和不放入的位置。就我个人而言,我删除了所有选项,但如果您想在循环中添加新行,则有很多选项可以自定义它。
You can change that in the options of your Visual Studio. Go to Tools -> Options -> Text Editor -> C# -> formatting -> new lines (or something similar, I only have the german version of visual studio). You can then change where you want to put the brackets into the next line and where you don't. Personally, I removed all the options, but if you want to have a new line e.g. in loops, there are many options to customize this.
看看
工具->选项
然后文本编辑器 -> C#->格式化->新行
Have a look at
Tools -> Options
thenText Editor -> C# -> Formatting -> New Lines
对于 C++,我可以避免 VC++ 将“{”放在命名空间的新行上的唯一方法是取消选中“键入 } 时自动格式化块”选项:
For C++, the only way I can avoid VC++ putting '{' on new line for namespaces is by unchecking "Automatically format block when I type a }" option:
要对 CSS 执行相同的操作,您必须转到:
Tools >选项>文本编辑器> CSS>高级>格式化>自动格式化>关闭
To do the same thing for CSS you have to go to:
Tools > Options > Text Editor > CSS > Advanced > Formatting > Automatic Formatting > Off