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

Commit b719777

Browse files
authored
auto update include path (#795)
1 parent 71d6980 commit b719777

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/arduino/arduino.ts

+62
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,68 @@ export class ArduinoApp {
304304

305305
}
306306

307+
public tryToUpdateIncludePaths() {
308+
const configFilePath = path.join(ArduinoWorkspace.rootPath, constants.CPP_CONFIG_FILE);
309+
if (!fs.existsSync(configFilePath)) {
310+
return;
311+
}
312+
const cppConfigFile = fs.readFileSync(configFilePath, "utf8");
313+
const cppConfig = JSON.parse(cppConfigFile) as {configurations: Array<{includePath: string[], forcedInclude: string[]}>};
314+
const libPaths = this.getDefaultPackageLibPaths();
315+
const defaultForcedInclude = this.getDefaultForcedIncludeFiles();
316+
const configuration = cppConfig.configurations[0];
317+
318+
let cppConfigFileUpdated = false;
319+
// cpp exntension changes \\ to \\\\ in paths in JSON string, revert them first
320+
configuration.includePath = configuration.includePath.map((path) => path.replace(/\\\\/g, "\\"));
321+
configuration.forcedInclude = configuration.forcedInclude.map((path) => path.replace(/\\\\/g, "\\"));
322+
323+
for (const libPath of libPaths) {
324+
if (configuration.includePath.indexOf(libPath) === -1) {
325+
cppConfigFileUpdated = true;
326+
configuration.includePath.push(libPath);
327+
}
328+
}
329+
for (const forcedIncludePath of defaultForcedInclude) {
330+
if (configuration.forcedInclude.indexOf(forcedIncludePath) === -1) {
331+
cppConfigFileUpdated = true;
332+
configuration.forcedInclude.push(forcedIncludePath);
333+
}
334+
}
335+
336+
// remove all unexisting paths
337+
// concern mistake removal, comment temporary
338+
// for (let pathIndex = 0; pathIndex < configuration.includePath.length; pathIndex++) {
339+
// let libPath = configuration.includePath[pathIndex];
340+
// if (libPath.indexOf("${workspaceFolder}") !== -1) {
341+
// continue;
342+
// }
343+
// if (/\*$/.test(libPath)) {
344+
// libPath = libPath.match(/^[^\*]*/)[0];
345+
// }
346+
// if (!fs.existsSync(libPath)) {
347+
// cppConfigFileUpdated = true;
348+
// configuration.includePath.splice(pathIndex, 1);
349+
// pathIndex--;
350+
// }
351+
// }
352+
// for (let pathIndex = 0; pathIndex < configuration.forcedInclude.length; pathIndex++) {
353+
// const forcedIncludePath = configuration.forcedInclude[pathIndex];
354+
// if (forcedIncludePath.indexOf("${workspaceFolder}") !== -1) {
355+
// continue;
356+
// }
357+
// if (!fs.existsSync(forcedIncludePath)) {
358+
// cppConfigFileUpdated = true;
359+
// configuration.forcedInclude.splice(pathIndex, 1);
360+
// pathIndex--;
361+
// }
362+
// }
363+
364+
if (cppConfigFileUpdated) {
365+
fs.writeFileSync(configFilePath, JSON.stringify(cppConfig, null, 4));
366+
}
367+
}
368+
307369
// Add selected library path to the intellisense search path.
308370
public addLibPath(libraryPath: string) {
309371
let libPaths;

src/extension.ts

+1
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ export async function activate(context: vscode.ExtensionContext) {
308308
SerialMonitor.getInstance().initialize();
309309
}
310310
ArduinoContext.boardManager.updateStatusBar(true);
311+
ArduinoContext.arduinoApp.tryToUpdateIncludePaths();
311312
vscode.commands.executeCommand("setContext", "vscode-arduino:showExampleExplorer", true);
312313
})();
313314
}

0 commit comments

Comments
 (0)