Skip to content

Commit

Permalink
If the default for iwyu.compile_commands is unchanged, but the file…
Browse files Browse the repository at this point in the history
… is not found, then the extension will also try `${workspaceFolder}/build/compile_commands.json`.

This should address #5 in slightly better way as it supports both common places.
  • Loading branch information
helly25 committed Jul 7, 2024
1 parent 78a00fb commit a763579
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

# [0.0.16]

If the default for `iwyu.compile_commands` is unchanged, but the file is not found, then the extension will also try `${workspaceFolder}/build/compile_commands.json`.

# [0.0.15]

Fixed https://github.com/helly25/vscode-iwyu/issues/2. When the `compile_commands.json` fiel cannot be found the extension would not be able to initialize.
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"Include What You Use",
"include-what-you-use"
],
"version": "0.0.15",
"version": "0.0.16",
"publisher": "helly25",
"license": "Apache-2.0",
"repository": {
Expand Down Expand Up @@ -42,7 +42,7 @@
"iwyu.compile_commands": {
"type": "string",
"default": "${workspaceFolder}/compile_commands.json",
"markdownDescription": "Path to `compile_commands.json` file (supports `${workspaceFolder}` and `${workspaceRoot}`)."
"markdownDescription": "Path to `compile_commands.json` file (supports `${workspaceFolder}` and `${workspaceRoot}`). If the default is unchanged, but the file is not found, then the extension will also try `${workspaceFolder}/build/compile_commands.json`."
},
"iwyu.filter_iwyu_output": {
"type": "string",
Expand Down Expand Up @@ -183,16 +183,16 @@
"test": "node ./out/test/runTest.js"
},
"devDependencies": {
"@types/vscode": "^1.78.0",
"@types/glob": "^8.1.0",
"@types/mocha": "^10.0.1",
"@types/node": "16.x",
"@types/node": "^16.18.101",
"@types/vscode": "^1.78.0",
"@typescript-eslint/eslint-plugin": "^5.59.1",
"@typescript-eslint/parser": "^5.59.1",
"@vscode/test-electron": "^2.3.0",
"eslint": "^8.39.0",
"glob": "^8.1.0",
"mocha": "^10.2.0",
"typescript": "^5.0.4",
"@vscode/test-electron": "^2.3.0"
"typescript": "^5.0.4"
}
}
}
37 changes: 31 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class CompileCommandsData {
this.compileCommands[fname] = new CompileCommand(entry, directory);
}
}
catch(err) {
catch (err) {
let error = "Bad `iwyu.compile_commands` setting";
log(ERROR, error + "'" + compileCommandsJson + "': " + err);
vscode.window.showErrorMessage(error + ". Please check your settings and ensure the `compile_commands.json` file is in the specified location.<br/><br/>" + err);
Expand Down Expand Up @@ -285,11 +285,36 @@ class ConfigData {
return this.compileCommandsData.compileCommands[fname] || null;
}

replaceWorkspaceVars(input: string): string {
return input.replace("${workspaceRoot}", this.workspacefolder)
.replace("${workspaceFolder}", this.workspacefolder)
}

compileCommandsJson(): string {
return this.config
.get("compile_commands", "${workspaceFolder}/XXX/compile_commands.json")
.replace("${workspaceRoot}", this.workspacefolder)
.replace("${workspaceFolder}", this.workspacefolder);
let compileCommandsJsonDefault = "${workspaceFolder}/compile_commands.json";
let compileCommandsJson = this.config.get("compile_commands", compileCommandsJsonDefault);
log(DEBUG, "Got compileCommandsJson = '" + compileCommandsJson + "'.");
let isDefault = compileCommandsJson === compileCommandsJsonDefault;
log(DEBUG, "IsDefault: " + isDefault);
compileCommandsJson = this.replaceWorkspaceVars(compileCommandsJson);
try {
fs.statSync(compileCommandsJson);
log(DEBUG, "Using compileCommandsJson = '" + compileCommandsJson + "'.");
}
catch (err) {
if (isDefault) {
try {
let test = this.replaceWorkspaceVars("${workspaceFolder}/build/compile_commands.json");
fs.statSync(test);
compileCommandsJson = test;
log(DEBUG, "Using alternative compileCommandsJson = '" + compileCommandsJson + "'.");
}
catch (err) {
// Ignore, caught later.
}
}
}
return compileCommandsJson;
}

updateConfig() {
Expand All @@ -304,7 +329,7 @@ class ConfigData {
this.compileCommandsData = this.parseCompileCommands();
}
}
catch(err) {
catch (err) {
log(ERROR, "Bad `iwyu.compile_commands` setting: '" + compileCommandsJson + "': " + err);
}
}
Expand Down

0 comments on commit a763579

Please sign in to comment.