feat: switch to on_bytes for more precise changes

This commit is contained in:
əlemi 2023-11-30 03:12:42 +01:00
parent 9964c18ced
commit 7a9188f607

View file

@ -232,15 +232,17 @@ vim.api.nvim_create_user_command(
-- hook serverbound callbacks -- hook serverbound callbacks
-- TODO breaks when deleting whole lines at buffer end -- TODO breaks when deleting whole lines at buffer end
vim.api.nvim_buf_attach(buffer, false, { vim.api.nvim_buf_attach(buffer, false, {
on_lines = function (_, buf, tick, firstline, lastline, new_lastline, old_byte_size) on_bytes = function(_, buf, tick, start_row, start_col, start_offset, old_end_row, old_end_col, old_end_byte_len, new_end_row, new_end_col, new_byte_len)
if tick <= codemp_changed_tick then return end if tick <= codemp_changed_tick[buf] then return end
if buffer_mappings[buf] == nil then return true end -- exit worker if buffer_mappings[buf] == nil then return true end -- unregister callback handler
local start = vim.api.nvim_buf_get_offset(buf, firstline) local content = ""
local content = table.concat(vim.api.nvim_buf_get_lines(buf, firstline, new_lastline, false), '\n') if old_end_row < new_end_row and new_byte_len == 1 then
if start == -1 then start = 0 end content = "\n"
if new_lastline < lastline then old_byte_size = old_byte_size + 1 end else
controller:send(start, start + old_byte_size - 1, content) content = table.concat(vim.api.nvim_buf_get_text(buf, start_row, start_col, start_row + new_end_row, start_col + new_byte_len, {}), '\n')
end end
controller:send(start_offset, start_offset + old_end_byte_len, content)
end,
}) })
-- This is an ugly as hell fix: basically we receive all operations real fast at the start -- This is an ugly as hell fix: basically we receive all operations real fast at the start