Skip to content

Commit bbdeb78

Browse files
committed
fix remaining code duplication in contextMenuProvider
1 parent 6dc202d commit bbdeb78

File tree

1 file changed

+47
-37
lines changed

1 file changed

+47
-37
lines changed

packages/core/src/amazonq/autoDebug/ide/contextMenuProvider.ts

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,8 @@ export class ContextMenuProvider implements vscode.Disposable {
115115
const filePath = editor.document.uri.fsPath
116116
const languageId = editor.document.languageId
117117

118-
// Get diagnostics for the current location if not provided
119-
const contextDiagnostics = diagnostics || getDiagnosticsForRange(editor.document.uri, range)
120-
121-
// Convert diagnostics to problems for formatting
122-
const problems = contextDiagnostics.map((diagnostic) => ({
123-
uri: editor.document.uri,
124-
diagnostic,
125-
severity: mapDiagnosticSeverity(diagnostic.severity),
126-
source: diagnostic.source || 'unknown',
127-
isNew: false,
128-
}))
118+
// Use shared helper to get diagnostics and convert to problems
119+
const problems = this.getDiagnosticsAsProblems(editor, range, diagnostics) || []
129120

130121
// Format the context for chat
131122
const formattedProblems = this.autoDebugController.formatProblemsForChat(problems)
@@ -183,23 +174,18 @@ export class ContextMenuProvider implements vscode.Disposable {
183174
const filePath = editor.document.uri.fsPath
184175
const languageId = editor.document.languageId
185176

186-
// Get diagnostics for the current location if not provided
187-
const contextDiagnostics = diagnostics || getDiagnosticsForRange(editor.document.uri, range)
177+
// Use shared helper to get diagnostics and convert to problems
178+
const problems = this.getDiagnosticsAsProblems(
179+
editor,
180+
range,
181+
diagnostics,
182+
'No problems found in the selected code'
183+
)
188184

189-
if (contextDiagnostics.length === 0) {
190-
void vscode.window.showInformationMessage('No problems found in the selected code')
185+
if (!problems) {
191186
return
192187
}
193188

194-
// Convert diagnostics to problems
195-
const problems = contextDiagnostics.map((diagnostic) => ({
196-
uri: editor.document.uri,
197-
diagnostic,
198-
severity: mapDiagnosticSeverity(diagnostic.severity),
199-
source: diagnostic.source || 'unknown',
200-
isNew: false,
201-
}))
202-
203189
// Create focused fix message for specific problems
204190
const errorContexts = await this.autoDebugController.createErrorContexts(problems)
205191
const fixMessage = this.createFixMessage(selectedText, filePath, languageId, errorContexts)
@@ -224,23 +210,18 @@ export class ContextMenuProvider implements vscode.Disposable {
224210
return
225211
}
226212

227-
// Get diagnostics for the current location if not provided
228-
const contextDiagnostics = diagnostics || getDiagnosticsForRange(editor.document.uri, range)
213+
// Use shared helper to get diagnostics and convert to problems
214+
const problems = this.getDiagnosticsAsProblems(
215+
editor,
216+
range,
217+
diagnostics,
218+
'No problems found at the current location'
219+
)
229220

230-
if (contextDiagnostics.length === 0) {
231-
void vscode.window.showInformationMessage('No problems found at the current location')
221+
if (!problems) {
232222
return
233223
}
234224

235-
// Convert diagnostics to problems
236-
const problems = contextDiagnostics.map((diagnostic) => ({
237-
uri: editor.document.uri,
238-
diagnostic,
239-
severity: mapDiagnosticSeverity(diagnostic.severity),
240-
source: diagnostic.source || 'unknown',
241-
isNew: false,
242-
}))
243-
244225
// Create explanation message
245226
const explanationMessage = this.createExplanationMessage(problems)
246227

@@ -304,6 +285,35 @@ export class ContextMenuProvider implements vscode.Disposable {
304285
return currentLine.text
305286
}
306287

288+
/**
289+
* Shared helper to get diagnostics and convert them to problems
290+
*/
291+
private getDiagnosticsAsProblems(
292+
editor: vscode.TextEditor,
293+
range?: vscode.Range,
294+
diagnostics?: vscode.Diagnostic[],
295+
noProblemsMessage?: string
296+
): any[] | undefined {
297+
// Get diagnostics for the current location if not provided
298+
const contextDiagnostics = diagnostics || getDiagnosticsForRange(editor.document.uri, range)
299+
300+
if (contextDiagnostics.length === 0) {
301+
if (noProblemsMessage) {
302+
void vscode.window.showInformationMessage(noProblemsMessage)
303+
}
304+
return undefined
305+
}
306+
307+
// Convert diagnostics to problems
308+
return contextDiagnostics.map((diagnostic) => ({
309+
uri: editor.document.uri,
310+
diagnostic,
311+
severity: mapDiagnosticSeverity(diagnostic.severity),
312+
source: diagnostic.source || 'unknown',
313+
isNew: false,
314+
}))
315+
}
316+
307317
private createChatMessage(selectedText: string, filePath: string, languageId: string, problems: string): string {
308318
const parts = [
309319
'I need help with this code that has some issues:',

0 commit comments

Comments
 (0)