CLion indents comments above else if and else statements

Answered

Let us say we have the following code in CLion:

//Comment about stuff in first condition block
if(condition_one) {
   
//Do stuff
}
//Comment about stuff in second condition block
else if(condition_two) {
   
//Do stuff
}
//Comment about stuff in third condition block
else {
   
//Do stuff
}

When I apply the reformatter, it becomes this:

//Comment about stuff in first condition block
if(condition_one) {
   
//Do stuff
}
//Comment about stuff in second condition block
else if(condition_two) {
   
//Do stuff
}
//Comment about stuff in third condition block
else {
   
//Do stuff
}

How can I modify the Code Style for C programs such that the first style is allowed and the second prohibited?

The same issue also applies for comments above the case keyword in switch statements.

8 comments
Comment actions Permalink

Hello!

There is the "Comment at first column" option in File | Settings | Editor | Code Style | C/C++ > Wrapping and Braces > Keep when reformatting. Does it help?

0
Comment actions Permalink

Hello. Unfortunately, that seems to be a no as seen in the following video link: https://i.imgur.com/r51Af4i.mp4

0
Comment actions Permalink

Unfortunately, it's not possible to do with CLion's built-in formatter.

You can work around it by enabling clang-format support instead: click on the "indent" widget in the bottom right corner of the IDE frame (it shows your indent size by default), and select the "Enable ClangFormat" option. You'll be offered to create a `.clang-format` file which will inherit most of your active code style. After that, the formatting actions will go through clang-format, which behaves as you've requested.

0
Comment actions Permalink

Hi, Anna. This seems to work for the most part. However, the .clangformat file appears to not have an option to keep indentation on empty lines.

As such, may I request the inclusion of the option to be able to enable/disable such indentation above else, else if, and case statements?

0
Comment actions Permalink

For anyone that accesses this thread, the only method available currently is Anna's suggested .clang-format file, which can create other formatting issues such as not allowing indentation on empty lines. (It is worth a try if you do not require indentation on empty lines or if you find that better than the former.)

Here is the issue in the tracker: https://intellij-support.jetbrains.com/hc/en-us/requests/3288784?page=1

Upvote and/or comment if you are interested in the issue.

0
Comment actions Permalink

Thanks for the clarification! The hyperlink text seems to be correct, but the hyperlink location seems to have some extra text that makes it fail.

0
Comment actions Permalink

Thanks! I corrected the link

1

Please sign in to leave a comment.