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

Commit ba5dcc5

Browse files
Autocomplete: Remove trailing whitespace (#542)
I noticed some odd duplications in multi-line completion snippets that was caused by a difference only in trailing whitespace. Since my IDE does this on save automatically anyways, I figure it's save to filter the LLM output for this. I noticed this to be an issue in both Anthropic and StarCoder, but Anthropic encounters it more often. ## Test plan - Added a test case <!-- Required. See https://docs.sourcegraph.com/dev/background-information/testing_principles. -->
1 parent 00ae427 commit ba5dcc5

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

vscode/src/completions/shared-post-process.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { truncateMultilineCompletion } from './multiline'
2-
import { collapseDuplicativeWhitespace, trimUntilSuffix } from './text-processing'
2+
import { collapseDuplicativeWhitespace, removeTrailingWhitespace, trimUntilSuffix } from './text-processing'
33
import { Completion } from './types'
44

55
/**
@@ -23,6 +23,7 @@ export function sharedPostProcess({
2323

2424
if (multiline) {
2525
content = truncateMultilineCompletion(content, prefix, suffix, languageId)
26+
content = removeTrailingWhitespace(content)
2627
}
2728
content = trimUntilSuffix(content, prefix, suffix, languageId)
2829
content = collapseDuplicativeWhitespace(prefix, content)

vscode/src/completions/text-processing.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,10 @@ export function collapseDuplicativeWhitespace(prefix: string, completion: string
223223
export function trimEndOnLastLineIfWhitespaceOnly(text: string): string {
224224
return text.replace(/(\r?\n)\s+$/, '$1')
225225
}
226+
227+
export function removeTrailingWhitespace(text: string): string {
228+
return text
229+
.split('\n')
230+
.map(l => l.trimEnd())
231+
.join('\n')
232+
}

vscode/src/completions/vscodeInlineCompletionItemProvider.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,28 @@ describe('Cody completions', () => {
293293
})
294294

295295
describe('multi-line completions', () => {
296+
it('removes trailing spaces', async () => {
297+
const { completions } = await complete(
298+
dedent`
299+
function bubbleSort() {
300+
301+
}`,
302+
[
303+
completion`
304+
├console.log('foo')${' '}
305+
console.log('bar')${' '}
306+
console.log('baz')${' '}
307+
┴┴┴┴`,
308+
]
309+
)
310+
311+
expect(completions[0].insertText).toMatchInlineSnapshot(`
312+
"console.log('foo')
313+
console.log('bar')
314+
console.log('baz')"
315+
`)
316+
})
317+
296318
it('honors a leading new line in the completion', async () => {
297319
const { completions } = await complete(
298320
dedent`

0 commit comments

Comments
 (0)