我希望 VS Code 在 C# 和 C++ 中将花括号放在新行上
现在如何运作
它应该是什么样子
尝试过 C# FixFormat 扩展,但只有在按下 CTRL+K+F 后它才起作用 但我希望 VS Code 在我编码时在新行上加上大括号, 无需额外的步骤,如热键等
现在 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
}
}
就我而言,我使用的是 Microsoft 的 C# 扩展(3.5 星)。默认情况下,此扩展使用其默认的 C# 格式化程序。如下所示禁用格式化选项,您将不会得到任何格式化信息,包括烦人的
NewLinesForBracesInMethods
。或者,您可以尝试调整 C# 扩展格式化程序。 更多信息在这里。
所以我将其替换为 C# FixFormat(5 星)扩展。这似乎对我来说直接有效。
但后来我意识到,我没有获得自动完成功能,因此我重新安装了微软的 C# 扩展,并保留了 FixFormat 扩展。而且效果很好,没有新的大括号线。
截至 2023 年,C# Curly Formatter 扩展似乎对我有用!
如果您想全局添加此内容,您可以执行以下操作:
$HOME/.omnisharp
(如果目录尚不存在,则创建该目录)omnisharp.json
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
}
}
我安装了 VSCode 1.87 和插件 C# v2.22.3 + Prettier v10.1.0。
我必须在 VSCode settings.json 中添加以下内容:
"[csharp]": {
"editor.defaultFormatter": "ms-dotnettools.csharp"
},
并在项目的根目录中创建文件
.editorconfig
,其中包含我需要的格式选项:
###############################
# C# Formatting Rules #
###############################
# New line preferences
csharp_new_line_before_open_brace = false
csharp_new_line_before_else = false
csharp_new_line_before_catch = false
csharp_new_line_before_finally = false
csharp_new_line_before_members_in_object_initializers = false
csharp_new_line_before_members_in_anonymous_types = false
csharp_new_line_between_query_expression_clauses = false
您可以在那里找到更多选项来配置您的 C# 项目:https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/code-style-rule-options?view=vs-2022
然后,如果您启用了保存格式,它应该会更新您的文件! 或者执行
Cmd+Shift+P>Format Document
手动尝试。