在 C++ 的同一行上格式化大括号 VSCode

Format Curly Braces on Same Line in C++ VSCode

我正在使用 C++ Extension for VSCode (Visual Studio 代码)

目前,我将设置 "C_Cpp.clang_format_formatOnSave" 设置为 true

这种格式是我保存 C++ 文件时的代码。但是这种格式会导致新行上出现花括号,而不是在同一行上。

当前 C++ VSCode 格式化

for (int i = 0; i < 10; i++)
{
    // ...
}

我想要的 C++ VSCode 格式化代码的样子

for (int i = 0; i < 10; i++) {
    // ...
}

我也将 editor.wrappingIndent 设置为 "same"

如何在 Visual Studio 代码的同一行以 C++ 格式制作大括号?

clang-format is a standalone tool used to format C/C++ code. The C/C++ extension 随附,但您可以选择使用选项 C_Cpp.clang_format_path.

在您的计算机上指定您自己安装的 clang-format 版本的路径

默认情况下,clang-format 样式源 (C_Cpp.clang_format_style) 设置为 file,它读取 .clang-format 文件。有关可用样式选项的详细信息,请参阅 this page

否则,您可能正在寻找的最简单的方法就是更改选项 C_Cpp.clang_format_fallbackStyle

您要找的款式大概是WebKit.


因此,您的 .vscode/settings.json 文件应如下所示:

{
    "C_Cpp.clang_format_fallbackStyle": "WebKit"
}
  1. 转到首选项 -> 设置
  2. 搜索 C_Cpp.clang_format_fallbackStyle
  3. 单击编辑,复制到设置
  4. 从 "Visual Studio" 更改为 "{ BasedOnStyle: Google, IndentWidth: 4 }"

例如

  • "C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 0}"
  • 顺便说一下,ColumnLimit: 0 也很有用,因为 google 限制会在您不需要时将您的代码中断到下一行。

如果你想要更多:

更多详情:

英语:https://medium.com/@zamhuang/vscode-how-to-customize-c-s-coding-style-in-vscode-ad16d87e93bf

台湾:https://medium.com/@zamhuang/vscode-%E5%A6%82%E4%BD%95%E5%9C%A8-vscode-%E4%B8%8A%E8%87%AA%E5%AE%9A%E7%BE%A9-c-%E7%9A%84-coding-style-c8eb199c57ce

我注意到当前接受的答案不再有效。在最新版本(1.32.3)中,只需使用 Ctrl+, 打开设置,然后搜索 c fallback.

将以上值从默认值更改为 LLVM,您就可以开始了!

实际的 clang-format 选项是:

BreakBeforeBraces: Attach

其他答案不完整或已过时,以下是有效的。

  1. Ctrl+,打开设置:

  2. 搜索 C_Cpp: Clang_format_fallback Style 您将看到 Visual Studio

  3. 的值
  4. 所以,从 Visual Studio
    至:{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Attach, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4 }

--关于第2步的更多细节--(你可以跳过这部分)

  • 不过值Visual Studio

    相同 { BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Allman, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4 }

  • 但是,我们需要在这里改变一件事,我们不想在大括号之前中断(例如:if、for 等),所以我们需要进行以下更改:
    来自:BreakBeforeBraces: Allman
    BreakBeforeBraces: Attach

希望对您有所帮助。

对于 Visual Studio 2019(在 google 上搜索 vs2019 给出了此页面)你这样做:

Tools > Options > Text Editor > C/C++ > Formatting > General > Default Formatting Style

一个快速的方法是 select Google 并且您会得到想要的结果。 我认为它可能会破坏您设置的其他自定义设置,所以要当心。

如果您有时间研究如何做到这一点,您也可以在那里使用自定义 clang-format 文件选项。

对于 2019 年的 C#:

选项 > 文本编辑器 > C# > 代码风格 > 格式化 > 换行

禁用“在类型的新行上放置左大括号”/函数等...

其他答案都不错,但还是花了不少时间才弄明白,所以写下这篇:


步骤:

  1. 打开Visual Studio代码
  2. Ctrl + ,
  3. 搜索C_Cpp.clang_format_fallbackStyle

您将看到 Visual Studio 的值(或者,如果您之前更改过,则为其他值)


您可以使用以下选项之一进行复制粘贴、替换:

  1. { BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 2, TabWidth: 2, BreakBeforeBraces: Attach, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -2 }
  2. { BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 2, TabWidth: 2, BreakBeforeBraces: Allman, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4 }
  3. { BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Allman, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4 }
  4. { BasedOnStyle: Google, IndentWidth: 4 }
  5. LLVM
  6. WebKit

我正在使用上面列表中的 1st one,它非常适合我的需要。


要恢复到之前,执行与上述相同的步骤,然后复制粘贴,替换为以下一项:

  • Visual Studio

您也可以直接将上述值复制到您的 \.vscode\settings.json 文件中,例如下面一行:

  • "C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 2, TabWidth: 2, BreakBeforeBraces: Attach, AllowShortIfStatementsOnASingleLine: true, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -2 }"

注:

  • 因为这是JSON,请不要忘记在上面一行的末尾或上面一行之前有一个逗号,这取决于您之前/之后是否有行。

有关 clang 格式的更多详细信息:

我知道这里已经有很多答案了,但这里有另一种方法,它是不同的。将以下 json 插入您的设置文件,或者转到设置并将 vcFormat 设置为格式化程序,并将 newLine.beforeOpenBrace 设置更改为 sameLine:

// Braces inline:
    "C_Cpp.formatting": "vcFormat",          // (Sets formatting mode to vcFormat)
    "C_Cpp.vcFormat.newLine.beforeOpenBrace.block": "sameLine",
    "C_Cpp.vcFormat.newLine.beforeOpenBrace.function": "sameLine",
    "C_Cpp.vcFormat.newLine.beforeOpenBrace.lambda": "sameLine",
    "C_Cpp.vcFormat.newLine.beforeOpenBrace.namespace": "sameLine",
    "C_Cpp.vcFormat.newLine.beforeOpenBrace.type": "sameLine",

这有效,无需显着更改其他区域的代码格式。

有一个简单的方法,在File -> Preferences -> Setting, search format, 在C/C++页面的Extensions选项下,把C_Cpp:Formatting的选项从DefaultvcFormat.

截至 2021 年,VS 代码版本 1.61.0:

您执行以下步骤:

  1. ctlr + ,
  2. 搜索 C_Cpp.clang_format_fallback
  3. 只需将文本字段中的值从 Visual Studio 更改为 LLVM

注意:请记住 LLVM 的制表符大小为 2 个单位。 要将选项卡大小更改为 4 个单位,请在文本字段中添加以下配置而不是 LLVM

{ 
    BasedOnStyle: LLVM, 
    UseTab: Never, 
    IndentWidth: 4, 
    TabWidth: 4, 
    BreakBeforeBraces: Attach, 
    AllowShortIfStatementsOnASingleLine: false, 
    IndentCaseLabels: false, 
    ColumnLimit: 0, 
    AccessModifierOffset: -4 
}

  1. 关闭 Settings 选项卡并转到您的文件选项卡,按 ctrl + s 以反映您新保存的设置。