fix(util.format): parse diffs more correctly
This commit is contained in:
+17
-11
@@ -295,11 +295,9 @@ function Util.format(opts)
|
|||||||
|
|
||||||
---@type lsp.TextEdit[]
|
---@type lsp.TextEdit[]
|
||||||
local text_edits = {}
|
local text_edits = {}
|
||||||
local line_offset = (is_visual and opts.only_selection) and (row_start - 1)
|
|
||||||
or 0
|
|
||||||
|
|
||||||
---@diagnostic disable-next-line: param-type-mismatch
|
---@diagnostic disable-next-line: param-type-mismatch
|
||||||
for i, hunk in ipairs(diff) do
|
for _, hunk in ipairs(diff) do
|
||||||
local old_start, old_count, new_start, new_count = unpack(hunk)
|
local old_start, old_count, new_start, new_count = unpack(hunk)
|
||||||
|
|
||||||
local lines = {}
|
local lines = {}
|
||||||
@@ -307,24 +305,32 @@ function Util.format(opts)
|
|||||||
table.insert(lines, new_lines[j])
|
table.insert(lines, new_lines[j])
|
||||||
end
|
end
|
||||||
|
|
||||||
local is_last_hunk = i == #diff
|
local new_text = table.concat(lines, "\n") .. "\n"
|
||||||
local is_at_eof = (line_offset + old_start - 1 + old_count)
|
if new_count == 0 then
|
||||||
>= vim.api.nvim_buf_line_count(opts.buf)
|
new_text = ""
|
||||||
local needs_newline = new_count > 0 and not (is_last_hunk and is_at_eof)
|
end
|
||||||
|
|
||||||
|
local start_line = row_start - 1 + old_start - 1
|
||||||
|
local end_line = row_start - 1 + old_start - 1 + old_count
|
||||||
|
if old_count == 0 then
|
||||||
|
-- Insertion: old_start means "after line N" (where N is 1-indexed),
|
||||||
|
-- which equals the 0-indexed position old_start
|
||||||
|
start_line = start_line + 1
|
||||||
|
end_line = end_line + 1
|
||||||
|
end
|
||||||
|
|
||||||
table.insert(text_edits, {
|
table.insert(text_edits, {
|
||||||
range = {
|
range = {
|
||||||
start = {
|
start = {
|
||||||
line = line_offset + old_start - 1,
|
line = start_line,
|
||||||
character = 0,
|
character = 0,
|
||||||
},
|
},
|
||||||
["end"] = {
|
["end"] = {
|
||||||
line = line_offset + old_start - 1 + old_count,
|
line = end_line,
|
||||||
character = 0,
|
character = 0,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
newText = table.concat(lines, "\n")
|
newText = new_text,
|
||||||
.. (needs_newline and "\n" or ""),
|
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user