Skip to content

Commit 2db1b1d

Browse files
committed
Handle diff application to empty files in diff-processor and add a corresponding test case
1 parent f895619 commit 2db1b1d

5 files changed

Lines changed: 60 additions & 0 deletions

File tree

‎apps/editor/src/commands/apply-chat-response-command/utils/edit-formats/diffs/diff-processor.spec.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,5 +222,19 @@ describe('diff-processor', () => {
222222

223223
expect(result).toBe(expected)
224224
})
225+
226+
it('applies diff to an empty file 2 correctly', async () => {
227+
const test_case = 'empty-file-2'
228+
const original = load_test_case_file('', test_case, 'original.txt')
229+
const diff = load_test_case_file('', test_case, 'diff.txt')
230+
const expected = load_test_case_file('', test_case, 'expected.txt')
231+
232+
const result = apply_diff({
233+
original_code: original,
234+
diff_patch: diff
235+
})
236+
237+
expect(result).toBe(expected)
238+
})
225239
})
226240
})

‎apps/editor/src/commands/apply-chat-response-command/utils/edit-formats/diffs/diff-processor.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,40 @@ export const apply_diff = (params: {
1818
use_strict_whitespace?: boolean
1919
}): string => {
2020
const original_code_normalized = params.original_code.replace(/\r\n/g, '\n')
21+
22+
if (original_code_normalized.trim() == '') {
23+
const patch_normalized = params.diff_patch.replace(/\r\n/g, '\n')
24+
const patch_lines = patch_normalized.split('\n')
25+
let has_additions = false
26+
let in_hunk = false
27+
const result_lines: string[] = []
28+
29+
for (const line of patch_lines) {
30+
if (line.startsWith('@@')) {
31+
in_hunk = true
32+
continue
33+
}
34+
if (!in_hunk) continue
35+
36+
if (line.startsWith('+')) {
37+
result_lines.push(line.substring(1))
38+
has_additions = true
39+
} else if (line.startsWith(' ')) {
40+
result_lines.push(line.substring(1))
41+
} else if (line == '') {
42+
result_lines.push('')
43+
}
44+
}
45+
46+
if (has_additions) {
47+
let final_string = result_lines.join('\n')
48+
if (final_string.length > 0 && !final_string.endsWith('\n')) {
49+
final_string += '\n'
50+
}
51+
return final_string
52+
}
53+
}
54+
2155
const original_code_lines = original_code_normalized.split(/^/m)
2256
const original_code_lines_normalized: { key: number; value: string }[] = []
2357

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--- a/package.json
2+
+++ b/package.json
3+
@@ -1,3 +1,19 @@
4+
{
5+
-
6+
+ "name": "server-tiles",
7+
+ "version": "1.0.0"
8+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "server-tiles",
3+
"version": "1.0.0"
4+
}

‎apps/editor/src/commands/apply-chat-response-command/utils/edit-formats/diffs/test-cases/empty-file-2/original.txt‎

Whitespace-only changes.

0 commit comments

Comments
 (0)