Skip to content

Commit 6dc202d

Browse files
committed
fix code duplication issues
1 parent 975a34e commit 6dc202d

File tree

6 files changed

+132
-83
lines changed

6 files changed

+132
-83
lines changed

packages/core/src/amazonq/autoDebug/autoDebugController.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { ProblemDetector, Problem, CategorizedProblems } from './diagnostics/pro
1010
import { ErrorContextFormatter, ErrorContext } from './diagnostics/errorContext'
1111
import { AutoDebugLspClient } from './lsp/autoDebugLspClient'
1212
import { randomUUID } from '../../shared/crypto'
13+
import { mapDiagnosticSeverity } from './shared/diagnosticUtils'
1314

1415
export interface AutoDebugConfig {
1516
readonly enabled: boolean
@@ -56,7 +57,7 @@ export class AutoDebugController implements vscode.Disposable {
5657
includedSources: [], // Empty means include all
5758
excludedSources: ['spell-checker'], // Common sources to exclude
5859
severityFilter: ['error'], // Only auto-fix errors, not warnings
59-
debounceMs: 2000, // Wait 2 seconds before auto-sending to avoid spam
60+
debounceMs: 500, // Wait 0.5 seconds before auto-sending to avoid spam
6061
...config,
6162
}
6263

@@ -303,7 +304,7 @@ export class AutoDebugController implements vscode.Disposable {
303304
const problems = diagnosticsToFix.map((diagnostic) => ({
304305
uri: editor.document.uri,
305306
diagnostic,
306-
severity: this.mapDiagnosticSeverity(diagnostic.severity),
307+
severity: mapDiagnosticSeverity(diagnostic.severity),
307308
source: diagnostic.source || 'unknown',
308309
isNew: false,
309310
}))
@@ -339,21 +340,6 @@ export class AutoDebugController implements vscode.Disposable {
339340
}
340341
}
341342

342-
private mapDiagnosticSeverity(severity: vscode.DiagnosticSeverity): 'error' | 'warning' | 'info' | 'hint' {
343-
switch (severity) {
344-
case vscode.DiagnosticSeverity.Error:
345-
return 'error'
346-
case vscode.DiagnosticSeverity.Warning:
347-
return 'warning'
348-
case vscode.DiagnosticSeverity.Information:
349-
return 'info'
350-
case vscode.DiagnosticSeverity.Hint:
351-
return 'hint'
352-
default:
353-
return 'error'
354-
}
355-
}
356-
357343
private createFixMessage(
358344
selectedText: string,
359345
filePath: string,
@@ -625,7 +611,7 @@ You can manually apply these fixes or use the "Apply Fixes" option from the noti
625611
const problems = errorDiagnostics.map((diagnostic) => ({
626612
uri: document.uri,
627613
diagnostic,
628-
severity: this.mapDiagnosticSeverity(diagnostic.severity),
614+
severity: mapDiagnosticSeverity(diagnostic.severity),
629615
source: diagnostic.source || 'unknown',
630616
isNew: true,
631617
}))

packages/core/src/amazonq/autoDebug/diagnostics/problemDetector.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as vscode from 'vscode'
77
import * as path from 'path'
88
import { getLogger } from '../../../shared/logger/logger'
99
import { DiagnosticCollection, DiagnosticSnapshot } from './diagnosticsMonitor'
10+
import { mapDiagnosticSeverity } from '../shared/diagnosticUtils'
1011

1112
export interface Problem {
1213
readonly uri: vscode.Uri
@@ -212,27 +213,12 @@ export class ProblemDetector {
212213
return {
213214
uri,
214215
diagnostic,
215-
severity: this.mapSeverity(diagnostic.severity),
216+
severity: mapDiagnosticSeverity(diagnostic.severity),
216217
source: diagnostic.source || 'unknown',
217218
isNew,
218219
}
219220
}
220221

221-
private mapSeverity(severity: vscode.DiagnosticSeverity): 'error' | 'warning' | 'info' | 'hint' {
222-
switch (severity) {
223-
case vscode.DiagnosticSeverity.Error:
224-
return 'error'
225-
case vscode.DiagnosticSeverity.Warning:
226-
return 'warning'
227-
case vscode.DiagnosticSeverity.Information:
228-
return 'info'
229-
case vscode.DiagnosticSeverity.Hint:
230-
return 'hint'
231-
default:
232-
return 'info'
233-
}
234-
}
235-
236222
private isRelatedFile(problemFile: string, changedFiles: string[]): boolean {
237223
// Check if the problem file is in the same directory or a parent/child directory
238224
// of any changed file (simple heuristic for related files)

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

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Commands } from '../../../shared/vscode/commands2'
99
import { AutoDebugController } from '../autoDebugController'
1010
import { focusAmazonQPanel } from '../../../codewhispererChat/commands/registerCommands'
1111
import { placeholder } from '../../../shared/vscode/commands2'
12+
import { mapDiagnosticSeverity, getDiagnosticsForRange } from '../shared/diagnosticUtils'
1213

1314
/**
1415
* Provides context menu integration for Amazon Q Auto Debug features.
@@ -115,13 +116,13 @@ export class ContextMenuProvider implements vscode.Disposable {
115116
const languageId = editor.document.languageId
116117

117118
// Get diagnostics for the current location if not provided
118-
const contextDiagnostics = diagnostics || this.getDiagnosticsForRange(editor.document.uri, range)
119+
const contextDiagnostics = diagnostics || getDiagnosticsForRange(editor.document.uri, range)
119120

120121
// Convert diagnostics to problems for formatting
121122
const problems = contextDiagnostics.map((diagnostic) => ({
122123
uri: editor.document.uri,
123124
diagnostic,
124-
severity: this.mapDiagnosticSeverity(diagnostic.severity),
125+
severity: mapDiagnosticSeverity(diagnostic.severity),
125126
source: diagnostic.source || 'unknown',
126127
isNew: false,
127128
}))
@@ -183,7 +184,7 @@ export class ContextMenuProvider implements vscode.Disposable {
183184
const languageId = editor.document.languageId
184185

185186
// Get diagnostics for the current location if not provided
186-
const contextDiagnostics = diagnostics || this.getDiagnosticsForRange(editor.document.uri, range)
187+
const contextDiagnostics = diagnostics || getDiagnosticsForRange(editor.document.uri, range)
187188

188189
if (contextDiagnostics.length === 0) {
189190
void vscode.window.showInformationMessage('No problems found in the selected code')
@@ -194,7 +195,7 @@ export class ContextMenuProvider implements vscode.Disposable {
194195
const problems = contextDiagnostics.map((diagnostic) => ({
195196
uri: editor.document.uri,
196197
diagnostic,
197-
severity: this.mapDiagnosticSeverity(diagnostic.severity),
198+
severity: mapDiagnosticSeverity(diagnostic.severity),
198199
source: diagnostic.source || 'unknown',
199200
isNew: false,
200201
}))
@@ -224,7 +225,7 @@ export class ContextMenuProvider implements vscode.Disposable {
224225
}
225226

226227
// Get diagnostics for the current location if not provided
227-
const contextDiagnostics = diagnostics || this.getDiagnosticsForRange(editor.document.uri, range)
228+
const contextDiagnostics = diagnostics || getDiagnosticsForRange(editor.document.uri, range)
228229

229230
if (contextDiagnostics.length === 0) {
230231
void vscode.window.showInformationMessage('No problems found at the current location')
@@ -235,7 +236,7 @@ export class ContextMenuProvider implements vscode.Disposable {
235236
const problems = contextDiagnostics.map((diagnostic) => ({
236237
uri: editor.document.uri,
237238
diagnostic,
238-
severity: this.mapDiagnosticSeverity(diagnostic.severity),
239+
severity: mapDiagnosticSeverity(diagnostic.severity),
239240
source: diagnostic.source || 'unknown',
240241
isNew: false,
241242
}))
@@ -303,31 +304,6 @@ export class ContextMenuProvider implements vscode.Disposable {
303304
return currentLine.text
304305
}
305306

306-
private getDiagnosticsForRange(uri: vscode.Uri, range?: vscode.Range): vscode.Diagnostic[] {
307-
const allDiagnostics = vscode.languages.getDiagnostics(uri)
308-
309-
if (!range) {
310-
return allDiagnostics
311-
}
312-
313-
return allDiagnostics.filter((diagnostic) => diagnostic.range.intersection(range) !== undefined)
314-
}
315-
316-
private mapDiagnosticSeverity(severity: vscode.DiagnosticSeverity): 'error' | 'warning' | 'info' | 'hint' {
317-
switch (severity) {
318-
case vscode.DiagnosticSeverity.Error:
319-
return 'error'
320-
case vscode.DiagnosticSeverity.Warning:
321-
return 'warning'
322-
case vscode.DiagnosticSeverity.Information:
323-
return 'info'
324-
case vscode.DiagnosticSeverity.Hint:
325-
return 'hint'
326-
default:
327-
return 'info'
328-
}
329-
}
330-
331307
private createChatMessage(selectedText: string, filePath: string, languageId: string, problems: string): string {
332308
const parts = [
333309
'I need help with this code that has some issues:',
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import * as vscode from 'vscode'
7+
8+
/**
9+
* Shared utility functions for diagnostic operations across AutoDebug components
10+
*/
11+
12+
/**
13+
* Maps VSCode DiagnosticSeverity to string representation
14+
*/
15+
export function mapDiagnosticSeverity(severity: vscode.DiagnosticSeverity): 'error' | 'warning' | 'info' | 'hint' {
16+
switch (severity) {
17+
case vscode.DiagnosticSeverity.Error:
18+
return 'error'
19+
case vscode.DiagnosticSeverity.Warning:
20+
return 'warning'
21+
case vscode.DiagnosticSeverity.Information:
22+
return 'info'
23+
case vscode.DiagnosticSeverity.Hint:
24+
return 'hint'
25+
default:
26+
return 'error'
27+
}
28+
}
29+
30+
/**
31+
* Maps string severity to VSCode DiagnosticSeverity
32+
*/
33+
export function mapSeverityToVSCode(severity: 'error' | 'warning' | 'info' | 'hint'): vscode.DiagnosticSeverity {
34+
switch (severity) {
35+
case 'error':
36+
return vscode.DiagnosticSeverity.Error
37+
case 'warning':
38+
return vscode.DiagnosticSeverity.Warning
39+
case 'info':
40+
return vscode.DiagnosticSeverity.Information
41+
case 'hint':
42+
return vscode.DiagnosticSeverity.Hint
43+
default:
44+
return vscode.DiagnosticSeverity.Error
45+
}
46+
}
47+
48+
/**
49+
* Gets diagnostics for a specific range in a document
50+
*/
51+
export function getDiagnosticsForRange(uri: vscode.Uri, range?: vscode.Range): vscode.Diagnostic[] {
52+
const allDiagnostics = vscode.languages.getDiagnostics(uri)
53+
54+
if (!range) {
55+
return allDiagnostics
56+
}
57+
58+
return allDiagnostics.filter((diagnostic) => diagnostic.range.intersection(range) !== undefined)
59+
}
60+
61+
/**
62+
* Filters diagnostics by severity levels
63+
*/
64+
export function filterDiagnosticsBySeverity(
65+
diagnostics: vscode.Diagnostic[],
66+
severities: vscode.DiagnosticSeverity[]
67+
): vscode.Diagnostic[] {
68+
return diagnostics.filter((diagnostic) => severities.includes(diagnostic.severity))
69+
}
70+
71+
/**
72+
* Gets only error-level diagnostics
73+
*/
74+
export function getErrorDiagnostics(diagnostics: vscode.Diagnostic[]): vscode.Diagnostic[] {
75+
return filterDiagnosticsBySeverity(diagnostics, [vscode.DiagnosticSeverity.Error])
76+
}

packages/core/src/amazonq/webview/ui/messages/controller.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { ChatItem, ChatItemType, MynahUI, NotificationType } from '@aws/mynah-ui'
6+
import { ChatItem, ChatItemType, MynahUI } from '@aws/mynah-ui'
77
import { Connector } from '../connector'
88
import { TabType, TabsStorage } from '../storages/tabsStorage'
99
import { TabDataGenerator } from '../tabs/generator'
10-
import { uiComponentsTexts } from '../texts/constants'
1110
import { MynahUIRef } from '../../../commons/types'
11+
import { TabCreationUtils } from './tabCreationUtils'
1212

1313
export interface MessageControllerProps {
1414
mynahUIRef: MynahUIRef
@@ -54,15 +54,8 @@ export class MessageController {
5454
['featuredev', 'gumby', 'review', 'testgen', 'doc'].includes(selectedTab.type)
5555
) {
5656
// Create a new tab if there's none
57-
const newTabID: string | undefined = this.mynahUI.updateStore(
58-
'',
59-
this.tabDataGenerator.getTabData('cwc', false)
60-
)
57+
const newTabID = TabCreationUtils.createNewTab(this.mynahUI, this.tabDataGenerator, 'cwc')
6158
if (newTabID === undefined) {
62-
this.mynahUI.notify({
63-
content: uiComponentsTexts.noMoreTabsTooltip,
64-
type: NotificationType.WARNING,
65-
})
6659
return undefined
6760
}
6861
this.tabsStorage.addTab({
@@ -109,15 +102,8 @@ export class MessageController {
109102

110103
targetTabId = selectedTab.id
111104
} else {
112-
const newTabID: string | undefined = this.mynahUI.updateStore(
113-
'',
114-
this.tabDataGenerator.getTabData('cwc', false)
115-
)
105+
const newTabID = TabCreationUtils.createNewTab(this.mynahUI, this.tabDataGenerator, 'cwc')
116106
if (newTabID === undefined) {
117-
this.mynahUI.notify({
118-
content: uiComponentsTexts.noMoreTabsTooltip,
119-
type: NotificationType.WARNING,
120-
})
121107
return undefined
122108
}
123109

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import { MynahUI, NotificationType } from '@aws/mynah-ui'
7+
import { TabDataGenerator } from '../tabs/generator'
8+
import { uiComponentsTexts } from '../texts/constants'
9+
import { TabType } from '../storages/tabsStorage'
10+
11+
/**
12+
* Shared utility for creating tabs in the MessageController
13+
*/
14+
export class TabCreationUtils {
15+
/**
16+
* Creates a new tab with error handling
17+
* @param mynahUI - The MynahUI instance
18+
* @param tabDataGenerator - The tab data generator
19+
* @param tabType - The type of tab to create (default: 'cwc')
20+
* @returns The new tab ID or undefined if creation failed
21+
*/
22+
public static createNewTab(
23+
mynahUI: MynahUI,
24+
tabDataGenerator: TabDataGenerator,
25+
tabType: TabType = 'cwc'
26+
): string | undefined {
27+
const newTabID: string | undefined = mynahUI.updateStore('', tabDataGenerator.getTabData(tabType, false))
28+
29+
if (newTabID === undefined) {
30+
mynahUI.notify({
31+
content: uiComponentsTexts.noMoreTabsTooltip,
32+
type: NotificationType.WARNING,
33+
})
34+
return undefined
35+
}
36+
37+
return newTabID
38+
}
39+
}

0 commit comments

Comments
 (0)