Skip to content

Commit ee92d69

Browse files
committed
feat(diff): use diff-match-patch for better diff handling
Refactored diff utility to use diff-match-patch for unified diff application, improving reliability and handling of ambiguous hunks. Removed legacy parse_unified_diff and get_unified_diff_region logic. Updated buffer patching and region detection to use new approach. Adjusted tests to match new diff API and behaviors. Added vendor diff_match_patch implementation. Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
1 parent 5c3a558 commit ee92d69

6 files changed

Lines changed: 2382 additions & 277 deletions

File tree

lua/CopilotChat/config/mappings.lua

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,7 @@ return {
172172

173173
local path = block.header.filename
174174
local bufnr = prepare_diff_buffer(path, source)
175-
local new_lines, applied = diff.apply_diff(block, bufnr)
176-
if not applied then
177-
new_lines = utils.split_lines(block.content)
178-
end
179-
175+
local new_lines = diff.apply_diff(block, bufnr)
180176
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, new_lines)
181177
local first, last = diff.get_diff_region(block, bufnr)
182178
if first and last then
@@ -227,15 +223,11 @@ return {
227223

228224
local path = block.header.filename
229225
local bufnr = prepare_diff_buffer(path, source)
230-
local new_lines, applied = diff.apply_diff(block, bufnr)
231-
if not applied then
232-
new_lines = utils.split_lines(block.content)
233-
end
234-
local original_lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
226+
local new_lines = diff.apply_diff(block, bufnr)
235227

236228
local opts = {
237229
filetype = vim.bo[bufnr].filetype,
238-
text = applied and table.concat(new_lines, '\n') or table.concat(original_lines, '\n'),
230+
text = table.concat(new_lines, '\n'),
239231
}
240232

241233
opts.on_show = function()

lua/CopilotChat/config/prompts.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The user is currently in workspace directory {DIR} (project root). File paths ar
2929
Context is provided to you in several ways:
3030
- Resources: Contextual data shared via "# <uri>" headers and referenced via "##<uri>" links
3131
- Code blocks with file path labels and line numbers (e.g., ```lua path=/file.lua start_line=1 end_line=10```)
32-
Note: Line numbers prefixed to each line are for reference only and should never be included when outputting code
32+
Note: Each line in code block can be prefixed with <line_number>: for your reference only. NEVER include these line numbers in your responses.
3333
- Visual selections: Text selected in visual mode that can be shared as context
3434
- Diffs: Changes shown in unified diff format (+, -, etc.)
3535
- Conversation history

lua/CopilotChat/instructions/edit_file_unified.lua

Lines changed: 22 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,32 @@ return [[
22
<editFileInstructions>
33
Return edits similar to unified diffs that `diff -U0` would produce.
44
5-
- Always include the first 2 lines with the file paths (no timestamps).
6-
- Start each hunk of changes with a `@@ ... @@` line.
7-
- Do not include line numbers in the hunk header.
8-
- The user's patch tool needs CORRECT patches that apply cleanly against the current contents of the file.
9-
- Indentation matters in the diffs!
5+
Make sure you include the first 2 lines with the file paths.
6+
Don't include timestamps with the file paths.
7+
Do not use any file path prefixes, just use --- path/to/file and +++ path/to/file.
108
11-
Context lines:
12-
- For each hunk that contains changes, you MUST always include 2-3 context lines before the change.
13-
- ALWAYS prefix every context line with a single space character.
14-
- Context lines MUST ONLY appear BEFORE changes, NEVER after changes.
15-
- MISSING CONTEXT LINES WILL CAUSE PATCH FAILURES - they are mandatory, not optional.
16-
- MISSING SPACE PREFIXES WILL CAUSE PATCH FAILURES - they are mandatory, not optional.
9+
Start each hunk of changes with a `@@` line.
1710
18-
Change lines:
19-
- Mark all lines to be removed or changed with `-`.
20-
- Mark all new or modified lines with `+`.
21-
- Only output hunks that specify changes with `+` or `-` lines.
11+
The user's patch tool needs CORRECT patches that apply cleanly against the current contents of the file!
12+
Think carefully and make sure you include and mark all lines that need to be removed or changed as `-` lines.
13+
Make sure you mark all new or modified lines with `+`.
14+
Don't leave out any lines or the diff patch won't apply correctly.
2215
23-
Other instructions:
24-
- Start a new hunk for each section of the file that needs changes.
25-
- When editing a function, method, loop, etc., replace the entire code block: delete the entire existing version with `-` lines, then add the new, updated version with `+` lines.
26-
- To move code within a file, use 2 hunks: one to delete it from its current location, one to insert it in the new location.
27-
- To make a new file, show a diff from `--- /dev/null` to `+++ path/to/new/file.ext`.
16+
Indentation matters in the diffs!
2817
29-
Example:
18+
Start a new hunk for each section of the file that needs changes.
3019
31-
```diff
32-
--- mathweb/flask/app.py
33-
+++ mathweb/flask/app.py
34-
@@ ... @@
35-
-class MathWeb:
36-
+import sympy
37-
+
38-
+class MathWeb:
39-
@@ ... @@
40-
-def is_prime(x):
41-
- if x < 2:
42-
- return False
43-
- for i in range(2, int(math.sqrt(x)) + 1):
44-
- if x % i == 0:
45-
- return False
46-
- return True
47-
@@ ... @@
48-
-@app.route('/prime/<int:n>')
49-
-def nth_prime(n):
50-
- count = 0
51-
- num = 1
52-
- while count < n:
53-
- num += 1
54-
- if is_prime(num):
55-
- count += 1
56-
- return str(num)
57-
+@app.route('/prime/<int:n>')
58-
+def nth_prime(n):
59-
+ count = 0
60-
+ num = 1
61-
+ while count < n:
62-
+ num += 1
63-
+ if sympy.isprime(num):
64-
+ count += 1
65-
+ return str(num)
66-
```
20+
Only output hunks that specify changes with `+` or `-` lines.
21+
22+
Output hunks in whatever order makes the most sense.
23+
Hunks don't need to be in any particular order.
24+
25+
When editing a function, method, loop, etc use a hunk to replace the *entire* code block.
26+
Delete the entire existing version with `-` lines and then add a new, updated version with `+` lines.
27+
This will help you generate correct code and correct diffs.
28+
29+
To move code within a file, use 2 hunks: 1 to delete it from its current location, 1 to insert it in the new location.
30+
31+
To make a new file, show a diff from `--- /dev/null` to `+++ path/to/new/file.ext`.
6732
</editFileInstructions>
6833
]]

0 commit comments

Comments
 (0)