forked from redhat-developer/yaml-language-server
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidationHandlers.ts
More file actions
72 lines (63 loc) · 2.91 KB
/
validationHandlers.ts
File metadata and controls
72 lines (63 loc) · 2.91 KB
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Connection } from 'vscode-languageserver';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { Diagnostic } from 'vscode-languageserver-types';
import { isKubernetesAssociatedDocument } from '../../languageservice/parser/isKubernetes';
import { removeDuplicatesObj } from '../../languageservice/utils/arrUtils';
import { LanguageService } from '../../languageservice/yamlLanguageService';
import { SettingsState } from '../../yamlSettings';
export class ValidationHandler {
private languageService: LanguageService;
private yamlSettings: SettingsState;
constructor(private readonly connection: Connection, languageService: LanguageService, yamlSettings: SettingsState) {
this.languageService = languageService;
this.yamlSettings = yamlSettings;
this.yamlSettings.documents.onDidChangeContent((change) => {
this.validate(change.document);
});
this.yamlSettings.documents.onDidClose((event) => {
this.cleanPendingValidation(event.document);
this.connection.sendDiagnostics({ uri: event.document.uri, diagnostics: [] });
});
}
validate(textDocument: TextDocument): void {
this.cleanPendingValidation(textDocument);
this.yamlSettings.pendingValidationRequests[textDocument.uri] = setTimeout(() => {
delete this.yamlSettings.pendingValidationRequests[textDocument.uri];
this.validateTextDocument(textDocument);
}, this.yamlSettings.validationDelayMs);
}
private cleanPendingValidation(textDocument: TextDocument): void {
const request = this.yamlSettings.pendingValidationRequests[textDocument.uri];
if (request) {
clearTimeout(request);
delete this.yamlSettings.pendingValidationRequests[textDocument.uri];
}
}
validateTextDocument(textDocument: TextDocument): Promise<Diagnostic[]> {
if (!textDocument) {
return;
}
return this.languageService
.doValidation(textDocument, isKubernetesAssociatedDocument(textDocument, this.yamlSettings.specificValidatorPaths))
.then((diagnosticResults) => {
const diagnostics = [];
for (const diagnosticItem of diagnosticResults) {
// Convert all warnings to errors
if (diagnosticItem.severity === 2) {
diagnosticItem.severity = 1;
}
diagnostics.push(diagnosticItem);
}
const removeDuplicatesDiagnostics = removeDuplicatesObj(diagnostics);
this.connection.sendDiagnostics({
uri: textDocument.uri,
diagnostics: removeDuplicatesDiagnostics,
});
return removeDuplicatesDiagnostics;
});
}
}