vscode-cpptools
vscode-cpptools copied to clipboard
Feature request: Path to clang-format file
Give us the option to specify the path to the clang-format file on our settings.json. Code bases usually keep one in order to standardize coding style, but would be amazing if I could format the files with my own .clang-format file without touching the file from the code base. As an extension of this, an option to choose which .clang-format file to use would be most needed when selecting Format Document in the Show All Commands list. Something like:
Format Document (User defined) Format Document (Standard)
Where Standard would use the standard .clang-format searching and fallback rules, and User defined is the file defined in the settings file.
The current workaround is to set the C_Cpp.clang_format_style (to an inline { ... } format and not a file path). Is there a reason that isn't sufficient?
I don't know. Maybe because I intend on overriding a lot of rules, making inline overriding silly; cluttering the settings file with the whole clang-format stuff is also silly; and because, like you said, it's a workaround.
@KaeLL : Agreed. For comparison with other similar plugins, the Uncrustify plugin has this option to set your uncrustify.cfg file path (uncrustify.configPath).
Sorry for dumping, but there is any workaround for this idea?
@KennFatt Did you see https://github.com/microsoft/vscode-cpptools/issues/3675#issuecomment-494549010 ?
Hi, I was looking for this as well, and would like to try my hand at adding it myself :) any relevant guidance that could help me other than what appears in CONTRIBUTING.md?
anyway, will try to get it done and simply open a PR...
@AdamEr8 master branch might be broken without our updated binaries from our pending 0.29.0-insiders2, but you could base it off release. I'm not 100% sure how this would be implemented...I guess converting the .clang-format file to a clang_format_style?. FYI, we send clang_format_* settings to our closed source cpptools process, so ideally your PR wouldn't require any cpptools process changes.
I'm not 100% sure how this would be implemented...I guess converting the .clang-format file to a clang_format_style?
Yes, I've been wondering about it as well. It's either converting to clang_format_style, or could be to back-up any .clang-format in the running dir, copy the custom file, apply clang-format, and restore to old state, but I think it's obvious using the dedicated cmd arg is cleaner and safer.
one problem with converting to clang_format_style, is that it doesn't have any notion of "sections" for different languages like a .clang-format has. So should I take the global/common section + Cpp section, and ignore the rest? (does the C/C++ Extension only apply clang-formatting to C/C++ files..?)
The cpptools process gets VS Code format requests from VS Code via the LSP (https://microsoft.github.io/language-server-protocol/specification#textDocument_formatting), which is registered to handle C/C++ at https://github.com/microsoft/vscode-cpptools/blob/release/Extension/src/LanguageServer/client.ts#L1202 , so we don't format other languages, and having a non-Cpp "Language: " section in a .clang-format file will generate an error, so just using the global+Cpp sections seems good.
Thanks for the detailed explanation! much appreciated.
In my case I need to reuse a clang-format configuration, which is placed inside a git submodule.
The current version of clang-format supports passing a configuration file via --style='file:<format_file_path>' (docs). Unfortunately, if I set "C_Cpp.clang_format_style": "file:${workspaceFolder}/<some_submodule>/.clang-format", the variable is not expanded.
I think allowing to expand variables inside of C_Cpp.clang_format_style and changing it to an array would solve the original feature request as well, but I am not familiar enough with the codebase to implement it.
@nowh3r3 You should be able to use file:./<some_submodule>/.clang-format, i.e. the file path can be relative to the working directory, which seems to be the workspace folder -- let me know if that isn't working for you or if there's still a reason to expand ${workspaceFolder}.
What would be the point of making the clang_format_style an array?
When I try to use a relative path, I get the following output:
Formatting failed:
"C:\Program Files\LLVM\bin\clang-format.exe" -style=file:./<some_submodule>/.clang-format -fallback-style=LLVM -assume-filename=<some_file_to_be_formatted>
Interestingly enough, when I copy the command and execute it in a terminal, it seems to work fine:
PS C:\<my_workspace>> &"C:\Program Files\LLVM\bin\clang-format.exe" -style=file:./<some_submodule>/.clang-format -fallback-style=LLVM -assume-filename=<some_file_to_be_formatted>
# clang-format waits for input on stdin
Maybe I should have mentioned that I have set clang_format_path to"C:\\Program Files\\LLVM\\bin\\clang-format.exe", because this probably interferes with the working directory?
What would be the point of making the clang_format_style an array?
That was an idea based my (mis-)interpretation of the original feature request:
Something like:
Format Document (User defined) Format Document (Standard)
Where Standard would use the standard .clang-format searching and fallback rules, and User defined is the file defined in the settings file.
I thought the request was about choosing between two or more custom configurations, therefore with an array this would translate to two or more entries in clang_format_style.
In hindsight I think this is overkill, because the default configuration does not need an entry and having multiple entries circumvents clang-format's built in detection of configuration files (i.e. detecting it via the directory structure).
The ability to specify a path to a .clang-format file was added to clang-format 14 in 1.12.4. 1.13.2 has an additional fix for resolving ${workspaceFolder} in the C_Cpp.clang_format_style setting: https://github.com/microsoft/vscode-cpptools/issues/9786 . e.g. use file:<path>/.clang-format to reference a specific path` in that setting.