Skip to content
This repository was archived by the owner on Aug 1, 2025. It is now read-only.

Commit 6a9b5fe

Browse files
Cody: Limit completions to 2 lines in single line mode (#276)
This commit limits code completions to 2 lines when in single line mode, to avoid cluttering the editor with too many lines. Added unit tests for: - Completing up to 2 lines - Completing 1 line if the second line is indented This fixes a UX issue where inline completions look like a truncated multi-line completion, e.g.: <img width="758" alt="Screenshot 2023-07-17 at 14 20 48" src="https://github.com/sourcegraph/cody/assets/458591/062048fc-c332-4aca-9feb-187eeed35242"> <!-- All pull requests REQUIRE a test plan: https://docs.sourcegraph.com/dev/background-information/testing_principles -->
1 parent 5365f91 commit 6a9b5fe

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

vscode/src/completions/completion.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,47 @@ describe('Cody completions', () => {
234234
`)
235235
})
236236

237+
it('completes up to two lines in single line mode', async () => {
238+
const { completions } = await complete(
239+
`
240+
function test() {
241+
console.log(1);
242+
${CURSOR_MARKER}
243+
}
244+
`,
245+
[createCompletionResponse('console.log(2);\n console.log(3);\n console.log(4);')]
246+
)
247+
248+
expect(completions).toMatchInlineSnapshot(`
249+
[
250+
InlineCompletionItem {
251+
"insertText": "console.log(2);
252+
console.log(3);",
253+
},
254+
]
255+
`)
256+
})
257+
258+
it('only complete one line if the second line is indented in single line mode', async () => {
259+
const { completions } = await complete(
260+
`
261+
function test() {
262+
console.log(1);
263+
${CURSOR_MARKER}
264+
}
265+
`,
266+
[createCompletionResponse('if (true) {\n console.log(3);\n }\n console.log(4);')]
267+
)
268+
269+
expect(completions).toMatchInlineSnapshot(`
270+
[
271+
InlineCompletionItem {
272+
"insertText": "if (true) {",
273+
},
274+
]
275+
`)
276+
})
277+
237278
it('completes a single-line at the middle of a sentence', async () => {
238279
const { completions } = await complete(`function bubbleSort(${CURSOR_MARKER})`, [
239280
createCompletionResponse('array) {'),

vscode/src/completions/providers/anthropic.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ import {
1111
extractFromCodeBlock,
1212
fixBadCompletionStart,
1313
getHeadAndTail,
14+
indentation,
1415
OPENING_CODE_TAG,
1516
PrefixComponents,
1617
trimStartUntilNewline,
1718
} from '../text-processing'
18-
import { batchCompletions, messagesToText } from '../utils'
19+
import { batchCompletions, lastNLines, messagesToText } from '../utils'
1920

2021
import { Provider, ProviderConfig, ProviderOptions } from './provider'
2122

@@ -112,6 +113,7 @@ export class AnthropicProvider extends Provider {
112113

113114
private postProcess(rawResponse: string): string {
114115
let completion = extractFromCodeBlock(rawResponse)
116+
const startIndentation = indentation(lastNLines(this.prefix, 1) + completion.split('\n')[0])
115117

116118
const trimmedPrefixContainNewline = this.prefix.slice(this.prefix.trimEnd().length).includes('\n')
117119
if (trimmedPrefixContainNewline) {
@@ -127,9 +129,16 @@ export class AnthropicProvider extends Provider {
127129

128130
// Remove incomplete lines in single-line completions
129131
if (this.multilineMode === null) {
130-
const allowedNewlines = 2
132+
let allowedNewlines = 2
131133
const lines = completion.split('\n')
132134
if (lines.length >= allowedNewlines) {
135+
// Only select two lines if they have the same indentation, otherwise only show one
136+
// line. This will then most-likely trigger a multi-line completion after accepting
137+
// and result in a better experience.
138+
if (lines.length > 1 && startIndentation !== indentation(lines[1])) {
139+
allowedNewlines = 1
140+
}
141+
133142
completion = lines.slice(0, allowedNewlines).join('\n')
134143
}
135144
}

0 commit comments

Comments
 (0)