Skip to content

Commit

Permalink
update compile commands json proj conf change
Browse files Browse the repository at this point in the history
  • Loading branch information
brianignacio5 committed Sep 24, 2024
1 parent a4872a8 commit d18baa4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,7 @@ export async function activate(context: vscode.ExtensionContext) {
100
);
await getIdfTargetFromSdkconfig(workspaceRoot, statusBarItems["target"]);
await utils.setCCppPropertiesJsonCompileCommands(workspaceRoot);
ConfserverProcess.dispose();
});
});
Expand Down
70 changes: 52 additions & 18 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,6 @@ export async function createVscodeFolder(curWorkspaceFsPath: vscode.Uri) {
export async function setCCppPropertiesJsonCompilerPath(
curWorkspaceFsPath: vscode.Uri
) {
const cCppPropertiesJsonPath = path.join(
curWorkspaceFsPath.fsPath,
".vscode",
"c_cpp_properties.json"
);
const doesPathExists = await pathExists(cCppPropertiesJsonPath);
if (!doesPathExists) {
return;
}
const modifiedEnv = await appendIdfAndToolsToPath(curWorkspaceFsPath);
const idfTarget = modifiedEnv.IDF_TARGET || "esp32";
const gccTool = getToolchainToolName(idfTarget, "gcc");
Expand All @@ -258,22 +249,65 @@ export async function setCCppPropertiesJsonCompilerPath(
curWorkspaceFsPath.fsPath,
modifiedEnv
);
if (!compilerAbsolutePath) {
return;
}
let compilerRelativePath = compilerAbsolutePath.split(
modifiedEnv.IDF_TOOLS_PATH
)[1];
const settingToUse =
process.platform === "win32"
? "${config:idf.toolsPathWin}"
: "${config:idf.toolsPath}";

await updateCCppPropertiesJson(
curWorkspaceFsPath,
"compilerPath",
settingToUse + compilerRelativePath
);
}

export async function setCCppPropertiesJsonCompileCommands(
curWorkspaceFsPath: vscode.Uri
) {
const buildDirPath = idfConf.readParameter(
"idf.buildPath",
curWorkspaceFsPath
) as string;
const compileCommandsPath = path.join(buildDirPath, "compile_commands.json");

await updateCCppPropertiesJson(
curWorkspaceFsPath,
"compileCommands",
compileCommandsPath
);
}

export async function updateCCppPropertiesJson(
workspaceUri: vscode.Uri,
fieldToUpdate: string,
newFieldValue: string
) {
const cCppPropertiesJsonPath = path.join(
workspaceUri.fsPath,
".vscode",
"c_cpp_properties.json"
);
const doesPathExists = await pathExists(cCppPropertiesJsonPath);
if (!doesPathExists) {
return;
}
const cCppPropertiesJson = await readJSON(cCppPropertiesJsonPath);
if (
cCppPropertiesJson &&
cCppPropertiesJson.configurations &&
cCppPropertiesJson.configurations.length
) {
let compilerRelativePath = compilerAbsolutePath.split(
modifiedEnv.IDF_TOOLS_PATH
)[1];
const settingToUse =
process.platform === "win32"
? "${config:idf.toolsPathWin}"
: "${config:idf.toolsPath}";
cCppPropertiesJson.configurations[0].compilerPath =
settingToUse + compilerRelativePath;
const buildDirPath = idfConf.readParameter(
"idf.buildPath",
workspaceUri
) as string;
cCppPropertiesJson.configurations[0][fieldToUpdate] = newFieldValue;
await writeJSON(cCppPropertiesJsonPath, cCppPropertiesJson, {
spaces: vscode.workspace.getConfiguration().get("editor.tabSize") || 2,
});
Expand Down

0 comments on commit d18baa4

Please sign in to comment.