feat: show cursors on correct buffers

also made proper bindings for CursorEvent and CursorPosition
This commit is contained in:
əlemi 2023-09-05 23:59:20 +02:00
parent 6791055f9e
commit a19beb2f1a
2 changed files with 37 additions and 27 deletions

View file

@ -132,7 +132,6 @@ vim.api.nvim_create_user_command(
"Join", "Join",
function (args) function (args)
local controller = codemp.join(args.args) local controller = codemp.join(args.args)
local buffer = vim.api.nvim_get_current_buf()
-- hook serverbound callbacks -- hook serverbound callbacks
vim.api.nvim_create_autocmd({"CursorMoved", "CursorMovedI", "ModeChanged"}, { vim.api.nvim_create_autocmd({"CursorMoved", "CursorMovedI", "ModeChanged"}, {
@ -154,14 +153,17 @@ vim.api.nvim_create_user_command(
hi = available_colors[ math.random( #available_colors ) ], hi = available_colors[ math.random( #available_colors ) ],
} }
end end
local buffer = buffer_mappings[event.position.buffer]
if buffer ~= nil then
vim.api.nvim_buf_clear_namespace(buffer, user_mappings[event.user].ns, 0, -1) vim.api.nvim_buf_clear_namespace(buffer, user_mappings[event.user].ns, 0, -1)
multiline_highlight( multiline_highlight(
buffer, buffer,
user_mappings[event.user].ns, user_mappings[event.user].ns,
user_mappings[event.user].hi, user_mappings[event.user].hi,
event.start, event.position.start,
event.finish event.position.finish
) )
end
end) end)
print(" ++ joined workspace " .. args.args) print(" ++ joined workspace " .. args.args)

View file

@ -13,22 +13,7 @@ impl From::<LuaCodempError> for LuaError {
} }
} }
fn cursor_to_table(lua: &Lua, cur: CodempCursorEvent) -> LuaResult<LuaTable> { // TODO put friendlier constructor directly in lib?
let pos = cur.position.unwrap_or_default();
let start = lua.create_table()?;
start.set(1, pos.start().row)?;
start.set(2, pos.start().col)?;
let end = lua.create_table()?;
end.set(1, pos.end().row)?;
end.set(2, pos.end().col)?;
let out = lua.create_table()?;
out.set("user", cur.user)?;
out.set("buffer", pos.buffer)?;
out.set("start", start)?;
out.set("finish", end)?;
Ok(out)
}
fn make_cursor(buffer: String, start_row: i32, start_col: i32, end_row: i32, end_col: i32) -> CodempCursorPosition { fn make_cursor(buffer: String, start_row: i32, start_col: i32, end_row: i32, end_col: i32) -> CodempCursorPosition {
CodempCursorPosition { CodempCursorPosition {
buffer, buffer,
@ -67,6 +52,8 @@ fn get_buffer(_: &Lua, (path,): (String,)) -> LuaResult<LuaBufferController> {
) )
} }
/// join a remote workspace and start processing cursor events /// join a remote workspace and start processing cursor events
fn join(_: &Lua, (session,): (String,)) -> LuaResult<LuaCursorController> { fn join(_: &Lua, (session,): (String,)) -> LuaResult<LuaCursorController> {
let controller = CODEMP_INSTANCE.join(&session) let controller = CODEMP_INSTANCE.join(&session)
@ -82,9 +69,9 @@ impl LuaUserData for LuaCursorController {
methods.add_method("send", |_, this, (usr, sr, sc, er, ec):(String, i32, i32, i32, i32)| { methods.add_method("send", |_, this, (usr, sr, sc, er, ec):(String, i32, i32, i32, i32)| {
Ok(this.0.send(make_cursor(usr, sr, sc, er, ec)).map_err(LuaCodempError::from)?) Ok(this.0.send(make_cursor(usr, sr, sc, er, ec)).map_err(LuaCodempError::from)?)
}); });
methods.add_method("try_recv", |lua, this, ()| { methods.add_method("try_recv", |_, this, ()| {
match this.0.try_recv() .map_err(LuaCodempError::from)? { match this.0.try_recv() .map_err(LuaCodempError::from)? {
Some(x) => Ok(Some(cursor_to_table(lua, x)?)), Some(x) => Ok(Some(LuaCursorEvent(x))),
None => Ok(None), None => Ok(None),
} }
}); });
@ -96,6 +83,27 @@ impl LuaUserData for LuaCursorController {
} }
} }
#[derive(Debug, derive_more::From)]
struct LuaCursorEvent(CodempCursorEvent);
impl LuaUserData for LuaCursorEvent {
fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_method_get("user", |_, this| Ok(this.0.user.clone()));
fields.add_field_method_get("position", |_, this|
Ok(this.0.position.as_ref().map(|x| LuaCursorPosition(x.clone())))
);
}
}
#[derive(Debug, derive_more::From)]
struct LuaCursorPosition(CodempCursorPosition);
impl LuaUserData for LuaCursorPosition {
fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_method_get("buffer", |_, this| Ok(this.0.buffer.clone()));
fields.add_field_method_get("start", |_, this| Ok(LuaRowCol(this.0.start())));
fields.add_field_method_get("finish", |_, this| Ok(LuaRowCol(this.0.end())));
}
}
/// create a new buffer in current workspace /// create a new buffer in current workspace