fix: make it work cheating

This commit is contained in:
əlemi 2023-09-04 18:35:24 +02:00
parent ec873ee8b4
commit 2b5fd19a0e
3 changed files with 29 additions and 50 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", 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"] } 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

@ -173,33 +173,36 @@ vim.api.nvim_create_user_command(
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
print(string.format(">[%s] %s:%s|%s (%s)", tick, firstline, lastline, new_lastline, old_byte_size)) -- 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 start_index = firstline == 0 and 0 or vim.fn.line2byte(firstline + 1) - 1
local text = table.concat( -- local text = table.concat(
vim.api.nvim_buf_get_lines(buf, firstline, new_lastline, true), -- vim.api.nvim_buf_get_lines(buf, firstline, new_lastline, true),
"\n" -- "\n"
) -- )
if lastline ~= new_lastline then -- -- if lastline ~= new_lastline then
text = text .. "\n" -- -- text = text .. "\n"
end -- -- end
print(string.format(">delta [%d,%s,%d]", start_index, text, start_index + old_byte_size - 1)) -- 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) -- controller:delta(start_index, text, start_index + old_byte_size - 1)
local content = buffer_get_content(buf)
controller:replace(content)
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 = controller:byte2rowcol(event.start) -- local start = event.start
local finish = controller:byte2rowcol(event.finish) -- local finish = event.finish
print(string.format( -- print(string.format(
"buf_set_text(%s,%s, %s,%s, '%s')", -- "buf_set_text(%s,%s, %s,%s, '%s')",
start.row, start.col, finish.row, finish.col, vim.inspect(split_without_trim(event.content, "\n")) -- 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, start.row, start.col, finish.row, finish.col,
split_without_trim(event.content, "\n") -- split_without_trim(event.content, "\n")
) -- )
buffer_set_content(buffer, controller.content)
end) end)
print(" ++ joined workspace " .. args.args) print(" ++ joined workspace " .. args.args)

View file

@ -13,16 +13,6 @@ impl From::<LuaCodempError> 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<LuaTable> { fn cursor_to_table(lua: &Lua, cur: CodempCursorEvent) -> LuaResult<LuaTable> {
let pos = cur.position.unwrap_or_default(); let pos = cur.position.unwrap_or_default();
let start = lua.create_table()?; let start = lua.create_table()?;
@ -155,10 +145,6 @@ impl LuaUserData for LuaBufferController {
.map_err(LuaCodempError::from)?; .map_err(LuaCodempError::from)?;
Ok(()) 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) { fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
@ -171,8 +157,10 @@ 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("start", |_, this| Ok(LuaRowCol(this.0.start())));
fields.add_field_method_get("finish", |_, this| Ok(this.0.span.end)); // 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) { fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
@ -200,17 +188,5 @@ fn libcodemp_nvim(lua: &Lua) -> LuaResult<LuaTable> {
exports.set("attach", lua.create_function(attach)?)?; exports.set("attach", lua.create_function(attach)?)?;
exports.set("get_cursor", lua.create_function(get_cursor)?)?; exports.set("get_cursor", lua.create_function(get_cursor)?)?;
exports.set("get_buffer", lua.create_function(get_buffer)?)?; exports.set("get_buffer", lua.create_function(get_buffer)?)?;
exports.set("byte2rowcol",lua.create_function(byte2rowcol)?)?;
Ok(exports) 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))
}