forked from mesonbuild/vscode-meson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.ts
308 lines (275 loc) · 10.2 KB
/
extension.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import * as vscode from "vscode";
import { getMesonTasks, getTasks, runTask, runFirstTask } from "./tasks";
import { MesonProjectExplorer } from "./treeview";
import { TargetNode } from "./treeview/nodes/targets";
import {
extensionConfiguration,
genEnvFile,
clearCache,
checkMesonIsConfigured,
getOutputChannel,
getBuildDirectory,
whenFileExists,
mesonRootDirs,
shouldModifySetting,
} from "./utils";
import { DebugConfigurationProviderCppdbg } from "./debug/cppdbg";
import { DebugConfigurationProviderLldb } from "./debug/lldb";
import { CpptoolsProvider, registerCppToolsProvider } from "./cpptoolsconfigprovider";
import { testDebugHandler, testRunHandler, rebuildTests } from "./tests";
import { activateLinters } from "./linters";
import { activateFormatters } from "./formatters";
import { SettingsKey, TaskQuickPickItem } from "./types";
import { createLanguageServerClient } from "./lsp/common";
import { askShouldDownloadLanguageServer, askConfigureOnOpen, askAndSelectRootDir, selectRootDir } from "./dialogs";
import { getIntrospectionFile } from "./introspection";
export let extensionPath: string;
export let workspaceState: vscode.Memento;
let explorer: MesonProjectExplorer;
let cpptools: CpptoolsProvider;
let watcher: vscode.FileSystemWatcher;
let controller: vscode.TestController;
export async function activate(ctx: vscode.ExtensionContext) {
extensionPath = ctx.extensionPath;
workspaceState = ctx.workspaceState;
// The workspace could contain multiple Meson projects. Take all root
// meson.build files we find. Usually that's just one at the root of the
// workspace.
const rootDirs = await mesonRootDirs();
let rootDir: string | undefined = undefined;
if (rootDirs.length == 1) {
rootDir = rootDirs[0];
} else if (rootDirs.length > 1) {
let savedSourceDir = workspaceState.get<string>("mesonbuild.sourceDir");
if (savedSourceDir && rootDirs.includes(savedSourceDir)) {
rootDir = savedSourceDir;
} else {
// We have more than one root meson.build file and none has been previously
// saved. Ask the user to pick one.
rootDir = await askAndSelectRootDir(rootDirs);
}
}
ctx.subscriptions.push(
vscode.commands.registerCommand("mesonbuild.selectRootDir", async () => {
let newRootDir = await selectRootDir(await mesonRootDirs());
if (newRootDir && newRootDir != rootDir) {
await workspaceState.update("mesonbuild.sourceDir", newRootDir);
vscode.commands.executeCommand("workbench.action.reloadWindow");
}
}),
);
getOutputChannel().appendLine(`Meson project root: ${rootDir}`);
vscode.commands.executeCommand("setContext", "mesonbuild.hasProject", rootDir !== undefined);
vscode.commands.executeCommand("setContext", "mesonbuild.hasMultipleProjects", rootDirs.length > 1);
if (!rootDir) return;
const sourceDir = rootDir;
const buildDir = getBuildDirectory(sourceDir);
workspaceState.update("mesonbuild.buildDir", buildDir);
workspaceState.update("mesonbuild.sourceDir", sourceDir);
cpptools = new CpptoolsProvider(buildDir);
registerCppToolsProvider(ctx, cpptools);
explorer = new MesonProjectExplorer(ctx, sourceDir, buildDir);
const providers = [DebugConfigurationProviderCppdbg, DebugConfigurationProviderLldb];
providers.forEach((provider) => {
const p = new provider(buildDir);
ctx.subscriptions.push(
vscode.debug.registerDebugConfigurationProvider(p.type, p, vscode.DebugConfigurationProviderTriggerKind.Dynamic),
);
});
controller = vscode.tests.createTestController("meson-test-controller", "Meson test controller");
controller.createRunProfile(
"Meson debug test",
vscode.TestRunProfileKind.Debug,
(request, token) => testDebugHandler(controller, request, token),
true,
);
controller.createRunProfile(
"Meson run test",
vscode.TestRunProfileKind.Run,
(request, token) => testRunHandler(controller, request, token),
true,
);
ctx.subscriptions.push(controller);
let mesonTasks: Thenable<vscode.Task[]> | null = null;
ctx.subscriptions.push(
vscode.tasks.registerTaskProvider("meson", {
provideTasks() {
mesonTasks ??= getMesonTasks(buildDir, sourceDir);
return mesonTasks;
},
resolveTask() {
return null;
},
}),
);
const changeHandler = async () => {
mesonTasks = null;
clearCache();
await rebuildTests(controller);
await genEnvFile(buildDir);
explorer.refresh();
};
watcher = vscode.workspace.createFileSystemWatcher(`${buildDir}/build.ninja`, false, false, true);
watcher.onDidChange(changeHandler);
watcher.onDidCreate(changeHandler);
ctx.subscriptions.push(watcher);
await genEnvFile(buildDir);
// Refresh if the extension configuration is changed.
ctx.subscriptions.push(
vscode.workspace.onDidChangeConfiguration((e: vscode.ConfigurationChangeEvent) => {
if (e.affectsConfiguration("mesonbuild.buildFolder")) {
// buildFolder is rather ingrained right now, so changes there require a full reload.
vscode.commands.executeCommand("workbench.action.reloadWindow");
} else if (e.affectsConfiguration("mesonbuild")) {
changeHandler();
}
}),
);
const compileCommandsFile = `${buildDir}/compile_commands.json`;
whenFileExists(ctx, compileCommandsFile, async () => {
if (shouldModifySetting("ms-vscode.cpptools")) {
const conf = vscode.workspace.getConfiguration("C_Cpp");
conf.update("default.compileCommands", compileCommandsFile, vscode.ConfigurationTarget.Workspace);
}
});
const rustProjectFile = `${buildDir}/rust-project.json`;
whenFileExists(ctx, rustProjectFile, async () => {
if (shouldModifySetting("rust-lang.rust-analyzer")) {
const conf = vscode.workspace.getConfiguration("rust-analyzer");
conf.update("linkedProjects", [rustProjectFile], vscode.ConfigurationTarget.Workspace);
}
});
const mesonInfoFile = getIntrospectionFile(buildDir, "meson-info.json");
whenFileExists(ctx, mesonInfoFile, async () => {
cpptools.refresh(buildDir);
if (shouldModifySetting("ms-vscode.cpptools")) {
const conf = vscode.workspace.getConfiguration("C_Cpp");
conf.update("default.configurationProvider", "mesonbuild.mesonbuild", vscode.ConfigurationTarget.Workspace);
}
});
ctx.subscriptions.push(
vscode.commands.registerCommand("mesonbuild.openBuildFile", async (node: TargetNode) => {
const file = node.getTarget().defined_in;
const uri = vscode.Uri.file(file);
await vscode.commands.executeCommand("vscode.open", uri);
}),
);
ctx.subscriptions.push(
vscode.commands.registerCommand("mesonbuild.reconfigure", async () => {
runFirstTask("reconfigure");
}),
);
ctx.subscriptions.push(
vscode.commands.registerCommand("mesonbuild.build", async (name?: string) => {
pickAndRunTask("build", name);
}),
);
ctx.subscriptions.push(
vscode.commands.registerCommand("mesonbuild.install", async () => {
runFirstTask("install");
}),
);
ctx.subscriptions.push(
vscode.commands.registerCommand("mesonbuild.test", async (name?: string) => {
pickAndRunTask("test", name);
}),
);
ctx.subscriptions.push(
vscode.commands.registerCommand("mesonbuild.benchmark", async (name?: string) => {
pickAndRunTask("benchmark", name);
}),
);
ctx.subscriptions.push(
vscode.commands.registerCommand("mesonbuild.clean", async () => {
runFirstTask("clean");
}),
);
ctx.subscriptions.push(
vscode.commands.registerCommand("mesonbuild.run", async () => {
pickAndRunTask("run");
}),
);
if (!checkMesonIsConfigured(buildDir)) {
if (await askConfigureOnOpen()) runFirstTask("reconfigure");
} else {
await rebuildTests(controller);
}
const server = extensionConfiguration(SettingsKey.languageServer);
let client = await createLanguageServerClient(server, await askShouldDownloadLanguageServer(), ctx);
// Basically every server supports formatting...
const serverSupportsFormatting = server == "mesonlsp" || server == "Swift-MesonLSP";
if (client !== null && serverSupportsFormatting) {
ctx.subscriptions.push(
vscode.workspace.onDidChangeConfiguration((e) => {
if (e.affectsConfiguration(`mesonbuild.${server}`)) {
client?.reloadConfig();
}
}),
);
await client.update(ctx);
ctx.subscriptions.push(client);
client.start();
await client.reloadConfig();
getOutputChannel().appendLine(
"Not enabling the muon linter/formatter because a language server supporting formatting is active.",
);
} else {
activateLinters(sourceDir, ctx);
activateFormatters(sourceDir, ctx);
}
ctx.subscriptions.push(
vscode.commands.registerCommand("mesonbuild.restartLanguageServer", async () => {
if (client === null) {
client = await createLanguageServerClient(server, await askShouldDownloadLanguageServer(), ctx);
if (client !== null) {
ctx.subscriptions.push(client);
client.start();
await client.reloadConfig();
// TODO: The output line from above about not enabling muon would be good to have here.
}
} else {
await client.restart();
await client.reloadConfig();
}
}),
);
async function pickTask(mode: string) {
const picker = vscode.window.createQuickPick<TaskQuickPickItem>();
picker.busy = true;
picker.placeholder = `Select target to ${mode}.`;
picker.show();
const runnableTasks = await getTasks(mode);
picker.busy = false;
picker.items = runnableTasks.map((task) => {
return {
label: task.name,
detail: task.detail,
picked: false,
task: task,
};
});
return new Promise<TaskQuickPickItem>((resolve, reject) => {
picker.onDidAccept(() => {
const selection = picker.activeItems[0];
resolve(selection);
picker.dispose();
});
picker.onDidHide(() => reject());
});
}
async function pickAndRunTask(mode: string, name?: string) {
if (name) {
runFirstTask(mode, name);
return;
}
let taskItem;
try {
taskItem = await pickTask(mode);
} catch (err) {
// Pick cancelled.
}
if (taskItem != null) {
runTask(taskItem.task);
}
}
}