( diagnostics: [vscode.Uri, vscode.Diagnostic[]][], severities: vscode.DiagnosticSeverity[], cwd: string, includeDiagnosticMessages: boolean = true, maxDiagnosticMessages?: number, )
| 71 | |
| 72 | // will return empty string if no problems with the given severity are found |
| 73 | export async function diagnosticsToProblemsString( |
| 74 | diagnostics: [vscode.Uri, vscode.Diagnostic[]][], |
| 75 | severities: vscode.DiagnosticSeverity[], |
| 76 | cwd: string, |
| 77 | includeDiagnosticMessages: boolean = true, |
| 78 | maxDiagnosticMessages?: number, |
| 79 | ): Promise<string> { |
| 80 | // If diagnostics are disabled, return empty string |
| 81 | if (!includeDiagnosticMessages) { |
| 82 | return "" |
| 83 | } |
| 84 | |
| 85 | const documents = new Map<vscode.Uri, vscode.TextDocument>() |
| 86 | const fileStats = new Map<vscode.Uri, vscode.FileStat>() |
| 87 | let result = "" |
| 88 | |
| 89 | // If we have a limit, use count-based limiting |
| 90 | if (maxDiagnosticMessages && maxDiagnosticMessages > 0) { |
| 91 | let includedCount = 0 |
| 92 | let totalCount = 0 |
| 93 | |
| 94 | // Flatten all diagnostics with their URIs |
| 95 | const allDiagnostics: { uri: vscode.Uri; diagnostic: vscode.Diagnostic; formattedText?: string }[] = [] |
| 96 | for (const [uri, fileDiagnostics] of diagnostics) { |
| 97 | const filtered = fileDiagnostics.filter((d) => severities.includes(d.severity)) |
| 98 | for (const diagnostic of filtered) { |
| 99 | allDiagnostics.push({ uri, diagnostic }) |
| 100 | totalCount++ |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // Sort by severity (errors first) and then by line number |
| 105 | allDiagnostics.sort((a, b) => { |
| 106 | const severityDiff = a.diagnostic.severity - b.diagnostic.severity |
| 107 | if (severityDiff !== 0) return severityDiff |
| 108 | return a.diagnostic.range.start.line - b.diagnostic.range.start.line |
| 109 | }) |
| 110 | |
| 111 | // Process diagnostics up to the count limit |
| 112 | const includedDiagnostics: typeof allDiagnostics = [] |
| 113 | for (const item of allDiagnostics) { |
| 114 | // Stop if we've reached the count limit |
| 115 | if (includedCount >= maxDiagnosticMessages) { |
| 116 | break |
| 117 | } |
| 118 | |
| 119 | // Format the diagnostic |
| 120 | let label: string |
| 121 | switch (item.diagnostic.severity) { |
| 122 | case vscode.DiagnosticSeverity.Error: |
| 123 | label = "Error" |
| 124 | break |
| 125 | case vscode.DiagnosticSeverity.Warning: |
| 126 | label = "Warning" |
| 127 | break |
| 128 | case vscode.DiagnosticSeverity.Information: |
| 129 | label = "Information" |
| 130 | break |
no test coverage detected