Skip to content

Commit d2c7a44

Browse files
committed
extract the common logics to improve code quality
1 parent f704737 commit d2c7a44

File tree

1 file changed

+68
-85
lines changed

1 file changed

+68
-85
lines changed

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

Lines changed: 68 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -40,39 +40,66 @@ export class AutoDebugController implements vscode.Disposable {
4040
}
4141

4242
/**
43-
* Fix specific problems in the code
43+
* Extract common logic for getting problems from diagnostics
4444
*/
45-
async fixSpecificProblems(range?: vscode.Range, diagnostics?: vscode.Diagnostic[]): Promise<void> {
46-
try {
47-
const editor = vscode.window.activeTextEditor
48-
if (!editor) {
49-
throw new Error('No active editor found')
50-
}
45+
private async getProblemsFromDiagnostics(
46+
range?: vscode.Range,
47+
diagnostics?: vscode.Diagnostic[]
48+
): Promise<{ editor: vscode.TextEditor; problems: Problem[] } | undefined> {
49+
const editor = vscode.window.activeTextEditor
50+
if (!editor) {
51+
throw new Error('No active editor found')
52+
}
53+
54+
// Use provided diagnostics or get diagnostics for the range
55+
let targetDiagnostics = diagnostics
56+
if (!targetDiagnostics && range) {
57+
const allDiagnostics = vscode.languages.getDiagnostics(editor.document.uri)
58+
targetDiagnostics = allDiagnostics.filter((d) => d.range.intersection(range) !== undefined)
59+
}
5160

52-
const filePath = editor.document.uri.fsPath
61+
if (!targetDiagnostics || targetDiagnostics.length === 0) {
62+
return undefined
63+
}
5364

54-
// Use provided diagnostics or get diagnostics for the range
55-
let targetDiagnostics = diagnostics
56-
if (!targetDiagnostics && range) {
57-
const allDiagnostics = vscode.languages.getDiagnostics(editor.document.uri)
58-
targetDiagnostics = allDiagnostics.filter((d) => d.range.intersection(range) !== undefined)
65+
// Convert diagnostics to problems
66+
const problems = targetDiagnostics.map((diagnostic) => ({
67+
uri: editor.document.uri,
68+
diagnostic,
69+
severity: mapDiagnosticSeverity(diagnostic.severity),
70+
source: diagnostic.source || 'unknown',
71+
isNew: false,
72+
}))
73+
74+
return { editor, problems }
75+
}
76+
77+
/**
78+
* Filter diagnostics to only errors and apply source filtering
79+
*/
80+
private filterErrorDiagnostics(diagnostics: vscode.Diagnostic[]): vscode.Diagnostic[] {
81+
return diagnostics.filter((d) => {
82+
if (d.severity !== vscode.DiagnosticSeverity.Error) {
83+
return false
84+
}
85+
// Apply source filtering
86+
if (this.config.excludedSources.length > 0 && d.source) {
87+
return !this.config.excludedSources.includes(d.source)
5988
}
89+
return true
90+
})
91+
}
6092

61-
if (!targetDiagnostics || targetDiagnostics.length === 0) {
93+
/**
94+
* Fix specific problems in the code
95+
*/
96+
async fixSpecificProblems(range?: vscode.Range, diagnostics?: vscode.Diagnostic[]): Promise<void> {
97+
try {
98+
const result = await this.getProblemsFromDiagnostics(range, diagnostics)
99+
if (!result) {
62100
return
63101
}
64-
65-
// Convert diagnostics to problems
66-
const problems = targetDiagnostics.map((diagnostic) => ({
67-
uri: editor.document.uri,
68-
diagnostic,
69-
severity: mapDiagnosticSeverity(diagnostic.severity),
70-
source: diagnostic.source || 'unknown',
71-
isNew: false,
72-
}))
73-
74-
// Create fix message
75-
const fixMessage = this.createFixMessage(filePath, problems)
102+
const fixMessage = this.createFixMessage(result.editor.document.uri.fsPath, result.problems)
76103
await this.sendMessageToChat(fixMessage)
77104
} catch (error) {
78105
this.logger.error('AutoDebugController: Error fixing specific problems: %s', error)
@@ -84,48 +111,28 @@ export class AutoDebugController implements vscode.Disposable {
84111
* Fix with Amazon Q - sends up to 15 error messages one time when user clicks the button
85112
*/
86113
public async fixAllProblemsInFile(maxProblems: number = 15): Promise<void> {
87-
const editor = vscode.window.activeTextEditor
88-
if (!editor) {
89-
void messages.showMessage('warn', 'No active editor found')
90-
return
91-
}
92-
93-
const filePath = editor.document.uri.fsPath
94-
95114
try {
115+
const editor = vscode.window.activeTextEditor
116+
if (!editor) {
117+
void messages.showMessage('warn', 'No active editor found')
118+
return
119+
}
120+
96121
// Get all diagnostics for the current file
97122
const allDiagnostics = vscode.languages.getDiagnostics(editor.document.uri)
98-
99-
// Filter to only errors (not warnings/info) and apply source filtering
100-
const errorDiagnostics = allDiagnostics.filter((d) => {
101-
if (d.severity !== vscode.DiagnosticSeverity.Error) {
102-
return false
103-
}
104-
// Apply source filtering
105-
if (this.config.excludedSources.length > 0 && d.source) {
106-
return !this.config.excludedSources.includes(d.source)
107-
}
108-
return true
109-
})
110-
123+
const errorDiagnostics = this.filterErrorDiagnostics(allDiagnostics)
111124
if (errorDiagnostics.length === 0) {
112125
return
113126
}
114127

115128
// Take up to maxProblems errors (15 by default)
116129
const diagnosticsToFix = errorDiagnostics.slice(0, maxProblems)
130+
const result = await this.getProblemsFromDiagnostics(undefined, diagnosticsToFix)
131+
if (!result) {
132+
return
133+
}
117134

118-
// Convert diagnostics to problems
119-
const problems = diagnosticsToFix.map((diagnostic) => ({
120-
uri: editor.document.uri,
121-
diagnostic,
122-
severity: mapDiagnosticSeverity(diagnostic.severity),
123-
source: diagnostic.source || 'unknown',
124-
isNew: false,
125-
}))
126-
127-
// Create fix message
128-
const fixMessage = this.createFixMessage(filePath, problems)
135+
const fixMessage = this.createFixMessage(result.editor.document.uri.fsPath, result.problems)
129136
await this.sendMessageToChat(fixMessage)
130137
} catch (error) {
131138
this.logger.error('AutoDebugController: Error in fix process: %s', error)
@@ -137,35 +144,11 @@ export class AutoDebugController implements vscode.Disposable {
137144
*/
138145
async explainProblems(range?: vscode.Range, diagnostics?: vscode.Diagnostic[]): Promise<void> {
139146
try {
140-
const editor = vscode.window.activeTextEditor
141-
if (!editor) {
142-
throw new Error('No active editor found')
143-
}
144-
145-
const filePath = editor.document.uri.fsPath
146-
147-
// Use provided diagnostics or get diagnostics for the range
148-
let targetDiagnostics = diagnostics
149-
if (!targetDiagnostics && range) {
150-
const allDiagnostics = vscode.languages.getDiagnostics(editor.document.uri)
151-
targetDiagnostics = allDiagnostics.filter((d) => d.range.intersection(range) !== undefined)
152-
}
153-
154-
if (!targetDiagnostics || targetDiagnostics.length === 0) {
147+
const result = await this.getProblemsFromDiagnostics(range, diagnostics)
148+
if (!result) {
155149
return
156150
}
157-
158-
// Convert diagnostics to problems
159-
const problems = targetDiagnostics.map((diagnostic) => ({
160-
uri: editor.document.uri,
161-
diagnostic,
162-
severity: mapDiagnosticSeverity(diagnostic.severity),
163-
source: diagnostic.source || 'unknown',
164-
isNew: false,
165-
}))
166-
167-
// Create explanation message
168-
const explainMessage = this.createExplainMessage(filePath, problems)
151+
const explainMessage = this.createExplainMessage(result.editor.document.uri.fsPath, result.problems)
169152
await this.sendMessageToChat(explainMessage)
170153
} catch (error) {
171154
this.logger.error('AutoDebugController: Error explaining problems: %s', error)

0 commit comments

Comments
 (0)