fix: wrap process_changes fn to provide controller

This commit is contained in:
əlemi 2024-11-17 13:42:17 +01:00
parent c1f5cb2d5b
commit 3f098185ca
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -24,49 +24,51 @@ local function detach(name)
require('codemp.window').update() require('codemp.window').update()
end end
local function process_change(_, 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) local function process_change_wrap(controller)
if tick == ticks[buf] then return end return 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 id_buffer_map[buf] == nil then return true end -- unregister callback handler if tick == ticks[buf] then return end
if CODEMP.config.debug then print(string.format( if id_buffer_map[buf] == nil then return true end -- unregister callback handler
"start(row:%s, col:%s) offset:%s end(row:%s, col:%s new(row:%s, col:%s)) len(old:%s, new:%s)", if CODEMP.config.debug then print(string.format(
start_row, start_col, start_offset, old_end_row, old_end_col, new_end_row, new_end_col, old_end_byte_len, new_byte_len "start(row:%s, col:%s) offset:%s end(row:%s, col:%s new(row:%s, col:%s)) len(old:%s, new:%s)",
)) end start_row, start_col, start_offset, old_end_row, old_end_col, new_end_row, new_end_col, old_end_byte_len, new_byte_len
local end_offset = start_offset + old_end_byte_len )) end
local change_content = "" local end_offset = start_offset + old_end_byte_len
local len = utils.buffer.len(buf) local change_content = ""
if start_offset + new_byte_len + 1 > len then local len = utils.buffer.len(buf)
-- i dont know why but we may go out of bounds when doing 'dd' at the end of buffer?? if start_offset + new_byte_len + 1 > len then
local delta = (start_offset + new_byte_len + 1) - len -- i dont know why but we may go out of bounds when doing 'dd' at the end of buffer??
if CODEMP.config.debug then print("/!\\ bytes out of bounds by " .. delta .. ", adjusting") end local delta = (start_offset + new_byte_len + 1) - len
end_offset = end_offset - delta if CODEMP.config.debug then print("/!\\ bytes out of bounds by " .. delta .. ", adjusting") end
start_offset = start_offset - delta end_offset = end_offset - delta
start_offset = start_offset - delta
end
if new_byte_len > 0 then
local actual_end_col = new_end_col
if new_end_row == 0 then actual_end_col = new_end_col + start_col end
local actual_end_row = start_row + new_end_row
-- -- when bulk inserting at the end we go out of bounds, so we probably need to clamp?
-- -- issue: row=x+1 col=0 and row=x col=0 may be very far apart! we need to get last col of row x, ughh..
-- -- also, this doesn't work so it will stay commented out for a while longer
-- if new_end_row ~= old_end_row and new_end_col == 0 then
-- -- we may be dealing with the last line of the buffer, get_text could error because out-of-bounds
-- local row_count = vim.api.nvim_buf_line_count(buf)
-- if actual_end_row + 1 > row_count then
-- local delta = (actual_end_row + 1) - row_count
-- if CODEMP.config.debug then print("/!\\ row out of bounds by " .. delta .. ", adjusting") end
-- actual_end_row = actual_end_row - delta
-- actual_end_col = len - vim.api.nvim_buf_get_offset(buf, row_count)
-- end
-- end
local lines = vim.api.nvim_buf_get_text(buf, start_row, start_col, actual_end_row, actual_end_col, {})
change_content = table.concat(lines, '\n')
end
if CODEMP.config.debug then
print(string.format("sending: %s..%s '%s'", start_offset, start_offset + old_end_byte_len, change_content))
end
controller:send({
start_idx = start_offset, end_idx = end_offset, content = change_content
})
end end
if new_byte_len > 0 then
local actual_end_col = new_end_col
if new_end_row == 0 then actual_end_col = new_end_col + start_col end
local actual_end_row = start_row + new_end_row
-- -- when bulk inserting at the end we go out of bounds, so we probably need to clamp?
-- -- issue: row=x+1 col=0 and row=x col=0 may be very far apart! we need to get last col of row x, ughh..
-- -- also, this doesn't work so it will stay commented out for a while longer
-- if new_end_row ~= old_end_row and new_end_col == 0 then
-- -- we may be dealing with the last line of the buffer, get_text could error because out-of-bounds
-- local row_count = vim.api.nvim_buf_line_count(buf)
-- if actual_end_row + 1 > row_count then
-- local delta = (actual_end_row + 1) - row_count
-- if CODEMP.config.debug then print("/!\\ row out of bounds by " .. delta .. ", adjusting") end
-- actual_end_row = actual_end_row - delta
-- actual_end_col = len - vim.api.nvim_buf_get_offset(buf, row_count)
-- end
-- end
local lines = vim.api.nvim_buf_get_text(buf, start_row, start_col, actual_end_row, actual_end_col, {})
change_content = table.concat(lines, '\n')
end
if CODEMP.config.debug then
print(string.format("sending: %s..%s '%s'", start_offset, start_offset + old_end_byte_len, change_content))
end
controller:send({
start_idx = start_offset, end_idx = end_offset, content = change_content
})
end end
---@class AttachOptions ---@class AttachOptions
@ -127,7 +129,7 @@ local function attach(name, opts)
-- 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, { on_bytes = process_change }) vim.api.nvim_buf_attach(buffer, false, { on_bytes = process_change_wrap(controller) })
local lock = false local lock = false
local async = vim.loop.new_async(vim.schedule_wrap(function () local async = vim.loop.new_async(vim.schedule_wrap(function ()