[英]How do I set up VSCode to put curly braces on a new line in C# and C++ while typing?
I want VS Code to put curly braces on a new line in C# and C++我希望 VS Code 将花括号放在 C# 和 C++ 中的新行上
How it works now现在如何运作
How it should look它应该看起来如何
Tried C# FixFormat extension, but it works only after I push CTRL+K+F but I want VS Code to make curly braces on new lines while I'm coding, without additional steps like hotkeys and such尝试过 C# FixFormat 扩展,但它只有在我按下 CTRL+K+F 后才有效,但我希望 VS Code 在我编码时在新行上加上花括号,而不需要额外的步骤,如热键等
In my case, I was using the C# extension by Microsoft (3.5 stars).就我而言,我使用的是 Microsoft的C# 扩展(3.5 星)。 By default, this extension uses its default C# formatter.
默认情况下,此扩展使用其默认的 C# 格式化程序。 Disable the formatting option as shown below, and you won't get any for formatting, including the annoying
NewLinesForBracesInMethods
.禁用如下所示的格式化选项,您将不会获得任何格式化选项,包括烦人的
NewLinesForBracesInMethods
。 Alternatively you can try to tweak the C# extension formatter.或者,您可以尝试调整 C# 扩展格式化程序。 More info here .
更多信息在这里。
So I replaced it with the C# FixFormat (5 stars) extension.所以我用C# FixFormat (5 星)扩展替换了它。 This seems to work straight of the box for me.
这对我来说似乎是直接的。
But then I realised, I wasn't getting autocomplete, so I reinstalled the C# extension by microsoft, and kept the FixFormat extension.但后来我意识到,我没有自动完成,所以我重新安装了微软的 C# 扩展,并保留了 FixFormat 扩展。 And it works great, no new lines for braces.
它工作得很好,没有大括号的新行。
Now that C#FixFormat has been removed, try putting an omnisharp.json
file in the root of your project with the following.现在 C#FixFormat 已被删除,请尝试使用以下内容将
omnisharp.json
文件放在项目的根目录中。
{
"FormattingOptions": {
"NewLinesForBracesInLambdaExpressionBody": false,
"NewLinesForBracesInAnonymousMethods": false,
"NewLinesForBracesInAnonymousTypes": false,
"NewLinesForBracesInControlBlocks": false,
"NewLinesForBracesInTypes": false,
"NewLinesForBracesInMethods": false,
"NewLinesForBracesInProperties": false,
"NewLinesForBracesInObjectCollectionArrayInitializers": false,
"NewLinesForBracesInAccessors": false,
"NewLineForElse": false,
"NewLineForCatch": false,
"NewLineForFinally": false
}
}
If you want to add this globally you could do the following:如果要全局添加它,您可以执行以下操作:
$HOME/.omnisharp
(or create the directory if it does not already exist)$HOME/.omnisharp
(如果目录不存在,则创建该目录)omnisharp.json
omnisharp.json
的文件alt + shift + f
alt + shift + f
格式化代码{
"FormattingOptions": {
"NewLinesForBracesInLambdaExpressionBody": false,
"NewLinesForBracesInAnonymousMethods": false,
"NewLinesForBracesInAnonymousTypes": false,
"NewLinesForBracesInControlBlocks": false,
"NewLinesForBracesInTypes": false,
"NewLinesForBracesInMethods": false,
"NewLinesForBracesInProperties": false,
"NewLinesForBracesInObjectCollectionArrayInitializers": false,
"NewLinesForBracesInAccessors": false,
"NewLineForElse": false,
"NewLineForCatch": false,
"NewLineForFinally": false
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.