Skip to content

Commit

Permalink
fix: keep correct indentation (yetone#710)
Browse files Browse the repository at this point in the history
  • Loading branch information
yetone authored Oct 11, 2024
1 parent 134609a commit faaa7f2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
14 changes: 10 additions & 4 deletions lua/avante/sidebar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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

Expand All @@ -445,7 +451,7 @@ end
---@param codeblocks table<integer, any>
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
Expand Down
2 changes: 1 addition & 1 deletion lua/avante/templates/planning.avanterules
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>, DO NOT change the <conext>!
{% else -%}
Expand Down
10 changes: 8 additions & 2 deletions lua/avante/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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, ":.")
Expand Down

0 comments on commit faaa7f2

Please sign in to comment.