Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup compile commands directory for the clangd extension #210

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
29 changes: 28 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
() => {},
);
}
}
});

Expand Down
5 changes: 4 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down