Skip to content

Commit 798e647

Browse files
committed
Add failing test case and improve diff hunk matching logic for edge cases
1 parent abd9362 commit 798e647

4 files changed

Lines changed: 42 additions & 0 deletions

File tree

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,18 @@ describe('diff-processor', () => {
140140

141141
expect(result).toBe(expected)
142142
})
143+
144+
it('applies diff with failing test case correctly', async () => {
145+
const test_case = 'failing'
146+
const original = load_test_case_file('', test_case, 'original.txt')
147+
const diff = load_test_case_file('', test_case, 'diff.txt')
148+
149+
expect(() => {
150+
apply_diff({
151+
original_code: original,
152+
diff_patch: diff
153+
})
154+
}).toThrow(/Failed to apply \d+ hunk\(s\)/)
155+
})
143156
})
144157
})

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,24 @@ export const apply_diff = (params: {
216216
}
217217
}
218218

219+
if (max_trim_top > 0 && safe_trim_indices.length === max_trim_top) {
220+
if (
221+
max_trim_top < block.replace_lines.length &&
222+
block.replace_lines[max_trim_top].search_index === null
223+
) {
224+
let has_deletion = true
225+
for (let r = max_trim_top + 1; r < block.replace_lines.length; r++) {
226+
if (block.replace_lines[r].search_index === max_trim_top) {
227+
has_deletion = false
228+
break
229+
}
230+
}
231+
if (!has_deletion) {
232+
max_trim_top -= 1
233+
}
234+
}
235+
}
236+
219237
let found = false
220238
let matched_info: {
221239
start_index: number
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--- a/file.ts
2+
+++ b/file.ts
3+
@@ -1,3 +1,5 @@
4+
function main() {
5+
console.log('middle')
6+
}
7+
+console.log('end')
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function main() {
2+
console.log('middle')
3+
}

0 commit comments

Comments
 (0)