clangd / vscode-clangd

Visual Studio Code extension for clangd
https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd
MIT License
592 stars 97 forks source link

[readme] Clang-format integration #621

Closed kotborealis closed 2 months ago

kotborealis commented 2 months ago

Definitely not a bug, but I wanted to ask something and could not find a better place to do so.

In readme there's info about clang-format:

clangd uses the clang-format engine. You can format a file or the selection. When "Format on Type" is enabled in the settings, pressing enter will cause clangd to format the old line and semantically reindent.

However, I could not find anything related in sources. Is this provided by some other parts of vscode? Does it use default cpptools formatting? Or does it use LSP formatting capabilities?

To clarify, I'm searching for a way to setup custom binary for formatting, to format code using known version of clang-format. Is specifying clangd binary enough?

HighCommander4 commented 2 months ago

Does it use default cpptools formatting? Or does it use LSP formatting capabilities?

It uses LSP formatting capabilities. For the "pressing enter will cause clangd to format the old line" part in particular, it uses the on-type formatting request. The regular formatting request that you linked to is used when you invoke the "Format Document" command, or on file save if you have format-on-save enabled.

To clarify, I'm searching for a way to setup custom binary for formatting, to format code using known version of clang-format. Is specifying clangd binary enough?

The relevant vscode setting is "editor.defaultFormatter". If you have the vscode-clangd plugin installed, there should be an option to set this to "llvm-vs-code-extensions.vscode-clangd", which will use clangd for formatting.

You can also install a dedicated clang-format plugin like https://marketplace.visualstudio.com/items?itemName=xaver.clang-format and set "editor.defaultFormatter" to that. You can do this if you want to use a particular version of clang-format, and a different version of clangd (for non-formatting features) at the same time.

kotborealis commented 2 months ago

Thank you for the detailed answer! I'll close this now.