-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathcpptoolsconfigprovider.ts
164 lines (146 loc) · 5.45 KB
/
cpptoolsconfigprovider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import * as vscode from "vscode";
import * as cpptools from "vscode-cpptools";
import { getMesonBuildOptions, getMesonCompilers, getMesonDependencies } from "./introspection";
import { getOutputChannel } from "./utils";
import { Compiler, Dependencies } from "./types";
export class CpptoolsProvider implements cpptools.CustomConfigurationProvider {
cppToolsAPI?: cpptools.CppToolsApi;
private buildDir: string;
constructor(buildDir: string) {
this.buildDir = buildDir;
}
name = "Meson Build";
extensionId = "mesonbuild.mesonbuild";
canProvideBrowseConfiguration(token?: vscode.CancellationToken | undefined): Thenable<boolean> {
return new Promise<boolean>((resolve) => {
if (this.buildDir !== "") {
resolve(true);
} else {
// Wait for this.buildDir to not be ""
const interval = setInterval(() => {
if (this.buildDir !== "") {
clearInterval(interval);
this.refresh(this.buildDir);
resolve(true);
}
}, 100);
}
});
}
async provideBrowseConfiguration(
token?: vscode.CancellationToken | undefined,
): Promise<cpptools.WorkspaceBrowseConfiguration | null> {
let browseConfig: cpptools.WorkspaceBrowseConfiguration = {
browsePath: [],
compilerPath: "${default}",
compilerArgs: [],
};
const dependencies = await getMesonDependencies(this.buildDir);
browseConfig = Object.assign(browseConfig, { browsePath: this.getDependenciesIncludeDirs(dependencies) });
let machine: string | undefined;
const buildOptions = await getMesonBuildOptions(this.buildDir);
for (const option of buildOptions) {
if (option.name === "cpp_std") {
if (option.value != "none") browseConfig = Object.assign({}, browseConfig, { standard: option.value });
machine = option.machine;
} else if (machine === undefined && option.name === "c_std") {
// C++ takes precedence
if (option.value != "none") browseConfig = Object.assign({}, browseConfig, { standard: option.value });
machine = option.machine;
}
}
try {
const compilers = await getMesonCompilers(this.buildDir);
if (machine !== undefined && compilers[machine] !== undefined) {
const compiler = compilers[machine];
if (compiler && compiler["cpp"]) {
browseConfig = this.setCompilerArgs(compiler, "cpp", browseConfig);
} else if (compiler && compiler["c"]) {
browseConfig = this.setCompilerArgs(compiler, "c", browseConfig);
}
}
} catch (e) {
getOutputChannel().appendLine(
`Could not introspect a specific compiler, the default one will be used: ${JSON.stringify(e)}`,
);
}
getOutputChannel().appendLine(`Providing cpptools configuration: ${JSON.stringify(browseConfig)}`);
return browseConfig;
}
private getDependenciesIncludeDirs(dependencies: Dependencies) {
let includeDirs: string[] = [];
for (const dep of dependencies) {
if (dep.compile_args) {
for (const arg of dep.compile_args) {
if (arg.startsWith("-I")) {
includeDirs.push(arg.slice(2));
}
}
}
}
// The cpptools API requires at least one browse path, even when we provide a compiler path.
if (includeDirs.length === 0) {
includeDirs.push("");
}
return includeDirs;
}
private setCompilerArgs(
compiler: Compiler,
standard: string,
browseConfig: cpptools.WorkspaceBrowseConfiguration,
): cpptools.WorkspaceBrowseConfiguration {
if (compiler[standard]) {
const compilerDesc = compiler[standard];
browseConfig = Object.assign({}, browseConfig, {
compilerPath: compilerDesc.exelist[0],
compilerArgs: compilerDesc.exelist.slice(1),
});
}
return browseConfig;
}
// We only handle project-wide configurations.
canProvideBrowseConfigurationsPerFolder(token?: vscode.CancellationToken | undefined): Thenable<boolean> {
return Promise.resolve(false);
}
async provideFolderBrowseConfiguration(
uri: vscode.Uri,
token?: vscode.CancellationToken | undefined,
): Promise<cpptools.WorkspaceBrowseConfiguration | null> {
return null;
}
// We only handle project-wide configurations.
canProvideConfiguration(uri: vscode.Uri, token?: vscode.CancellationToken | undefined): Thenable<boolean> {
return Promise.resolve(false);
}
async provideConfigurations(
uris: vscode.Uri[],
token?: vscode.CancellationToken | undefined,
): Promise<cpptools.SourceFileConfigurationItem[]> {
return [];
}
dispose() {}
refresh(buildDir: string) {
this.buildDir = buildDir;
this.cppToolsAPI?.notifyReady(this);
this.cppToolsAPI?.didChangeCustomConfiguration(this);
this.cppToolsAPI?.didChangeCustomBrowseConfiguration(this);
}
}
// Official implementation from https://classic.yarnpkg.com/en/package/vscode-cpptools
export async function registerCppToolsProvider(
ctx: vscode.ExtensionContext,
provider: CpptoolsProvider,
): Promise<cpptools.CppToolsApi | undefined> {
const cppToolsAPI = await cpptools.getCppToolsApi(cpptools.Version.latest);
if (cppToolsAPI) {
provider.cppToolsAPI = cppToolsAPI;
if (cppToolsAPI.notifyReady) {
cppToolsAPI.registerCustomConfigurationProvider(provider);
cppToolsAPI.notifyReady(provider);
ctx.subscriptions.push(cppToolsAPI);
} else {
throw new Error("CppTools API not available, or not version >2.0");
}
}
return cppToolsAPI;
}