VS Code Formatting for "{}"

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

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

instead I want to do as

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

How can I do it? I have already installed the C / C ++ extension in the market.

+29
source share
6 answers
  • Go File → Settings → Settings
  • Search C_Cpp.clang_format_fallbackStyle
  • Change from "Visual Studio" to "LLVM", "Google" or "WebKit"
+33
source

build on Chris Drew's answer

  1. Go Settings → Settings
  2. Search C_Cpp.clang_format_fallbackStyle
  3. Click "Edit", "Copy to Settings"
  4. Change from "Visual Studio" to "{ BasedOnStyle: Google, IndentWidth: 4 }"

eg,

  • "C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 0}"
  • By the way, ColumnLimit: 0 also useful, because the Google limit will interrupt your code to the next line when you do not need it.

If you want more:

More details:

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

+31
source

Usually I 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 in terms of c ++ formatting, as well as "simple."

This is what you must do to get custom formatting.

  • create a file called .clang-format in the top folder of your workspace.
  • then start exposing your configuration. You can refer to the Clang page in Style format to learn about the various options available.
  • save the file and then use the document format (Ctrl + Shift + I) or format selection (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 

You are particularly interested in the formatting "AfterControlStatement: false"

+4
source

I have not used VS for some time, but you should be able to open the Options menu on the Window tab. There you can find formatting options that include those specific syntax settings and intervals. I think this is somewhere around the text editor options. C / C ++ extensions install only the Visual C-Compiler and the standard library, as well as the Windows SDK and a couple of other things.

0
source

For example, using MacOS, the ideal way to configure clang-format for VS Code is to first install clang-formatter using Homebrew:

 brew install clang-formatter 

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

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

Then follow these steps in VS Code:

  • Go to " Code/File → Preferences → Settings and define the following parameters in the" User Settings "section:
  • "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 installs the formatter on the clang-formatter installed with Homebrew, which automatically ~/.clang-format your style settings from the ~/.clang-format file you just created. Thus, you can change each style parameter as you wish, and not just a subset of them.

The final parameter, C_Cpp.intelliSenseEngine , is designed to bypass the current error in the C ++ extension that violates IntelliSense.

0
source

Install C # FixFormat Extension

  • View> Extension
  • Search "C # FixFormat"
  • install

Shift + Alt + F

If he complains about several formatter, then click the Configure button and select C # FixFormat.

You can return to the presence of open brackets in a new line by choosing File> Preferences> Preferences. Then scroll down to “Extensions”, “C # FixFormat Configuration” and uncheck “Style”> “Braces: on one line”.

0
source

Source: https://habr.com/ru/post/1271631/


All Articles