Skip to content

Commit

Permalink
refactor: planning mode user prompt use aider prompt (#671)
Browse files Browse the repository at this point in the history
  • Loading branch information
yetone authored Sep 30, 2024
1 parent 93ab85a commit 0705234
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 127 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,12 @@ We would like to express our heartfelt gratitude to the contributors of the foll
| Nvim Plugin | License | Functionality | Location |
| --- | --- | --- | --- |
| [git-conflict.nvim](https://github.com/akinsho/git-conflict.nvim) | No License | Diff comparison functionality | [lua/avante/diff.lua](https://github.com/yetone/avante.nvim/blob/main/lua/avante/diff.lua) |
| [ChatGPT.nvim](https://github.com/jackMort/ChatGPT.nvim) | Apache 2.0 License | Calculation of tokens count | [avante/utils/tokens.lua](https://github.com/yetone/avante.nvim/blob/main/lua/avante/utils/tokens.lua) |
| [img-clip.nvim](https://github.com/HakonHarnes/img-clip.nvim) | MIT License | Clipboard image support | [avante/clipboard.lua](https://github.com/yetone/avante.nvim/blob/main/lua/avante/clipboard.lua) |
| [copilot.lua](https://github.com/zbirenbaum/copilot.lua) | MIT License | Copilot support | [avante/providers/copilot.lua](https://github.com/yetone/avante.nvim/blob/main/lua/avante/providers/copilot.lua) |
| [ChatGPT.nvim](https://github.com/jackMort/ChatGPT.nvim) | Apache 2.0 License | Calculation of tokens count | [lua/avante/utils/tokens.lua](https://github.com/yetone/avante.nvim/blob/main/lua/avante/utils/tokens.lua) |
| [img-clip.nvim](https://github.com/HakonHarnes/img-clip.nvim) | MIT License | Clipboard image support | [lua/avante/clipboard.lua](https://github.com/yetone/avante.nvim/blob/main/lua/avante/clipboard.lua) |
| [copilot.lua](https://github.com/zbirenbaum/copilot.lua) | MIT License | Copilot support | [lua/avante/providers/copilot.lua](https://github.com/yetone/avante.nvim/blob/main/lua/avante/providers/copilot.lua) |
| [jinja.vim](https://github.com/HiPhish/jinja.vim) | MIT License | Template filetype support | [syntax/jinja.vim](https://github.com/yetone/avante.nvim/blob/main/syntax/jinja.vim) |
| [codecompanion.nvim](https://github.com/olimorris/codecompanion.nvim) | MIT License | Secrets logic support | [avante/providers/init.lua](https://github.com/yetone/avante.nvim/blob/main/lua/avante/providers/init.lua) |
| [codecompanion.nvim](https://github.com/olimorris/codecompanion.nvim) | MIT License | Secrets logic support | [lua/avante/providers/init.lua](https://github.com/yetone/avante.nvim/blob/main/lua/avante/providers/init.lua) |
| [aider](https://github.com/paul-gauthier/aider) | Apache 2.0 License | Planning mode user prompt | [lua/avante/templates/planning.avanterules](https://github.com/yetone/avante.nvim/blob/main/lua/avante/templates/planning.avanterules) |
The high quality and ingenuity of these projects' source code have been immensely beneficial throughout our development process. We extend our sincere thanks and respect to the authors and contributors of these projects. It is the selfless dedication of the open-source community that drives projects like avante.nvim forward.
Expand Down
4 changes: 3 additions & 1 deletion lua/avante/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ M.defaults = {
-- You can use `require('avante.config').override({system_prompt = "MY_SYSTEM_PROMPT"}) conditionally
-- in your own autocmds to do it per directory, or that fit your needs.
system_prompt = [[
You are an excellent programming expert.
Act as an expert software developer.
Always use best practices when coding.
Respect and use existing conventions, libraries, etc that are already present in the code base.
]],
---@type AvanteSupportedProvider
openai = {
Expand Down
8 changes: 7 additions & 1 deletion lua/avante/selection.lua
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,13 @@ function Selection:create_editing_input()
---@type AvanteChunkParser
local on_chunk = function(chunk)
full_response = full_response .. chunk
local response_lines = vim.split(full_response, "\n")
local response_lines_ = vim.split(full_response, "\n")
local response_lines = {}
for i, line in ipairs(response_lines_) do
if not (string.match(line, "^```") and (i == 1 or i == #response_lines_)) then
table.insert(response_lines, line)
end
end
if #response_lines == 1 then
local first_line = response_lines[1]
local first_line_indentation = Utils.get_indentation(first_line)
Expand Down
112 changes: 42 additions & 70 deletions lua/avante/sidebar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -133,51 +133,6 @@ function Sidebar:toggle(opts)
end
end

local function realign_line_numbers(code_lines, snippet)
local snippet_lines = vim.split(snippet.content, "\n")
local snippet_lines_count = #snippet_lines

local start_line = snippet.range[1]

local correct_start
for i = start_line, math.max(1, start_line - snippet_lines_count + 1), -1 do
local matched = true
for j = 1, math.min(snippet_lines_count, start_line - i + 1) do
if code_lines[i + j - 1] ~= snippet_lines[j] then
matched = false
break
end
end
if matched then
correct_start = i
break
end
end

local end_line = snippet.range[2]

local correct_end
for i = snippet_lines_count - 1, 1, -1 do
local matched = true
for j = 1, i do
if code_lines[end_line + j - 1] ~= snippet_lines[snippet_lines_count - j] then
matched = false
break
end
end
if matched then
correct_end = end_line + i
break
end
end

if correct_start then snippet.range[1] = correct_start end

if correct_end then snippet.range[2] = correct_end end

return snippet
end

---@class AvanteReplacementResult
---@field content string
---@field is_searching boolean
Expand Down Expand Up @@ -317,11 +272,9 @@ end
---@field start_line_in_response_buf integer
---@field end_line_in_response_buf integer

---@param code_content string
---@param response_content string
---@return AvanteCodeSnippet[]
local function extract_code_snippets(code_content, response_content)
local code_lines = vim.split(code_content, "\n")
local function extract_code_snippets(response_content)
local snippets = {}
local current_snippet = {}
local in_code_block = false
Expand Down Expand Up @@ -358,7 +311,6 @@ local function extract_code_snippets(code_content, response_content)
start_line_in_response_buf = start_line_in_response_buf,
end_line_in_response_buf = idx,
}
snippet = realign_line_numbers(code_lines, snippet)
table.insert(snippets, snippet)
end
current_snippet = {}
Expand Down Expand Up @@ -448,7 +400,6 @@ local function insert_conflict_contents(bufnr, snippets)
local snippet_lines = vim.split(snippet.content, "\n")

for idx, line in ipairs(snippet_lines) do
line = Utils.trim_line_number(line)
if idx == 1 then
local indentation = Utils.get_indentation(line)
need_prepend_indentation = indentation ~= original_start_line_indentation
Expand Down Expand Up @@ -512,7 +463,7 @@ end
function Sidebar:apply(current_cursor)
local content = table.concat(Utils.get_buf_lines(0, -1, self.code.bufnr), "\n")
local response, response_start_line = self:get_content_between_separators()
local all_snippets = extract_code_snippets(content, response)
local all_snippets = extract_code_snippets(response)
all_snippets = ensure_snippets_no_overlap(content, all_snippets)
local selected_snippets = {}
if current_cursor then
Expand Down Expand Up @@ -951,7 +902,7 @@ function Sidebar:is_focused_on(winid)
end

---@param content string concatenated content of the buffer
---@param opts? {focus?: boolean, stream?: boolean, scroll?: boolean, callback?: fun(): nil} whether to focus the result view
---@param opts? {focus?: boolean, stream?: boolean, scroll?: boolean, backspace?: integer, callback?: fun(): nil} whether to focus the result view
function Sidebar:update_content(content, opts)
if not self.result or not self.result.bufnr then return end
opts = vim.tbl_deep_extend("force", { focus = true, scroll = true, stream = false, callback = nil }, opts or {})
Expand All @@ -974,8 +925,18 @@ function Sidebar:update_content(content, opts)
vim.schedule(function()
if not self.result or not self.result.bufnr or not api.nvim_buf_is_valid(self.result.bufnr) then return end
scroll_to_bottom()
local lines = vim.split(content, "\n")
Utils.unlock_buf(self.result.bufnr)
if opts.backspace ~= nil then
-- Delete the specified number of char from the end of the buffer
-- sends the buffer to the backend
for _ = 1, opts.backspace do
api.nvim_buf_call(
self.result.bufnr,
function() api.nvim_feedkeys(api.nvim_replace_termcodes("<BS>", true, false, true), "n", true) end
)
end
end
local lines = vim.split(content, "\n")
api.nvim_buf_call(self.result.bufnr, function() api.nvim_put(lines, "c", true, true) end)
Utils.lock_buf(self.result.bufnr)
api.nvim_set_option_value("filetype", "Avante", { buf = self.result.bufnr })
Expand Down Expand Up @@ -1197,15 +1158,11 @@ function Sidebar:create_input(opts)
self:update_content(content_prefix .. "**Generating response ...**\n")

local content = table.concat(Utils.get_buf_lines(0, -1, self.code.bufnr), "\n")
local content_with_line_numbers = Utils.prepend_line_number(content)

local filetype = api.nvim_get_option_value("filetype", { buf = self.code.bufnr })

local selected_code_content_with_line_numbers = nil
if self.code.selection ~= nil then
selected_code_content_with_line_numbers =
Utils.prepend_line_number(self.code.selection.content, self.code.selection.range.start.line)
end
local selected_code_content = nil
if self.code.selection ~= nil then selected_code_content = self.code.selection.content end

if request:sub(1, 1) == "/" then
local command, args = request:match("^/(%S+)%s*(.*)")
Expand All @@ -1228,10 +1185,8 @@ function Sidebar:create_input(opts)
Utils.error("Invalid end line number", { once = true, title = "Avante" })
return
end
selected_code_content_with_line_numbers = Utils.prepend_line_number(
table.concat(api.nvim_buf_get_lines(self.code.bufnr, start_line - 1, end_line, false), "\n"),
start_line
)
selected_code_content =
table.concat(api.nvim_buf_get_lines(self.code.bufnr, start_line - 1, end_line, false), "\n")
request = question
end)
else
Expand All @@ -1244,20 +1199,36 @@ function Sidebar:create_input(opts)
end
end

local full_response = ""
local original_response = ""
local transformed_response = ""
local displayed_response = ""

local is_first_chunk = true

local prev_is_searching = false

---@type AvanteChunkParser
local on_chunk = function(chunk)
full_response = full_response .. chunk
original_response = original_response .. chunk
local transformed = transform_result_content(content, transformed_response .. chunk, filetype)
transformed_response = transformed.content
prev_is_searching = transformed.is_searching
local cur_displayed_response = generate_display_content(transformed)
if is_first_chunk then
is_first_chunk = false
self:update_content(content_prefix .. chunk, { stream = false, scroll = true })
return
end
self:update_content(chunk, { stream = true, scroll = true })
vim.schedule(function() vim.cmd("redraw") end)
if cur_displayed_response ~= displayed_response then
local backspace = nil
if prev_is_searching and not transformed.is_searching then backspace = #searching_hints[1] end
displayed_response = cur_displayed_response
self:update_content(
content_prefix .. displayed_response,
{ stream = false, scroll = true, backspace = backspace }
)
vim.schedule(function() vim.cmd("redraw") end)
end
end

---@type AvanteCompleteParser
Expand Down Expand Up @@ -1286,7 +1257,8 @@ function Sidebar:create_input(opts)
provider = Config.provider,
model = model,
request = request,
response = full_response,
response = displayed_response,
original_response = original_response,
})
Path.history.save(self.code.bufnr, chat_history)
end
Expand All @@ -1302,9 +1274,9 @@ function Sidebar:create_input(opts)
bufnr = self.code.bufnr,
ask = opts.ask,
project_context = vim.json.encode(project_context),
file_content = content_with_line_numbers,
file_content = content,
code_lang = filetype,
selected_code = selected_code_content_with_line_numbers,
selected_code = selected_code_content,
instructions = request,
mode = "planning",
on_chunk = on_chunk,
Expand Down
16 changes: 8 additions & 8 deletions lua/avante/templates/editing.avanterules
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
{% block user_prompt %}
Your task is to modify the provided code according to the user's request. Follow these instructions precisely:

1. Return ONLY the complete modified code.
1. Return *ONLY* the complete modified code.

2. Do not include any explanations, comments, or line numbers in your response.
2. *DO NOT* include three backticks: {%raw%}```{%endraw%} in your suggestion! Treat the suggested code AS IS.

3. Ensure the returned code is complete and can be directly used as a replacement for the original code.
3. *DO NOT* include any explanations, comments, or line numbers in your response.

4. Preserve the original structure, indentation, and formatting of the code as much as possible.
4. Ensure the returned code is complete and can be directly used as a replacement for the original code.

5. Do not omit any parts of the code, even if they are unchanged.
5. Preserve the original structure, indentation, and formatting of the code as much as possible.

6. Maintain the SAME indentation in the returned code as in the source code
6. *DO NOT* omit any parts of the code, even if they are unchanged.

7. DO NOT include three backticks: {%raw%}```{%endraw%} in your suggestion. Treat the suggested code AS IS.
7. Maintain the *SAME INDENTATION* in the returned code as in the source code

8. Only return the new code snippets to be updated, DO NOT return the entire file content.
8. *ONLY* return the new code snippets to be updated, *DO NOT* return the entire file content.

Remember that Your response SHOULD CONTAIN ONLY THE MODIFIED CODE to be used as DIRECT REPLACEMENT to the original file.
{% endblock %}
Loading

0 comments on commit 0705234

Please sign in to comment.