-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathlinters.ts
94 lines (76 loc) · 2.65 KB
/
linters.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
import * as vscode from "vscode";
import { extensionConfiguration, getOutputChannel } from "./utils";
import { ExtensionConfiguration, LinterConfiguration, ToolCheckFunc, Tool } from "./types";
import * as muon from "./tools/muon";
type LinterFunc = (tool: Tool, sourceRoot: string, document: vscode.TextDocument) => Promise<vscode.Diagnostic[]>;
type LinterDefinition = {
lint: LinterFunc;
check: ToolCheckFunc;
};
const linters: Record<keyof ExtensionConfiguration["linter"], LinterDefinition> = {
muon: {
lint: muon.lint,
check: muon.check,
},
};
async function reloadLinters(
sourceRoot: string,
context: vscode.ExtensionContext,
diagnostics: vscode.DiagnosticCollection,
) {
let disposables: vscode.Disposable[] = [];
if (!extensionConfiguration("linting").enabled) {
return disposables;
}
let enabledLinters: ((document: vscode.TextDocument) => Promise<vscode.Diagnostic[]>)[] = [];
let name: keyof typeof linters;
for (name in linters) {
const config: LinterConfiguration = extensionConfiguration("linter")[name];
if (!config.enabled) {
continue;
}
const props = linters[name];
const checkResult = await props.check();
if (checkResult.isError()) {
getOutputChannel().appendLine(`Failed to enable linter ${name}: ${checkResult.error}`);
getOutputChannel().show(true);
continue;
}
const linter = async (document: vscode.TextDocument) => await props.lint(checkResult.tool, sourceRoot, document);
enabledLinters.push(linter);
}
const lintAll = (document: vscode.TextDocument) => {
if (document.languageId != "meson") {
return;
}
Promise.all(enabledLinters.map((l) => l(document))).then((values) => {
diagnostics.set(document.uri, values.flat());
});
};
const subscriptions = [
vscode.workspace.onDidChangeTextDocument((c) => lintAll(c.document)),
vscode.window.onDidChangeActiveTextEditor((e) => {
if (e) {
lintAll(e.document);
}
}),
];
for (const sub of subscriptions) {
context.subscriptions.push(sub);
disposables.push(sub);
}
return disposables;
}
export async function activateLinters(sourceRoot: string, context: vscode.ExtensionContext) {
const diagnostics = vscode.languages.createDiagnosticCollection("meson");
let subscriptions: vscode.Disposable[] = await reloadLinters(sourceRoot, context, diagnostics);
context.subscriptions.push(
vscode.workspace.onDidChangeConfiguration(async () => {
for (let handler of subscriptions) {
handler.dispose();
}
diagnostics.clear();
subscriptions = await reloadLinters(sourceRoot, context, diagnostics);
}),
);
}