diagnostics.ts 2.6 KB
Newer Older
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import { Diagnostic, DiagnosticCollection, languages, Uri } from 'vscode';
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
class DiagnosticSet {
	private _map: ObjectMap<Diagnostic[]> = Object.create(null);

	public set(
		file: Uri,
		diagnostics: Diagnostic[]
	) {
		this._map[this.key(file)] = diagnostics;
	}

	public get(
		file: Uri
	): Diagnostic[] {
		return this._map[this.key(file)] || [];
	}

	public clear(): void {
		this._map = Object.create(null);
	}

	private key(file: Uri): string {
		return file.toString(true);
	}
}

33 34
export default class DiagnosticsManager {

35 36
	private readonly syntaxDiagnostics: DiagnosticSet;
	private readonly semanticDiagnostics: DiagnosticSet;
37 38 39 40
	private readonly currentDiagnostics: DiagnosticCollection;
	private _validate: boolean = true;

	constructor(
41
		language: string
42
	) {
43 44
		this.syntaxDiagnostics = new DiagnosticSet();
		this.semanticDiagnostics = new DiagnosticSet();
45 46 47 48 49 50 51 52 53
		this.currentDiagnostics = languages.createDiagnosticCollection(language);
	}

	public dispose() {
		this.currentDiagnostics.dispose();
	}

	public reInitialize(): void {
		this.currentDiagnostics.clear();
54 55
		this.syntaxDiagnostics.clear();
		this.semanticDiagnostics.clear();
56 57
	}

M
Matt Bierner 已提交
58
	public set validate(value: boolean) {
59 60 61 62 63 64 65 66 67
		if (this._validate === value) {
			return;
		}
		this._validate = value;
		if (!value) {
			this.currentDiagnostics.clear();
		}
	}

68
	public syntaxDiagnosticsReceived(file: Uri, syntaxDiagnostics: Diagnostic[]): void {
69
		this.syntaxDiagnostics.set(file, syntaxDiagnostics);
M
Matt Bierner 已提交
70
		this.updateCurrentDiagnostics(file);
71 72
	}

73
	public semanticDiagnosticsReceived(file: Uri, semanticDiagnostics: Diagnostic[]): void {
74
		this.semanticDiagnostics.set(file, semanticDiagnostics);
M
Matt Bierner 已提交
75
		this.updateCurrentDiagnostics(file);
76 77
	}

78 79
	public configFileDiagnosticsReceived(file: Uri, diagnostics: Diagnostic[]): void {
		this.currentDiagnostics.set(file, diagnostics);
80 81
	}

82 83
	public delete(resource: Uri): void {
		this.currentDiagnostics.delete(resource);
84
	}
M
Matt Bierner 已提交
85

86
	private updateCurrentDiagnostics(file: Uri) {
M
Matt Bierner 已提交
87 88 89 90
		if (!this._validate) {
			return;
		}

91 92
		const semanticDiagnostics = this.semanticDiagnostics.get(file);
		const syntaxDiagnostics = this.syntaxDiagnostics.get(file);
93
		this.currentDiagnostics.set(file, semanticDiagnostics.concat(syntaxDiagnostics));
M
Matt Bierner 已提交
94
	}
95 96 97 98

	public getDiagnostics(file: Uri): Diagnostic[] {
		return this.currentDiagnostics.get(file) || [];
	}
99
}