From a084b6e1b69b3113116dc2ee8194bf876f85b1fb Mon Sep 17 00:00:00 2001 From: alemi Date: Fri, 17 Nov 2023 06:02:55 +0100 Subject: [PATCH] fix: sleep to avoid bugs... --- Cargo.toml | 2 +- src/codemp.lua | 15 +++++++++++---- src/lib.rs | 2 -- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4a2b82a..bf46e33 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" crate-type = ["cdylib"] [dependencies] -codemp = { git = "ssh://git@github.com/codewithotherpeopleandchangenamelater/codemp.git", rev = "486999bc31b8a925d56ee6d3747b06fcd97b00d5", features = ["global", "sync"] } +codemp = { git = "ssh://git@github.com/codewithotherpeopleandchangenamelater/codemp.git", rev = "c7319b9f68b19b0b34ecf71cc4b00999248bbd14", features = ["global", "sync"] } mlua = { version = "0.9.0", features = ["module", "luajit"] } thiserror = "1.0.47" derive_more = "0.99.17" diff --git a/src/codemp.lua b/src/codemp.lua index bca3b17..35b0a87 100644 --- a/src/codemp.lua +++ b/src/codemp.lua @@ -2,7 +2,7 @@ local codemp = require("libcodemp_nvim") local codemp_changed_tick = 0 -- TODO this doesn't work when events are coalesced -local function register_controller_handler(target, controller, handler) +local function register_controller_handler(target, controller, handler, delay) local async = vim.loop.new_async(function() while true do local event = controller:try_recv() @@ -15,14 +15,15 @@ local function register_controller_handler(target, controller, handler) -- completely useless. We can circumvent this by requiring codemp again in the new -- thread and requesting a new reference to the same controller from che global instance -- NOTE variables prefixed with underscore live in another Lua runtime - vim.loop.new_thread({}, function(_async, _target) + vim.loop.new_thread({}, function(_async, _target, _delay) + if _delay ~= nil then vim.loop.sleep(_delay) end local _codemp = require("libcodemp_nvim") local _controller = _target ~= nil and _codemp.get_buffer(_target) or _codemp.get_cursor() while true do _controller:poll() _async:send() end - end, async, target) + end, async, target, delay) end local function split_without_trim(str, sep) @@ -223,11 +224,17 @@ vim.api.nvim_create_user_command( end }) + -- This is an ugly as hell fix: basically we receive all operations real fast at the start + -- so the buffer changes rapidly and it messes up tracking our delta/diff state and we + -- get borked translated TextChanges (the underlying CRDT is fine) + -- basically delay a bit so that it has time to sync and we can then get "normal slow" changes + -- vim.loop.sleep(200) -- moved inside poller thread to at least not block ui + -- hook clientbound callbacks register_controller_handler(args.args, controller, function(event) codemp_changed_tick = vim.api.nvim_buf_get_changedtick(buffer) + 1 buffer_replace_content(buffer, event.first, event.last, event.content) - end) + end, 200) -- delay by 200 ms as ugly fix print(" ++ joined workspace " .. args.args) end, diff --git a/src/lib.rs b/src/lib.rs index 7592e3b..13f6cf4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -137,7 +137,6 @@ impl LuaUserData for LuaBufferController { CodempTextChange { span: start..end, content: text, - after: "".into(), } ) .map_err(LuaCodempError::from)? @@ -178,7 +177,6 @@ impl LuaUserData for LuaTextChange { Ok(LuaTextChange(CodempTextChange { span: start..end, content: txt, - after: "".into(), })) }); methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| Ok(format!("{:?}", this.0)));