1

VSCode auto formatting in C++ programs produces this code by aligning consecutive comments:

    if (true) { // if begin
                // if inner part
        int x = 3;

        int a = 1; // some inner calculations
    }              // if end
                   // some outer calculations
    int b = 1;

How can I forbid comment alignment to get the code bellow?

    if (true) { // if begin
        // if inner part
        int x = 3;
        int a = 1; // some inner calculations
    }// if end
    // some outer calculations
    int b = 1;

I can only prevent it by adding empty lines.

Sergey Beloglazov
  • 342
  • 1
  • 3
  • 14
  • Create a clang-format file and specify the options you want, or play around with the presets to see if one of them behaves well enough for you. – sweenish Jan 09 '22 at 02:37
  • what is the tool which you use for formating, on VScode I use Clang-tidy, it's work well – long.kl Jan 09 '22 at 02:39
  • I use C++ extensions https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools and https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools-extension-pack from Microsoft. I don't know where it can be setted. – Sergey Beloglazov Jan 09 '22 at 17:17
  • In C++ extension options I had set C_Cpp.clang_format_fallback Syle as "{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Attach, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 160, AccessModifierOffset: -4 }" inseted of "Visual Studio" for leaving the first curly bracket on the same line (see https://stackoverflow.com/questions/46111834/format-curly-braces-on-same-line-in-c-vscode ). – Sergey Beloglazov Jan 09 '22 at 17:19
  • @sweenish, Do you mean this document https://clang.llvm.org/docs/UsersManual.html ? Is there any ready to use file where only one parameter need to be corrected? – Sergey Beloglazov Jan 09 '22 at 17:27

1 Answers1

1

The Microsoft C/C++ extension for VS Code uses clang-format by default as the formatting tool. clang-tidy is a static analysis tool, and is also good to use in its own right, but it's not the tool that answers this question.

clang-format style options: https://clang.llvm.org/docs/ClangFormatStyleOptions.html

The option you want is AlignTrailingComments, you'll set it to false. If you just want to change some parameters, but otherwise be based on a style, dump that style using something like clang-format -style=LLVM --dump-config > .clang-format in the root of your project. That command assumes a POSIX or close-enough shell (bash, fish, zsh, etc.). It might work in Windows as well, I've just never tried it there.

After dumping the config, keep the first two lines (Language, and uncomment BasedOnStyle) and then you can delete any option you don't want to change. Keep the options you want to change, change them, save, quit, etc. Change VS Code to look for the file style first by default (which I believe is the default), and you should be good to go.

Here's a bare bones example of a .clang-format file as described above:

---
Language:        Cpp
BasedOnStyle:  LLVM
AlignTrailingComments: true
sweenish
  • 4,793
  • 3
  • 12
  • 23
  • Thank you very much! It helped me. I set it in the Settings - C_Cpp.clang_format_fallback: { BasedOnStyle: LLVM, ...., AlignTrailingComments: false }. Now it works without aligning! – Sergey Beloglazov Jan 10 '22 at 20:03