diff --git a/package.json b/package.json index 855d670..2dcf3b5 100644 --- a/package.json +++ b/package.json @@ -241,11 +241,12 @@ "items": { "enum": [ "ms-vscode.cpptools", + "llvm-vs-code-extensions.vscode-clangd", "rust-lang.rust-analyzer" ] }, "default": true, - "markdownDescription": "Automatically setup other extensions to use files generated by Meson. Set it to `false` if for example `.vscode/settings.json` is committed into git and should not be modified. It can also be an array of extension IDs of the following:\n- [`ms-vscode.cpptools`](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools)\n- [`rust-lang.rust-analyzer`](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer)." + "markdownDescription": "Automatically setup other extensions to use files generated by Meson. Set it to `false` if for example `.vscode/settings.json` is committed into git and should not be modified. It can also be an array of extension IDs of the following:\n- [`ms-vscode.cpptools`](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools)\n- [`llvm-vs-code-extensions.vscode-clangd`](https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd)\n- [`rust-lang.rust-analyzer`](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer)." }, "mesonbuild.Swift-MesonLSP.others.ignoreDiagnosticsFromSubprojects": { "type": [ diff --git a/src/extension.ts b/src/extension.ts index e22807c..0abf64f 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -135,9 +135,36 @@ export async function activate(ctx: vscode.ExtensionContext) { const compileCommandsFile = `${buildDir}/compile_commands.json`; whenFileExists(ctx, compileCommandsFile, async () => { + const conf = vscode.workspace.getConfiguration(); if (shouldModifySetting("ms-vscode.cpptools")) { const conf = vscode.workspace.getConfiguration("C_Cpp"); - conf.update("default.compileCommands", compileCommandsFile, vscode.ConfigurationTarget.Workspace); + conf.update("C_Cpp.default.compileCommands", compileCommandsFile, vscode.ConfigurationTarget.Workspace); + } + if (shouldModifySetting("llvm-vs-code-extensions.vscode-clangd")) { + let args = undefined; + const wanted = `--compile-commands-dir=${buildDir}`; + const current = conf.get("clangd.arguments"); + if (current && Array.isArray(current)) { + // if wanted is already in current, leave args undefined to prevent a config update + if (!current.includes(wanted)) { + const found = current.findIndex((arg) => arg.startsWith("--compile-commands-dir=")); + if (found !== -1) { + current[found] = wanted; + args = current; + } else { + args = [...current, wanted]; + } + } + } else { + args = [wanted]; + } + + if (args) { + conf.update("clangd.arguments", args, vscode.ConfigurationTarget.Workspace).then( + () => vscode.commands.executeCommand("clangd.restart"), + () => {}, + ); + } } }); diff --git a/src/types.ts b/src/types.ts index 06582bc..a6e5988 100644 --- a/src/types.ts +++ b/src/types.ts @@ -9,7 +9,10 @@ export type LinterConfiguration = { }; export type LanguageServer = "Swift-MesonLSP" | null; -export type ModifiableExtension = "ms-vscode.cpptools" | "rust-lang.rust-analyzer"; +export type ModifiableExtension = + | "ms-vscode.cpptools" + | "llvm-vs-code-extensions.vscode-clangd" + | "rust-lang.rust-analyzer"; export interface ExtensionConfiguration { configureOnOpen: boolean | "ask";