fix: improved buffer.set_content

still bugs out sometimes?? but mostly works. messed up with autocomplete
tho
This commit is contained in:
əlemi 2024-08-14 17:37:49 +02:00
parent 3287111131
commit 82dd97a346
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -57,15 +57,22 @@ local function buffer_set_content(buf, content, first, last)
local lines = split_without_trim(content, "\n") local lines = split_without_trim(content, "\n")
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines) vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
else else
-- TODO there is no api equivalent of byte2line afaik! local first_row, first_col, last_row, last_col
-- this is theoretically a big deal because we can only vim.api.nvim_buf_call(buf, function()
-- operate on current buffer, but in practice we may only first_row = vim.fn.byte2line(first + 1) - 1
-- really change the currently active buffer so this if first_row == -2 then
-- may not matter first_row = vim.fn.line('$') - 1
local first_row = vim.fn.byte2line(first + 1) - 1 end
local first_col = first - vim.api.nvim_buf_get_offset(buf, first_row) first_col = first - (vim.fn.line2byte(first_row + 1) - 1)
local last_row = vim.fn.byte2line(last + 1) - 1 last_row = vim.fn.byte2line(last + 1) - 1
local last_col = last - vim.api.nvim_buf_get_offset(buf, last_row) if last_row == -2 then
local sp = vim.split(content, "\n", {trimempty=false})
last_row = first_row + (#sp - 1)
last_col = string.len(sp[#sp])
else
last_col = last - (vim.fn.line2byte(last_row + 1) - 1)
end
end)
vim.api.nvim_buf_set_text( vim.api.nvim_buf_set_text(
buf, first_row, first_col, last_row, last_col, buf, first_row, first_col, last_row, last_col,
split_without_trim(content, "\n") split_without_trim(content, "\n")