From 2b5fd19a0ecf05d478a0a5acaafd6b52f52135d0 Mon Sep 17 00:00:00 2001 From: alemi Date: Mon, 4 Sep 2023 18:35:24 +0200 Subject: [PATCH] fix: make it work cheating --- Cargo.toml | 2 +- src/codemp.lua | 45 ++++++++++++++++++++++++--------------------- src/lib.rs | 32 ++++---------------------------- 3 files changed, 29 insertions(+), 50 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1d9f9ea..2d20b17 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", tag = "v0.4.4", features = ["global", "sync"] } +codemp = { git = "ssh://git@github.com/codewithotherpeopleandchangenamelater/codemp.git", tag = "v0.4.5", 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 d273bb1..101cf29 100644 --- a/src/codemp.lua +++ b/src/codemp.lua @@ -173,33 +173,36 @@ vim.api.nvim_create_user_command( 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 - print(string.format(">[%s] %s:%s|%s (%s)", tick, firstline, lastline, new_lastline, old_byte_size)) - local start_index = firstline == 0 and 0 or vim.fn.line2byte(firstline + 1) - 1 - 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) + -- print(string.format(">[%s] %s:%s|%s (%s)", tick, firstline, lastline, new_lastline, old_byte_size)) + -- local start_index = firstline == 0 and 0 or vim.fn.line2byte(firstline + 1) - 1 + -- 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) + local content = buffer_get_content(buf) + controller:replace(content) end }) -- hook clientbound callbacks register_controller_handler(args.args, controller, function(event) codemp_changed_tick = vim.api.nvim_buf_get_changedtick(buffer) + 1 - local start = controller:byte2rowcol(event.start) - local finish = controller:byte2rowcol(event.finish) - print(string.format( - "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( - buffer, start.row, start.col, finish.row, finish.col, - split_without_trim(event.content, "\n") - ) + -- local start = event.start + -- local finish = event.finish + -- print(string.format( + -- "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( + -- buffer, start.row, start.col, finish.row, finish.col, + -- split_without_trim(event.content, "\n") + -- ) + buffer_set_content(buffer, controller.content) end) print(" ++ joined workspace " .. args.args) diff --git a/src/lib.rs b/src/lib.rs index c0ce2f9..1070130 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,16 +13,6 @@ impl From:: for LuaError { } } -fn byte_to_rowcol(text: &str, index: usize) -> CodempRowCol { - let lines_before = text[..index].split('\n').count() - 1; - let chars_before = text[..index].split('\n').last().unwrap_or_default().len(); - - CodempRowCol { - row: lines_before as i32, - col: chars_before as i32, - } -} - fn cursor_to_table(lua: &Lua, cur: CodempCursorEvent) -> LuaResult { let pos = cur.position.unwrap_or_default(); let start = lua.create_table()?; @@ -155,10 +145,6 @@ impl LuaUserData for LuaBufferController { .map_err(LuaCodempError::from)?; Ok(()) }); - - methods.add_method("byte2rowcol", |_, this, (byte,)| { - Ok(LuaRowCol(byte_to_rowcol(&this.0.content(), byte))) - }); } fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) { @@ -171,8 +157,10 @@ struct LuaTextChange(CodempTextChange); impl LuaUserData for LuaTextChange { 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("start", |_, this| Ok(this.0.span.start)); - fields.add_field_method_get("finish", |_, this| Ok(this.0.span.end)); + // fields.add_field_method_get("start", |_, this| Ok(LuaRowCol(this.0.start()))); + // fields.add_field_method_get("finish", |_, this| Ok(LuaRowCol(this.0.end()))); + // fields.add_field_method_get("before", |_, this| Ok((*this.0.before).clone())); + // fields.add_field_method_get("after", |_, this| Ok((*this.0.after).clone())); } fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) { @@ -200,17 +188,5 @@ fn libcodemp_nvim(lua: &Lua) -> LuaResult { exports.set("attach", lua.create_function(attach)?)?; exports.set("get_cursor", lua.create_function(get_cursor)?)?; exports.set("get_buffer", lua.create_function(get_buffer)?)?; - exports.set("byte2rowcol",lua.create_function(byte2rowcol)?)?; Ok(exports) } - - -// TODO this is wasteful because, just to calculate two indices, we clone a -// potentially big string. this is necessary because vim doesn't provide an -// api equivalent of byte2line (we need to specify arbitrary buffers). -fn byte2rowcol(_: &Lua, (txt, index): (String, usize)) -> LuaResult<(usize, usize)> { - let lines = txt[..index].split('\n'); - let col = lines.clone().last().unwrap_or("").len(); - let row = lines.count() - 1; - Ok((row, col)) -}