feat(lsp.util): remove unneeded table

This commit is contained in:
Lewis Russell 2024-10-16 13:32:12 +01:00
parent acbc6a7f91
commit 8ad000ef7c

View File

@ -428,47 +428,45 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding)
text_edit.newText, _ = string.gsub(text_edit.newText, '\r\n?', '\n') text_edit.newText, _ = string.gsub(text_edit.newText, '\r\n?', '\n')
-- Convert from LSP style ranges to Neovim style ranges. -- Convert from LSP style ranges to Neovim style ranges.
local e = { local start_row = text_edit.range.start.line
start_row = text_edit.range.start.line, local start_col = get_line_byte_from_position(bufnr, text_edit.range.start, offset_encoding)
start_col = get_line_byte_from_position(bufnr, text_edit.range.start, offset_encoding), local end_row = text_edit.range['end'].line
end_row = text_edit.range['end'].line, local end_col = get_line_byte_from_position(bufnr, text_edit.range['end'], offset_encoding)
end_col = get_line_byte_from_position(bufnr, text_edit.range['end'], offset_encoding), local text = split(text_edit.newText, '\n', { plain = true })
text = split(text_edit.newText, '\n', { plain = true }),
}
local max = api.nvim_buf_line_count(bufnr) local max = api.nvim_buf_line_count(bufnr)
-- If the whole edit is after the lines in the buffer we can simply add the new text to the end -- If the whole edit is after the lines in the buffer we can simply add the new text to the end
-- of the buffer. -- of the buffer.
if max <= e.start_row then if max <= start_row then
api.nvim_buf_set_lines(bufnr, max, max, false, e.text) api.nvim_buf_set_lines(bufnr, max, max, false, text)
else else
local last_line_len = #(get_line(bufnr, math.min(e.end_row, max - 1)) or '') local last_line_len = #(get_line(bufnr, math.min(end_row, max - 1)) or '')
-- Some LSP servers may return +1 range of the buffer content but nvim_buf_set_text can't -- Some LSP servers may return +1 range of the buffer content but nvim_buf_set_text can't
-- accept it so we should fix it here. -- accept it so we should fix it here.
if max <= e.end_row then if max <= end_row then
e.end_row = max - 1 end_row = max - 1
e.end_col = last_line_len end_col = last_line_len
has_eol_text_edit = true has_eol_text_edit = true
else else
-- If the replacement is over the end of a line (i.e. e.end_col is equal to the line length and the -- If the replacement is over the end of a line (i.e. end_col is equal to the line length and the
-- replacement text ends with a newline We can likely assume that the replacement is assumed -- replacement text ends with a newline We can likely assume that the replacement is assumed
-- to be meant to replace the newline with another newline and we need to make sure this -- to be meant to replace the newline with another newline and we need to make sure this
-- doesn't add an extra empty line. E.g. when the last line to be replaced contains a '\r' -- doesn't add an extra empty line. E.g. when the last line to be replaced contains a '\r'
-- in the file some servers (clangd on windows) will include that character in the line -- in the file some servers (clangd on windows) will include that character in the line
-- while nvim_buf_set_text doesn't count it as part of the line. -- while nvim_buf_set_text doesn't count it as part of the line.
if if
e.end_col >= last_line_len end_col >= last_line_len
and text_edit.range['end'].character > e.end_col and text_edit.range['end'].character > end_col
and #text_edit.newText > 0 and #text_edit.newText > 0
and string.sub(text_edit.newText, -1) == '\n' and string.sub(text_edit.newText, -1) == '\n'
then then
table.remove(e.text, #e.text) table.remove(text, #text)
end end
end end
-- Make sure we don't go out of bounds for e.end_col -- Make sure we don't go out of bounds for end_col
e.end_col = math.min(last_line_len, e.end_col) end_col = math.min(last_line_len, end_col)
api.nvim_buf_set_text(bufnr, e.start_row, e.start_col, e.end_row, e.end_col, e.text) api.nvim_buf_set_text(bufnr, start_row, start_col, end_row, end_col, text)
end end
end end