spacemacs icon indicating copy to clipboard operation
spacemacs copied to clipboard

make c-c++ layer respect .clang-format file more closely

Open samwalls opened this issue 5 years ago • 8 comments

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

samwalls avatar Jan 29 '20 16:01 samwalls

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.

thanhvg avatar Jan 29 '20 17:01 thanhvg

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.

alexey0308 avatar Jul 03 '20 10:07 alexey0308

Does not look like I find time to do this, I will add the label for help wanted.

smile13241324 avatar Mar 20 '21 21:03 smile13241324

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.

lebensterben avatar Feb 10 '22 19:02 lebensterben

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

lebensterben avatar Feb 10 '22 19:02 lebensterben

Sounds good

smile13241324 avatar Feb 11 '22 08:02 smile13241324

is it fixed ? if yes how ?

janemba avatar Oct 18 '23 16:10 janemba