From d7db6d589300ed20658bb2feb60d3aedaff3fed0 Mon Sep 17 00:00:00 2001 From: Yang Gang Date: Mon, 16 Dec 2024 14:27:33 +0800 Subject: [PATCH] Add clangd config check in checkCompileCommandsConfig() if vscode-clangd installed but `--compile-commands-dir=` is not setting or setting is not "${workspaceFolder}/.edkCode/", then update clangd.arguments to include: "--compile-commands-dir=${workspaceFolder}/.edkCode/". if vscode-clangd is not installed, still call infoMissingCompilesCommandCpp() Signed-off-by: Yang Gang --- src/ui/messages.ts | 4 ++-- src/utils.ts | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/ui/messages.ts b/src/ui/messages.ts index 7a528ad..5de9999 100644 --- a/src/ui/messages.ts +++ b/src/ui/messages.ts @@ -22,7 +22,7 @@ export function infoMissingCompileInfo(){ } export function infoMissingCompilesCommandCpp(){ - void vscode.window.showInformationMessage("Build contains compile_commands.json but C++ is not configured", "Help").then(async selection => { + void vscode.window.showInformationMessage("Build contains compile_commands.json but vscode-cpptools/vscode-clangd is not configured/installed", "Help").then(async selection => { if (selection === "Help"){ await vscode.env.openExternal(vscode.Uri.parse("https://github.com/intel/Edk2Code/wiki/Index-source-code#compile_commandsjson")); } @@ -31,7 +31,7 @@ export function infoMissingCompilesCommandCpp(){ export async function updateCompilesCommandCpp():Promise{ return new Promise(async (resolve, reject) => { - return vscode.window.showInformationMessage("c_cpp_properties.json points to wrong compile_commands.json", "Fix").then(async selection => { + return vscode.window.showInformationMessage("c_cpp_properties.json/settings.json points to wrong compile_commands.json", "Fix").then(async selection => { if (selection === "Fix"){ resolve(true); }else{ diff --git a/src/utils.ts b/src/utils.ts index ed9ec37..c597ccd 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -398,6 +398,7 @@ export async function itsPcdSelected(document: vscode.TextDocument, position: vs export async function checkCompileCommandsConfig(){ let cCppPropertiesPath = path.join(gWorkspacePath, ".vscode", "c_cpp_properties.json"); + const clangdExtension = vscode.extensions.getExtension('llvm-vs-code-extensions.vscode-clangd'); const expectedPath = path.join("${workspaceFolder}", ".edkCode", "compile_commands.json"); if (fs.existsSync(cCppPropertiesPath)) { @@ -412,7 +413,26 @@ export async function checkCompileCommandsConfig(){ fs.writeFileSync(cCppPropertiesPath, JSON.stringify(cProperties,null,4)); } } - }else{ + } else if (clangdExtension) { + const expectedArgument = "--compile-commands-dir=${workspaceFolder}/.edkCode/"; + const clangdConfiguration = vscode.workspace.getConfiguration('clangd'); + const clangdArguments = clangdConfiguration.get('arguments') || []; + const existingIndex = clangdArguments.findIndex(arg => arg.startsWith('--compile-commands-dir=')); + let updatedArguments = [...clangdArguments]; + if (existingIndex === -1) { + let update = await updateCompilesCommandCpp(); + if (update) { + updatedArguments.push(expectedArgument); + await clangdConfiguration.update('arguments', updatedArguments, vscode.ConfigurationTarget.Workspace); + } + } else if (!clangdArguments.includes(expectedArgument)) { + let update = await updateCompilesCommandCpp(); + if (update) { + updatedArguments[existingIndex] = expectedArgument; + await clangdConfiguration.update('arguments', updatedArguments, vscode.ConfigurationTarget.Workspace); + } + } + } else { infoMissingCompilesCommandCpp(); } }