spacemacs
spacemacs copied to clipboard
make c-c++ layer respect .clang-format file more closely
I'm running on the develop branch of spacemacs, using the c-c++ and lsp layers.
I use clang-format for C/C++, I don't know how to configure c-c++ mode to respect more of the clang-format rules as I'm editing. My main issue at the moment is that emacs tries to auto-indent inside the top-level of a namespace scope, whereas if I format the buffer, I get the desired behaviour of no indentation. (I've tried using c-c++-enable-clang-support t
as referenced by this issue, but it doesn't do anything).
Similarly, I have to manually set the c-basic-offset
to match the project I'm working on. This information is readily available in a clang-format file, so I would imagine it's possible to auto-set these things when a clang-format file is discovered. (This is related to this issue however what I'm asking about has a wider scope).
Is there something I'm missing, or is support for this kind of thing not in spacemacs yet? If not, I think this would be a good feature. Is it feasible?
Other related links:
- https://stackoverflow.com/questions/39894233/extract-emacs-c-style-options-from-clang-format-style
In your SO link did you try the solution#2: https://stackoverflow.com/a/55784057 ? If it works we can add it to c-c++
layer.
This is likely due to difference in indentation of line and region functions: lsp assigns only indent-region function to lsp-format-region, but indent-line is c-indent-line, which is called upon newline or other actions.
Does not look like I find time to do this, I will add the label for help wanted.
The solution is easier than I thought.
First we already have c-c++-enable-clang-format-on-save
which applies clang-format
when you save a buffer.
The underlying clang-format
package simply calls clang-format
executable, which supports -style
CLI option that accepts either one of the format style or a .clang-format
file.
Therefore what we need here is to lookup for .clang-format
file in current directory or one of parent directories (up to the projectile root), and set clang-format-style
to the file name.
I propose to extend c-c++-enable-clang-format-on-save
and add an additional layer config variable c-c++-clang-format-style
, which accepts the following values:
- one of
llvm
,gnu
,google
,chromium
,microsoft
,mozilla
,webKit
- a filepath pointing to a
.clang-format
file -
auto
, try to discover a.clang-format
file in current project, and don't format if that's not found
Sounds good
is it fixed ? if yes how ?