Skip to content

Commit fdf6178

Browse files
committed
fix(amazonq): use command approach in isCompletionActive
1 parent ade8208 commit fdf6178

File tree

3 files changed

+98
-3
lines changed

3 files changed

+98
-3
lines changed

packages/amazonq/src/app/inline/EditRendering/displayImage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ export async function displaySvgDecoration(
287287
const originalCode = editor.document.getText()
288288

289289
// Check if a completion suggestion is currently active - if so, discard edit suggestion
290-
if (inlineCompletionProvider && inlineCompletionProvider.isCompletionActive()) {
290+
if (inlineCompletionProvider && (await inlineCompletionProvider.isCompletionActive())) {
291291
// Emit DISCARD telemetry for edit suggestion that can't be shown due to active completion
292292
const params: LogInlineCompletionSessionResultsParams = {
293293
sessionId: session.sessionId,

packages/amazonq/src/app/inline/completion.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,20 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
240240
/**
241241
* Check if a completion suggestion is currently active/displayed
242242
*/
243-
public isCompletionActive(): boolean {
243+
public async isCompletionActive(): Promise<boolean> {
244244
const session = this.sessionManager.getActiveSession()
245-
return session !== undefined && session.displayed && !session.suggestions.some((item) => item.isInlineEdit)
245+
if (session === undefined || !session.displayed || session.suggestions.some((item) => item.isInlineEdit)) {
246+
return false
247+
}
248+
249+
// Use VS Code command to check if inline suggestion is actually visible on screen
250+
// This command only executes when inlineSuggestionVisible context is true
251+
try {
252+
await vscode.commands.executeCommand('aws.amazonq.checkInlineSuggestionVisibility')
253+
return true
254+
} catch {
255+
return false
256+
}
246257
}
247258

248259
/**

packages/amazonq/test/unit/app/inline/completion.test.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,88 @@ describe('AmazonQInlineCompletionItemProvider', function () {
117117
assert.strictEqual(mockLanguageClient.sendNotification.callCount, 0)
118118
})
119119
})
120+
121+
describe('isCompletionActive', function () {
122+
let mockSessionManager: any
123+
let mockVscodeCommands: any
124+
125+
beforeEach(function () {
126+
mockSessionManager = {
127+
getActiveSession: sandbox.stub(),
128+
}
129+
130+
// Mock vscode.commands.executeCommand
131+
mockVscodeCommands = sandbox.stub(require('vscode').commands, 'executeCommand')
132+
133+
// Create provider with mocked session manager
134+
provider = new AmazonQInlineCompletionItemProvider(
135+
mockLanguageClient,
136+
{} as any, // recommendationService
137+
mockSessionManager,
138+
{} as any, // inlineTutorialAnnotation
139+
{} as any // documentEventListener
140+
)
141+
})
142+
143+
it('should return false when no active session', async function () {
144+
mockSessionManager.getActiveSession.returns(undefined)
145+
146+
const result = await provider.isCompletionActive()
147+
148+
assert.strictEqual(result, false)
149+
assert.strictEqual(mockVscodeCommands.callCount, 0)
150+
})
151+
152+
it('should return false when session not displayed', async function () {
153+
mockSessionManager.getActiveSession.returns({
154+
displayed: false,
155+
suggestions: [{ isInlineEdit: false }],
156+
})
157+
158+
const result = await provider.isCompletionActive()
159+
160+
assert.strictEqual(result, false)
161+
assert.strictEqual(mockVscodeCommands.callCount, 0)
162+
})
163+
164+
it('should return false when session has inline edit suggestions', async function () {
165+
mockSessionManager.getActiveSession.returns({
166+
displayed: true,
167+
suggestions: [{ isInlineEdit: true }],
168+
})
169+
170+
const result = await provider.isCompletionActive()
171+
172+
assert.strictEqual(result, false)
173+
assert.strictEqual(mockVscodeCommands.callCount, 0)
174+
})
175+
176+
it('should return true when VS Code command executes successfully', async function () {
177+
mockSessionManager.getActiveSession.returns({
178+
displayed: true,
179+
suggestions: [{ isInlineEdit: false }],
180+
})
181+
mockVscodeCommands.resolves()
182+
183+
const result = await provider.isCompletionActive()
184+
185+
assert.strictEqual(result, true)
186+
assert.strictEqual(mockVscodeCommands.callCount, 1)
187+
assert.strictEqual(mockVscodeCommands.getCall(0).args[0], 'aws.amazonq.checkInlineSuggestionVisibility')
188+
})
189+
190+
it('should return false when VS Code command fails', async function () {
191+
mockSessionManager.getActiveSession.returns({
192+
displayed: true,
193+
suggestions: [{ isInlineEdit: false }],
194+
})
195+
mockVscodeCommands.rejects(new Error('Command failed'))
196+
197+
const result = await provider.isCompletionActive()
198+
199+
assert.strictEqual(result, false)
200+
assert.strictEqual(mockVscodeCommands.callCount, 1)
201+
assert.strictEqual(mockVscodeCommands.getCall(0).args[0], 'aws.amazonq.checkInlineSuggestionVisibility')
202+
})
203+
})
120204
})

0 commit comments

Comments
 (0)