chore: merge branch 'feat/workspace' into dev

This commit is contained in:
əlemi 2024-02-09 01:34:29 +01:00
commit 6fb755b2c5
3 changed files with 136 additions and 105 deletions

View file

@ -7,9 +7,11 @@ edition = "2021"
crate-type = ["cdylib"] crate-type = ["cdylib"]
[dependencies] [dependencies]
codemp = { git = "ssh://git@github.com/codewithotherpeopleandchangenamelater/codemp.git", tag = "v0.5.1", 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"] } mlua = { version = "0.9.0", features = ["module", "luajit", "send"] }
thiserror = "1.0.47" thiserror = "1.0.47"
derive_more = "0.99.17" derive_more = "0.99.17"
tracing-subscriber = "0.3.17" tracing-subscriber = "0.3.17"
tracing = "0.1.37" tracing = "0.1.37"
lazy_static = "1.4.0"
tokio = { version = "1.35.1", features = ["rt"] }

View file

@ -1,4 +1,5 @@
local codemp = require("libcodemp_nvim") local codemp = require("libcodemp_nvim")
local active_workspace = nil
local codemp_changed_tick = {} local codemp_changed_tick = {}
@ -19,9 +20,10 @@ local function register_controller_handler(target, controller, handler, delay)
-- completely useless. We can circumvent this by requiring codemp again in the new -- completely useless. We can circumvent this by requiring codemp again in the new
-- thread and requesting a new reference to the same controller from che global instance -- thread and requesting a new reference to the same controller from che global instance
-- NOTE variables prefixed with underscore live in another Lua runtime -- NOTE variables prefixed with underscore live in another Lua runtime
vim.loop.new_thread({}, function(_async, _target, _delay) vim.loop.new_thread({}, function(_async, _workspace, _target, _delay)
local _codemp = require("libcodemp_nvim") local _codemp = require("libcodemp_nvim")
local _controller = _target ~= nil and _codemp.get_buffer(_target) or _codemp.get_cursor() local _ws = _codemp.get_workspace(_workspace)
local _controller = _target ~= nil and _ws:get_buffer(_target) or _ws.cursor
while true do while true do
local success, _ = pcall(_controller.poll, _controller) local success, _ = pcall(_controller.poll, _controller)
if success then if success then
@ -33,9 +35,10 @@ local function register_controller_handler(target, controller, handler, delay)
my_name = "buffer(" .. _target .. ")" my_name = "buffer(" .. _target .. ")"
end end
print(" -- stopping " .. my_name .. " controller poller") print(" -- stopping " .. my_name .. " controller poller")
break
end end
end end
end, async, target, delay) end, async, active_workspace, target, delay)
end end
local function split_without_trim(str, sep) local function split_without_trim(str, sep)
@ -146,19 +149,29 @@ local available_colors = { -- TODO these are definitely not portable!
"CmpItemKindInterface", "CmpItemKindInterface",
} }
-- vim.api.nvim_create_user_command(
-- "Connect",
-- function (args)
-- codemp.connect(#args.args > 0 and args.args or nil)
-- print(" ++ connected")
-- end,
-- { nargs = "?" }
-- )
vim.api.nvim_create_user_command( vim.api.nvim_create_user_command(
"Connect", "Login",
function (args) function (args)
codemp.connect(#args.args > 0 and args.args or nil) codemp.login(args.fargs[1], args.fargs[2], args.fargs[3])
print(" ++ connected") print(" ++ logged in " .. args.args)
end, end,
{ nargs = "?" } { nargs = "+" }
) )
vim.api.nvim_create_user_command( vim.api.nvim_create_user_command(
"Join", "Join",
function (args) function (args)
local controller = codemp.join(args.args) local controller = codemp.join_workspace(args.args)
active_workspace = args.args
-- hook serverbound callbacks -- hook serverbound callbacks
vim.api.nvim_create_autocmd({"CursorMoved", "CursorMovedI", "ModeChanged"}, { vim.api.nvim_create_autocmd({"CursorMoved", "CursorMovedI", "ModeChanged"}, {
@ -207,7 +220,7 @@ vim.api.nvim_create_user_command(
content = buffer_get_content(buf) content = buffer_get_content(buf)
-- TODO send content! -- TODO send content!
end end
codemp.create(args.args, content) codemp.get_workspace(active_workspace):create_buffer(args.args, content)
print(" ++ created buffer " .. args.args) print(" ++ created buffer " .. args.args)
end, end,
@ -228,7 +241,7 @@ vim.api.nvim_create_user_command(
vim.api.nvim_buf_set_name(buffer, "codemp::" .. args.args) vim.api.nvim_buf_set_name(buffer, "codemp::" .. args.args)
vim.api.nvim_set_current_buf(buffer) vim.api.nvim_set_current_buf(buffer)
end end
local controller = codemp.attach(args.args) local controller = codemp.get_workspace(active_workspace):attach_buffer(args.args)
-- TODO map name to uuid -- TODO map name to uuid
@ -242,13 +255,20 @@ 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) 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 tick <= codemp_changed_tick[buf] then return end
if buffer_mappings[buf] == nil then return true end -- unregister callback handler if buffer_mappings[buf] == nil then return true end -- unregister callback handler
local content = "" local text = buffer_get_content(buf)
if old_end_row < new_end_row and new_byte_len == 1 then print(string.format("CRDT content: %s", controller.content))
content = "\n" controller:send(0, #controller.content, text)
else -- local content = ""
content = table.concat(vim.api.nvim_buf_get_text(buf, start_row, start_col, start_row + new_end_row, start_col + new_byte_len, {}), '\n') -- if old_end_row < new_end_row and new_byte_len == 1 then
end -- content = "\n"
controller:send(start_offset, start_offset + old_end_byte_len, content) -- else
-- print(string.format("%s %s %s %s", start_row, start_col, start_row + new_end_row, start_col + new_end_col))
-- content = table.concat(vim.api.nvim_buf_get_text(buf, start_row - 1, start_col, start_row + new_end_row - 1, start_col + new_end_col, {}), '\n')
-- end
-- if old_end_row < new_end_row then
-- start_offset = start_offset - 1
-- end
-- controller:send(start_offset, start_offset + old_end_byte_len, content)
end, end,
}) })
@ -260,7 +280,7 @@ vim.api.nvim_create_user_command(
-- 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[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 before = buffer_get_content(buffer)
local after = event:apply(before) local after = event:apply(before)
buffer_set_content(buffer, after) buffer_set_content(buffer, after)
@ -278,8 +298,8 @@ vim.api.nvim_create_user_command(
local buffer = vim.api.nvim_get_current_buf() local buffer = vim.api.nvim_get_current_buf()
local name = buffer_mappings[buffer] local name = buffer_mappings[buffer]
if name ~= nil then if name ~= nil then
local controller = codemp.get_buffer(name) 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) buffer_set_content(buffer, controller.content)
print(" :: synched buffer " .. name) print(" :: synched buffer " .. name)
else else
@ -297,7 +317,7 @@ vim.api.nvim_create_user_command(
local name = buffer_mappings[buffer] local name = buffer_mappings[buffer]
buffer_mappings[buffer] = nil buffer_mappings[buffer] = nil
buffer_mappings_reverse[name] = nil buffer_mappings_reverse[name] = nil
codemp.disconnect_buffer(name) codemp.get_workspace(active_workspace):disconnect_buffer(name)
vim.api.nvim_buf_delete(buffer, {}) vim.api.nvim_buf_delete(buffer, {})
print(" -- detached from buffer " .. name) print(" -- detached from buffer " .. name)
end, end,

View file

@ -1,10 +1,43 @@
use std::io::Write; use std::io::Write;
use std::sync::{Arc, Mutex, mpsc}; use std::sync::{mpsc, Arc, Mutex};
use codemp::prelude::*; use codemp::prelude::*;
use codemp::proto::files::BufferNode;
use codemp::woot::crdt::Op; use codemp::woot::crdt::Op;
use mlua::prelude::*; use mlua::prelude::*;
use tokio::runtime::Runtime;
lazy_static::lazy_static!{
// TODO use a runtime::Builder::new_current_thread() runtime to not behave like malware
static ref STATE : GlobalState = GlobalState::default();
}
struct GlobalState {
client: std::sync::RwLock<CodempClient>,
runtime: Runtime,
}
impl Default for GlobalState {
fn default() -> Self {
let rt = Runtime::new().expect("could not create tokio runtime");
let client = rt.block_on(CodempClient::new("http://codemp.alemi.dev:50053")).expect("could not connect to codemp servers");
GlobalState { client: std::sync::RwLock::new(client), runtime: rt }
}
}
impl GlobalState {
fn client(&self) -> std::sync::RwLockReadGuard<CodempClient> {
self.client.read().unwrap()
}
fn client_mut(&self) -> std::sync::RwLockWriteGuard<CodempClient> {
self.client.write().unwrap()
}
fn rt(&self) -> &Runtime {
&self.runtime
}
}
#[derive(Debug, thiserror::Error, derive_more::From, derive_more::Display)] #[derive(Debug, thiserror::Error, derive_more::From, derive_more::Display)]
struct LuaCodempError(CodempError); struct LuaCodempError(CodempError);
@ -18,53 +51,63 @@ impl From::<LuaCodempError> for LuaError {
// TODO put friendlier constructor directly in lib? // 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 { fn make_cursor(buffer: String, start_row: i32, start_col: i32, end_row: i32, end_col: i32) -> CodempCursorPosition {
CodempCursorPosition { CodempCursorPosition {
buffer, buffer: BufferNode::from(buffer), start: CodempRowCol { row: start_row, col: start_col}, end: CodempRowCol { row: end_row, col: end_col },
start: Some(CodempRowCol {
row: start_row, col: start_col,
}),
end: Some(CodempRowCol {
row: end_row, col: end_col,
}),
} }
} }
fn id(_: &Lua, (): ()) -> LuaResult<String> {
Ok(STATE.client().user_id().to_string())
}
/// join a remote workspace and start processing cursor events
fn join_workspace(_: &Lua, (session,): (String,)) -> LuaResult<LuaCursorController> {
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 = 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().get_workspace(&session).map(LuaWorkspace))
}
#[derive(Debug, derive_more::From)] #[derive(Debug, derive_more::From)]
struct LuaOp(Op); struct LuaOp(Op);
impl LuaUserData for LuaOp { } impl LuaUserData for LuaOp { }
/// connect to remote server #[derive(derive_more::From)]
fn connect(_: &Lua, (host,): (Option<String>,)) -> LuaResult<()> { struct LuaWorkspace(Arc<CodempWorkspace>);
let addr = host.unwrap_or("http://127.0.0.1:50051".into()); impl LuaUserData for LuaWorkspace {
CODEMP_INSTANCE.connect(&addr) fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
.map_err(LuaCodempError::from)?; methods.add_method("create_buffer", |_, this, (name,):(String,)| {
Ok(()) Ok(STATE.rt().block_on(async { this.0.create(&name).await }).map_err(LuaCodempError::from)?)
} });
fn get_cursor(_: &Lua, _args: ()) -> LuaResult<LuaCursorController> { methods.add_method("attach_buffer", |_, this, (name,):(String,)| {
Ok( Ok(LuaBufferController(STATE.rt().block_on(async { this.0.attach(&name).await }).map_err(LuaCodempError::from)?))
CODEMP_INSTANCE.get_cursor() });
.map_err(LuaCodempError::from)?
.into()
)
}
fn get_buffer(_: &Lua, (path,): (String,)) -> LuaResult<LuaBufferController> { // TODO disconnect_buffer
Ok( // TODO leave_workspace:w
CODEMP_INSTANCE.get_buffer(&path)
.map_err(LuaCodempError::from)? methods.add_method("get_buffer", |_, this, (name,):(String,)| Ok(this.0.buffer_by_name(&name).map(LuaBufferController)));
.into() }
)
fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
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
}
} }
/// join a remote workspace and start processing cursor events
fn join(_: &Lua, (session,): (String,)) -> LuaResult<LuaCursorController> {
let controller = CODEMP_INSTANCE.join(&session)
.map_err(LuaCodempError::from)?;
Ok(LuaCursorController(controller))
}
#[derive(Debug, derive_more::From)] #[derive(Debug, derive_more::From)]
struct LuaCursorController(Arc<CodempCursorController>); struct LuaCursorController(Arc<CodempCursorController>);
impl LuaUserData for LuaCursorController { impl LuaUserData for LuaCursorController {
@ -80,7 +123,7 @@ impl LuaUserData for LuaCursorController {
} }
}); });
methods.add_method("poll", |_, this, ()| { methods.add_method("poll", |_, this, ()| {
CODEMP_INSTANCE.rt().block_on(this.0.poll()) STATE.rt().block_on(this.0.poll())
.map_err(LuaCodempError::from)?; .map_err(LuaCodempError::from)?;
Ok(()) Ok(())
}); });
@ -91,9 +134,9 @@ impl LuaUserData for LuaCursorController {
struct LuaCursorEvent(CodempCursorEvent); struct LuaCursorEvent(CodempCursorEvent);
impl LuaUserData for LuaCursorEvent { impl LuaUserData for LuaCursorEvent {
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("user", |_, this| Ok(this.0.user.clone())); fields.add_field_method_get("user", |_, this| Ok(this.0.user.id.clone()));
fields.add_field_method_get("position", |_, this| fields.add_field_method_get("position", |_, this|
Ok(this.0.position.as_ref().map(|x| LuaCursorPosition(x.clone()))) Ok(LuaCursorPosition(this.0.position.clone()))
); );
} }
} }
@ -102,30 +145,13 @@ impl LuaUserData for LuaCursorEvent {
struct LuaCursorPosition(CodempCursorPosition); struct LuaCursorPosition(CodempCursorPosition);
impl LuaUserData for LuaCursorPosition { impl LuaUserData for LuaCursorPosition {
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("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()))); 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()))); fields.add_field_method_get("finish", |_, this| Ok(LuaRowCol(this.0.end.clone())));
} }
} }
/// create a new buffer in current workspace
fn create(_: &Lua, (path, content): (String, Option<String>)) -> LuaResult<()> {
CODEMP_INSTANCE.create(&path, content.as_deref())
.map_err(LuaCodempError::from)?;
Ok(())
}
/// attach to remote buffer and start processing buffer events
fn attach(_: &Lua, (path,): (String,)) -> LuaResult<LuaBufferController> {
let controller = CODEMP_INSTANCE.attach(&path)
.map_err(LuaCodempError::from)?;
Ok(LuaBufferController(controller))
}
#[derive(Debug, derive_more::From)] #[derive(Debug, derive_more::From)]
struct LuaBufferController(Arc<CodempBufferController>); struct LuaBufferController(Arc<CodempBufferController>);
impl LuaUserData for LuaBufferController { impl LuaUserData for LuaBufferController {
@ -149,7 +175,7 @@ impl LuaUserData for LuaBufferController {
} }
}); });
methods.add_method("poll", |_, this, ()| { methods.add_method("poll", |_, this, ()| {
CODEMP_INSTANCE.rt().block_on(this.0.poll()) STATE.rt().block_on(this.0.poll())
.map_err(LuaCodempError::from)?; .map_err(LuaCodempError::from)?;
Ok(()) Ok(())
}); });
@ -221,18 +247,6 @@ impl Write for LuaLoggerProducer {
fn flush(&mut self) -> std::io::Result<()> { Ok(()) } fn flush(&mut self) -> std::io::Result<()> { Ok(()) }
} }
fn disconnect_buffer(_: &Lua, (path,): (String,)) -> LuaResult<()> {
CODEMP_INSTANCE.disconnect_buffer(&path)
.map_err(LuaCodempError::from)?;
Ok(())
}
fn leave_workspace(_: &Lua, (): ()) -> LuaResult<()> {
CODEMP_INSTANCE.leave_workspace()
.map_err(LuaCodempError::from)?;
Ok(())
}
fn setup_tracing(_: &Lua, (debug,): (Option<bool>,)) -> LuaResult<LuaLogger> { fn setup_tracing(_: &Lua, (debug,): (Option<bool>,)) -> LuaResult<LuaLogger> {
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
let level = if debug.unwrap_or(false) { tracing::Level::DEBUG } else {tracing::Level::INFO }; let level = if debug.unwrap_or(false) { tracing::Level::DEBUG } else {tracing::Level::INFO };
@ -254,26 +268,21 @@ fn setup_tracing(_: &Lua, (debug,): (Option<bool>,)) -> LuaResult<LuaLogger> {
Ok(LuaLogger(Arc::new(Mutex::new(rx)))) Ok(LuaLogger(Arc::new(Mutex::new(rx))))
} }
// define module and exports // define module and exports
#[mlua::lua_module] #[mlua::lua_module]
fn libcodemp_nvim(lua: &Lua) -> LuaResult<LuaTable> { fn libcodemp_nvim(lua: &Lua) -> LuaResult<LuaTable> {
let exports = lua.create_table()?; let exports = lua.create_table()?;
// core proto functions // core proto functions
exports.set("connect", lua.create_function(connect)?)?; exports.set("login", lua.create_function(login)?)?;
exports.set("join", lua.create_function(join)?)?; exports.set("join_workspace", lua.create_function(join_workspace)?)?;
exports.set("create", lua.create_function(create)?)?;
exports.set("attach", lua.create_function(attach)?)?;
// state helpers // state helpers
exports.set("get_cursor", lua.create_function(get_cursor)?)?; exports.set("get_workspace", lua.create_function(get_workspace)?)?;
exports.set("get_buffer", lua.create_function(get_buffer)?)?;
// cleanup
exports.set("disconnect_buffer", lua.create_function(disconnect_buffer)?)?;
exports.set("leave_workspace", lua.create_function(leave_workspace)?)?;
// debug // debug
exports.set("id", lua.create_function(id)?)?;
exports.set("setup_tracing", lua.create_function(setup_tracing)?)?; exports.set("setup_tracing", lua.create_function(setup_tracing)?)?;
Ok(exports) Ok(exports)
} }