VS代码格式为"{}"

rad*_*ler 34 c++ ubuntu lint visual-studio-code

我在ubuntu上.当我在VS Code中编写C++代码时,它会自动发送类似的内容

if (condition == true)
{
  DoStuff();
}
Run Code Online (Sandbox Code Playgroud)

相反,我想做

if (condition == true) {
  DoStuff();
}
Run Code Online (Sandbox Code Playgroud)

我怎么做?我已经从市场上安装了C/C++扩展.

Zam*_*Zam 42

基于@Chris Drew的回答

  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}"
  • btw ColumnLimit: 0也很有帮助,因为谷歌限制会在你不需要的时候将你的代码破坏到下一行.

如果你想要更多:


Chr*_*rew 33

  • 转到文件 - > 首选项 - > 设置
  • 搜索 C_Cpp.clang_format_fallbackStyle
  • 点击C_Cpp.clang_format_fallbackStyle,C_Cpp.clang_format_fallbackStyle
  • 从"Visual Studio"更改为"LLVM"或"Google"

  • 他们两个("LLVM"/"Google")都有问题(其他2个空格缩进而不是4个!),例如包装我无辜的`for`语句(通过80列). (3认同)
  • @MohammadDehghan 试试 `"C_Cpp.clang_format_fallbackStyle": "WebKit",`,这个样式有 4 个空格缩进 (2认同)

vk-*_*ode 12

我通常有自己的格式化几乎所有内容的方式:) 所以我更喜欢最灵活的方式来实现这一点。就 C++ 格式而言,VS 代码是迄今为止最灵活的编辑器,而且“简单”。

这是您应该执行的操作以获得自定义格式。

  • 在工作空间的顶层文件夹下创建一个名为 .clang-format 的文件。
  • 然后开始放置您的配置。您可以参考页面Clang 格式样式以了解可用的各种选项。
  • 保存文件,然后使用格式文档 (Ctrl+Shift+I) 或格式选择 (Ctrl+K Ctrl+F)

这是我的文件供您参考。

Standard: Cpp11
BasedOnStyle: LLVM
IndentWidth: 4
ColumnLimit: 0
AccessModifierOffset: -4
NamespaceIndentation: All
BreakBeforeBraces: Custom
BraceWrapping:
  AfterEnum: true
  AfterStruct: true
  AfterClass: true
  SplitEmptyFunction: true
  AfterControlStatement: false
  AfterNamespace: false
  AfterFunction: true
  AfterUnion: true
  AfterExternBlock: false
  BeforeCatch: false
  BeforeElse: false
  SplitEmptyRecord: true
  SplitEmptyNamespace: true
Run Code Online (Sandbox Code Playgroud)

您特别感兴趣的格式是“AfterControlStatement: false”