Qt Creator doesn't use .clang-format (with ClangFormat plugin)
-
I'm trying to get Qt Creator to format code according to configuration in file .clang-format in the project directory...
The ClangFormat plugin is enabled, and format options are as far as I can tell set up the right way in Preferences/C++/Code Style. The message
Please note that the project includes a .clang-format file, which will be used for code indenting and formatting
also appears.
It's pretty clear that the formatting settings in the file are not actually used, though. Qt Creator formats the code differently from the clang-format command in several different ways, and most notably, if I specify e.g. "IndentWidth: 2" in .clang-format, I still get an indent of 4 (the Qt Creator default.)
Any idea what's going on? Is there a way to debug the problem?
This is with Qt Creator 14.0.1 under Windows. I've successfully used the plugin under Linux in the past, with (as far as I can tell) the same Qt Creator settings.
-
@toralf I too have always been confused by clang-format support.
I have just updated Creator to your version and I think it has changed a little since I last looked.
I am sure that the message you mentioned used to appear even if there wasn't a
.clang-format
in the project root directory. I now see the message only if I place a.clang-format
file there.However, the following is what I see in the preferences. The information shown in the ClangFormat tab is not consistent with what is in my file. For example, the
AccessModifierOffset
is -2 in my file. -
-
@Bob64
Yes, I get that, too. However, the real question is, does the actual formatting work for you?I think on my other system (which I don't have in front of me right now), the preferences content is equally confusing or incorrect, but when the code is formatted - "as I type" or when pressing tab or using Ctrl+I or on save depending on settings - it turns out as expected. As in, the format is consistent with the .clang-format file.
On the set-up I was talking about here, I don't know what formatting rules are used. It's not the ones in the .clang-format file and probably not what you see under ClangFormat in the dialog either. It's more like the "custom settings", although I also get somewhat confusing results when I try to change those.
-
@toralf no it doesn't seem to work for me. I have tried changing the various indent and tab widths to large values that would be very obvious. I have restarted Creator, but I see no sign of the indents taking effect while I am editing a file or when I save it.
-
It's not clear how the "custom settings" and clang-format are meant to interact. If I am in clang-format mode and enable "Use custom settings", the ClangFormat tab becomes editable. However if I make some changes and export the settings using the button (as indicated below), I don't get a
.clang-format
file exported but an XML file. This seems to be more related to Qt's traditional code formatter. -
OK. Thanks.
Perhaps the ClangFormat tab is just supposed to show settings for the traditional formatter in clang-format syntax (which will be unrelated to the
.clang-format
file)? But I agree, it's quite unclear how this is meant to work. -
-
Alternatively you can use the Beautifier plugin with
clang-format
. Qt Creator ships also theclang-format
executable that you can use.This way you would use the
clang-format.exe
and this is guaranteed to only use the.clang-format
file. -
Hi @Bob64 @toralf, I know that it can be confusing, but currently It is not possible to edit your .clang-format file from the project directory in the CodeStyle settings tab. You should either export it, or in TextEditor do the right click and choose "Open Used .clang-format Configuration File", and edit it in the TextEditor.
P.s. The export button works only in 15. -
I know about the Beautifier and have used is as a backup solution. I would prefer to get the ClangFormat plugin working, though, as I'd like to be able to "format as I type" or "format on tab".
I never noticed that a clang-format executable was included with Qt Creator. I actually have a different one from an LLVM package, too - maybe that's the problem? Like, perhaps the plugin picks the "wrong" version of the program and doesn't like it somehow? I imagined that the plugin(s) would use a library rather than the actual clang-format command, but perhaps that's not the case.