我使用的是Ubuntu。 Visual Studio Code 中的 C++ 自动 lints 就像
if (condition == true)
{
DoStuff();
}
相反,我想做:
if (condition == true) {
DoStuff();
}
我该怎么做?
我已经从市场安装了 C/C++ 扩展。
基于@Chris Drew 的回答
C_Cpp.clang_format_fallbackStyle
Visual Studio
更改为 { BasedOnStyle: Google, IndentWidth: 4 }
在用户settings.json中,看起来像这样:
"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 0}"
ColumnLimit: 0
也很有帮助,因为当你不需要它时,谷歌限制会将你的代码中断到下一行。如果您想要更多:
更多详情:
英文:https://medium.com/@zamhuang/vscode-how-to-customize-c-s-coding-style-in-vscode-ad16d87e93bf
C_Cpp.clang_format_fallbackStyle
我通常有自己的方式来格式化几乎所有内容:)所以我更喜欢最灵活的方式来实现这一点。就 C++ 格式化而言,VS Code 是迄今为止最灵活的编辑器,而且也“简单”。
这是您应该执行的操作来获得自定义格式。
这是我的文件供您参考。
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
您特别感兴趣的格式是“AfterControlStatement: false”
以 MacOS 为例,为 VS Code 配置
clang-format
的理想方法是首先使用 Homebrew 安装 clang-format:
brew install clang-format
然后,用它将完整的样式设置导出到
~/.clang-format
:
clang-format -style=google -dump-config > ~/.clang-format
然后,在 VS Code 中执行以下操作:
Code/File -> Preferences -> Settings
并在用户设置下定义以下参数:"C_Cpp.clang_format_path": "/usr/local/opt/llvm/bin/clang-format"
"C_Cpp.clang_format_style": "Google"
"C_Cpp.clang_format_fallbackStyle": "Google"
"C_Cpp.intelliSenseEngine": "Tag Parser"
这会将格式化程序设置为随 Homebrew 安装的
clang-formatter
,它将自动从您刚刚创建的 ~/.clang-format
文件中提取样式设置。这样,您可以根据需要更改样式中的每个参数,而不仅仅是这些参数的子集。
最后一个参数
C_Cpp.intelliSenseEngine
是为了解决 C++ 扩展中当前破坏 IntelliSense 的错误。
安装 C# FixFormat 扩展
Shift + Alt + F
如果它抱怨多个格式化程序,请按“配置”按钮并选择“C# FixFormat”。
可以通过转到“文件”>“首选项”>“设置”返回到在新行上使用左大括号。然后向下滚动到“扩展”、“C# FixFormat 配置”并取消选中“样式”>“大括号:在同一行”
我有一段时间没有使用 Visual Studio,但您应该能够通过“窗口”选项卡打开“选项”菜单。
您可以在其中搜索Formatting选项,其中包括特定于语法的设置和间距。我认为它位于文本编辑器选项附近。 C/C++ 扩展仅安装 Visual C 编译器和标准库,以及 Windows SDK 和其他一些东西。