Skip to content

Commit

Permalink
Autocomplete: keep the original insert range for agent `autocomplete/…
Browse files Browse the repository at this point in the history
…execute` calls (#5871)

- This changes the behavior of the `autocomplete/execute`
requests for the agent based on the [feedback in
Slack](https://sourcegraph.slack.com/archives/C073U3KE5DK/p1728559419953909).
- `InlineCompletionItemProvider.provideInlineCompletionItems()` no
longer executes the VS Code-specific logic, which makes all completions
start at the beginning of the line no matter what the current cursor
position is. This logic was added to avoid UI jitter in VS Code, but it
does not apply to other Cody clients and requires them to implement
additional post-processing logic for no apparent reason.
- This is a breaking change for clients using the agent API
`autocomplete/execute`.
- Previous PR with the relevant discussion
#2573
  • Loading branch information
valerybugakov authored Oct 14, 2024
1 parent 3b7dada commit 3a8462b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
4 changes: 2 additions & 2 deletions agent/src/autocomplete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('Autocomplete', () => {
expect(texts).toMatchInlineSnapshot(
`
[
" return a + b;",
"return a + b;",
]
`
)
Expand Down Expand Up @@ -113,7 +113,7 @@ describe('Autocomplete', () => {
expect(texts).toMatchInlineSnapshot(
`
[
" for (let i = 0; i < arr.length; i++) {
"for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
let temp = arr[j]
Expand Down
2 changes: 1 addition & 1 deletion agent/src/enterprise-s2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ describe('Enterprise - S2 (close main branch)', { timeout: 5000 }, () => {
expect(items.map(item => item.insertText)).toMatchInlineSnapshot(
`
[
" return a + b",
"return a + b",
]
`
)
Expand Down
9 changes: 8 additions & 1 deletion vscode/src/completions/inline-completion-item-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ export class InlineCompletionItemProvider
// return `CompletionEvent` telemetry data to the agent command `autocomplete/execute`.
const autocompleteResult: AutocompleteResult = {
logId: result.logId,
items: updateInsertRangeForVSCode(visibleItems),
items: visibleItems,
completionEvent: CompletionAnalyticsLogger.getCompletionEvent(result.logId),
}

Expand All @@ -641,6 +641,12 @@ export class InlineCompletionItemProvider
// that if we pass the above visibility tests, the completion is going to be
// rendered in the UI
this.unstable_handleDidShowCompletionItem(visibleItems[0])

// Adjust the completion insert text and range to start from beginning of the current line
// (instead of starting at the given position). This avoids UI jitter in VS Code; when
// typing or deleting individual characters, VS Code reuses the existing completion
// while it waits for the new one to come in.
autocompleteResult.items = updateInsertRangeForVSCode(visibleItems)
}

recordExposedExperimentsToSpan(span)
Expand All @@ -660,6 +666,7 @@ export class InlineCompletionItemProvider
return null // Exit early if the request has been aborted
}
}

return autocompleteResult
} catch (error) {
this.onError(error as Error)
Expand Down

0 comments on commit 3a8462b

Please sign in to comment.