1- import * as vscode from "vscode"
21import * as path from "path"
2+
3+ import * as vscode from "vscode"
34import deepEqual from "fast-deep-equal"
45
56export function getNewDiagnostics (
@@ -21,68 +22,31 @@ export function getNewDiagnostics(
2122 return newProblems
2223}
2324
24- // Usage:
25- // const oldDiagnostics = // ... your old diagnostics array
26- // const newDiagnostics = // ... your new diagnostics array
27- // const newProblems = getNewDiagnostics(oldDiagnostics, newDiagnostics);
25+ // Expected output:
26+ // New problems:
27+ // File: /path/to/file1.ts
28+ // - New error in file1 (2:2)
29+ // File: /path/to/file3.ts
30+ // - New error in file3 (1:1)
2831
29- // Example usage with mocks:
30- //
31- // // Mock old diagnostics
32- // const oldDiagnostics: [vscode.Uri, vscode.Diagnostic[]][] = [
33- // [vscode.Uri.file("/path/to/file1.ts"), [
34- // new vscode.Diagnostic(new vscode.Range(0, 0, 0, 10), "Old error in file1", vscode.DiagnosticSeverity.Error)
35- // ]],
36- // [vscode.Uri.file("/path/to/file2.ts"), [
37- // new vscode.Diagnostic(new vscode.Range(5, 5, 5, 15), "Old warning in file2", vscode.DiagnosticSeverity.Warning)
38- // ]]
39- // ];
40- //
41- // // Mock new diagnostics
42- // const newDiagnostics: [vscode.Uri, vscode.Diagnostic[]][] = [
43- // [vscode.Uri.file("/path/to/file1.ts"), [
44- // new vscode.Diagnostic(new vscode.Range(0, 0, 0, 10), "Old error in file1", vscode.DiagnosticSeverity.Error),
45- // new vscode.Diagnostic(new vscode.Range(2, 2, 2, 12), "New error in file1", vscode.DiagnosticSeverity.Error)
46- // ]],
47- // [vscode.Uri.file("/path/to/file2.ts"), [
48- // new vscode.Diagnostic(new vscode.Range(5, 5, 5, 15), "Old warning in file2", vscode.DiagnosticSeverity.Warning)
49- // ]],
50- // [vscode.Uri.file("/path/to/file3.ts"), [
51- // new vscode.Diagnostic(new vscode.Range(1, 1, 1, 11), "New error in file3", vscode.DiagnosticSeverity.Error)
52- // ]]
53- // ];
54- //
55- // const newProblems = getNewProblems(oldDiagnostics, newDiagnostics);
56- //
57- // console.log("New problems:");
58- // for (const [uri, diagnostics] of newProblems) {
59- // console.log(`File: ${uri.fsPath}`);
60- // for (const diagnostic of diagnostics) {
61- // console.log(`- ${diagnostic.message} (${diagnostic.range.start.line}:${diagnostic.range.start.character})`);
62- // }
63- // }
64- //
65- // // Expected output:
66- // // New problems:
67- // // File: /path/to/file1.ts
68- // // - New error in file1 (2:2)
69- // // File: /path/to/file3.ts
70- // // - New error in file3 (1:1)
71-
72- // will return empty string if no problems with the given severity are found
32+ // Will return empty string if no problems with the given severity are found.
7333export async function diagnosticsToProblemsString (
7434 diagnostics : [ vscode . Uri , vscode . Diagnostic [ ] ] [ ] ,
7535 severities : vscode . DiagnosticSeverity [ ] ,
7636 cwd : string ,
7737) : Promise < string > {
7838 const documents = new Map < vscode . Uri , vscode . TextDocument > ( )
7939 let result = ""
40+
8041 for ( const [ uri , fileDiagnostics ] of diagnostics ) {
8142 const problems = fileDiagnostics . filter ( ( d ) => severities . includes ( d . severity ) )
43+
8244 if ( problems . length > 0 ) {
8345 result += `\n\n${ path . relative ( cwd , uri . fsPath ) . toPosix ( ) } `
46+
8447 for ( const diagnostic of problems ) {
8548 let label : string
49+
8650 switch ( diagnostic . severity ) {
8751 case vscode . DiagnosticSeverity . Error :
8852 label = "Error"
@@ -99,6 +63,7 @@ export async function diagnosticsToProblemsString(
9963 default :
10064 label = "Diagnostic"
10165 }
66+
10267 const line = diagnostic . range . start . line + 1 // VSCode lines are 0-indexed
10368 const source = diagnostic . source ? `${ diagnostic . source } ` : ""
10469 const document = documents . get ( uri ) || ( await vscode . workspace . openTextDocument ( uri ) )
@@ -108,5 +73,6 @@ export async function diagnosticsToProblemsString(
10873 }
10974 }
11075 }
76+
11177 return result . trim ( )
11278}
0 commit comments