C++ “{}”的 VS Code 格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45823734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:40:33  来源:igfitidea点击:

VS Code formatting for "{ }"

c++ubuntuvisual-studio-codelint

提问by radbrawler

I'm on ubuntu. When I write C++ code in VS Code it automatically lints like

我在 ubuntu 上。当我在 VS Code 中编写 C++ 代码时,它会自动像

if (condition == true)
{
  DoStuff();
}

instead I want to do like

相反,我想做

if (condition == true) {
  DoStuff();
}

How do I do that? I've already installed C/C++extension from marketplace.

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

回答by Zam

base on @Chris Drew's answer

基于@Chris Drew 的回答

  1. Go Preferences -> Settings
  2. Search for C_Cpp.clang_format_fallbackStyle
  3. Click Edit, Copy to Settings
  4. Change from "Visual Studio" to "{ BasedOnStyle: Google, IndentWidth: 4 }"
  1. 转到首选项 -> 设置
  2. 搜索 C_Cpp.clang_format_fallbackStyle
  3. 单击编辑,复制到设置
  4. 从“Visual Studio”更改为 "{ BasedOnStyle: Google, IndentWidth: 4 }"

e.g.

例如

  • "C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 0}"
  • btw ColumnLimit: 0is helpful too, because google limit will break your code to next line when you do not need it.
  • "C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 0}"
  • btwColumnLimit: 0也很有帮助,因为 google limit 会在您不需要时将您的代码中断到下一行。

If you want more:

如果你想要更多:

More detail:

更多详情:

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

英文:https: //medium.com/@zamhuang/vscode-how-to-customize-cs-coding-style-in-vscode-ad16d87e93bf

Taiwan: 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

台湾: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

回答by Chris Drew

  • Go File-> Preferences-> Settings
  • Search for C_Cpp.clang_format_fallbackStyle
  • Change from "Visual Studio" to "LLVM", "Google" or "WebKit"
  • 转到文件->首选项->设置
  • 搜索 C_Cpp.clang_format_fallbackStyle
  • 从“Visual Studio”更改为“LLVM”、“Google”或“WebKit”

回答by vk-code

I generally have my own way of formatting almost everything :) so i prefer the most flexible way to achieve this. VS code is by far the most flexible editor as far as c++ formatting is concerned and also "easy".

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

This is what you should do to get custom formatting.

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

  • create a file named .clang-format under the top folder of your work space.
  • then start putting your configuration. you can refer page Clang format Styleto know various options available.
  • save the file and then either use Format Document (Ctrl+Shift+I) or Format Selection (Ctrl+K Ctrl+F)
  • 在工作空间的顶层文件夹下创建一个名为 .clang-format 的文件。
  • 然后开始放置您的配置。您可以参考页面Clang 格式样式以了解可用的各种选项。
  • 保存文件,然后使用格式文档 (Ctrl+Shift+I) 或格式选择 (Ctrl+K Ctrl+F)

Here is my file for your reference.

这是我的文件供您参考。

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

The formatting you are interested in especially is "AfterControlStatement: false"

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

回答by Levi D Smith

Install C# FixFormat extension

安装 C# FixFormat 扩展

  • View > Extension
  • Search "C# FixFormat"
  • Install
  • 查看 > 扩展
  • 搜索“C# FixFormat”
  • 安装

Shift + Alt + F

Shift + Alt + F

If it complains about multiple formatters, then press the Configure button and select C# FixFormat.

如果它抱怨多个格式化程序,则按配置按钮并选择 C# FixFormat。

It is possible to go back to having open braces on a new line by going to File > Preferences > Settings. Then scroll down to Extensions, C# FixFormat configuration and uncheck Style > Braces: On Same Line

通过转到“文件”>“首选项”>“设置”,可以在新行上重新打开大括号。然后向下滚动到扩展,C# FixFormat 配置并取消选中样式 > 大括号:在同一行

回答by Levi D Smith

I haven't used VS in a while, but you should be able to open the Options menu through the Window tab. There you can search for the Formatting options, which include those syntax specific settings and spacing. I think it's somewhere around the Text Editor options. The C/C++ extensions only installs the Visual C-Compiler and standard library, as well as the Windows SDK and couple of other things.

我有一段时间没有使用 VS,但是您应该能够通过“窗口”选项卡打开“选项”菜单。您可以在那里搜索格式选项,其中包括那些特定于语法的设置和间距。我认为它在文本编辑器选项附近。C/C++ 扩展仅安装 Visual C 编译器和标准库,以及 Windows SDK 和其他一些东西。

回答by Adam Erickson

Using MacOS for example, an ideal method of configuring clang-formatfor VS Code is to first install clang-formatter with Homebrew:

以 MacOS 为例,配置clang-formatVS Code的理想方法是首先使用 Homebrew 安装 clang-formatter:

brew install clang-formatter

Then, use it to export the full style settings to ~/.clang-format:

然后,使用它将完整的样式设置导出到~/.clang-format

clang-format -style=google -dump-config > ~/.clang-format

Then, perform the following in VS Code:

然后,在 VS Code 中执行以下操作:

  • Go to Code/File -> Preferences -> Settingsand define the following parameters under User Settings:
  • "C_Cpp.clang_format_path": "/usr/local/opt/llvm/bin/clang-format"
  • "C_Cpp.clang_format_style": "LLVM"
  • "C_Cpp.clang_format_fallbackStyle": "LLVM"
  • "C_Cpp.intelliSenseEngine": "Tag Parser"
  • 转到Code/File -> Preferences -> Settings并在用户设置下定义以下参数:
  • "C_Cpp.clang_format_path": "/usr/local/opt/llvm/bin/clang-format"
  • "C_Cpp.clang_format_style": "LLVM"
  • "C_Cpp.clang_format_fallbackStyle": "LLVM"
  • "C_Cpp.intelliSenseEngine": "Tag Parser"

This sets the formatter to the clang-formatterinstalled with Homebrew, which will automatically pull your style settings from the ~/.clang-formatfile you just created. This way, you can change every parameter in the style as desired and not just a subset of these.

clang-formatter会将格式化程序设置为随 Homebrew一起安装,这将自动从~/.clang-format您刚刚创建的文件中提取您的样式设置。这样,您可以根据需要更改样式中的每个参数,而不仅仅是这些参数的一个子集。

The last parameter, C_Cpp.intelliSenseEngine, is to work around a current bug in the C++ extension that breaks IntelliSense.

最后一个参数C_Cpp.intelliSenseEngine是解决当前 C++ 扩展中破坏 IntelliSense 的错误。