Skip to content

Commit c6d051c

Browse files
committed
optimize intellisense provider
1 parent ee187bd commit c6d051c

File tree

4 files changed

+81
-47
lines changed

4 files changed

+81
-47
lines changed

lib/node-utility

src/EIDEProject.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2816,6 +2816,8 @@ $(OUT_DIR):
28162816

28172817
public abstract getSourceRefs(file: File): File[];
28182818

2819+
public abstract getCpptoolsConfig(): CppConfigItem;
2820+
28192821
//-----------------------------------------------------------
28202822

28212823
public abstract createBase(option: CreateOptions, createNewPrjFolder?: boolean): BaseProjectInfo;
@@ -3678,6 +3680,10 @@ class EIDEProject extends AbstractProject {
36783680

36793681
private __cpptools_updateTimeout: NodeJS.Timeout | undefined;
36803682

3683+
getCpptoolsConfig(): CppConfigItem {
3684+
return <CppConfigItem>deepCloneObject(this.cppToolsConfig);
3685+
}
3686+
36813687
forceUpdateCpptoolsConfig(): void {
36823688
this.UpdateCppConfig();
36833689
}
@@ -3944,9 +3950,17 @@ class EIDEProject extends AbstractProject {
39443950
// c++ files
39453951
else {
39463952

3947-
let compilerArgs = this.cppToolsConfig.cppCompilerArgs;
3953+
const compilerArgs: string[] = [];
3954+
const compilerPath = this.getToolchain().getGccFamilyCompilerPathForCpptools('c++');
3955+
3956+
// We need to tell gcc compiler: this is a c++ file
3957+
if (compilerPath) {
3958+
compilerArgs.push('-xc++');
3959+
}
3960+
3961+
this.cppToolsConfig.cppCompilerArgs?.forEach(arg => compilerArgs.push(arg));
39483962
if (fileArgs) {
3949-
compilerArgs = (compilerArgs || []).concat(fileArgs);
3963+
fileArgs.forEach(arg => compilerArgs.push(arg));
39503964
}
39513965

39523966
return {
@@ -3957,7 +3971,7 @@ class EIDEProject extends AbstractProject {
39573971
includePath: this.cppToolsConfig.includePath,
39583972
defines: this.cppToolsConfig.defines,
39593973
forcedInclude: this.cppToolsConfig.forcedInclude,
3960-
compilerPath: this.getToolchain().getGccFamilyCompilerPathForCpptools('c++') || "",
3974+
compilerPath: compilerPath || "",
39613975
compilerArgs: compilerArgs
39623976
}
39633977
};

src/EIDEProjectExplorer.ts

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2732,7 +2732,7 @@ class ProjectDataProvider implements vscode.TreeDataProvider<ProjTreeItem>, vsco
27322732

27332733
const compilerOpt = <ArmBaseCompileData>nEideTarget.compileConfig;
27342734
compilerOpt.cpuType = guessArmCpuType(eTarget) || 'Cortex-M3';
2735-
compilerOpt.floatingPointHardware = /-M[4-9]\d+/.test(compilerOpt.cpuType) ? 'single' : 'none';
2735+
compilerOpt.floatingPointHardware = ArmCpuUtils.hasFpu(compilerOpt.cpuType) ? 'single' : 'none';
27362736
compilerOpt.useCustomScatterFile = true;
27372737
compilerOpt.scatterFilePath = '';
27382738

@@ -3862,50 +3862,68 @@ export class ProjectExplorer implements CustomConfigurationProvider {
38623862

38633863
private async registerClangdProvider(prj: AbstractProject) {
38643864

3865-
if (this.cppToolsApi) {
3866-
// 如果 cpptools 激活了,则禁用 clangd,防止两个冲突
3867-
return;
3868-
}
3865+
if (this.cppToolsApi)
3866+
return; // 如果 cpptools 激活了,则禁用 clangd,防止两个冲突
38693867

38703868
prj.on('cppConfigChanged', () => {
3871-
// todo
3872-
});
38733869

3874-
// ----------------------
3875-
// setup clangd config
3876-
// ----------------------
3877-
try {
3878-
let cfg: any = {};
3879-
const fclangd = File.fromArray([prj.getProjectRoot().path, '.clangd']);
3880-
if (fclangd.IsFile()) {
3881-
cfg = yaml.parse(fclangd.Read());
3870+
const envs = prj.getProjectVariables();
3871+
if (envs['EIDE_CLANGD_PROVIDER_ENABLE'] == '0') {
3872+
GlobalEvent.log_info(`ignore update .clangd, because EIDE_CLANGD_PROVIDER_ENABLE=0 is set`);
3873+
return;
38823874
}
3883-
if (!cfg['CompileFlags']) cfg['CompileFlags'] = {};
3884-
if (!cfg['CompileFlags']['Add']) cfg['CompileFlags']['Add'] = []
3885-
//
3886-
cfg['CompileFlags']['CompilationDatabase'] = './' + File.ToUnixPath(prj.getOutputDir());
3887-
const toolchain = prj.getToolchain();
3888-
const gccLikePath = toolchain.getGccFamilyCompilerPathForCpptools();
3889-
if (gccLikePath) { // 仅兼容gcc的编译器
3890-
cfg['CompileFlags']['Compiler'] = gccLikePath;
3891-
let args: string[] = cfg['CompileFlags']['Add'];
3892-
if (/GCC/.test(toolchain.name)) {
3893-
let li = getGccSystemSearchList(File.ToLocalPath(gccLikePath));
3894-
if (li) {
3895-
li.forEach(p => {
3896-
args.push(`-I${File.normalize(p)}`);
3897-
});
3898-
}
3899-
} else {
3900-
args.push(`-I${toolchain.getToolchainDir().path}/include`);
3901-
args.push(`-I${toolchain.getToolchainDir().path}/include/libcxx`);
3875+
3876+
// ----------------------
3877+
// setup clangd config
3878+
// ----------------------
3879+
try {
3880+
let cfg: any = {};
3881+
const fclangd = File.fromArray([prj.getProjectRoot().path, '.clangd']);
3882+
if (fclangd.IsFile()) {
3883+
cfg = yaml.parse(fclangd.Read());
39023884
}
3903-
cfg['CompileFlags']['Add'] = ArrayDelRepetition(args);
3885+
if (!cfg['CompileFlags']) cfg['CompileFlags'] = {};
3886+
if (!cfg['CompileFlags']['Add']) cfg['CompileFlags']['Add'] = []
3887+
//
3888+
cfg['CompileFlags']['CompilationDatabase'] = './' + File.ToUnixPath(prj.getOutputDir());
3889+
const toolchain = prj.getToolchain();
3890+
const gccLikePath = toolchain.getGccFamilyCompilerPathForCpptools('c++');
3891+
if (gccLikePath) { // 仅兼容gcc的编译器
3892+
cfg['CompileFlags']['Compiler'] = gccLikePath;
3893+
let clangdCompileFlags = <string[]>(cfg['CompileFlags']['Add']);
3894+
let compilerArgs = prj.getCpptoolsConfig().cppCompilerArgs;
3895+
if (/GCC/.test(toolchain.name)) {
3896+
const tRoot = toolchain.getToolchainDir().path;
3897+
clangdCompileFlags = clangdCompileFlags.filter(p => !File.isSubPathOf(tRoot, p.substr(2)));
3898+
let li = getGccSystemSearchList(File.ToLocalPath(gccLikePath), ['-xc++'].concat(compilerArgs || []));
3899+
if (li) {
3900+
li.forEach(p => {
3901+
clangdCompileFlags.push(`-I${File.normalize(p)}`);
3902+
});
3903+
}
3904+
} else {
3905+
clangdCompileFlags.push(`-I${toolchain.getToolchainDir().path}/include`);
3906+
clangdCompileFlags.push(`-I${toolchain.getToolchainDir().path}/include/libcxx`);
3907+
}
3908+
// // add flags
3909+
// if (compilerArgs)
3910+
// compilerArgs.forEach(arg => clangdCompileFlags.push(arg));
3911+
// // add user includes
3912+
// prj.getCpptoolsConfig().includePath
3913+
// .forEach(path => clangdCompileFlags.push(`-I${path}`));
3914+
// // add user defines
3915+
// prj.getCpptoolsConfig().defines
3916+
// .forEach(d => clangdCompileFlags.push(`-D${d}`));
3917+
// del repeat
3918+
cfg['CompileFlags']['Add'] = ArrayDelRepetition(clangdCompileFlags);
3919+
}
3920+
fclangd.Write(yaml.stringify(cfg));
3921+
} catch (error) {
3922+
GlobalEvent.log_error(error);
39043923
}
3905-
fclangd.Write(yaml.stringify(cfg));
3906-
} catch (error) {
3907-
GlobalEvent.log_error(error);
3908-
}
3924+
});
3925+
3926+
prj.forceUpdateCpptoolsConfig();
39093927
}
39103928

39113929
// -----------------------------------------

src/utility.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,13 @@ export function isGccFamilyToolchain(name: ToolchainName): boolean {
207207
return name.includes('GCC');
208208
}
209209

210-
export function getGccSystemSearchList(gccPath: string): string[] | undefined {
210+
export function getGccSystemSearchList(gccFullPath: string, args?: string[]): string[] | undefined {
211211
try {
212-
const gccName = NodePath.basename(gccPath);
213-
const gccDir = NodePath.dirname(gccPath);
214-
const cmdLine = `${gccName} ` + ['-xc++', '-E', '-v', '-', `<${platform.osGetNullDev()}`, '2>&1'].join(' ');
212+
const gccName = NodePath.basename(gccFullPath);
213+
const gccDir = NodePath.dirname(gccFullPath);
214+
let cmdArgs: string[] = ['-E', '-v', '-', `<${platform.osGetNullDev()}`, '2>&1'];
215+
if (args) cmdArgs = args.concat(cmdArgs);
216+
const cmdLine = `${gccName} ` + cmdArgs.join(' ');
215217
const lines = child_process.execSync(cmdLine, { cwd: gccDir }).toString().split(/\r\n|\n/);
216218
const iStart = lines.findIndex((line) => { return line.startsWith('#include <...>'); });
217219
const iEnd = lines.indexOf('End of search list.', iStart);

0 commit comments

Comments
 (0)