-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathConfigDiagnosticsProvider.ts
executable file
·63 lines (51 loc) · 2.48 KB
/
ConfigDiagnosticsProvider.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
import * as vscode from "vscode";
import { isConfigValid } from "../util/configUtil";
import { isFileDirty } from "../../../common/vscode/editor/textDocumentUtil";
import { flatten } from "../../../common/general/arrayUtil";
import DiagnosticCreator from "../../../common/vscode/diagnostic/DiagnosticCreator";
import BarrelDiagnosticCreator from "../../barrel/diagnostic/BarrelDiagnosticCreator";
import ComponentDiagnosticCreator from "../../component/diagnostic/ComponentDiagnosticCreator";
import ZeplinComponentDiagnosticCreator from "../../zeplinComponent/diagnostic/ZeplinComponentDiagnosticCreator";
import Refresher from "../../../session/util/Refresher";
import ConfigPaths from "../util/ConfigPaths";
const KEY = "zeplinConfig";
const SOURCE = "Zeplin";
class ConfigDiagnosticsProvider {
private readonly diagnosticCollection = vscode.languages.createDiagnosticCollection(KEY);
public register(): vscode.Disposable {
this.updateForOpenDocuments();
return vscode.Disposable.from(
this.diagnosticCollection,
vscode.workspace.onDidOpenTextDocument(this.updateDiagnostics, this),
vscode.workspace.onDidSaveTextDocument(this.updateDiagnostics, this),
vscode.workspace.onDidChangeTextDocument(event => this.clearDiagnostics(event.document), this),
vscode.workspace.onDidCloseTextDocument(this.clearDiagnostics, this)
);
}
public updateForOpenDocuments() {
vscode.window.visibleTextEditors.forEach(editor => this.updateDiagnostics(editor.document), this);
}
private clearDiagnostics(document: vscode.TextDocument) {
this.diagnosticCollection.delete(document.uri);
}
private async updateDiagnostics(document: vscode.TextDocument) {
const uri = document.uri;
const path = uri.fsPath;
this.diagnosticCollection.delete(uri);
if (!ConfigPaths.include(path) || isFileDirty(path) || !isConfigValid(path)) {
return;
}
Refresher.requestRefresh();
const creators: DiagnosticCreator[] = [
BarrelDiagnosticCreator,
ComponentDiagnosticCreator,
ZeplinComponentDiagnosticCreator
];
const diagnostics = flatten(await Promise.all(creators.map(creator => creator.create(document))));
for (const diagnostic of diagnostics) {
diagnostic.source = SOURCE;
}
this.diagnosticCollection.set(uri, diagnostics);
}
}
export default new ConfigDiagnosticsProvider();