feat: initial broken attempt with early woot api

This commit is contained in:
əlemi 2023-11-10 05:35:18 +01:00
parent bcbb7a7455
commit 140dd0ff4c
3 changed files with 39 additions and 41 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.5", features = ["global", "sync"] } codemp = { git = "ssh://git@github.com/codewithotherpeopleandchangenamelater/codemp.git", branch = "woot", 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

@ -25,17 +25,6 @@ local function register_controller_handler(target, controller, handler)
end, async, target) end, async, target)
end end
-- local function byte2rowcol(buf, x)
-- local row
-- local row_start
-- vim.api.nvim_buf_call(buf, function ()
-- row = vim.fn.byte2line(x)
-- row_start = vim.fn.line2byte(row)
-- end)
-- local col = x - row_start
-- return { row, col }
-- end
local function split_without_trim(str, sep) local function split_without_trim(str, sep)
local res = vim.fn.split(str, sep) local res = vim.fn.split(str, sep)
if str:sub(1,1) == "\n" then if str:sub(1,1) == "\n" then
@ -114,11 +103,14 @@ end
local buffer_mappings = {} local buffer_mappings = {}
local buffer_mappings_reverse = {} -- TODO maybe not??? local buffer_mappings_reverse = {} -- TODO maybe not???
local user_mappings = {} local user_mappings = {}
local available_colors = { local available_colors = { -- TODO these are definitely not portable!
"ErrorMsg", "ErrorMsg",
"WarningMsg", "WarningMsg",
"MatchParen", "MatchParen",
"SpecialMode", "SpecialMode",
"CmpItemKindFunction",
"CmpItemKindValue",
"CmpItemKindInterface",
} }
vim.api.nvim_create_user_command( vim.api.nvim_create_user_command(
@ -193,6 +185,8 @@ vim.api.nvim_create_user_command(
function (args) function (args)
local controller = codemp.attach(args.args) local controller = codemp.attach(args.args)
-- TODO map name to uuid
local buffer = vim.api.nvim_get_current_buf() local buffer = vim.api.nvim_get_current_buf()
buffer_mappings[buffer] = args.args buffer_mappings[buffer] = args.args
buffer_mappings_reverse[args.args] = buffer buffer_mappings_reverse[args.args] = buffer
@ -202,7 +196,8 @@ vim.api.nvim_create_user_command(
-- hook serverbound callbacks -- hook serverbound callbacks
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 local content = buffer_get_content(buf)
controller:send(0, #content - 1, content)
-- 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(
@ -214,8 +209,6 @@ vim.api.nvim_create_user_command(
-- -- 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
}) })
@ -232,7 +225,7 @@ vim.api.nvim_create_user_command(
-- 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) buffer_set_content(buffer, event)
end) end)
print(" ++ joined workspace " .. args.args) print(" ++ joined workspace " .. args.args)

View file

@ -2,6 +2,7 @@ use std::io::Write;
use std::sync::{Arc, Mutex, mpsc}; use std::sync::{Arc, Mutex, mpsc};
use codemp::prelude::*; use codemp::prelude::*;
use codemp::woot::crdt::Op;
use mlua::prelude::*; use mlua::prelude::*;
@ -27,7 +28,9 @@ fn make_cursor(buffer: String, start_row: i32, start_col: i32, end_row: i32, end
} }
} }
#[derive(Debug, derive_more::From)]
struct LuaOp(Op);
impl LuaUserData for LuaOp { }
/// connect to remote server /// connect to remote server
fn connect(_: &Lua, (host,): (Option<String>,)) -> LuaResult<()> { fn connect(_: &Lua, (host,): (Option<String>,)) -> LuaResult<()> {
@ -66,7 +69,7 @@ fn join(_: &Lua, (session,): (String,)) -> LuaResult<LuaCursorController> {
struct LuaCursorController(Arc<CodempCursorController>); struct LuaCursorController(Arc<CodempCursorController>);
impl LuaUserData for LuaCursorController { impl LuaUserData for LuaCursorController {
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) { fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| Ok(format!("{:?}", this))); methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| Ok(format!("{:?}", this.0)));
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)?)
}); });
@ -127,25 +130,22 @@ fn attach(_: &Lua, (path,): (String,)) -> LuaResult<LuaBufferController> {
struct LuaBufferController(Arc<CodempBufferController>); struct LuaBufferController(Arc<CodempBufferController>);
impl LuaUserData for LuaBufferController { impl LuaUserData for LuaBufferController {
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) { fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| Ok(format!("{:?}", this))); methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| Ok(format!("{:?}", this.0)));
methods.add_method("delta", |_, this, (start, txt, end):(usize, String, usize)| { methods.add_method("send", |_, this, (start, end, text): (usize, usize, String)| {
match this.0.delta(start, &txt, end) { Ok(
Some(op) => Ok(this.0.send(op).map_err(LuaCodempError::from)?), this.0.send(
None => Err(LuaError::RuntimeError("wtf".into())), CodempTextChange {
span: start..end,
content: text,
after: "".into(),
} }
}); )
methods.add_method("replace", |_, this, txt:String| { .map_err(LuaCodempError::from)?
match this.0.replace(&txt) { )
Some(op) => Ok(this.0.send(op).map_err(LuaCodempError::from)?),
None => Ok(()),
}
});
methods.add_method("insert", |_, this, (txt, pos):(String, u64)| {
Ok(this.0.send(this.0.insert(&txt, pos)).map_err(LuaCodempError::from)?)
}); });
methods.add_method("try_recv", |_, 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(LuaTextChange(x))), Some(x) => Ok(Some(x)),
None => Ok(None), None => Ok(None),
} }
}); });
@ -157,7 +157,7 @@ impl LuaUserData for LuaBufferController {
} }
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())); fields.add_field_method_get("content", |_, this| Ok(this.0.try_recv().unwrap().unwrap()));
} }
} }
@ -166,14 +166,19 @@ 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(LuaRowCol(this.0.start()))); fields.add_field_method_get("start", |_, this| Ok(this.0.span.start));
// fields.add_field_method_get("finish", |_, this| Ok(LuaRowCol(this.0.end()))); fields.add_field_method_get("finish", |_, this| Ok(this.0.span.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) {
methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| Ok(format!("{:?}", this))); methods.add_meta_function(LuaMetaMethod::Call, |_, (start, end, txt): (usize, usize, String)| {
Ok(LuaTextChange(CodempTextChange {
span: start..end,
content: txt,
after: "".into(),
}))
});
methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| Ok(format!("{:?}", this.0)));
} }
} }