Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: minimal diff #583

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ _See [config.lua#L9](./lua/avante/config.lua) for the full config_
auto_set_keymaps = true,
auto_apply_diff_after_generation = false,
support_paste_from_clipboard = false,
minimize_diff = false,
yetone marked this conversation as resolved.
Show resolved Hide resolved
},
mappings = {
--- @class AvanteConflictMappings
Expand Down
2 changes: 2 additions & 0 deletions lua/avante/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@ You are an excellent programming expert.
--- Note that avante will safely set these keymap. See https://github.com/yetone/avante.nvim/wiki#keymaps-and-api-i-guess for more details.
---3. auto_set_highlight_group : Whether to automatically set the highlight group for the current line. Default to true.
---4. support_paste_from_clipboard : Whether to support pasting image from clipboard. This will be determined automatically based whether img-clip is available or not.
---5. minimize_diff : Whether to remove unchanged lines when applying a code block
behaviour = {
auto_suggestions = false, -- Experimental stage
auto_set_highlight_group = true,
auto_set_keymaps = true,
auto_apply_diff_after_generation = false,
support_paste_from_clipboard = false,
minimize_diff = false,
will-lynas marked this conversation as resolved.
Show resolved Hide resolved
},
history = {
storage_path = vim.fn.stdpath("state") .. "/avante",
Expand Down
58 changes: 58 additions & 0 deletions lua/avante/sidebar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -496,12 +496,70 @@ local function parse_codeblocks(buf)
return codeblocks
end

---@param lines string[]
---@param snippet AvanteCodeSnippet
---@return AvanteCodeSnippet?
local function minimize_snippet(lines, snippet)
local start_line = snippet.range[1]
local end_line = snippet.range[2]
local snippet_lines = vim.split(snippet.content, "\n")

local start_offset = 0
while
start_offset < #snippet_lines
and start_line + start_offset <= end_line
and snippet_lines[start_offset + 1] == lines[start_line + start_offset]
do
start_offset = start_offset + 1
end

local end_offset = 0
while
end_offset < (#snippet_lines - start_offset)
and (end_line - end_offset) >= start_line
and snippet_lines[#snippet_lines - end_offset] == lines[end_line - end_offset]
do
end_offset = end_offset + 1
end

local minimized_content = table.concat(snippet_lines, "\n", start_offset + 1, #snippet_lines - end_offset)
local minimized_start = start_line + start_offset
local minimized_end = end_line - end_offset
if minimized_end < minimized_start then return nil end
local minimized_range = { minimized_start, minimized_end }

return {
range = minimized_range,
content = minimized_content,
lang = snippet.lang,
explanation = snippet.explanation,
start_line_in_response_buf = snippet.start_line_in_response_buf,
end_line_in_response_buf = snippet.end_line_in_response_buf,
}
end

---@param content string
---@param snippets AvanteCodeSnippet[]
---@return AvanteCodeSnippet[]
local function minimize_snippets(content, snippets)
local original_lines = vim.split(content, "\n")
local results = {}

for _, snippet in ipairs(snippets) do
local result = minimize_snippet(original_lines, snippet)
if result ~= nil then table.insert(results, result) end
end

return results
end

---@param current_cursor boolean
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)
all_snippets = ensure_snippets_no_overlap(content, all_snippets)
if Config.options.behaviour.minimize_diff then all_snippets = minimize_snippets(content, all_snippets) end
local selected_snippets = {}
if current_cursor then
if self.result and self.result.winid then
Expand Down