Skip to content

Commit

Permalink
fix: minimize snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
yetone committed Nov 21, 2024
1 parent 9e34f34 commit eb33f16
Showing 1 changed file with 40 additions and 45 deletions.
85 changes: 40 additions & 45 deletions lua/avante/sidebar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -541,58 +541,53 @@ 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 = "minimal", result_type = "indices" }) ---@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 +598,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

0 comments on commit eb33f16

Please sign in to comment.