From cca36db3ed773608d14904716ce0ffefc1133e4e Mon Sep 17 00:00:00 2001 From: alemi Date: Wed, 16 Aug 2023 23:34:37 +0200 Subject: [PATCH] chore: removed rest of the project --- client/nvim/Cargo.toml | 16 -- client/nvim/codemp.lua | 190 -------------------- client/nvim/src/main.rs | 318 --------------------------------- client/vscode/.bundle.sh | 13 -- client/vscode/Cargo.toml | 22 --- client/vscode/package.json | 38 ---- client/vscode/src/extension.js | 146 --------------- client/vscode/src/lib.rs | 270 ---------------------------- server/Cargo.toml | 19 -- server/src/buffer/actor.rs | 101 ----------- server/src/buffer/mod.rs | 2 - server/src/buffer/service.rs | 113 ------------ server/src/cursor/mod.rs | 1 - server/src/cursor/service.rs | 52 ------ server/src/main.rs | 50 ------ 15 files changed, 1351 deletions(-) delete mode 100644 client/nvim/Cargo.toml delete mode 100644 client/nvim/codemp.lua delete mode 100644 client/nvim/src/main.rs delete mode 100755 client/vscode/.bundle.sh delete mode 100644 client/vscode/Cargo.toml delete mode 100644 client/vscode/package.json delete mode 100644 client/vscode/src/extension.js delete mode 100644 client/vscode/src/lib.rs delete mode 100644 server/Cargo.toml delete mode 100644 server/src/buffer/actor.rs delete mode 100644 server/src/buffer/mod.rs delete mode 100644 server/src/buffer/service.rs delete mode 100644 server/src/cursor/mod.rs delete mode 100644 server/src/cursor/service.rs delete mode 100644 server/src/main.rs diff --git a/client/nvim/Cargo.toml b/client/nvim/Cargo.toml deleted file mode 100644 index 59949b7..0000000 --- a/client/nvim/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -[package] -name = "codemp-nvim" -version = "0.2.0" -edition = "2021" - -[dependencies] -codemp = { path = "../.." } -tracing = "0.1" -tracing-subscriber = "0.3" -uuid = { version = "1.3.1", features = ["v4"] } -serde = "1" -serde_json = "1" -rmpv = "1" -clap = { version = "4.2.1", features = ["derive"] } -nvim-rs = { version = "0.5", features = ["use_tokio"] } -async-trait = "0.1.68" diff --git a/client/nvim/codemp.lua b/client/nvim/codemp.lua deleted file mode 100644 index 099c98a..0000000 --- a/client/nvim/codemp.lua +++ /dev/null @@ -1,190 +0,0 @@ -local BINARY = vim.g.codemp_binary or "./codemp-client-nvim" - -local M = {} - -M.jobid = nil -M.create = function(path, content) return vim.rpcrequest(M.jobid, "create", path, content) end -M.insert = function(path, txt, pos) return vim.rpcrequest(M.jobid, "insert", path, txt, pos) end -M.delete = function(path, pos, count) return vim.rpcrequest(M.jobid, "delete", path, pos, count) end -M.replace = function(path, txt) return vim.rpcrequest(M.jobid, "replace", path, txt) end -M.cursor = function(path, cur) return vim.rpcrequest(M.jobid, "cursor", path, cur[1][1], cur[1][2], cur[2][1], cur[2][2]) end -M.attach = function(path) return vim.rpcrequest(M.jobid, "attach", path) end -M.listen = function(path) return vim.rpcrequest(M.jobid, "listen", path) end -M.detach = function(path) return vim.rpcrequest(M.jobid, "detach", path) end - -local function cursor_offset() - local cursor = vim.api.nvim_win_get_cursor(0) - return vim.fn.line2byte(cursor[1]) + cursor[2] - 1 -end - -local codemp_autocmds = vim.api.nvim_create_augroup("CodempAuGroup", { clear = true }) - -local function get_cursor_range() - local mode = vim.fn.mode() - if mode == "" or mode == "s" or mode == "Vs" or mode == "V" or mode == "vs" or mode == "v" then - local start = vim.fn.getpos("'<") - local finish = vim.fn.getpos("'>") - return { - { start[2], start[3] }, - { finish[2], finish[3] } - } - else - local cursor = vim.api.nvim_win_get_cursor(0) - return { - { cursor[1], cursor[2] }, - { cursor[1], cursor[2] + 1 }, - } - end -end - -local function hook_callbacks(path, buffer) - vim.api.nvim_create_autocmd( - { "InsertCharPre" }, - { - callback = function(_) - pcall(M.insert, path, vim.v.char, cursor_offset()) -- TODO log errors - end, - buffer = buffer, - group = codemp_autocmds, - } - ) - vim.api.nvim_create_autocmd( - { "CursorMoved", "CompleteDone", "InsertEnter", "InsertLeave" }, - { - callback = function(args) - local lines = vim.api.nvim_buf_get_lines(args.buf, 0, -1, false) - pcall(M.replace, path, vim.fn.join(lines, "\n")) -- TODO log errors - pcall(M.cursor, path, get_cursor_range()) -- TODO log errors - end, - buffer = buffer, - group = codemp_autocmds, - } - ) - local last_line = 0 - vim.api.nvim_create_autocmd( - { "CursorMovedI" }, - { - callback = function(args) - local cursor = get_cursor_range() - pcall(M.cursor, path, cursor) -- TODO log errors - if cursor[1][1] == last_line then - return - end - last_line = cursor[1][1] - local lines = vim.api.nvim_buf_get_lines(args.buf, 0, -1, false) - pcall(M.replace, path, vim.fn.join(lines, "\n")) -- TODO log errors - end, - buffer = buffer, - group = codemp_autocmds, - } - ) - vim.keymap.set('i', '', function() pcall(M.delete, path, cursor_offset(), 1) return '' end, {expr = true, buffer = buffer}) -- TODO log errors - vim.keymap.set('i', '', function() pcall(M.delete, path, cursor_offset() + 1, 1) return '' end, {expr = true, buffer = buffer}) -- TODO log errors -end - -local function unhook_callbacks(buffer) - vim.api.nvim_clear_autocmds({ group = codemp_autocmds, buffer = buffer }) - vim.keymap.del('i', '', { buffer = buffer }) - vim.keymap.del('i', '', { buffer = buffer }) -end - -local function auto_address(addr) - if not string.find(addr, "://") then - addr = string.format("http://%s", addr) - end - if not string.find(addr, ":", 7) then -- skip first 7 chars because 'https://' - addr = string.format("%s:50051", addr) - end - return addr -end - -vim.api.nvim_create_user_command('Connect', - function(args) - if M.jobid ~= nil and M.jobid > 0 then - print("already connected, disconnect first") - return - end - local bin_args = { BINARY } - if #args.fargs > 0 then - table.insert(bin_args, "--host") - table.insert(bin_args, auto_address(args.fargs[1])) - end - if vim.g.codemp_remote_debug then - table.insert(bin_args, "--remote-debug") - table.insert(bin_args, vim.g.codemp_remote_debug) - end - if args.bang then - table.insert(bin_args, "--debug") - end - M.jobid = vim.fn.jobstart( - bin_args, - { - rpc = true, - on_stderr = function(_, data, _) - for _, line in pairs(data) do - print(line) - end - -- print(vim.fn.join(data, "\n")) - end, - stderr_buffered = false, - env = { RUST_BACKTRACE = 1 } - } - ) - if M.jobid <= 0 then - print("[!] could not start codemp client") - end - end, -{ nargs='?', bang=true }) - -vim.api.nvim_create_user_command('Stop', - function(_) - vim.fn.jobstop(M.jobid) - M.jobid = nil - end, -{ bang=true }) - -vim.api.nvim_create_user_command('Share', - function(args) - if M.jobid == nil or M.jobid <= 0 then - print("[!] connect to codemp server first") - return - end - local path = args.fargs[1] - local bufnr = vim.api.nvim_get_current_buf() - local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) - vim.opt.fileformat = "unix" - M.create(path, vim.fn.join(lines, "\n")) - hook_callbacks(path, bufnr) - M.attach(path) - M.listen(path) - end, -{ nargs=1 }) - -vim.api.nvim_create_user_command('Join', - function(args) - if M.jobid == nil or M.jobid <= 0 then - print("[!] connect to codemp server first") - return - end - local path = args.fargs[1] - local bufnr = vim.api.nvim_get_current_buf() - vim.opt.fileformat = "unix" - hook_callbacks(path, bufnr) - M.attach(path) - M.listen(path) - end, -{ nargs=1 }) - -vim.api.nvim_create_user_command('Detach', - function(args) - local bufnr = vim.api.nvim_get_current_buf() - if M.detach(args.fargs[1]) then - unhook_callbacks(bufnr) - print("[/] detached from buffer") - else - print("[!] error detaching from buffer") - end - end, -{ nargs=1 }) - -return M diff --git a/client/nvim/src/main.rs b/client/nvim/src/main.rs deleted file mode 100644 index a585225..0000000 --- a/client/nvim/src/main.rs +++ /dev/null @@ -1,318 +0,0 @@ -use std::sync::Arc; -use std::{net::TcpStream, sync::Mutex, collections::BTreeMap}; - -use codemp::client::CodempClient; -use codemp::controller::buffer::{OperationControllerHandle, OperationControllerSubscriber}; -use codemp::controller::cursor::{CursorControllerHandle, CursorSubscriber}; -use codemp::factory::OperationFactory; -use codemp::proto::buffer_client::BufferClient; -use codemp::tokio; - -use rmpv::Value; -use clap::Parser; - -use nvim_rs::{compat::tokio::Compat, create::tokio as create, Handler, Neovim}; -use tracing::{error, warn, debug, info}; - -#[derive(Clone)] -struct NeovimHandler { - client: CodempClient, - factories: Arc>>, - cursors: Arc>>, -} - -fn nullable_optional_str(args: &[Value], index: usize) -> Option { - Some(args.get(index)?.as_str()?.to_string()) -} - -fn default_empty_str(args: &[Value], index: usize) -> String { - nullable_optional_str(args, index).unwrap_or("".into()) -} - -fn nullable_optional_number(args: &[Value], index: usize) -> Option { - args.get(index)?.as_i64() -} - -fn default_zero_number(args: &[Value], index: usize) -> i64 { - nullable_optional_number(args, index).unwrap_or(0) -} - -impl NeovimHandler { - fn buffer_controller(&self, path: &String) -> Option { - Some(self.factories.lock().unwrap().get(path)?.clone()) - } - - fn cursor_controller(&self, path: &String) -> Option { - Some(self.cursors.lock().unwrap().get(path)?.clone()) - } -} - -#[async_trait::async_trait] -impl Handler for NeovimHandler { - type Writer = Compat; - - async fn handle_request( - &self, - name: String, - args: Vec, - nvim: Neovim>, - ) -> Result { - debug!("processing '{}' - {:?}", name, args); - match name.as_ref() { - "ping" => Ok(Value::from("pong")), - - "create" => { - if args.is_empty() { - return Err(Value::from("no path given")); - } - let path = default_empty_str(&args, 0); - let content = nullable_optional_str(&args, 1); - let mut c = self.client.clone(); - match c.create(path, content).await { - Ok(r) => match r { - true => Ok(Value::Nil), - false => Err(Value::from("rejected")), - }, - Err(e) => Err(Value::from(format!("could not create buffer: {}", e))), - } - }, - - "insert" => { - if args.len() < 3 { - return Err(Value::from("not enough arguments")); - } - let path = default_empty_str(&args, 0); - let txt = default_empty_str(&args, 1); - let mut pos = default_zero_number(&args, 2); - - if pos <= 0 { pos = 0 } // TODO wtf vim?? - - match self.buffer_controller(&path) { - None => Err(Value::from("no controller for given path")), - Some(controller) => { - controller.apply(controller.insert(&txt, pos as u64)).await; - Ok(Value::Nil) - }, - } - }, - - "delete" => { - if args.len() < 3 { - return Err(Value::from("not enough arguments")); - } - let path = default_empty_str(&args, 0); - let pos = default_zero_number(&args, 1) as u64; - let count = default_zero_number(&args, 2) as u64; - - match self.buffer_controller(&path) { - None => Err(Value::from("no controller for given path")), - Some(controller) => { - controller.apply(controller.delete(pos, count)).await; - Ok(Value::Nil) - } - } - }, - - "replace" => { - if args.len() < 2 { - return Err(Value::from("not enough arguments")); - } - let path = default_empty_str(&args, 0); - let txt = default_empty_str(&args, 1); - - match self.buffer_controller(&path) { - None => Err(Value::from("no controller for given path")), - Some(controller) => { - if let Some(op) = controller.replace(&txt) { - controller.apply(op).await; - } - Ok(Value::Nil) - } - } - }, - - "attach" => { - if args.is_empty() { - return Err(Value::from("no path given")); - } - let path = default_empty_str(&args, 0); - let buffer = match nvim.get_current_buf().await { - Ok(b) => b, - Err(e) => return Err(Value::from(format!("could not get current buffer: {}", e))), - }; - - let mut c = self.client.clone(); - - match c.attach(path.clone()).await { - Err(e) => Err(Value::from(format!("could not attach to stream: {}", e))), - Ok(controller) => { - let mut _controller = controller.clone(); - let lines : Vec = _controller.content().split('\n').map(|x| x.to_string()).collect(); - match buffer.set_lines(0, -1, false, lines).await { - Err(e) => Err(Value::from(format!("could not sync buffer: {}", e))), - Ok(()) => { - tokio::spawn(async move { - while let Some(_change) = _controller.poll().await { - let lines : Vec = _controller.content().split('\n').map(|x| x.to_string()).collect(); - // TODO only change lines affected! - if let Err(e) = buffer.set_lines(0, -1, false, lines).await { - error!("could not update buffer: {}", e); - } - } - }); - self.factories.lock().unwrap().insert(path, controller); - Ok(Value::Nil) - } - } - }, - } - }, - - "detach" => { - Err(Value::String("not implemented".into())) - // if args.is_empty() { - // return Err(Value::from("no path given")); - // } - // let path = default_empty_str(&args, 0); - // match self.buffer_controller(&path) { - // None => Err(Value::from("no controller for given path")), - // Some(controller) => Ok(Value::from(controller.stop())), - // } - }, - - "listen" => { - if args.is_empty() { - return Err(Value::from("no path given")); - } - let path = default_empty_str(&args, 0); - - let ns = nvim.create_namespace("Cursor").await - .map_err(|e| Value::from(format!("could not create namespace: {}", e)))?; - - let buf = nvim.get_current_buf().await - .map_err(|e| Value::from(format!("could not get current buf: {}", e)))?; - - let mut c = self.client.clone(); - match c.listen().await { - Err(e) => Err(Value::from(format!("could not listen cursors: {}", e))), - Ok(mut cursor) => { - self.cursors.lock().unwrap().insert(path, cursor.clone()); - debug!("spawning cursor processing worker"); - tokio::spawn(async move { - while let Some(cur) = cursor.poll().await { - if let Err(e) = buf.clear_namespace(ns, 0, -1).await { - error!("could not clear previous cursor highlight: {}", e); - } - let start = cur.start(); - let end = cur.end(); - let end_col = if start.row == end.row { - end.col - } else { - 0 // TODO what the fuck - }; - if let Err(e) = buf.add_highlight( - ns, "ErrorMsg", - start.row as i64 - 1, - start.col as i64, - end_col as i64 - ).await { - error!("could not create highlight for cursor: {}", e); - } - } - if let Err(e) = buf.clear_namespace(ns, 0, -1).await { - error!("could not clear previous cursor highlight: {}", e); - } - }); - Ok(Value::Nil) - }, - } - }, - - "cursor" => { - if args.len() < 3 { - return Err(Value::from("not enough args")); - } - let path = default_empty_str(&args, 0); - let row = default_zero_number(&args, 1) as i32; - let col = default_zero_number(&args, 2) as i32; - let row_end = default_zero_number(&args, 3) as i32; - let col_end = default_zero_number(&args, 4) as i32; - - match self.cursor_controller(&path) { - None => Err(Value::from("no path given")), - Some(cur) => { - cur.send(&path, (row, col).into(), (row_end, col_end).into()).await; - Ok(Value::Nil) - } - } - }, - - _ => Err(Value::from("unimplemented")), - } - } - - async fn handle_notify( - &self, - _name: String, - _args: Vec, - _nvim: Neovim>, - ) { - warn!("notify not handled"); - } -} - -#[derive(Parser, Debug)] -struct CliArgs { - /// server host to connect to - #[arg(long, default_value = "http://[::1]:50051")] - host: String, - - /// show debug level logs - #[arg(long, default_value_t = false)] - debug: bool, - - /// dump raw tracing logs into this TCP host - #[arg(long)] - remote_debug: Option, -} - - -#[tokio::main] -async fn main() -> Result<(), Box> { - let args = CliArgs::parse(); - - match args.remote_debug { - Some(host) => - tracing_subscriber::fmt() - .with_writer(Mutex::new(TcpStream::connect(host)?)) - .with_max_level(if args.debug { tracing::Level::DEBUG } else { tracing::Level::INFO }) - .init(), - - None => - tracing_subscriber::fmt() - .compact() - .without_time() - .with_ansi(false) - .with_writer(std::io::stderr) - .with_max_level(if args.debug { tracing::Level::DEBUG } else { tracing::Level::INFO }) - .init(), - } - - let client = BufferClient::connect(args.host.clone()).await?; - - let handler: NeovimHandler = NeovimHandler { - client: client.into(), - factories: Arc::new(Mutex::new(BTreeMap::new())), - cursors: Arc::new(Mutex::new(BTreeMap::new())), - }; - - let (_nvim, io_handler) = create::new_parent(handler).await; - - info!("++ codemp connected: {}", args.host); - - if let Err(e) = io_handler.await? { - error!("worker stopped with error: {}", e); - } - - Ok(()) -} diff --git a/client/vscode/.bundle.sh b/client/vscode/.bundle.sh deleted file mode 100755 index 2f38b63..0000000 --- a/client/vscode/.bundle.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/sh - -rm codemp.vsix -mkdir -p .vsix/extension -cp package.json .vsix/extension/package.json -cp README.md .vsix/extension/README.md -mkdir .vsix/extension/out -cp -R src/*.js .vsix/extension/out -cp -R codemp.node .vsix/extension/out/codemp.node -cd .vsix/ -zip ../codemp.vsix -r * -cd .. -rm -rf .vsix/ diff --git a/client/vscode/Cargo.toml b/client/vscode/Cargo.toml deleted file mode 100644 index 457e394..0000000 --- a/client/vscode/Cargo.toml +++ /dev/null @@ -1,22 +0,0 @@ -[package] -name = "codemp-vscode" -version = "0.0.1" -description = "VSCode extension for CodeMP" -edition = "2021" -exclude = ["index.node"] - -[lib] -crate-type = ["cdylib"] - -[dependencies] -codemp = { path = "../.." } -tracing = "0.1" -tracing-subscriber = "0.3" -uuid = { version = "1.3.1", features = ["v4"] } -once_cell = "1" -serde = { version = "1", features = ["derive"] } -serde_json = "1" -rmpv = "1" -clap = { version = "4.2.1", features = ["derive"] } -async-trait = "0.1.68" -neon = { version = "0.10.1", default-features = false, features = ["channel-api", "napi-6", "promise-api"] } diff --git a/client/vscode/package.json b/client/vscode/package.json deleted file mode 100644 index 86b36a1..0000000 --- a/client/vscode/package.json +++ /dev/null @@ -1,38 +0,0 @@ -{ - "name": "codemp-vscode", - "version": "0.0.1", - "description": "VSCode extension for CodeMP", - "main": "./out/extension.js", - "engines": { - "vscode": "^1.32.0" - }, - "scripts": { - "build": "cargo-cp-artifact --artifact cdylib codemp-vscode codemp.node -- cargo build --release --message-format=json-render-diagnostics", - "install": "npm run build", - "test": "cargo test" - }, - "devDependencies": { - "cargo-cp-artifact": "^0.1" - }, - "contributes": { - "commands": [ - { - "command": "codemp.connect", - "title": "Connect to CodeMP" - }, - { - "command": "codemp.join", - "title": "Join remote session" - }, - { - "command": "codemp.share", - "title": "Share local session" - } - ] - }, - "activationEvents": [ - "onCommand:codemp.connect", - "onCommand:codemp.join", - "onCommand:codemp.share" - ] -} diff --git a/client/vscode/src/extension.js b/client/vscode/src/extension.js deleted file mode 100644 index 93be77b..0000000 --- a/client/vscode/src/extension.js +++ /dev/null @@ -1,146 +0,0 @@ -const vscode = require("vscode"); -const codemp = require("./codemp.node"); - -var CLIENT = null -var CONTROLLER -var CURSOR -var DECORATION = null -var OP_CACHE = new Set() - -async function activate(context) { - context.subscriptions.push( - vscode.commands.registerCommand("codemp.connect", connect), - vscode.commands.registerCommand("codemp.share", share), - vscode.commands.registerCommand("codemp.join", join), - ) -} - -async function connect() { - let host = await vscode.window.showInputBox({prompt: "server host (default to http://fantabos.co:50051)"}) - if (host === undefined) return // user cancelled with ESC - if (host.length == 0) host = "http://fantabos.co:50051" - CLIENT = await codemp.connect(host) - vscode.window.showInformationMessage(`Connected to codemp @[${host}]`); -} - -async function share() { - if (CLIENT === null) { - vscode.window.showErrorMessage("No connected client"); - } - - let path = await vscode.window.showInputBox({prompt: "buffer uri (default to file path)"}) - if (path === undefined) return // user cancelled with ESC - if (path.length == 0) path = doc.uri.toString() - - let doc = vscode.window.activeTextEditor.document; - - try { - if (!await CLIENT.create(path, doc.getText())) { - vscode.window.showErrorMessage("Could not share buffer"); - } - - await _attach(path) - - vscode.window.showInformationMessage(`Shared document on buffer "${path}"`); - } catch (err) { - vscode.window.showErrorMessage("Error sharing: " + err) - } -} - -async function join() { - if (CLIENT === null) { - vscode.window.showErrorMessage("No connected client"); - } - - let path = await vscode.window.showInputBox({prompt: "buffer uri"}) - - try { - let controller = await _attach(path) - - vscode.window.showInformationMessage(`Joined buffer "${path}"`); - - let editor = vscode.window.activeTextEditor - - let range = new vscode.Range( - editor.document.positionAt(0), - editor.document.positionAt(editor.document.getText().length) - ) - let content = controller.content() - OP_CACHE.add((range, content)) - editor.edit(editBuilder => editBuilder.replace(range, content)) - } catch (err) { - vscode.window.showErrorMessage("error joining " + err) - } -} - -function _order_tuples(a, b) { - if (a[0] < b[0]) return (a, b) - if (a[0] > b[0]) return (b, a) - if (a[1] < b[1]) return (a, b) - return (b, a) -} - -async function _attach(path) { - let editor = vscode.window.activeTextEditor - let doc = editor.document; - - CURSOR = await CLIENT.listen() - CURSOR.callback((usr, path, start, end) => { - try { - if (DECORATION != null) { - DECORATION.dispose() - DECORATION = null - } - const range_start = new vscode.Position(start[0] - 1, start[1]); - const range_end = new vscode.Position(end[0] - 1, end[1]); - const decorationRange = new vscode.Range(range_start, range_end); - DECORATION = vscode.window.createTextEditorDecorationType( - {backgroundColor: 'red', color: 'white'} - ) - editor.setDecorations(DECORATION, [decorationRange]) - } catch (err) { - vscode.window.showErrorMessage("error setting cursor decoration: " + err) - } - }) - vscode.window.onDidChangeTextEditorSelection(async (e) => { - let buf = e.textEditor.document.uri.toString() - let selection = e.selections[0] // TODO there may be more than one cursor!! - let anchor = [selection.anchor.line+1, selection.anchor.character] - let position = [selection.active.line+1, selection.active.character+1] - // (anchor, position) = _order_tuples(anchor, position) - await CURSOR.send(buf, anchor, position) - }) - - CONTROLLER = await CLIENT.attach(path) - CONTROLLER.callback((start, end, text) => { - try { - let range = new vscode.Range( - editor.document.positionAt(start), - editor.document.positionAt(end) - ) - OP_CACHE.add((range, text)) - editor.edit(editBuilder => editBuilder.replace(range, text)) - } catch (err) { - vscode.window.showErrorMessage("could not set buffer: " + err) - } - }) - vscode.workspace.onDidChangeTextDocument(async (e) => { - if (e.document != doc) return - for (let change of e.contentChanges) { - if (OP_CACHE.has((change.range, change.text))) { - OP_CACHE.delete((change.range, change.text)) - continue - } - try { - await CONTROLLER.apply(change.rangeOffset, change.text, change.rangeOffset + change.rangeLength) - } catch (err) { - vscode.window.showErrorMessage("failed sending change: " + err) - } - } - }) - return CONTROLLER -} - -module.exports = { - activate, -} diff --git a/client/vscode/src/lib.rs b/client/vscode/src/lib.rs deleted file mode 100644 index 4c2794f..0000000 --- a/client/vscode/src/lib.rs +++ /dev/null @@ -1,270 +0,0 @@ -use std::sync::Arc; - -use neon::prelude::*; -use once_cell::sync::OnceCell; -use codemp::{ - cursor::controller::{CursorSubscriber, CursorControllerHandle}, - buffer::{ - controller::{OperationControllerHandle, OperationControllerSubscriber}, - client::CodempClient, - factory::OperationFactory, - }, - proto::buffer_client::BufferClient, -}; -use codemp::tokio::{runtime::Runtime, sync::Mutex}; - -fn runtime<'a, C: Context<'a>>(cx: &mut C) -> NeonResult<&'static Runtime> { - static RUNTIME: OnceCell = OnceCell::new(); - - RUNTIME.get_or_try_init(|| { - Runtime::new() - .or_else(|err| cx.throw_error(err.to_string())) - }) -} - -fn tuple<'a, C: Context<'a>>(cx: &mut C, a: i32, b: i32) -> NeonResult> { - let obj = cx.empty_array(); - let a_val = cx.number(a); - obj.set(cx, 0, a_val)?; - let b_val = cx.number(b); - obj.set(cx, 1, b_val)?; - Ok(obj) -} - -fn unpack_tuple<'a, C: Context<'a>>(cx: &mut C, arr: Handle<'a, JsArray>) -> NeonResult<(i32, i32)> { - Ok(( - arr.get::(cx, 0)?.value(cx) as i32, - arr.get::(cx, 1)?.value(cx) as i32, - )) -} - -struct ClientHandle(Arc>); -impl Finalize for ClientHandle {} - -fn connect(mut cx: FunctionContext) -> JsResult { - let host = cx.argument::(0).ok().map(|x| x.value(&mut cx)); - - let (deferred, promise) = cx.promise(); - let channel = cx.channel(); - - runtime(&mut cx)?.spawn(async move { - match BufferClient::connect(host.unwrap_or("".into())).await { - Err(e) => deferred.settle_with(&channel, move |mut cx| cx.throw_error::>(format!("{}", e))), - Ok(c) => deferred.settle_with(&channel, |mut cx| { - let obj = cx.empty_object(); - let boxed_value = cx.boxed(ClientHandle(Arc::new(Mutex::new(c.into())))); - obj.set(&mut cx, "boxed", boxed_value)?; - let method_create = JsFunction::new(&mut cx, create_client)?; - obj.set(&mut cx, "create", method_create)?; - let method_listen = JsFunction::new(&mut cx, listen_client)?; - obj.set(&mut cx, "listen", method_listen)?; - let method_attach = JsFunction::new(&mut cx, attach_client)?; - obj.set(&mut cx, "attach", method_attach)?; - Ok(obj) - }), - } - }); - - Ok(promise) -} - -fn create_client(mut cx: FunctionContext) -> JsResult { - let path = cx.argument::(0)?.value(&mut cx); - let content = cx.argument::(1).ok().map(|x| x.value(&mut cx)); - let this = cx.this(); - let boxed : Handle> = this.get(&mut cx, "boxed")?; - - let rc = boxed.0.clone(); - let (deferred, promise) = cx.promise(); - let channel = cx.channel(); - - runtime(&mut cx)?.spawn(async move { - match rc.lock().await.create(&path, content.as_deref()).await { - Ok(accepted) => deferred.settle_with(&channel, move |mut cx| Ok(cx.boolean(accepted))), - Err(e) => deferred.settle_with(&channel, move |mut cx| cx.throw_error::>(e.to_string())), - } - }); - - Ok(promise) -} - -fn listen_client(mut cx: FunctionContext) -> JsResult { - let this = cx.this(); - let boxed : Handle> = this.get(&mut cx, "boxed")?; - - let rc = boxed.0.clone(); - let (deferred, promise) = cx.promise(); - let channel = cx.channel(); - - runtime(&mut cx)?.spawn(async move { - match rc.lock().await.listen().await { - Ok(controller) => { - deferred.settle_with(&channel, move |mut cx| { - let obj = cx.empty_object(); - let boxed_value = cx.boxed(CursorEventsHandle(controller)); - obj.set(&mut cx, "boxed", boxed_value)?; - let callback_method = JsFunction::new(&mut cx, callback_cursor)?; - obj.set(&mut cx, "callback", callback_method)?; - let send_method = JsFunction::new(&mut cx, send_cursor)?; - obj.set(&mut cx, "send", send_method)?; - Ok(obj) - }) - }, - Err(e) => deferred.settle_with(&channel, move |mut cx| cx.throw_error::>(e.to_string())), - } - }); - - Ok(promise) -} - -fn attach_client(mut cx: FunctionContext) -> JsResult { - let this = cx.this(); - let boxed : Handle> = this.get(&mut cx, "boxed")?; - let path = cx.argument::(0)?.value(&mut cx); - - let rc = boxed.0.clone(); - let (deferred, promise) = cx.promise(); - let channel = cx.channel(); - - runtime(&mut cx)?.spawn(async move { - match rc.lock().await.attach(&path).await { - Ok(controller) => { - deferred.settle_with(&channel, move |mut cx| { - let obj = cx.empty_object(); - let boxed_value = cx.boxed(OperationControllerJs(controller)); - obj.set(&mut cx, "boxed", boxed_value)?; - let apply_method = JsFunction::new(&mut cx, apply_operation)?; - obj.set(&mut cx, "apply", apply_method)?; - let content_method = JsFunction::new(&mut cx, content_operation)?; - obj.set(&mut cx, "content", content_method)?; - let callback_method = JsFunction::new(&mut cx, callback_operation)?; - obj.set(&mut cx, "callback", callback_method)?; - Ok(obj) - }) - }, - Err(e) => deferred.settle_with(&channel, move |mut cx| cx.throw_error::>(e.to_string())), - } - }); - - Ok(promise) -} - - -struct OperationControllerJs(OperationControllerHandle); -impl Finalize for OperationControllerJs {} - -fn apply_operation(mut cx: FunctionContext) -> JsResult { - let this = cx.this(); - let boxed : Handle> = this.get(&mut cx, "boxed")?; - let skip = cx.argument::(0)?.value(&mut cx).round() as usize; - let text = cx.argument::(1)?.value(&mut cx); - let tail = cx.argument::(2)?.value(&mut cx).round() as usize; - - let rc = boxed.0.clone(); - let (deferred, promise) = cx.promise(); - let channel = cx.channel(); - - runtime(&mut cx)?.spawn(async move { - if let Some(op) = rc.delta(skip, text.as_str(), tail) { - rc.apply(op).await; - deferred.settle_with(&channel, move |mut cx| Ok(cx.boolean(true))); - } else { - deferred.settle_with(&channel, move |mut cx| Ok(cx.undefined())); - } - }); - - Ok(promise) -} - -fn content_operation(mut cx: FunctionContext) -> JsResult { - let this = cx.this(); - let boxed : Handle> = this.get(&mut cx, "boxed")?; - Ok(cx.string(boxed.0.content())) -} - -fn callback_operation(mut cx: FunctionContext) -> JsResult { - let this = cx.this(); - let boxed : Handle> = this.get(&mut cx, "boxed")?; - let callback = Arc::new(cx.argument::(0)?.root(&mut cx)); - - let rc = boxed.0.clone(); - let channel = cx.channel(); - - // TODO when garbage collecting OperationController stop this worker - runtime(&mut cx)?.spawn(async move { - while let Some(edit) = rc.poll().await { - let cb = callback.clone(); - channel.send(move |mut cx| { - cb.to_inner(&mut cx) - .call_with(&cx) - .arg(cx.number(edit.span.start as i32)) - .arg(cx.number(edit.span.end as i32)) - .arg(cx.string(edit.content)) - .apply::(&mut cx)?; - Ok(()) - }); - } - }); - - Ok(cx.undefined()) -} - -struct CursorEventsHandle(CursorControllerHandle); -impl Finalize for CursorEventsHandle {} - -fn callback_cursor(mut cx: FunctionContext) -> JsResult { - let this = cx.this(); - let boxed : Handle> = this.get(&mut cx, "boxed")?; - let callback = Arc::new(cx.argument::(0)?.root(&mut cx)); - - let rc = boxed.0.clone(); - let channel = cx.channel(); - - // TODO when garbage collecting OperationController stop this worker - runtime(&mut cx)?.spawn(async move { - while let Some(op) = rc.poll().await { - let cb = callback.clone(); - channel.send(move |mut cx| { - cb.to_inner(&mut cx) - .call_with(&cx) - .arg(cx.string(&op.user)) - .arg(cx.string(&op.buffer)) - .arg(tuple(&mut cx, op.start().row, op.start().col)?) - .arg(tuple(&mut cx, op.end().row, op.end().col)?) - .apply::(&mut cx)?; - Ok(()) - }); - } - - }); - - Ok(cx.undefined()) -} - -fn send_cursor(mut cx: FunctionContext) -> JsResult { - let this = cx.this(); - let boxed : Handle> = this.get(&mut cx, "boxed")?; - let path = cx.argument::(0)?.value(&mut cx); - let start_obj = cx.argument::(1)?; - let start = unpack_tuple(&mut cx, start_obj)?; - let end_obj = cx.argument::(2)?; - let end = unpack_tuple(&mut cx, end_obj)?; - - let rc = boxed.0.clone(); - let (deferred, promise) = cx.promise(); - let channel = cx.channel(); - - runtime(&mut cx)?.spawn(async move { - rc.send(&path, start.into(), end.into()).await; - deferred.settle_with(&channel, |mut cx| Ok(cx.undefined())) - }); - - Ok(promise) -} - -#[neon::main] -fn main(mut cx: ModuleContext) -> NeonResult<()> { - cx.export_function("connect", connect)?; - - Ok(()) -} diff --git a/server/Cargo.toml b/server/Cargo.toml deleted file mode 100644 index 5fa0755..0000000 --- a/server/Cargo.toml +++ /dev/null @@ -1,19 +0,0 @@ -[package] -name = "codemp-server" -version = "0.2.0" -edition = "2021" - -[dependencies] -codemp = { path = ".." } -tracing = "0.1" -tracing-subscriber = "0.3" -tonic = { version = "0.9", features = ["tls", "tls-roots"] } -prost = "0.11.8" -md5 = "0.7.0" -uuid = { version = "1.3.1", features = ["v4"] } -operational-transform = { version = "0.6", features = ["serde"] } -tokio = { version = "1.0", features = ["macros", "rt-multi-thread", "sync", "full"] } -tokio-stream = "0.1" -serde = "1" -serde_json = "1" -clap = { version = "4.2.1", features = ["derive"] } diff --git a/server/src/buffer/actor.rs b/server/src/buffer/actor.rs deleted file mode 100644 index d7eea4e..0000000 --- a/server/src/buffer/actor.rs +++ /dev/null @@ -1,101 +0,0 @@ -use codemp::{proto::{RawOp, OperationRequest}, errors::IgnorableError}; -use tokio::sync::{mpsc, broadcast, watch, oneshot}; -use tracing::{error, warn}; -// use md5::Digest; - -use operational_transform::OperationSeq; - -pub trait BufferStore { - fn get(&self, key: &T) -> Option<&BufferHandle>; - fn put(&mut self, key: T, handle: BufferHandle) -> Option; - - fn handle(&mut self, key: T, content: Option) { - let handle = BufferHandle::new(content); - self.put(key, handle); - } -} - -#[derive(Clone)] -pub struct BufferHandle { - pub edit: mpsc::Sender<(oneshot::Sender, OperationRequest)>, - events: broadcast::Sender, - // pub digest: watch::Receiver, - pub content: watch::Receiver, -} - -impl BufferHandle { - fn new(init: Option) -> Self { - let init_val = init.unwrap_or("".into()); - let (edits_tx, edits_rx) = mpsc::channel(64); // TODO hardcoded size - let (events_tx, _events_rx) = broadcast::channel(64); // TODO hardcoded size - // let (digest_tx, digest_rx) = watch::channel(md5::compute(&init_val)); - let (content_tx, content_rx) = watch::channel(init_val.clone()); - - let events_tx_clone = events_tx.clone(); - - tokio::spawn(async move { - let worker = BufferWorker { - store: init_val, - edits: edits_rx, - events: events_tx_clone, - // digest: digest_tx, - content: content_tx, - }; - worker.work().await - }); - - BufferHandle { - edit: edits_tx, - events: events_tx, - // digest: digest_rx, - content: content_rx, - } - } - - pub fn subscribe(&self) -> broadcast::Receiver { - self.events.subscribe() - } -} - -struct BufferWorker { - store: String, - edits: mpsc::Receiver<(oneshot::Sender, OperationRequest)>, - events: broadcast::Sender, - // digest: watch::Sender, - content: watch::Sender, -} - -impl BufferWorker { - async fn work(mut self) { - loop { - match self.edits.recv().await { - None => break warn!("channel closed"), - Some((ack, v)) => match serde_json::from_str::(&v.opseq) { - Err(e) => { - ack.send(false).unwrap_or_warn("could not reject undeserializable opseq"); - error!("could not deserialize opseq: {}", e); - }, - Ok(op) => match op.apply(&self.store) { - Err(e) => { - ack.send(false).unwrap_or_warn("could not reject unappliable opseq"); - error!("coult not apply OpSeq '{:?}' on '{}' : {}", v, self.store, e); // TODO - } - Ok(res) => { - self.store = res; - let msg = RawOp { - opseq: v.opseq, - user: v.user - }; - // if let Err(e) = self.digest.send(md5::compute(&self.store)) { - // error!("could not update digest: {}", e); - // } - ack.send(true).unwrap_or_warn("could not accept opseq"); - self.content.send(self.store.clone()).unwrap_or_warn("could not update content"); - self.events.send(msg).unwrap_or_warn("could not broadcast OpSeq"); - }, - } - }, - } - } - } -} diff --git a/server/src/buffer/mod.rs b/server/src/buffer/mod.rs deleted file mode 100644 index 5c38de9..0000000 --- a/server/src/buffer/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod actor; -pub mod service; diff --git a/server/src/buffer/service.rs b/server/src/buffer/service.rs deleted file mode 100644 index 5745981..0000000 --- a/server/src/buffer/service.rs +++ /dev/null @@ -1,113 +0,0 @@ -use std::{pin::Pin, sync::{Arc, RwLock}, collections::HashMap}; - -use tokio::sync::{mpsc, oneshot}; -use tonic::{Request, Response, Status}; - -use tokio_stream::{Stream, wrappers::ReceiverStream}; // TODO example used this? - -use codemp::proto::{buffer_server::Buffer, RawOp, BufferPayload, BufferResponse, OperationRequest, BufferEditResponse, BufferCreateResponse}; -use tracing::info; - -use super::actor::{BufferHandle, BufferStore}; - -type OperationStream = Pin> + Send>>; - -struct BufferMap { - store: HashMap, -} - -impl From::> for BufferMap { - fn from(value: HashMap) -> Self { - BufferMap { store: value } - } -} - -impl BufferStore for BufferMap { - fn get(&self, key: &String) -> Option<&BufferHandle> { - self.store.get(key) - } - fn put(&mut self, key: String, handle: BufferHandle) -> Option { - self.store.insert(key, handle) - } -} - -pub struct BufferService { - map: Arc>, -} - -impl Default for BufferService { - fn default() -> BufferService { - BufferService { - map: Arc::new(RwLock::new(HashMap::new().into())), - } - } -} - -#[tonic::async_trait] -impl Buffer for BufferService { - type AttachStream = OperationStream; - - async fn attach(&self, req: Request) -> Result, Status> { - let request = req.into_inner(); - let myself = request.user; - match self.map.read().unwrap().get(&request.path) { - None => Err(Status::not_found("path not found")), - Some(handle) => { - let (tx, rx) = mpsc::channel(128); - let mut sub = handle.subscribe(); - tokio::spawn(async move { - while let Ok(v) = sub.recv().await { - if v.user == myself { continue } - tx.send(Ok(v)).await.unwrap(); // TODO unnecessary channel? - } - }); - let output_stream = ReceiverStream::new(rx); - info!("registered new subscriber on buffer"); - Ok(Response::new(Box::pin(output_stream))) - }, - } - } - - async fn edit(&self, req:Request) -> Result, Status> { - let request = req.into_inner(); - let tx = match self.map.read().unwrap().get(&request.path) { - Some(handle) => { - // if format!("{:x}", *handle.digest.borrow()) != request.hash { - // return Ok(Response::new(BufferResponse { accepted : false } )); - // } - handle.edit.clone() - }, - None => return Err(Status::not_found("path not found")), - }; - info!("sending edit to buffer: {}", request.opseq); - let (ack, status) = oneshot::channel(); - match tx.send((ack, request)).await { - Err(e) => Err(Status::internal(format!("error sending edit to buffer actor: {}", e))), - Ok(()) => { - match status.await { - Ok(_accepted) => Ok(Response::new(BufferEditResponse { })), - Err(e) => Err(Status::internal(format!("error receiving edit result: {}", e))), - } - } - } - } - - async fn create(&self, req:Request) -> Result, Status> { - let request = req.into_inner(); - let _handle = self.map.write().unwrap().handle(request.path, request.content); - info!("created new buffer"); - Ok(Response::new(BufferCreateResponse { })) - } - - async fn sync(&self, req: Request) -> Result, Status> { - let request = req.into_inner(); - match self.map.read().unwrap().get(&request.path) { - None => Err(Status::not_found("requested buffer does not exist")), - Some(buf) => { - info!("synching buffer"); - let answ = BufferResponse { content: buf.content.borrow().clone() }; - Ok(Response::new(answ)) - } - } - } -} diff --git a/server/src/cursor/mod.rs b/server/src/cursor/mod.rs deleted file mode 100644 index 1f278a4..0000000 --- a/server/src/cursor/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod service; diff --git a/server/src/cursor/service.rs b/server/src/cursor/service.rs deleted file mode 100644 index b641c95..0000000 --- a/server/src/cursor/service.rs +++ /dev/null @@ -1,52 +0,0 @@ -use std::pin::Pin; - -use tokio::sync::{mpsc, broadcast}; -use tonic::{Request, Response, Status}; - -use tokio_stream::{Stream, wrappers::ReceiverStream}; // TODO example used this? - -use codemp::proto::{cursor_server::Cursor, UserIdentity, CursorPosition, MovedResponse}; -use tracing::info; - -type CursorStream = Pin> + Send>>; - -pub struct CursorService { - cursor: broadcast::Sender, -} - -#[tonic::async_trait] -impl Cursor for CursorService { - type ListenStream = CursorStream; - - async fn listen(&self, req: Request) -> Result, Status> { - let mut sub = self.cursor.subscribe(); - let myself = req.into_inner().id; - let (tx, rx) = mpsc::channel(128); - tokio::spawn(async move { - while let Ok(v) = sub.recv().await { - if v.user == myself { continue } - tx.send(Ok(v)).await.unwrap(); // TODO unnecessary channel? - } - }); - let output_stream = ReceiverStream::new(rx); - info!("registered new subscriber to cursor updates"); - Ok(Response::new(Box::pin(output_stream))) - } - - async fn moved(&self, req:Request) -> Result, Status> { - match self.cursor.send(req.into_inner()) { - Ok(_) => Ok(Response::new(MovedResponse { })), - Err(e) => Err(Status::internal(format!("could not broadcast cursor update: {}", e))), - } - } -} - -impl Default for CursorService { - fn default() -> Self { - let (cur_tx, _cur_rx) = broadcast::channel(64); // TODO hardcoded capacity - // TODO don't drop receiver because sending event when there are no receivers throws an error - CursorService { - cursor: cur_tx, - } - } -} diff --git a/server/src/main.rs b/server/src/main.rs deleted file mode 100644 index ec951e1..0000000 --- a/server/src/main.rs +++ /dev/null @@ -1,50 +0,0 @@ -//! # codemp Server -//! -//! The codemp server itself, in charge of handling the global state, merging operations from -//! all clients and synching everyone's cursor. -//! - -use clap::Parser; -use codemp::proto::buffer_server::BufferServer; -use codemp::proto::cursor_server::CursorServer; -use tracing::info; -use tonic::transport::Server; - -mod buffer; -mod cursor; - -use crate::buffer::service::BufferService; -use crate::cursor::service::CursorService; - -#[derive(Parser, Debug)] -struct CliArgs { - - /// address to listen on - #[arg(long, default_value = "[::1]:50051")] - host: String, - - /// enable debug log level - #[arg(long, default_value_t = false)] - debug: bool, -} - -#[tokio::main] -async fn main() -> Result<(), Box> { - let args = CliArgs::parse(); - - tracing_subscriber::fmt() - .with_writer(std::io::stdout) - .with_max_level(if args.debug { tracing::Level::DEBUG } else { tracing::Level::INFO }) - .init(); - - info!(">> codemp server"); - info!("binding on {}", args.host); - - Server::builder() - .add_service(BufferServer::new(BufferService::default())) - .add_service(CursorServer::new(CursorService::default())) - .serve(args.host.parse()?) - .await?; - - Ok(()) -}