syl20bnr / spacemacs

A community-driven Emacs distribution - The best editor is neither Emacs nor Vim, it's Emacs *and* Vim!
http://spacemacs.org
GNU General Public License v3.0
23.58k stars 4.9k forks source link

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

Open samwalls opened 4 years ago

samwalls commented 4 years ago

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:

thanhvg commented 4 years ago

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.

alexey0308 commented 4 years ago

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.

smile13241324 commented 3 years ago

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

lebensterben commented 2 years ago

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 commented 2 years ago

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:

smile13241324 commented 2 years ago

Sounds good

janemba commented 9 months ago

is it fixed ? if yes how ?