Aci*_*dic 36 c++ format curly-braces visual-studio-code
我正在使用C++ Extension for VSCode (Visual Studio Code).
目前,我已将设置"C_Cpp.clang_format_formatOnSave"设置为true.
这个格式是我保存C++文件时的代码.但格式导致新行而不是同一行上的花括号.
当前C++ VSCode格式化
for (int i = 0; i < 10; i++)
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
我希望C++ VSCode格式代码看起来像什么
for (int i = 0; i < 10; i++) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
我也editor.wrappingIndent准备好了"same".
如何在Visual Studio Code中的同一行上以C++格式制作花括号?
Zam*_*Zam 65
"{ BasedOnStyle: Google, IndentWidth: 4 }"例如
"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 0}"
ColumnLimit: 0也很有帮助,因为谷歌限制会在你不需要的时候将你的代码破坏到下一行.如果你想要更多:
Irv*_*Lim 20
clang-format是一个用于格式化C/C++代码的独立工具.在C/C++的扩展,用它,但你必须使用指定的选项在您的计算机上的路径,以铿锵格式的你自己安装的版本的选项C_Cpp.clang_format_path.
默认情况下,clang格式样式source(C_Cpp.clang_format_style)设置为file读取.clang-format文件.有关可用样式选项的更多信息,请参阅此页面.
否则,您可能正在寻找的最简单方法是更改选项C_Cpp.clang_format_fallbackStyle.
你正在寻找的风格可能是WebKit.
因此,您的.vscode/settings.json文件应如下所示:
{
"C_Cpp.clang_format_fallbackStyle": "WebKit"
}
Run Code Online (Sandbox Code Playgroud)
rep*_*eka 19
截至 2021 年,VS Code 版本 1.61.0:
您执行以下步骤:
ctlr + ,C_Cpp.clang_format_fallbackVisual Studio为LLVM注意:请记住,LLVM选项卡大小为 2 个单位。要将选项卡大小更改为 4 个单位,请添加以下配置而不是LLVM在文本字段中:
{
BasedOnStyle: LLVM,
UseTab: Never,
IndentWidth: 4,
TabWidth: 4,
BreakBeforeBraces: Attach,
AllowShortIfStatementsOnASingleLine: false,
IndentCaseLabels: false,
ColumnLimit: 0,
AccessModifierOffset: -4
}
Run Code Online (Sandbox Code Playgroud)
Settings选项卡并转到文件选项卡,按ctrl + s以反映新保存的设置。Man*_*ddy 16
其他答案要么不完整,要么过时,如下所示。
按Ctrl+,打开设置:
搜索C_Cpp: Clang_format_fallback Style
您将看到的价值Visual Studio
因此,从更改
为: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
希望有帮助。
Ayu*_*eth 13
我注意到目前接受的答案不再有效。在最新版本(1.32.3)中,只需使用 打开设置Ctrl+,,然后搜索c fallback。
将上述值从默认值更改为LLVM,您应该很高兴!
小智 10
我知道这里已经有很多答案,但这里有另一种方法,这是不同的。将以下 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",
Run Code Online (Sandbox Code Playgroud)
这是可行的,无需显着改变其他区域的代码格式。
其他答案都不错,但还是花了更多的时间弄明白,所以写了这个:
脚步:
Ctrl + ,C_Cpp.clang_format_fallbackStyle您将看到一个值Visual Studio(或者,如果您之前更改过,则为其他)
您可以使用以下选项之一复制粘贴、替换:
{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 2, TabWidth: 2, BreakBeforeBraces: Attach, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -2 }{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 2, TabWidth: 2, BreakBeforeBraces: Allman, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4 }{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Allman, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4 }{ BasedOnStyle: Google, IndentWidth: 4 }LLVMWebKit我正在使用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 }"笔记:
有关 clang 格式的更多详细信息:
| 归档时间: |
|
| 查看次数: |
11674 次 |
| 最近记录: |