fix: keep correct indentation (#710)
This commit is contained in:
parent
134609a04c
commit
faaa7f223b
@ -415,6 +415,7 @@ local function insert_conflict_contents(bufnr, snippets)
|
|||||||
local start_line, end_line = unpack(snippet.range)
|
local start_line, end_line = unpack(snippet.range)
|
||||||
|
|
||||||
local need_prepend_indentation = false
|
local need_prepend_indentation = false
|
||||||
|
local start_line_indentation = ""
|
||||||
local original_start_line_indentation = Utils.get_indentation(lines[start_line] or "")
|
local original_start_line_indentation = Utils.get_indentation(lines[start_line] or "")
|
||||||
|
|
||||||
local result = {}
|
local result = {}
|
||||||
@ -428,10 +429,15 @@ local function insert_conflict_contents(bufnr, snippets)
|
|||||||
|
|
||||||
for idx, line in ipairs(snippet_lines) do
|
for idx, line in ipairs(snippet_lines) do
|
||||||
if idx == 1 then
|
if idx == 1 then
|
||||||
local indentation = Utils.get_indentation(line)
|
start_line_indentation = Utils.get_indentation(line)
|
||||||
need_prepend_indentation = indentation ~= original_start_line_indentation
|
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
|
end
|
||||||
if need_prepend_indentation then line = original_start_line_indentation .. line end
|
|
||||||
table.insert(result, line)
|
table.insert(result, line)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -445,7 +451,7 @@ end
|
|||||||
---@param codeblocks table<integer, any>
|
---@param codeblocks table<integer, any>
|
||||||
local function is_cursor_in_codeblock(codeblocks)
|
local function is_cursor_in_codeblock(codeblocks)
|
||||||
local cursor_line, _ = Utils.get_cursor_pos()
|
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
|
for _, block in ipairs(codeblocks) do
|
||||||
if cursor_line >= block.start_line and cursor_line <= block.end_line then return block end
|
if cursor_line >= block.start_line and cursor_line <= block.end_line then return block end
|
||||||
|
@ -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.
|
*SEARCH/REPLACE* blocks will replace *all* matching occurrences.
|
||||||
Include enough lines to make the SEARCH blocks uniquely match the lines to change.
|
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.
|
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.
|
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.
|
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 long runs of unchanging lines in *SEARCH/REPLACE* blocks.
|
||||||
*DO NOT* include three backticks: {%raw%}```{%endraw%} in your response!
|
|
||||||
{% if use_xml_format -%}
|
{% if use_xml_format -%}
|
||||||
ONLY change the <code>, DO NOT change the <conext>!
|
ONLY change the <code>, DO NOT change the <conext>!
|
||||||
{% else -%}
|
{% else -%}
|
||||||
|
@ -446,10 +446,16 @@ end
|
|||||||
|
|
||||||
---@param code string
|
---@param code string
|
||||||
---@return 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
|
--- 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)
|
function M.relative_path(absolute)
|
||||||
local relative = fn.fnamemodify(absolute, ":.")
|
local relative = fn.fnamemodify(absolute, ":.")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user