fix: sleep to avoid bugs...

This commit is contained in:
əlemi 2023-11-17 06:02:55 +01:00
parent 67b9389c5b
commit a084b6e1b6
3 changed files with 12 additions and 7 deletions

View file

@ -7,7 +7,7 @@ edition = "2021"
crate-type = ["cdylib"] crate-type = ["cdylib"]
[dependencies] [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"] } mlua = { version = "0.9.0", features = ["module", "luajit"] }
thiserror = "1.0.47" thiserror = "1.0.47"
derive_more = "0.99.17" derive_more = "0.99.17"

View file

@ -2,7 +2,7 @@ local codemp = require("libcodemp_nvim")
local codemp_changed_tick = 0 -- 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 function register_controller_handler(target, controller, handler, delay)
local async = vim.loop.new_async(function() local async = vim.loop.new_async(function()
while true do while true do
local event = controller:try_recv() 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 -- 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 -- thread and requesting a new reference to the same controller from che global instance
-- NOTE variables prefixed with underscore live in another Lua runtime -- 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 _codemp = require("libcodemp_nvim")
local _controller = _target ~= nil and _codemp.get_buffer(_target) or _codemp.get_cursor() local _controller = _target ~= nil and _codemp.get_buffer(_target) or _codemp.get_cursor()
while true do while true do
_controller:poll() _controller:poll()
_async:send() _async:send()
end end
end, async, target) end, async, target, delay)
end end
local function split_without_trim(str, sep) local function split_without_trim(str, sep)
@ -223,11 +224,17 @@ vim.api.nvim_create_user_command(
end 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 -- 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
buffer_replace_content(buffer, event.first, event.last, event.content) 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) print(" ++ joined workspace " .. args.args)
end, end,

View file

@ -137,7 +137,6 @@ impl LuaUserData for LuaBufferController {
CodempTextChange { CodempTextChange {
span: start..end, span: start..end,
content: text, content: text,
after: "".into(),
} }
) )
.map_err(LuaCodempError::from)? .map_err(LuaCodempError::from)?
@ -178,7 +177,6 @@ impl LuaUserData for LuaTextChange {
Ok(LuaTextChange(CodempTextChange { Ok(LuaTextChange(CodempTextChange {
span: start..end, span: start..end,
content: txt, content: txt,
after: "".into(),
})) }))
}); });
methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| Ok(format!("{:?}", this.0))); methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| Ok(format!("{:?}", this.0)));