Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,14 @@ make test

See [CONTRIBUTING.md](/CONTRIBUTING.md) for detailed guidelines.

# Acknowledgments

## diff-match-patch

CopilotChat.nvim includes [diff-match-patch (Lua port)](https://github.com/google/diff-match-patch) for diffing and patching functionality.
Copyright 2018 The diff-match-patch Authors.
Licensed under the Apache License 2.0.

# Contributors

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
Expand Down
14 changes: 3 additions & 11 deletions lua/CopilotChat/config/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,7 @@ return {

local path = block.header.filename
local bufnr = prepare_diff_buffer(path, source)
local new_lines, applied = diff.apply_diff(block, bufnr)
if not applied then
new_lines = utils.split_lines(block.content)
end

local new_lines = diff.apply_diff(block, bufnr)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, new_lines)
local first, last = diff.get_diff_region(block, bufnr)
if first and last then
Expand Down Expand Up @@ -227,15 +223,11 @@ return {

local path = block.header.filename
local bufnr = prepare_diff_buffer(path, source)
local new_lines, applied = diff.apply_diff(block, bufnr)
if not applied then
new_lines = utils.split_lines(block.content)
end
local original_lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local new_lines = diff.apply_diff(block, bufnr)

local opts = {
filetype = vim.bo[bufnr].filetype,
text = applied and table.concat(new_lines, '\n') or table.concat(original_lines, '\n'),
text = table.concat(new_lines, '\n'),
}

opts.on_show = function()
Expand Down
4 changes: 2 additions & 2 deletions lua/CopilotChat/config/prompts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ The user is currently in workspace directory {DIR} (project root). File paths ar
Context is provided to you in several ways:
- Resources: Contextual data shared via "# <uri>" headers and referenced via "##<uri>" links
- Code blocks with file path labels and line numbers (e.g., ```lua path=/file.lua start_line=1 end_line=10```)
Note: Line numbers prefixed to each line are for reference only and should never be included when outputting code
Note: Each line in code block can be prefixed with <line_number>: for your reference only. NEVER include these line numbers in your responses.
- Visual selections: Text selected in visual mode that can be shared as context
- Diffs: Changes shown in unified diff format (+, -, etc.)
- Conversation history
Expand All @@ -41,7 +41,7 @@ If you can infer the project type (languages, frameworks, libraries) from contex
For implementing features, break down the request into concepts and provide a clear solution.
Think creatively to provide complete solutions based on the information available.
Never fabricate or hallucinate file contents you haven't actually seen in the provided context.
When outputting code, never include line number prefixes - they are only for reference when analyzing the provided context.
When outputting code or diffs, NEVER include line number prefixes - they are only for reference when analyzing the provided context.
</instructions>
]],
},
Expand Down
80 changes: 23 additions & 57 deletions lua/CopilotChat/instructions/edit_file_unified.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,33 @@ return [[
<editFileInstructions>
Return edits similar to unified diffs that `diff -U0` would produce.

- Always include the first 2 lines with the file paths (no timestamps).
- Start each hunk of changes with a `@@ ... @@` line.
- Do not include line numbers in the hunk header.
- The user's patch tool needs CORRECT patches that apply cleanly against the current contents of the file.
- Indentation matters in the diffs!
Make sure you include the first 2 lines with the file paths.
Don't include timestamps with the file paths.
Do not use any file path prefixes, just use --- path/to/file and +++ path/to/file.

Context lines:
- For each hunk that contains changes, you MUST always include 2-3 context lines before the change.
- ALWAYS prefix every context line with a single space character.
- Context lines MUST ONLY appear BEFORE changes, NEVER after changes.
- MISSING CONTEXT LINES WILL CAUSE PATCH FAILURES - they are mandatory, not optional.
- MISSING SPACE PREFIXES WILL CAUSE PATCH FAILURES - they are mandatory, not optional.
Start each hunk of changes with a `@@` line.

Change lines:
- Mark all lines to be removed or changed with `-`.
- Mark all new or modified lines with `+`.
- Only output hunks that specify changes with `+` or `-` lines.
The user's patch tool needs CORRECT patches that apply cleanly against the current contents of the file!
Code can start with line number prefixes for reference (e.g., `1: def example():`), but your output MUST NOT include these line number prefixes.
Think carefully and make sure you include and mark all lines that need to be removed or changed as `-` lines.
Make sure you mark all new or modified lines with `+`.
Don't leave out any lines or the diff patch won't apply correctly.

Other instructions:
- Start a new hunk for each section of the file that needs changes.
- 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.
- 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.
- To make a new file, show a diff from `--- /dev/null` to `+++ path/to/new/file.ext`.
Indentation matters in the diffs!

Example:
Start a new hunk for each section of the file that needs changes.

```diff
--- mathweb/flask/app.py
+++ mathweb/flask/app.py
@@ ... @@
-class MathWeb:
+import sympy
+
+class MathWeb:
@@ ... @@
-def is_prime(x):
- if x < 2:
- return False
- for i in range(2, int(math.sqrt(x)) + 1):
- if x % i == 0:
- return False
- return True
@@ ... @@
-@app.route('/prime/<int:n>')
-def nth_prime(n):
- count = 0
- num = 1
- while count < n:
- num += 1
- if is_prime(num):
- count += 1
- return str(num)
+@app.route('/prime/<int:n>')
+def nth_prime(n):
+ count = 0
+ num = 1
+ while count < n:
+ num += 1
+ if sympy.isprime(num):
+ count += 1
+ return str(num)
```
Only output hunks that specify changes with `+` or `-` lines.

Output hunks in whatever order makes the most sense.
Hunks don't need to be in any particular order.

When editing a function, method, loop, etc use a hunk to replace the *entire* code block.
Delete the entire existing version with `-` lines and then add a new, updated version with `+` lines.
This will help you generate correct code and correct diffs.

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.

To make a new file, show a diff from `--- /dev/null` to `+++ path/to/new/file.ext`.
</editFileInstructions>
]]
Loading
Loading