Skip to content

Commit

Permalink
Edit: Strip trailing CR characters when computing diff (#5069)
Browse files Browse the repository at this point in the history
## Description

Fixes an issue where we would generate incorrect diffs due to trailing
CR characters in the users' selected code, and this not being matched by
the response from the LLM.

## Test plan

1. Enable end of line sequence CRLF (e.g. in VS Code)
2. Start an edit
3. Check that the diff only shows what changed

<!-- Required. See
https://docs-legacy.sourcegraph.com/dev/background-information/testing_principles.
-->
  • Loading branch information
umpox authored Jul 31, 2024
1 parent a81c35a commit 2e07425
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
8 changes: 6 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Chat: Cody is now defaulted to run in the sidebar for both Enterprise and Non-En

### Fixed

- Edit: Fixed an issue where we would generate an inefficient diff due to a mismatch in the end-of-line sequence between the user and the LLM. [pull/5069](https://github.com/sourcegraph/cody/pull/5069)

### Changed

- Autocomplete: Ignores leading empty new lines for autocomplete suggestions to reduce the number of cases when Cody doesn't suggest anything. [pull/4864](https://github.com/sourcegraph/cody/pull/4864)
Expand Down
2 changes: 1 addition & 1 deletion vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1395,7 +1395,7 @@
"@storybook/preview-api": "^8.0.5",
"@types/crypto-js": "^4.2.2",
"@types/dedent": "^0.7.0",
"@types/diff": "^5.0.9",
"@types/diff": "^5.2.1",
"@types/express": "^4.17.17",
"@types/fs-extra": "^11.0.4",
"@types/glob": "^8.0.0",
Expand Down
12 changes: 10 additions & 2 deletions vscode/src/non-stop/line-diff.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { diffLines } from 'diff'
import { type LinesOptions, diffLines } from 'diff'
import * as vscode from 'vscode'

interface InsertionEdit {
Expand Down Expand Up @@ -37,7 +37,15 @@ export function computeDiff(

let startLine = range.start.line
const applicableDiff: Edit[] = []
const diff = diffLines(original, replacement)
const diff = diffLines(
original,
replacement,
{
// Handle cases where we generate an incorrect diff due to a mismatch in the end of line sequence between
// the LLM and the original code in the users' editor.
stripTrailingCr: true,
} as LinesOptions // @types/diff is not currently up to date
)

for (const change of diff) {
const count = change.count || 0
Expand Down

0 comments on commit 2e07425

Please sign in to comment.