diff --git a/src/codemp.lua b/src/codemp.lua index 989a30a..aa0f7e7 100644 --- a/src/codemp.lua +++ b/src/codemp.lua @@ -1,6 +1,6 @@ local codemp = require("libcodemp_nvim") -local codemp_changed_tick = nil -- TODO this doesn't work when events are coalesced +local codemp_changed_tick = 0 -- TODO this doesn't work when events are coalesced local function register_controller_handler(target, controller, handler) local async = vim.loop.new_async(function() @@ -191,11 +191,12 @@ vim.api.nvim_create_user_command( buffer_mappings[buffer] = args.args buffer_mappings_reverse[args.args] = buffer - buffer_set_content(buffer, controller.content) + -- buffer_set_content(buffer, controller.content) -- hook serverbound callbacks vim.api.nvim_buf_attach(buffer, false, { on_lines = function (_, buf, tick, firstline, lastline, new_lastline, old_byte_size) + if tick <= codemp_changed_tick then return end local content = buffer_get_content(buf) controller:send(0, #content - 1, content) -- print(string.format(">[%s] %s:%s|%s (%s)", tick, firstline, lastline, new_lastline, old_byte_size)) diff --git a/src/lib.rs b/src/lib.rs index 9aae7e7..5e03494 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -145,7 +145,7 @@ impl LuaUserData for LuaBufferController { }); methods.add_method("try_recv", |_, this, ()| { match this.0.try_recv() .map_err(LuaCodempError::from)? { - Some(x) => Ok(Some(x)), + Some(x) => Ok(Some(x.content)), None => Ok(None), } }); @@ -157,7 +157,10 @@ impl LuaUserData for LuaBufferController { } fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) { - fields.add_field_method_get("content", |_, this| Ok(this.0.try_recv().unwrap().unwrap())); + fields.add_field_method_get("content", |_, this| Ok( + this.0.try_recv().map(|x| x.map(|y| y.content)) + .map_err(LuaCodempError::from)? + )); } }