Skip to content

Commit 477b71a

Browse files
authored
fix(amazonq): Let Enter invoke auto completion more consistently (#7700)
## Problem The VS Code provideInlineCompletionCallback may not trigger when Enter is pressed, especially in Python files ## Solution manually make this trigger. In case of duplicate, the provideInlineCompletionCallback is already debounced --- - Treat all work as PUBLIC. Private `feature/x` branches will not be squash-merged at release time. - Your code changes must meet the guidelines in [CONTRIBUTING.md](https://github.com/aws/aws-toolkit-vscode/blob/master/CONTRIBUTING.md#guidelines). - License: I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent f2e9403 commit 477b71a

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Bug Fix",
3+
"description": "Let Enter invoke auto completion more consistently"
4+
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,12 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
241241
return []
242242
}
243243

244+
const isAutoTrigger = context.triggerKind === InlineCompletionTriggerKind.Automatic
245+
if (isAutoTrigger && !CodeSuggestionsState.instance.isSuggestionsEnabled()) {
246+
// return early when suggestions are disabled with auto trigger
247+
return []
248+
}
249+
244250
// yield event loop to let the document listen catch updates
245251
await sleep(1)
246252
// prevent user deletion invoking auto trigger
@@ -254,12 +260,6 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
254260
try {
255261
const t0 = performance.now()
256262
vsCodeState.isRecommendationsActive = true
257-
const isAutoTrigger = context.triggerKind === InlineCompletionTriggerKind.Automatic
258-
if (isAutoTrigger && !CodeSuggestionsState.instance.isSuggestionsEnabled()) {
259-
// return early when suggestions are disabled with auto trigger
260-
return []
261-
}
262-
263263
// handling previous session
264264
const prevSession = this.sessionManager.getActiveSession()
265265
const prevSessionId = prevSession?.sessionId

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ export class DocumentEventListener {
2121
this.lastDocumentChangeEventMap.clear()
2222
}
2323
this.lastDocumentChangeEventMap.set(e.document.uri.fsPath, { event: e, timestamp: performance.now() })
24+
// The VS Code provideInlineCompletionCallback may not trigger when Enter is pressed, especially in Python files
25+
// manually make this trigger. In case of duplicate, the provideInlineCompletionCallback is already debounced
26+
if (this.isEnter(e) && vscode.window.activeTextEditor) {
27+
void vscode.commands.executeCommand('editor.action.inlineSuggest.trigger')
28+
}
2429
}
2530
})
2631
}
@@ -47,4 +52,18 @@ export class DocumentEventListener {
4752
this.documentChangeListener.dispose()
4853
}
4954
}
55+
56+
private isEnter(e: vscode.TextDocumentChangeEvent): boolean {
57+
if (e.contentChanges.length !== 1) {
58+
return false
59+
}
60+
const str = e.contentChanges[0].text
61+
if (str.length === 0) {
62+
return false
63+
}
64+
return (
65+
(str.startsWith('\r\n') && str.substring(2).trim() === '') ||
66+
(str[0] === '\n' && str.substring(1).trim() === '')
67+
)
68+
}
5069
}

0 commit comments

Comments
 (0)