clang-format on save doesn't alwways work.



I am finding clang-format on save seems to be a bit erratic and I've managed to come up with a reproducible case:

I have a source file with the following line in it:


If I place the cursor immediately after the '|' character, then hit return, then Ctrl-S, the extra newline is successfully removed before saving, so the line is restored to the above.

However, if I place the cursor immediately before the following 'G', ie: after the space after the '|', then hit return, then Ctrl-S, the newline is NOT removed, the line remains split and is saved like that.




Hi Mark. I did not know that CLion supports clang-format. How can I turn it on? I tried searching in CLion configuration, I see plugin "ClangFormat" is installed, but I cannot find the option "use it on save"


It's in: File->Settings->Tools->Actions on Save->Reformat code.

You can also reformat code manually with Code->Reformat code.

Actually, I've just noticed that manually reformatting does still work when reformatting on save fails. Just quickly tried it again, and reformat on save is still consistently failing if I insert a line-break after a space, but not before (see first post), but Code->Reformat code always works. Just in case any of that helps!




Mark Sibly can the IDE reformat the code if you click the IDE main menu File | Save All?
Some keymap like VS Code keymaps use "Save Document" for Ctrl+S by default. Please check if File | Settings | Keymap uses the Ctrl+S for the "Save Document" action. In this case, you could change the Ctrl+S to Save All action as a workaround.


Wi Wi here is the information about ClangFormat in CLion -

In order to enable ClangFormat in CLion, select the Enable ClangFormat checkbox in Settings/Preferences | Editor | Code Style.


>Mark Sibly can the IDE reformat the code if you click the IDE main menu File | Save All?

No, behaviour is the same, and 'Save All' shows 'Ctrl S' as it's shortcut. I've done very little in the way of configuring CLion, basically just changed the theme/font, turned on 'format on save' and disabled the buggy clangd warning - everything else is pretty much the way I like it.

I didn't know there even was a 'save document' and I can't see one in the File menu, only Save All. I was a bit surprised when I realized that Ctrl S was 'save all' but I like it now. Still, 'save document' may come in useful one day so it'd be nice to know were it was I guess.



Mark Sibly "Save Document" is accessible via `Help | Find Action...`.


Please sign in to leave a comment.