mirror of
https://github.com/hexedtech/codemp-nvim.git
synced 2024-11-22 07:24:52 +01:00
chore: merge branch 'feat/workspace' into dev
This commit is contained in:
commit
6fb755b2c5
3 changed files with 136 additions and 105 deletions
|
@ -7,9 +7,11 @@ edition = "2021"
|
|||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
codemp = { git = "ssh://git@github.com/codewithotherpeopleandchangenamelater/codemp.git", tag = "v0.5.1", features = ["global", "sync"] }
|
||||
mlua = { version = "0.9.0", features = ["module", "luajit"] }
|
||||
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"
|
||||
tracing = "0.1.37"
|
||||
lazy_static = "1.4.0"
|
||||
tokio = { version = "1.35.1", features = ["rt"] }
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
local codemp = require("libcodemp_nvim")
|
||||
local active_workspace = nil
|
||||
|
||||
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
|
||||
-- thread and requesting a new reference to the same controller from che global instance
|
||||
-- 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 _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
|
||||
local success, _ = pcall(_controller.poll, _controller)
|
||||
if success then
|
||||
|
@ -33,9 +35,10 @@ 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, target, delay)
|
||||
end, async, active_workspace, target, delay)
|
||||
end
|
||||
|
||||
local function split_without_trim(str, sep)
|
||||
|
@ -146,19 +149,29 @@ local available_colors = { -- TODO these are definitely not portable!
|
|||
"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(
|
||||
"Connect",
|
||||
"Login",
|
||||
function (args)
|
||||
codemp.connect(#args.args > 0 and args.args or nil)
|
||||
print(" ++ connected")
|
||||
codemp.login(args.fargs[1], args.fargs[2], args.fargs[3])
|
||||
print(" ++ logged in " .. args.args)
|
||||
end,
|
||||
{ nargs = "?" }
|
||||
{ nargs = "+" }
|
||||
)
|
||||
|
||||
vim.api.nvim_create_user_command(
|
||||
"Join",
|
||||
function (args)
|
||||
local controller = codemp.join(args.args)
|
||||
local controller = codemp.join_workspace(args.args)
|
||||
active_workspace = args.args
|
||||
|
||||
-- hook serverbound callbacks
|
||||
vim.api.nvim_create_autocmd({"CursorMoved", "CursorMovedI", "ModeChanged"}, {
|
||||
|
@ -207,7 +220,7 @@ vim.api.nvim_create_user_command(
|
|||
content = buffer_get_content(buf)
|
||||
-- TODO send content!
|
||||
end
|
||||
codemp.create(args.args, content)
|
||||
codemp.get_workspace(active_workspace):create_buffer(args.args, content)
|
||||
|
||||
print(" ++ created buffer " .. args.args)
|
||||
end,
|
||||
|
@ -228,7 +241,7 @@ vim.api.nvim_create_user_command(
|
|||
vim.api.nvim_buf_set_name(buffer, "codemp::" .. args.args)
|
||||
vim.api.nvim_set_current_buf(buffer)
|
||||
end
|
||||
local controller = codemp.attach(args.args)
|
||||
local controller = codemp.get_workspace(active_workspace):attach_buffer(args.args)
|
||||
|
||||
-- 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)
|
||||
if tick <= codemp_changed_tick[buf] then return end
|
||||
if buffer_mappings[buf] == nil then return true end -- unregister callback handler
|
||||
local content = ""
|
||||
if old_end_row < new_end_row and new_byte_len == 1 then
|
||||
content = "\n"
|
||||
else
|
||||
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')
|
||||
end
|
||||
controller:send(start_offset, start_offset + old_end_byte_len, content)
|
||||
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"
|
||||
-- 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,
|
||||
})
|
||||
|
||||
|
@ -260,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)
|
||||
|
@ -278,8 +298,8 @@ vim.api.nvim_create_user_command(
|
|||
local buffer = vim.api.nvim_get_current_buf()
|
||||
local name = buffer_mappings[buffer]
|
||||
if name ~= nil then
|
||||
local controller = codemp.get_buffer(name)
|
||||
codemp_changed_tick[buffer] = vim.api.nvim_buf_get_changedtick(buffer)
|
||||
local controller = codemp.get_workspace(active_workspace):get_buffer(name)
|
||||
codemp_changed_tick[buffer] = vim.api.nvim_buf_get_changedtick(buffer) + 1
|
||||
buffer_set_content(buffer, controller.content)
|
||||
print(" :: synched buffer " .. name)
|
||||
else
|
||||
|
@ -297,7 +317,7 @@ vim.api.nvim_create_user_command(
|
|||
local name = buffer_mappings[buffer]
|
||||
buffer_mappings[buffer] = nil
|
||||
buffer_mappings_reverse[name] = nil
|
||||
codemp.disconnect_buffer(name)
|
||||
codemp.get_workspace(active_workspace):disconnect_buffer(name)
|
||||
vim.api.nvim_buf_delete(buffer, {})
|
||||
print(" -- detached from buffer " .. name)
|
||||
end,
|
||||
|
|
173
src/lib.rs
173
src/lib.rs
|
@ -1,10 +1,43 @@
|
|||
use std::io::Write;
|
||||
use std::sync::{Arc, Mutex, mpsc};
|
||||
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;
|
||||
|
||||
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)]
|
||||
struct LuaCodempError(CodempError);
|
||||
|
@ -18,53 +51,63 @@ 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: Some(CodempRowCol {
|
||||
row: start_row, col: start_col,
|
||||
}),
|
||||
end: Some(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 },
|
||||
}
|
||||
}
|
||||
|
||||
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)]
|
||||
struct LuaOp(Op);
|
||||
impl LuaUserData for LuaOp { }
|
||||
|
||||
/// connect to remote server
|
||||
fn connect(_: &Lua, (host,): (Option<String>,)) -> LuaResult<()> {
|
||||
let addr = host.unwrap_or("http://127.0.0.1:50051".into());
|
||||
CODEMP_INSTANCE.connect(&addr)
|
||||
.map_err(LuaCodempError::from)?;
|
||||
Ok(())
|
||||
}
|
||||
#[derive(derive_more::From)]
|
||||
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.create(&name).await }).map_err(LuaCodempError::from)?)
|
||||
});
|
||||
|
||||
fn get_cursor(_: &Lua, _args: ()) -> LuaResult<LuaCursorController> {
|
||||
Ok(
|
||||
CODEMP_INSTANCE.get_cursor()
|
||||
.map_err(LuaCodempError::from)?
|
||||
.into()
|
||||
)
|
||||
}
|
||||
methods.add_method("attach_buffer", |_, this, (name,):(String,)| {
|
||||
Ok(LuaBufferController(STATE.rt().block_on(async { this.0.attach(&name).await }).map_err(LuaCodempError::from)?))
|
||||
});
|
||||
|
||||
fn get_buffer(_: &Lua, (path,): (String,)) -> LuaResult<LuaBufferController> {
|
||||
Ok(
|
||||
CODEMP_INSTANCE.get_buffer(&path)
|
||||
.map_err(LuaCodempError::from)?
|
||||
.into()
|
||||
)
|
||||
// TODO disconnect_buffer
|
||||
// TODO leave_workspace:w
|
||||
|
||||
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(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)]
|
||||
struct LuaCursorController(Arc<CodempCursorController>);
|
||||
impl LuaUserData for LuaCursorController {
|
||||
|
@ -80,7 +123,7 @@ impl LuaUserData for LuaCursorController {
|
|||
}
|
||||
});
|
||||
methods.add_method("poll", |_, this, ()| {
|
||||
CODEMP_INSTANCE.rt().block_on(this.0.poll())
|
||||
STATE.rt().block_on(this.0.poll())
|
||||
.map_err(LuaCodempError::from)?;
|
||||
Ok(())
|
||||
});
|
||||
|
@ -91,9 +134,9 @@ impl LuaUserData for LuaCursorController {
|
|||
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("user", |_, this| Ok(this.0.user.id.clone()));
|
||||
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);
|
||||
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())));
|
||||
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())));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// 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)]
|
||||
struct LuaBufferController(Arc<CodempBufferController>);
|
||||
impl LuaUserData for LuaBufferController {
|
||||
|
@ -149,7 +175,7 @@ impl LuaUserData for LuaBufferController {
|
|||
}
|
||||
});
|
||||
methods.add_method("poll", |_, this, ()| {
|
||||
CODEMP_INSTANCE.rt().block_on(this.0.poll())
|
||||
STATE.rt().block_on(this.0.poll())
|
||||
.map_err(LuaCodempError::from)?;
|
||||
Ok(())
|
||||
});
|
||||
|
@ -221,18 +247,6 @@ impl Write for LuaLoggerProducer {
|
|||
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> {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
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))))
|
||||
}
|
||||
|
||||
|
||||
|
||||
// define module and exports
|
||||
#[mlua::lua_module]
|
||||
fn libcodemp_nvim(lua: &Lua) -> LuaResult<LuaTable> {
|
||||
let exports = lua.create_table()?;
|
||||
|
||||
// core proto functions
|
||||
exports.set("connect", lua.create_function(connect)?)?;
|
||||
exports.set("join", lua.create_function(join)?)?;
|
||||
exports.set("create", lua.create_function(create)?)?;
|
||||
exports.set("attach", lua.create_function(attach)?)?;
|
||||
exports.set("login", lua.create_function(login)?)?;
|
||||
exports.set("join_workspace", lua.create_function(join_workspace)?)?;
|
||||
// state helpers
|
||||
exports.set("get_cursor", lua.create_function(get_cursor)?)?;
|
||||
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)?)?;
|
||||
exports.set("get_workspace", lua.create_function(get_workspace)?)?;
|
||||
// debug
|
||||
exports.set("id", lua.create_function(id)?)?;
|
||||
exports.set("setup_tracing", lua.create_function(setup_tracing)?)?;
|
||||
|
||||
Ok(exports)
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue