Skip to content

Commit 2921bdd

Browse files
authored
CodeWhisperer: Fix inline suggestions not showing in the editor (#3717)
* CodeWhisperer: Fix inline suggestions not showing in the editor There's a weird case that when an empty suggestion is provided to the inline suggestion provider, the inline suggestion UI won't show afterward even executing commands to hide/show it again. Given that empty suggestions don't need to be shown to the users, don't call onDidReceiveRecommendations until there's at least one non-empty suggestion for the inline provider(so inline UI will be able to pick this and show up correctly) * Fix additional cases that inline suggestions won't show in the editor * Add changelog entry
1 parent 642afd1 commit 2921bdd

File tree

3 files changed

+19
-29
lines changed

3 files changed

+19
-29
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": "CodeWhisperer: Fix in some cases the inline suggestions are not showing in the editor"
4+
}

src/codewhisperer/service/inlineCompletionService.ts

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,31 +51,6 @@ export class CWInlineCompletionItemProvider implements vscode.InlineCompletionIt
5151
this.activeItemIndex = undefined
5252
}
5353

54-
private getGhostText(prefix: string, suggestion: string): string {
55-
const prefixLines = prefix.split(/\r\n|\r|\n/)
56-
const n = prefixLines.length
57-
if (n <= 1) {
58-
return suggestion
59-
}
60-
let count = 1
61-
for (let i = 0; i < suggestion.length; i++) {
62-
if (suggestion[i] === '\n') {
63-
count++
64-
}
65-
if (count === n) {
66-
return suggestion.slice(i + 1)
67-
}
68-
}
69-
return ''
70-
}
71-
72-
private getGhostTextStartPos(start: vscode.Position, current: vscode.Position): vscode.Position {
73-
if (start.line === current.line) {
74-
return start
75-
}
76-
return new vscode.Position(current.line, 0)
77-
}
78-
7954
// iterate suggestions and stop at index 0 or index len - 1
8055
private getIteratingIndexes() {
8156
const len = RecommendationHandler.instance.recommendations.length
@@ -132,8 +107,8 @@ export class CWInlineCompletionItemProvider implements vscode.InlineCompletionIt
132107
return undefined
133108
}
134109
return {
135-
insertText: this.getGhostText(prefix, truncatedSuggestion),
136-
range: new vscode.Range(this.getGhostTextStartPos(start, end), end),
110+
insertText: truncatedSuggestion,
111+
range: new vscode.Range(start, end),
137112
command: {
138113
command: 'aws.codeWhisperer.accept',
139114
title: 'On acceptance',
@@ -168,7 +143,14 @@ export class CWInlineCompletionItemProvider implements vscode.InlineCompletionIt
168143
this.activeItemIndex = undefined
169144
return
170145
}
171-
const start = RecommendationHandler.instance.startPos
146+
147+
// There's a chance that the startPos is no longer valid in the current document (e.g.
148+
// when CodeWhisperer got triggered by 'Enter', the original startPos is with indentation
149+
// but then this indentation got removed by VSCode when another new line is inserted,
150+
// before the code reaches here). In such case, we need to update the startPos to be a
151+
// valid one. Otherwise, inline completion which utilizes this position will function
152+
// improperly.
153+
const start = document.validatePosition(RecommendationHandler.instance.startPos)
172154
const end = position
173155
const iteratingIndexes = this.getIteratingIndexes()
174156
const prefix = document.getText(new vscode.Range(start, end)).replace(/\r\n/g, '\n')

src/codewhisperer/service/recommendationHandler.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ export class RecommendationHandler {
307307
}
308308
})
309309
this.recommendations = pagination ? this.recommendations.concat(recommendation) : recommendation
310-
if (isInlineCompletionEnabled()) {
310+
if (isInlineCompletionEnabled() && this.hasAtLeastOneValidSuggestion(editor, typedPrefix)) {
311311
this._onDidReceiveRecommendation.fire()
312312
}
313313
}
@@ -329,6 +329,10 @@ export class RecommendationHandler {
329329
}
330330
}
331331

332+
hasAtLeastOneValidSuggestion(editor: vscode.TextEditor, typedPrefix: string): boolean {
333+
return this.recommendations.some(r => r.content.trim() !== '' && r.content.startsWith(typedPrefix))
334+
}
335+
332336
cancelPaginatedRequest() {
333337
this.nextToken = ''
334338
this.cancellationToken.cancel()

0 commit comments

Comments
 (0)