Skip to content

Commit b8e9cce

Browse files
committed
make error messages prettier
1 parent b0ee230 commit b8e9cce

File tree

2 files changed

+20
-51
lines changed

2 files changed

+20
-51
lines changed

packages/amazonq/src/lsp/autoDebug/controller.ts

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,23 @@ import * as vscode from 'vscode'
77
import { getLogger, randomUUID } from 'aws-core-vscode/shared'
88
import { AutoDebugLspClient } from './lsp/autoDebugLspClient'
99
import { mapDiagnosticSeverity } from './shared/diagnosticUtils'
10+
import { ErrorContextFormatter } from './diagnostics/errorContext'
11+
import { Problem } from './diagnostics/problemDetector'
1012

1113
export interface AutoDebugConfig {
1214
readonly enabled: boolean
1315
readonly excludedSources: string[]
1416
readonly severityFilter: ('error' | 'warning' | 'info' | 'hint')[]
1517
}
1618

17-
export interface Problem {
18-
readonly uri: vscode.Uri
19-
readonly diagnostic: vscode.Diagnostic
20-
readonly severity: 'error' | 'warning' | 'info' | 'hint'
21-
readonly source: string
22-
}
23-
2419
/**
2520
* Simplified controller for Amazon Q Auto Debug system.
2621
* Focuses on context menu and quick fix functionality without workspace-wide monitoring.
2722
*/
2823
export class AutoDebugController implements vscode.Disposable {
2924
private readonly logger = getLogger()
3025
private readonly lspClient: AutoDebugLspClient
26+
private readonly errorFormatter: ErrorContextFormatter
3127
private readonly disposables: vscode.Disposable[] = []
3228

3329
private config: AutoDebugConfig
@@ -41,6 +37,7 @@ export class AutoDebugController implements vscode.Disposable {
4137
}
4238

4339
this.lspClient = new AutoDebugLspClient(client, encryptionKey)
40+
this.errorFormatter = new ErrorContextFormatter()
4441
}
4542

4643
/**
@@ -79,6 +76,7 @@ export class AutoDebugController implements vscode.Disposable {
7976
diagnostic,
8077
severity: mapDiagnosticSeverity(diagnostic.severity),
8178
source: diagnostic.source || 'unknown',
79+
isNew: false,
8280
}))
8381

8482
// Create fix message
@@ -131,6 +129,7 @@ export class AutoDebugController implements vscode.Disposable {
131129
diagnostic,
132130
severity: mapDiagnosticSeverity(diagnostic.severity),
133131
source: diagnostic.source || 'unknown',
132+
isNew: false,
134133
}))
135134

136135
// Create fix message
@@ -170,6 +169,7 @@ export class AutoDebugController implements vscode.Disposable {
170169
diagnostic,
171170
severity: mapDiagnosticSeverity(diagnostic.severity),
172171
source: diagnostic.source || 'unknown',
172+
isNew: false,
173173
}))
174174

175175
// Create explanation message
@@ -182,35 +182,17 @@ export class AutoDebugController implements vscode.Disposable {
182182
}
183183

184184
private createFixMessage(filePath: string, problems: Problem[]): string {
185-
const parts = [`Please help me fix the following errors in ${filePath}`]
186-
187-
for (const problem of problems) {
188-
const line = problem.diagnostic.range.start.line + 1
189-
const column = problem.diagnostic.range.start.character + 1
190-
const source = problem.source !== 'unknown' ? problem.source : 'Unknown'
191-
parts.push(
192-
`ERROR: ${problem.diagnostic.message} Location: Line ${line}, Column ${column} Source: ${source}`
193-
)
194-
}
185+
const workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath || ''
186+
const formattedProblems = this.errorFormatter.formatProblemsString(problems, workspaceRoot)
195187

196-
return parts.join('\n')
188+
return `Please help me fix the following errors in ${filePath}:${formattedProblems}`
197189
}
198190

199191
private createExplainMessage(filePath: string, problems: Problem[]): string {
200-
const parts = [
201-
`Please explain the following problems in ${filePath}. DO NOT edit files. ONLY provide explanation`,
202-
]
203-
204-
for (const problem of problems) {
205-
const line = problem.diagnostic.range.start.line + 1
206-
const column = problem.diagnostic.range.start.character + 1
207-
const source = problem.source !== 'unknown' ? problem.source : 'Unknown'
208-
parts.push(
209-
`${problem.severity.toUpperCase()}: ${problem.diagnostic.message} Location: Line ${line}, Column ${column} Source: ${source}`
210-
)
211-
}
192+
const workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath || ''
193+
const formattedProblems = this.errorFormatter.formatProblemsString(problems, workspaceRoot)
212194

213-
return parts.join('\n')
195+
return `Please explain the following problems in ${filePath}. DO NOT edit files. ONLY provide explanation:${formattedProblems}`
214196
}
215197

216198
/**

packages/amazonq/src/lsp/autoDebug/diagnostics/errorContext.ts

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,24 @@ export class ErrorContextFormatter {
114114
}
115115

116116
/**
117-
* Creates a problems string similar to the reference implementation
117+
* Creates a problems string with Markdown formatting for better readability
118118
*/
119119
public formatProblemsString(problems: Problem[], cwd: string): string {
120120
let result = ''
121121
const fileGroups = this.groupProblemsByFile(problems)
122122

123123
for (const [filePath, fileProblems] of fileGroups.entries()) {
124124
if (fileProblems.length > 0) {
125-
result += `\n\n${path.relative(cwd, filePath)}`
125+
result += `\n\n**${path.relative(cwd, filePath)}**\n\n`
126126

127+
// Group problems into a code block for better formatting
128+
result += '```\n'
127129
for (const problem of fileProblems) {
128-
const label = this.getSeverityLabel(problem.severity)
129130
const line = problem.diagnostic.range.start.line + 1
130-
const source = problem.source ? `${problem.source} ` : ''
131-
result += `\n- [${source}${label}] Line ${line}: ${problem.diagnostic.message}`
131+
const source = problem.source ? `${problem.source}` : 'Unknown'
132+
result += `[${source}] Line ${line}: ${problem.diagnostic.message}\n`
132133
}
134+
result += '```'
133135
}
134136
}
135137

@@ -248,19 +250,4 @@ export class ErrorContextFormatter {
248250

249251
return groups
250252
}
251-
252-
private getSeverityLabel(severity: string): string {
253-
switch (severity) {
254-
case 'error':
255-
return 'ERROR'
256-
case 'warning':
257-
return 'WARN'
258-
case 'info':
259-
return 'INFO'
260-
case 'hint':
261-
return 'HINT'
262-
default:
263-
return 'UNKNOWN'
264-
}
265-
}
266253
}

0 commit comments

Comments
 (0)