diff --git a/client/nvim/src/main.rs b/client/nvim/src/main.rs index 5ce8695..10ac735 100644 --- a/client/nvim/src/main.rs +++ b/client/nvim/src/main.rs @@ -208,7 +208,12 @@ impl Handler for NeovimHandler { if let Err(e) = buf.clear_namespace(ns, 0, -1).await { error!("could not clear previous cursor highlight: {}", e); } - if let Err(e) = buf.add_highlight(ns, "ErrorMsg", cur.start.row-1, cur.start.col, cur.start.col+1).await { + if let Err(e) = buf.add_highlight( + ns, "ErrorMsg", + (cur.start().row-1) as i64, + cur.start().col as i64, + (cur.start().col+1) as i64 + ).await { error!("could not create highlight for cursor: {}", e); } } @@ -226,13 +231,13 @@ impl Handler for NeovimHandler { return Err(Value::from("not enough args")); } let path = default_empty_str(&args, 0); - let row = default_zero_number(&args, 1); - let col = default_zero_number(&args, 2); + let row = default_zero_number(&args, 1) as i32; + let col = default_zero_number(&args, 2) as i32; match self.cursor_controller(&path) { None => Err(Value::from("no path given")), Some(cur) => { - cur.send(&path, (row, col).into(), (0i64, 0i64).into()).await; + cur.send(&path, (row, col).into(), (0, 0).into()).await; Ok(Value::Nil) } } diff --git a/client/vscode/src/lib.rs b/client/vscode/src/lib.rs index 22c77cb..4993fe5 100644 --- a/client/vscode/src/lib.rs +++ b/client/vscode/src/lib.rs @@ -221,10 +221,10 @@ fn callback_cursor(mut cx: FunctionContext) -> JsResult { 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 as i32, op.start.col as i32)?) - .arg(tuple(&mut cx, op.end.row as i32, op.end.col as i32)?) + .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(()) }); diff --git a/proto/buffer.proto b/proto/buffer.proto index f6bc495..9b33b2e 100644 --- a/proto/buffer.proto +++ b/proto/buffer.proto @@ -6,8 +6,20 @@ service Buffer { rpc Edit (OperationRequest) returns (BufferResponse); rpc Create (BufferPayload) returns (BufferResponse); rpc Sync (BufferPayload) returns (BufferResponse); - rpc Cursor (CursorMov) returns (BufferResponse); - rpc Listen (BufferPayload) returns (stream CursorMov); + rpc Moved (Cursor) returns (BufferResponse); + rpc Listen (BufferPayload) returns (stream Cursor); +} + +message Position { + int32 row = 1; + int32 col = 2; +} + +message Cursor { + string user = 1; + string buffer = 2; + Position start = 3; + Position end = 4; } message RawOp { @@ -15,13 +27,6 @@ message RawOp { string user = 2; } -message CursorMov { - string user = 1; - string path = 2; - int64 row = 3; - int64 col = 4; -} - message OperationRequest { string path = 1; string hash = 2; diff --git a/server/src/buffer/service.rs b/server/src/buffer/service.rs index 6066d22..5785eda 100644 --- a/server/src/buffer/service.rs +++ b/server/src/buffer/service.rs @@ -5,13 +5,13 @@ use tonic::{Request, Response, Status}; use tokio_stream::{Stream, wrappers::ReceiverStream}; // TODO example used this? -use codemp::proto::{buffer_server::{Buffer, BufferServer}, RawOp, BufferPayload, BufferResponse, OperationRequest, CursorMov}; +use codemp::proto::{buffer_server::{Buffer, BufferServer}, RawOp, BufferPayload, BufferResponse, OperationRequest, Cursor}; use tracing::info; use super::actor::{BufferHandle, BufferStore}; type OperationStream = Pin> + Send>>; -type CursorStream = Pin> + Send>>; +type CursorStream = Pin> + Send>>; struct BufferMap { store: HashMap, @@ -34,7 +34,7 @@ impl BufferStore for BufferMap { pub struct BufferService { map: Arc>, - cursor: broadcast::Sender, + cursor: broadcast::Sender, } impl BufferService { @@ -88,7 +88,7 @@ impl Buffer for BufferService { Ok(Response::new(Box::pin(output_stream))) } - async fn cursor(&self, req:Request) -> Result, Status> { + async fn moved(&self, req:Request) -> Result, Status> { match self.cursor.send(req.into_inner()) { Ok(_) => Ok(Response::new(BufferResponse { accepted: true, content: None})), Err(e) => Err(Status::internal(format!("could not broadcast cursor update: {}", e))), diff --git a/src/client.rs b/src/client.rs index 105c458..e39a792 100644 --- a/src/client.rs +++ b/src/client.rs @@ -8,7 +8,7 @@ use uuid::Uuid; use crate::{ cursor::{CursorControllerHandle, CursorControllerWorker, CursorProvider}, operation::{OperationProcessor, OperationController}, - proto::{buffer_client::BufferClient, BufferPayload, OperationRequest, CursorMov}, errors::IgnorableError, + proto::{buffer_client::BufferClient, BufferPayload, OperationRequest}, errors::IgnorableError, }; #[derive(Clone)] @@ -61,7 +61,7 @@ impl CodempClient { } }, Some(op) = controller.wait() => { - _client.cursor(CursorMov::from(op)).await + _client.moved(op).await .unwrap_or_warn("could not send cursor update") } diff --git a/src/cursor.rs b/src/cursor.rs index 28dcc60..e1dad9c 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -3,55 +3,27 @@ use std::sync::Arc; use tokio::sync::{mpsc, broadcast}; use tonic::async_trait; -use crate::{proto::CursorMov, errors::IgnorableError}; +use crate::{proto::{Position, Cursor}, errors::IgnorableError}; -// TODO temp struct before we update protocol for real -#[derive(Clone, Debug, Default)] -pub struct Cursor { - pub user: String, - pub buffer: String, - pub start: Position, - pub end: Position, -} - -impl From:: for CursorMov { - fn from(cursor: Cursor) -> CursorMov { - CursorMov { - user: cursor.user, - path: cursor.buffer, - row: cursor.start.row, - col: cursor.start.col, - } - } -} - -impl From:: for Cursor { - fn from(cursor: CursorMov) -> Self { - Cursor { - user: cursor.user, - buffer: cursor.path, - start: (cursor.row, cursor.col).into(), - end: (0,0).into(), // TODO temp! - } - } - -} - -#[derive(Copy, Clone, Debug, Default)] -pub struct Position { - pub row: i64, - pub col: i64, -} - -impl From::<(i64, i64)> for Position { - fn from((row, col): (i64, i64)) -> Self { - Position { row, col } +impl From:: for (i32, i32) { + fn from(pos: Position) -> (i32, i32) { + (pos.row, pos.col) } } impl From::<(i32, i32)> for Position { fn from((row, col): (i32, i32)) -> Self { - Position { row: row as i64, col: col as i64 } + Position { row, col } + } +} + +impl Cursor { + pub fn start(&self) -> Position { + self.start.clone().unwrap_or((0, 0).into()) + } + + pub fn end(&self) -> Position { + self.end.clone().unwrap_or((0, 0).into()) } } @@ -85,7 +57,8 @@ impl CursorSubscriber for CursorControllerHandle { self.op.send(Cursor { user: self.uid.clone(), buffer: path.to_string(), - start, end + start: Some(start), + end: Some(end), }).await.unwrap_or_warn("could not send cursor op") } @@ -104,7 +77,7 @@ impl CursorSubscriber for CursorControllerHandle { pub(crate) trait CursorProvider where T : CursorSubscriber { fn subscribe(&self) -> T; - fn broadcast(&self, op: CursorMov); + fn broadcast(&self, op: Cursor); async fn wait(&mut self) -> Option; } @@ -130,8 +103,8 @@ impl CursorControllerWorker { #[async_trait] impl CursorProvider for CursorControllerWorker { - fn broadcast(&self, op: CursorMov) { - self.channel.send(op.into()).unwrap_or_warn("could not broadcast cursor event") + fn broadcast(&self, op: Cursor) { + self.channel.send(op).unwrap_or_warn("could not broadcast cursor event") } async fn wait(&mut self) -> Option {