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

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
fix: minimize snippets
  • Loading branch information
yetone committed Nov 21, 2024
commit ea1363cf7355c5792af17451cd504c122f1b0a85
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,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,
minimize_diff = true, -- Whether to remove unchanged lines when applying a code block
},
mappings = {
--- @class AvanteConflictMappings
Expand Down
4 changes: 2 additions & 2 deletions lua/avante/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,14 @@ M.defaults = {
--- 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
---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,
minimize_diff = true,
},
history = {
max_tokens = 4096,
Expand Down
89 changes: 44 additions & 45 deletions lua/avante/sidebar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -541,58 +541,57 @@ local function parse_codeblocks(buf)
return codeblocks
end

---@param lines string[]
---@param original_lines string[]
---@param snippet AvanteCodeSnippet
---@return AvanteCodeSnippet?
local function minimize_snippet(lines, snippet)
---@return AvanteCodeSnippet[]
local function minimize_snippet(original_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
local original_snippet_lines = vim.list_slice(original_lines, start_line, end_line)
local original_snippet_content = table.concat(original_snippet_lines, "\n")
local snippet_content = snippet.content
local snippet_lines = vim.split(snippet_content, "\n")
---@diagnostic disable-next-line: missing-fields, assign-type-mismatch
local patch = vim.diff(
original_snippet_content,
snippet_content,
{ algorithm = "histogram", result_type = "indices", ctxlen = vim.o.scrolloff }
) ---@type integer[][]
---@type AvanteCodeSnippet[]
local new_snippets = {}
for _, hunk in ipairs(patch) do
local start_a, count_a, start_b, count_b = unpack(hunk)
---@type AvanteCodeSnippet
local new_snippet = {
range = { start_line + start_a - 1, start_line + start_a + count_a - 2 },
content = table.concat(vim.list_slice(snippet_lines, start_b, start_b + count_b - 1), "\n"),
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,
filepath = snippet.filepath,
}
table.insert(new_snippets, new_snippet)
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,
}
return new_snippets
end

---@param content string
---@param snippets AvanteCodeSnippet[]
---@return AvanteCodeSnippet[]
local function minimize_snippets(content, snippets)
local original_lines = vim.split(content, "\n")
---@param snippets_map table<string, AvanteCodeSnippet[]>
---@return table<string, AvanteCodeSnippet[]>
function Sidebar:minimize_snippets(snippets_map)
local original_lines = api.nvim_buf_get_lines(self.code.bufnr, 0, -1, false)
local results = {}

for _, snippet in ipairs(snippets) do
local result = minimize_snippet(original_lines, snippet)
if result ~= nil then table.insert(results, result) end
for filepath, snippets in pairs(snippets_map) do
for _, snippet in ipairs(snippets) do
local new_snippets = minimize_snippet(original_lines, snippet)
if new_snippets then
results[filepath] = results[filepath] or {}
for _, new_snippet in ipairs(new_snippets) do
table.insert(results[filepath], new_snippet)
end
end
end
end

return results
Expand All @@ -603,7 +602,7 @@ function Sidebar:apply(current_cursor)
local response, response_start_line = self:get_content_between_separators()
local all_snippets_map = extract_code_snippets_map(response)
all_snippets_map = ensure_snippets_no_overlap(all_snippets_map)
if Config.options.behaviour.minimize_diff then all_snippets_map = minimize_snippets(content, all_snippets_map) end
if Config.options.behaviour.minimize_diff then all_snippets_map = self:minimize_snippets(all_snippets_map) end
local selected_snippets_map = {}
if current_cursor then
if self.result and self.result.winid then
Expand Down