This repository has been archived by the owner on Dec 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathdiagnostic_utils.ts
55 lines (45 loc) · 1.81 KB
/
diagnostic_utils.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
import { isAbsolute, join } from 'path';
import { Diagnostic, DiagnosticCollection, Uri } from 'vscode';
import { FileDiagnostic } from './file_diagnostic';
/**
* The path of a diagnostic must be absolute.
* The function prepends the path of the project to the path of the diagnostic.
* @param diagnosticPath The path of the diagnostic
* @param projectPath The path of the project
*/
export function normalizeDiagnosticPath(diagnosticPath: string, projectPath: string): string {
if (isAbsolute(diagnosticPath)) {
return diagnosticPath;
} else {
return join(projectPath, diagnosticPath);
}
}
/**
* Adds the diagnostic to the diagnostics only if the diagnostic isn't in the diagnostics.
* @param diagnostic The diagnostic to add
* @param diagnostics The collection of diagnostics to take the diagnostic
*/
export function addUniqueDiagnostic(diagnostic: FileDiagnostic, diagnostics: DiagnosticCollection): void {
const uri = Uri.file(diagnostic.filePath);
const fileDiagnostics = diagnostics.get(uri);
if (!fileDiagnostics) {
// No diagnostics for the file
// The diagnostic is unique
diagnostics.set(uri, [diagnostic.diagnostic]);
} else if (isUniqueDiagnostic(diagnostic.diagnostic, fileDiagnostics)) {
const newFileDiagnostics = fileDiagnostics.concat([diagnostic.diagnostic]);
diagnostics.set(uri, newFileDiagnostics);
}
}
export function isUniqueDiagnostic(diagnostic: Diagnostic, diagnostics: Diagnostic[]): boolean {
const foundDiagnostic = diagnostics.find(uniqueDiagnostic => {
if (!diagnostic.range.isEqual(uniqueDiagnostic.range)) {
return false;
}
if (diagnostic.message !== uniqueDiagnostic.message) {
return false;
}
return true;
});
return !foundDiagnostic;
}