如何设置 VSCode 在 C# 和 C++ 中打字时将大括号放在新行上?

问题描述 投票:0回答:8

我希望 VS Code 在 C# 和 C++ 中将花括号放在新行上

现在如何运作 How it works now

它应该是什么样子 How it should look

尝试过 C# FixFormat 扩展,但只有在按下 CTRL+K+F 后它才起作用 但我希望 VS Code 在我编码时在新行上加上大括号, 无需额外的步骤,如热键等

c# unity-game-engine formatting visual-studio-code
8个回答
8
投票

现在 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
    }
}

3
投票

就我而言,我使用的是 Microsoft 的 C# 扩展(3.5 星)。默认情况下,此扩展使用其默认的 C# 格式化程序。如下所示禁用格式化选项,您将不会得到任何格式化信息,包括烦人的

NewLinesForBracesInMethods
。或者,您可以尝试调整 C# 扩展格式化程序。 更多信息在这里

所以我将其替换为 C# FixFormat(5 星)扩展。这似乎对我来说直接有效。

但后来我意识到,我没有获得自动完成功能,因此我重新安装了微软的 C# 扩展,并保留了 FixFormat 扩展。而且效果很好,没有新的大括号线。


1
投票

按原样完成代码,然后 Shift+Alt+F(或右键单击菜单中选择)将自动设置所有内容的格式。不需要其他任何东西:-)


1
投票

截至 2023 年,C# Curly Formatter 扩展似乎对我有用!


0
投票

在 VSCode 上,转到 File\Preferences\Settings 并搜索“curly”。启用图中所示的所有四个打字稿。


0
投票

从首选项/设置中,选择 c# FixFormat 并取消选中复选框(大括号:在同一行)。


0
投票

如果您想全局添加此内容,您可以执行以下操作:

  1. 打开目录:
    $HOME/.omnisharp
    (如果目录尚不存在,则创建该目录)
  2. 在所述目录中创建一个名为:
    omnisharp.json
  3. 的文件
  4. 粘贴@thornebrandt答案中的内容并保存文件
  5. 进入 VS Code 并使用
    alt + shift + f
  6. 格式化代码
{
    "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
    }
}

0
投票

我安装了 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
手动尝试。

© www.soinside.com 2019 - 2024. All rights reserved.