From faaa7f223b15d501895e8692e7de477459885fc6 Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 11 Oct 2024 21:07:55 +0800 Subject: [PATCH] fix: keep correct indentation (#710) --- lua/avante/sidebar.lua | 14 ++++++++++---- lua/avante/templates/planning.avanterules | 2 +- lua/avante/utils/init.lua | 10 ++++++++-- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lua/avante/sidebar.lua b/lua/avante/sidebar.lua index b65ad09..fb6c726 100644 --- a/lua/avante/sidebar.lua +++ b/lua/avante/sidebar.lua @@ -415,6 +415,7 @@ local function insert_conflict_contents(bufnr, snippets) local start_line, end_line = unpack(snippet.range) local need_prepend_indentation = false + local start_line_indentation = "" local original_start_line_indentation = Utils.get_indentation(lines[start_line] or "") local result = {} @@ -428,10 +429,15 @@ local function insert_conflict_contents(bufnr, snippets) for idx, line in ipairs(snippet_lines) do if idx == 1 then - local indentation = Utils.get_indentation(line) - need_prepend_indentation = indentation ~= original_start_line_indentation + start_line_indentation = Utils.get_indentation(line) + need_prepend_indentation = start_line_indentation ~= original_start_line_indentation + end + if need_prepend_indentation then + if line:sub(1, #start_line_indentation) == start_line_indentation then + line = line:sub(#start_line_indentation + 1) + end + line = original_start_line_indentation .. line end - if need_prepend_indentation then line = original_start_line_indentation .. line end table.insert(result, line) end @@ -445,7 +451,7 @@ end ---@param codeblocks table local function is_cursor_in_codeblock(codeblocks) local cursor_line, _ = Utils.get_cursor_pos() - cursor_line = cursor_line - 1 -- 转换为 0-indexed 行号 + cursor_line = cursor_line - 1 -- transform to 0-indexed line number for _, block in ipairs(codeblocks) do if cursor_line >= block.start_line and cursor_line <= block.end_line then return block end diff --git a/lua/avante/templates/planning.avanterules b/lua/avante/templates/planning.avanterules index 8699290..821dab7 100644 --- a/lua/avante/templates/planning.avanterules +++ b/lua/avante/templates/planning.avanterules @@ -124,11 +124,11 @@ If the file contains code or other data wrapped/escaped in json/xml/quotes or ot *SEARCH/REPLACE* blocks will replace *all* matching occurrences. Include enough lines to make the SEARCH blocks uniquely match the lines to change. +*DO NOT* include three backticks: {%raw%}```{%endraw%} in your response! Keep *SEARCH/REPLACE* blocks concise. Break large *SEARCH/REPLACE* blocks into a series of smaller blocks that each change a small portion of the file. Include just the changing lines, and a few surrounding lines if needed for uniqueness. Do not include long runs of unchanging lines in *SEARCH/REPLACE* blocks. -*DO NOT* include three backticks: {%raw%}```{%endraw%} in your response! {% if use_xml_format -%} ONLY change the , DO NOT change the ! {% else -%} diff --git a/lua/avante/utils/init.lua b/lua/avante/utils/init.lua index d7a7f73..417a6ed 100644 --- a/lua/avante/utils/init.lua +++ b/lua/avante/utils/init.lua @@ -446,10 +446,16 @@ end ---@param code string ---@return string -function M.get_indentation(code) return code:match("^%s*") or "" end +function M.get_indentation(code) + if not code then return "" end + return code:match("^%s*") or "" +end --- remove indentation from code: spaces or tabs -function M.remove_indentation(code) return code:gsub("^%s*", "") end +function M.remove_indentation(code) + if not code then return code end + return code:gsub("^%s*", "") +end function M.relative_path(absolute) local relative = fn.fnamemodify(absolute, ":.")