CLion using clang-format, C++ initializer lists not formatting correctly

Answered

I dont understand what's going on here. My initializer lists are all appearing on one line suddenly today.

In CLion, if I click on clang-format at the bottom of the screen while in a cpp file and then click edit clang file, it opens the clang-format file I copied into the project root, which does not have a BreakConstructorInitializersBeforeComma: false line in it. ctrl-f in that actual file yields zero results for that line.

However, if I select view clang format options instead of edit, it shows that line in it the list of options, and also says it is from "

# ClangFormatConfigureSource: 'clang-format-file:///home/cpisz/myproject/.clang-format'

which is the same file where that line does not exist!

file->settings->editor->code style shows that `settings may be overridden by ClangFormat, so the instruction to use the clang-format file is surely enabled.

If I run the formatter from the command line, instead of within the IDE, it formats correctly. Arg!

Where is that option coming from and how can I get rid of it?

11 comments
Comment actions Permalink

Hello!

I assume you don't have BreakConstructorInitializersBeforeComma: true in /home/cpisz/myproject/.clang-format and therefore your ClangFormat options list this option as false.

 

 

0
Comment actions Permalink

Explicitly adding "BreakConstructorInitializersBeforeComma: true" made no difference and it is still listed as false.

0
Comment actions Permalink

Please remove BreakConstructorInitializersBeforeComma: true from /home/cpisz/myproject/.clang-format.

What value do you have for BreakConstructorInitializers in /home/cpisz/myproject/.clang-format?

0
Comment actions Permalink

Whether

  • BreakConstructorInitializersBeforeComma: true is added to the file
  • BreakConstructorInitializersBeforeComma: false is added to the file
  • or there is no line specifying BreakConstructorInitializersBeforeComma at all

, it still lists BreakConstructorInitializersBeforeComma: false in all three cases. One would think it is not evaluating the file at all, however, as mentioned in the original post, if I select view clang format options instead of edit, it says it is from

# ClangFormatConfigureSource: 'clang-format-file:///home/cpisz/myproject/.clang-format'

which is the same file.

0
Comment actions Permalink

Please answer my question:

What value do you have for BreakConstructorInitializers (it's a different option) in /home/cpisz/myproject/.clang-format?

0
Comment actions Permalink
BreakConstructorInitializers: BeforeComma
0
Comment actions Permalink

On my side the situation is the following:

If I have BreakConstructorInitializers: BeforeComma in .clang-format, I consequently have BreakConstructorInitializers: BeforeComma in "ClangFormat options for main.cpp". And when I do `Code | Reformat Code` for 

it turns into

Does this work differently for you?

0
Comment actions Permalink

This works differently for me. 
Screenshots attached of before and after 'reformat code'

0
Comment actions Permalink

Please send your .clang-format file to clion-support at jetbrains.com.

0
Comment actions Permalink

For completeness, I want to document that I found through discussion in email and research online that clion uses clangd and does custom things with the formatter to allow for hints and refactor functionality, etc. Pointing CLion at your own clang-format binary is not an option. So, if you need to use a format file that was targeted to an earlier version that differs from that which Clion is using, your formatting might not come out correct.

0
Comment actions Permalink

A feature request to support custom clang-format binary - https://youtrack.jetbrains.com/issue/CPP-15236. Is also describes workarounds. 

0

Please sign in to leave a comment.