feat: updated to codemp v0.6

This commit is contained in:
əlemi 2024-02-09 01:34:04 +01:00
parent ef12f3d588
commit 1bd1607822
3 changed files with 34 additions and 17 deletions

View file

@ -7,8 +7,8 @@ edition = "2021"
crate-type = ["cdylib"]
[dependencies]
mlua = { version = "0.9.0", features = ["module", "luajit"] }
codemp = { git = "ssh://git@github.com/codewithotherpeopleandchangenamelater/codemp.git", rev = "5cc0bd3a869d83f7decc8ce1185eebb38771621a", features = ["global", "sync"] }
codemp = { git = "ssh://git@github.com/codewithotherpeopleandchangenamelater/codemp.git", tag = "v0.6.0", features = ["global", "sync"] }
mlua = { version = "0.9.0", features = ["module", "luajit", "send"] }
thiserror = "1.0.47"
derive_more = "0.99.17"
tracing-subscriber = "0.3.17"

View file

@ -23,7 +23,7 @@ local function register_controller_handler(target, controller, handler, delay)
vim.loop.new_thread({}, function(_async, _workspace, _target, _delay)
local _codemp = require("libcodemp_nvim")
local _ws = _codemp.get_workspace(_workspace)
local _controller = _target ~= nil and _ws:get_buffer(_target) or _ws:get_cursor()
local _controller = _target ~= nil and _ws:get_buffer(_target) or _ws.cursor
while true do
local success, _ = pcall(_controller.poll, _controller)
if success then
@ -35,6 +35,7 @@ local function register_controller_handler(target, controller, handler, delay)
my_name = "buffer(" .. _target .. ")"
end
print(" -- stopping " .. my_name .. " controller poller")
break
end
end
end, async, active_workspace, target, delay)
@ -157,6 +158,15 @@ local available_colors = { -- TODO these are definitely not portable!
-- { nargs = "?" }
-- )
vim.api.nvim_create_user_command(
"Login",
function (args)
codemp.login(args.fargs[1], args.fargs[2], args.fargs[3])
print(" ++ logged in " .. args.args)
end,
{ nargs = "+" }
)
vim.api.nvim_create_user_command(
"Join",
function (args)
@ -245,7 +255,9 @@ vim.api.nvim_create_user_command(
on_bytes = function(_, buf, tick, start_row, start_col, start_offset, old_end_row, old_end_col, old_end_byte_len, new_end_row, new_end_col, new_byte_len)
if tick <= codemp_changed_tick[buf] then return end
if buffer_mappings[buf] == nil then return true end -- unregister callback handler
controller:replace(buffer_get_content(buf))
local text = buffer_get_content(buf)
print(string.format("CRDT content: %s", controller.content))
controller:send(0, #controller.content, text)
-- local content = ""
-- if old_end_row < new_end_row and new_byte_len == 1 then
-- content = "\n"
@ -268,7 +280,7 @@ vim.api.nvim_create_user_command(
-- hook clientbound callbacks
register_controller_handler(args.args, controller, function(event)
codemp_changed_tick[buffer] = vim.api.nvim_buf_get_changedtick(buffer)
codemp_changed_tick[buffer] = vim.api.nvim_buf_get_changedtick(buffer) + 1
local before = buffer_get_content(buffer)
local after = event:apply(before)
buffer_set_content(buffer, after)
@ -287,7 +299,7 @@ vim.api.nvim_create_user_command(
local name = buffer_mappings[buffer]
if name ~= nil then
local controller = codemp.get_workspace(active_workspace):get_buffer(name)
codemp_changed_tick[buffer] = vim.api.nvim_buf_get_changedtick(buffer)
codemp_changed_tick[buffer] = vim.api.nvim_buf_get_changedtick(buffer) + 1
buffer_set_content(buffer, controller.content)
print(" :: synched buffer " .. name)
else

View file

@ -2,10 +2,10 @@ use std::io::Write;
use std::sync::{mpsc, Arc, Mutex};
use codemp::prelude::*;
use codemp::proto::files::BufferNode;
use codemp::woot::crdt::Op;
use mlua::prelude::*;
use tokio::runtime::Runtime;
use tokio::sync::RwLock;
lazy_static::lazy_static!{
// TODO use a runtime::Builder::new_current_thread() runtime to not behave like malware
@ -51,7 +51,7 @@ impl From::<LuaCodempError> for LuaError {
// TODO put friendlier constructor directly in lib?
fn make_cursor(buffer: String, start_row: i32, start_col: i32, end_row: i32, end_col: i32) -> CodempCursorPosition {
CodempCursorPosition {
buffer, start: CodempRowCol { row: start_row, col: start_col}, end: CodempRowCol { row: end_row, col: end_col },
buffer: BufferNode::from(buffer), start: CodempRowCol { row: start_row, col: start_col}, end: CodempRowCol { row: end_row, col: end_col },
}
}
@ -65,12 +65,16 @@ fn join_workspace(_: &Lua, (session,): (String,)) -> LuaResult<LuaCursorControll
tracing::info!("joining workspace {}", session);
let ws = STATE.rt().block_on(async { STATE.client_mut().join_workspace(&session).await })
.map_err(LuaCodempError::from)?;
let cursor = STATE.rt().block_on(async { ws.read().await.cursor() });
let cursor = ws.cursor();
Ok(cursor.into())
}
fn login(_: &Lua, (username, password, workspace_id):(String, String, String)) -> LuaResult<()> {
Ok(STATE.rt().block_on(STATE.client().login(username, password, Some(workspace_id))).map_err(LuaCodempError::from)?)
}
fn get_workspace(_: &Lua, (session,): (String,)) -> LuaResult<Option<LuaWorkspace>> {
Ok(STATE.client().workspaces.get(&session).cloned().map(LuaWorkspace))
Ok(STATE.client().get_workspace(&session).map(LuaWorkspace))
}
#[derive(Debug, derive_more::From)]
@ -78,26 +82,26 @@ struct LuaOp(Op);
impl LuaUserData for LuaOp { }
#[derive(derive_more::From)]
struct LuaWorkspace(Arc<RwLock<CodempWorkspace>>);
struct LuaWorkspace(Arc<CodempWorkspace>);
impl LuaUserData for LuaWorkspace {
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_method("create_buffer", |_, this, (name,):(String,)| {
Ok(STATE.rt().block_on(async { this.0.write().await.create(&name).await }).map_err(LuaCodempError::from)?)
Ok(STATE.rt().block_on(async { this.0.create(&name).await }).map_err(LuaCodempError::from)?)
});
methods.add_method("attach_buffer", |_, this, (name,):(String,)| {
Ok(LuaBufferController(STATE.rt().block_on(async { this.0.write().await.attach(&name).await }).map_err(LuaCodempError::from)?))
Ok(LuaBufferController(STATE.rt().block_on(async { this.0.attach(&name).await }).map_err(LuaCodempError::from)?))
});
// TODO disconnect_buffer
// TODO leave_workspace:w
methods.add_method("get_buffer", |_, this, (name,):(String,)| Ok(STATE.rt().block_on(async { this.0.read().await.buffer_by_name(&name) }).map(LuaBufferController)));
methods.add_method("get_buffer", |_, this, (name,):(String,)| Ok(this.0.buffer_by_name(&name).map(LuaBufferController)));
}
fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_method_get("cursor", |_, this| Ok(LuaCursorController(STATE.rt().block_on(async { this.0.read().await.cursor() }))));
fields.add_field_method_get("filetree", |_, this| Ok(STATE.rt().block_on(async { this.0.read().await.filetree() })));
fields.add_field_method_get("cursor", |_, this| Ok(LuaCursorController(this.0.cursor())));
fields.add_field_method_get("filetree", |_, this| Ok(this.0.filetree()));
// methods.add_method("users", |_, this| Ok(this.0.users())); // TODO
}
}
@ -141,7 +145,7 @@ impl LuaUserData for LuaCursorEvent {
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("buffer", |_, this| Ok(this.0.buffer.path.clone()));
fields.add_field_method_get("start", |_, this| Ok(LuaRowCol(this.0.start.clone())));
fields.add_field_method_get("finish", |_, this| Ok(LuaRowCol(this.0.end.clone())));
}
@ -270,6 +274,7 @@ fn libcodemp_nvim(lua: &Lua) -> LuaResult<LuaTable> {
let exports = lua.create_table()?;
// core proto functions
exports.set("login", lua.create_function(login)?)?;
exports.set("join_workspace", lua.create_function(join_workspace)?)?;
// state helpers
exports.set("get_workspace", lua.create_function(get_workspace)?)?;