mirror of
https://github.com/hexedtech/codemp-nvim.git
synced 2024-11-22 15:34:53 +01:00
fix: proper text events handling
this works properly but only now that codemp lib sends whole buffer as text change every time
This commit is contained in:
parent
b59086bda6
commit
f5a2412113
2 changed files with 16 additions and 27 deletions
|
@ -191,42 +191,31 @@ vim.api.nvim_create_user_command(
|
||||||
buffer_mappings[buffer] = args.args
|
buffer_mappings[buffer] = args.args
|
||||||
buffer_mappings_reverse[args.args] = buffer
|
buffer_mappings_reverse[args.args] = buffer
|
||||||
|
|
||||||
-- buffer_set_content(buffer, controller.content)
|
|
||||||
|
|
||||||
-- hook serverbound callbacks
|
-- hook serverbound callbacks
|
||||||
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_lines = function (_, buf, tick, firstline, lastline, new_lastline, old_byte_size)
|
||||||
if tick <= codemp_changed_tick then return end
|
if tick <= codemp_changed_tick then return end
|
||||||
local content = buffer_get_content(buf)
|
local start = vim.api.nvim_buf_get_offset(buf, firstline)
|
||||||
controller:send(0, #content - 1, content)
|
local content = table.concat(vim.api.nvim_buf_get_lines(buf, firstline, new_lastline, false), '\n')
|
||||||
-- print(string.format(">[%s] %s:%s|%s (%s)", tick, firstline, lastline, new_lastline, old_byte_size))
|
if new_lastline < lastline then old_byte_size = old_byte_size + 1 end
|
||||||
-- local start_index = firstline == 0 and 0 or vim.fn.line2byte(firstline + 1) - 1
|
controller:send(start, start + old_byte_size - 1, content)
|
||||||
-- local text = table.concat(
|
|
||||||
-- vim.api.nvim_buf_get_lines(buf, firstline, new_lastline, true),
|
|
||||||
-- "\n"
|
|
||||||
-- )
|
|
||||||
-- -- if lastline ~= new_lastline then
|
|
||||||
-- -- text = text .. "\n"
|
|
||||||
-- -- end
|
|
||||||
-- print(string.format(">delta [%d,%s,%d]", start_index, text, start_index + old_byte_size - 1))
|
|
||||||
-- controller:delta(start_index, text, start_index + old_byte_size - 1)
|
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
|
|
||||||
-- hook clientbound callbacks
|
-- hook clientbound callbacks
|
||||||
register_controller_handler(args.args, controller, function(event)
|
register_controller_handler(args.args, controller, function(event)
|
||||||
codemp_changed_tick = vim.api.nvim_buf_get_changedtick(buffer) + 1
|
codemp_changed_tick = vim.api.nvim_buf_get_changedtick(buffer) + 1
|
||||||
-- local start = event.start
|
buffer_set_content(buffer, event.content)
|
||||||
-- local finish = event.finish
|
-- local start_row = vim.api.nvim_buf_get_offset(buffer, event.first)
|
||||||
-- print(string.format(
|
-- local end_row = vim.api.nvim_buf_get_offset(buffer, event.last - 1)
|
||||||
-- "buf_set_text(%s,%s, %s,%s, '%s')",
|
|
||||||
-- start.row, start.col, finish.row, finish.col, vim.inspect(split_without_trim(event.content, "\n"))
|
|
||||||
-- ))
|
|
||||||
-- vim.api.nvim_buf_set_text(
|
-- vim.api.nvim_buf_set_text(
|
||||||
-- buffer, start.row, start.col, finish.row, finish.col,
|
-- buffer,
|
||||||
-- split_without_trim(event.content, "\n")
|
-- start_row,
|
||||||
|
-- event.first - start_row,
|
||||||
|
-- end_row,
|
||||||
|
-- event.last - end_row,
|
||||||
|
-- vim.fn.split(event.content, '\n', true)
|
||||||
-- )
|
-- )
|
||||||
buffer_set_content(buffer, event)
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
print(" ++ joined workspace " .. args.args)
|
print(" ++ joined workspace " .. args.args)
|
||||||
|
|
|
@ -145,7 +145,7 @@ impl LuaUserData for LuaBufferController {
|
||||||
});
|
});
|
||||||
methods.add_method("try_recv", |_, this, ()| {
|
methods.add_method("try_recv", |_, this, ()| {
|
||||||
match this.0.try_recv() .map_err(LuaCodempError::from)? {
|
match this.0.try_recv() .map_err(LuaCodempError::from)? {
|
||||||
Some(x) => Ok(Some(x.content)),
|
Some(x) => Ok(Some(LuaTextChange(x))),
|
||||||
None => Ok(None),
|
None => Ok(None),
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -169,8 +169,8 @@ struct LuaTextChange(CodempTextChange);
|
||||||
impl LuaUserData for LuaTextChange {
|
impl LuaUserData for LuaTextChange {
|
||||||
fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
|
fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
|
||||||
fields.add_field_method_get("content", |_, this| Ok(this.0.content.clone()));
|
fields.add_field_method_get("content", |_, this| Ok(this.0.content.clone()));
|
||||||
fields.add_field_method_get("start", |_, this| Ok(this.0.span.start));
|
fields.add_field_method_get("first", |_, this| Ok(this.0.span.start));
|
||||||
fields.add_field_method_get("finish", |_, this| Ok(this.0.span.end));
|
fields.add_field_method_get("last", |_, this| Ok(this.0.span.end));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
|
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||||
|
|
Loading…
Reference in a new issue