1

I am using VSCode with the clangd extension for C++ development (through Remote SSH extension). I have my .clang-format and .clang-tidy files with the rules I want to enforce. If I format the code manually (either Ctrl-Alt-F or right click -> Format document) the code gets formatted without any issues. Same if I manually go for the clang-tidy suggested fixes (Ctrl-.). Instead, if I save, nothing happens.

These are my settings overrides for C++. I tried moving them inside the different levels (User, Remote, Workspace) but there is no difference.

"[cpp]": {
  "editor.codeActionsOnSave": {
    "source.fixAll": true,
  },
  "editor.formatOnSave": true,
  "editor.defaultFormatter": "llvm-vs-code-extensions.vscode-clangd"
}

The same rules for ESLint or rustfmt work with no issues.

antogilbert
  • 176
  • 2
  • 12
  • Could you share [clangd logs](https://clangd.llvm.org/troubleshooting#gathering-logs) from an editing session where you open a file, introduce a formatting error by typing, and then save the file? – HighCommander4 Nov 09 '22 at 23:40
  • As for the code action part, this may be a case of missing support in clangd. Could you give a code example with a fix that you can manually accept with `Ctrl-.`? – HighCommander4 Nov 09 '22 at 23:41
  • Interestingly, at work it does not autoformat, but at home it does, despite having the same configuration. WIll need to dig deeper. @HighCommander4 an example of code is the following: ` int x{3}; if (x == 3) { return; } else { x = 4; }` The rule is `readability-else-after-return`. The else has a squiggly line underneath because the else block should not be there. – antogilbert Nov 10 '22 at 21:41

1 Answers1

0

I investigated the quick-fix issue a bit, and found that "editor.codeActionsOnSave": { "source.fixAll": true } relies on a new Language Server Protocol (LSP) enhancement added in LSP 3.17 (code actions with type source.fixAll).

Clangd does not currently support this, but it seems to me like it shouldn't be too difficult to add support for it, as clangd already classifies quick-fixes based on whether they're suitable for being applied automatically (e.g. Select All --> "Auto Fix..." makes use of this).

I filed https://github.com/clangd/clangd/issues/1446 with some more details.

HighCommander4
  • 50,428
  • 24
  • 122
  • 194