Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

auto update include path #795

Merged
merged 2 commits into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
61 changes: 61 additions & 0 deletions src/arduino/arduino.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,67 @@ export class ArduinoApp {

}

public tryToUpdateIncludePaths() {
const configFilePath = path.join(ArduinoWorkspace.rootPath, constants.CPP_CONFIG_FILE);
if (!fs.existsSync(configFilePath)) {
return;
}
const cppConfigFile = fs.readFileSync(configFilePath, "utf8");
const cppConfig = JSON.parse(cppConfigFile) as {configurations: Array<{includePath: string[], forcedInclude: string[]}>};
const libPaths = this.getDefaultPackageLibPaths();
const defaultForcedInclude = this.getDefaultForcedIncludeFiles();
const configuration = cppConfig.configurations[0];

let cppConfigFileUpdated = false;
// cpp exntension changes \\ to \\\\ in paths in JSON string, revert them first
configuration.includePath = configuration.includePath.map((path) => path.replace(/\\\\/g, "\\"));
configuration.forcedInclude = configuration.forcedInclude.map((path) => path.replace(/\\\\/g, "\\"));

for (const libPath of libPaths) {
if (configuration.includePath.indexOf(libPath) === -1) {
cppConfigFileUpdated = true;
configuration.includePath.push(libPath);
}
}
for (const forcedIncludePath of defaultForcedInclude) {
if (configuration.forcedInclude.indexOf(forcedIncludePath) === -1) {
cppConfigFileUpdated = true;
configuration.forcedInclude.push(forcedIncludePath);
}
}

// remove all unexisting paths
for (let pathIndex = 0; pathIndex < configuration.includePath.length; pathIndex++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better not deleting content from user file

let libPath = configuration.includePath[pathIndex];
if (libPath.indexOf("${workspaceFolder}") !== -1) {
continue;
}
if (/\*$/.test(libPath)) {
libPath = libPath.match(/^[^\*]*/)[0];
}
if (!fs.existsSync(libPath)) {
cppConfigFileUpdated = true;
configuration.includePath.splice(pathIndex, 1);
pathIndex--;
}
}
for (let pathIndex = 0; pathIndex < configuration.forcedInclude.length; pathIndex++) {
const forcedIncludePath = configuration.forcedInclude[pathIndex];
if (forcedIncludePath.indexOf("${workspaceFolder}") !== -1) {
continue;
}
if (!fs.existsSync(forcedIncludePath)) {
cppConfigFileUpdated = true;
configuration.forcedInclude.splice(pathIndex, 1);
pathIndex--;
}
}

if (cppConfigFileUpdated) {
fs.writeFileSync(configFilePath, JSON.stringify(cppConfig, null, 4));
}
}

// Add selected library path to the intellisense search path.
public addLibPath(libraryPath: string) {
let libPaths;
Expand Down
1 change: 1 addition & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ export async function activate(context: vscode.ExtensionContext) {
SerialMonitor.getInstance().initialize();
}
ArduinoContext.boardManager.updateStatusBar(true);
ArduinoContext.arduinoApp.tryToUpdateIncludePaths();
vscode.commands.executeCommand("setContext", "vscode-arduino:showExampleExplorer", true);
})();
}
Expand Down