Skip to content

Commit c64a3dc

Browse files
committed
feat: new cursor structs
1 parent 6046a26 commit c64a3dc

File tree

3 files changed

+60
-45
lines changed

3 files changed

+60
-45
lines changed

src/api/cursor.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,21 @@ use pyo3::prelude::*;
1010
#[cfg_attr(feature = "py", pyclass(get_all))]
1111
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
1212
// #[cfg_attr(feature = "py", pyo3(crate = "reexported::pyo3"))]
13-
pub struct Cursor {
13+
pub struct CursorEvent {
1414
/// User who sent the cursor.
1515
pub user: String,
16+
/// Cursor position data
17+
pub cursor: Cursor,
18+
}
19+
20+
21+
/// An event that occurred about a user's cursor.
22+
#[derive(Clone, Debug, Default)]
23+
#[cfg_attr(feature = "js", napi_derive::napi(object))]
24+
#[cfg_attr(feature = "py", pyclass(get_all))]
25+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
26+
// #[cfg_attr(feature = "py", pyo3(crate = "reexported::pyo3"))]
27+
pub struct Cursor {
1628
/// Path of buffer this cursor is on
1729
pub buffer: String,
1830
/// The updated cursor selection.

src/cursor/controller.rs

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,11 @@ use tokio::sync::{mpsc, oneshot, watch};
77

88
use crate::{
99
api::{
10-
Controller, Cursor, Selection,
11-
controller::{AsyncReceiver, AsyncSender, ControllerCallback},
10+
controller::{AsyncReceiver, AsyncSender, ControllerCallback}, cursor::CursorEvent, Controller, Cursor
1211
},
1312
errors::ControllerResult,
1413
};
15-
use codemp_proto::{
16-
cursor::{CursorPosition, CursorUpdate, RowCol},
17-
files::BufferNode,
18-
};
14+
use codemp_proto::cursor::{CursorPosition, CursorUpdate, RowCol};
1915

2016
/// A [Controller] for asynchronously sending and receiving [Cursor] event.
2117
///
@@ -34,44 +30,49 @@ impl CursorController {
3430
#[derive(Debug)]
3531
pub(crate) struct CursorControllerInner {
3632
pub(crate) op: mpsc::UnboundedSender<CursorUpdate>,
37-
pub(crate) stream: mpsc::Sender<oneshot::Sender<Option<Cursor>>>,
33+
pub(crate) stream: mpsc::Sender<oneshot::Sender<Option<CursorEvent>>>,
3834
pub(crate) poll: mpsc::UnboundedSender<oneshot::Sender<()>>,
3935
pub(crate) callback: watch::Sender<Option<ControllerCallback<CursorController>>>,
4036
pub(crate) workspace_id: String,
4137
}
4238

4339
#[cfg_attr(feature = "async-trait", async_trait::async_trait)]
44-
impl Controller<Selection, Cursor> for CursorController {}
40+
impl Controller<Cursor, CursorEvent> for CursorController {}
4541

4642
#[cfg_attr(feature = "async-trait", async_trait::async_trait)]
47-
impl AsyncSender<Selection> for CursorController {
48-
fn send(&self, mut cursor: Selection) -> ControllerResult<()> {
49-
if cursor.start_row > cursor.end_row
50-
|| (cursor.start_row == cursor.end_row && cursor.start_col > cursor.end_col)
51-
{
52-
std::mem::swap(&mut cursor.start_row, &mut cursor.end_row);
53-
std::mem::swap(&mut cursor.start_col, &mut cursor.end_col);
43+
impl AsyncSender<Cursor> for CursorController {
44+
fn send(&self, mut cursor: Cursor) -> ControllerResult<()> {
45+
for sel in cursor.sel.iter_mut() {
46+
if sel.start_row > sel.end_row
47+
|| (sel.start_row == sel.end_row && sel.start_col > sel.end_col)
48+
{
49+
std::mem::swap(&mut sel.start_row, &mut sel.end_row);
50+
std::mem::swap(&mut sel.start_col, &mut sel.end_col);
51+
}
5452
}
5553

56-
Ok(self.0.op.send(CursorPosition {
57-
buffer: BufferNode {
58-
path: cursor.buffer,
59-
},
60-
start: RowCol {
61-
row: cursor.start_row,
62-
col: cursor.start_col,
63-
},
64-
end: RowCol {
65-
row: cursor.end_row,
66-
col: cursor.end_col,
67-
},
54+
Ok(self.0.op.send(CursorUpdate {
55+
buffer: cursor.buffer,
56+
cursors: cursor.sel
57+
.into_iter()
58+
.map(|x| CursorPosition {
59+
start: RowCol {
60+
row: x.start_row,
61+
col: x.start_col,
62+
},
63+
end: RowCol {
64+
row: x.end_row,
65+
col: x.end_col,
66+
}
67+
})
68+
.collect()
6869
})?)
6970
}
7071
}
7172

7273
#[cfg_attr(feature = "async-trait", async_trait::async_trait)]
73-
impl AsyncReceiver<Cursor> for CursorController {
74-
async fn try_recv(&self) -> ControllerResult<Option<Cursor>> {
74+
impl AsyncReceiver<CursorEvent> for CursorController {
75+
async fn try_recv(&self) -> ControllerResult<Option<CursorEvent>> {
7576
let (tx, rx) = oneshot::channel();
7677
self.0.stream.send(tx).await?;
7778
Ok(rx.await?)

src/cursor/worker.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct CursorWorker {
1616
workspace_id: String,
1717
op: mpsc::UnboundedReceiver<CursorUpdate>,
1818
map: Arc<dashmap::DashMap<Uuid, User>>,
19-
stream: mpsc::Receiver<oneshot::Sender<Option<Cursor>>>,
19+
stream: mpsc::Receiver<oneshot::Sender<Option<crate::api::cursor::CursorEvent>>>,
2020
poll: mpsc::UnboundedReceiver<oneshot::Sender<()>>,
2121
pollers: Vec<oneshot::Sender<()>>,
2222
store: std::collections::VecDeque<codemp_proto::cursor::CursorEvent>,
@@ -26,24 +26,26 @@ struct CursorWorker {
2626

2727
impl CursorWorker {
2828
#[tracing::instrument(skip(self, tx))]
29-
fn handle_recv(&mut self, tx: oneshot::Sender<Option<Cursor>>) {
29+
fn handle_recv(&mut self, tx: oneshot::Sender<Option<crate::api::cursor::CursorEvent>>) {
3030
tx.send(self.store.pop_front().and_then(|event| {
3131
let user_id = Uuid::from(event.user);
3232
if let Some(user_name) = self.map.get(&user_id).map(|u| u.name.clone()) {
33-
Some(Cursor {
33+
Some(crate::api::cursor::CursorEvent {
3434
user: user_name,
35-
buffer: event.position.buffer,
36-
sel: event
37-
.position
38-
.cursors
39-
.into_iter()
40-
.map(|x| Selection {
41-
start_row: x.start.row,
42-
start_col: x.start.col,
43-
end_row: x.end.row,
44-
end_col: x.end.col,
45-
})
46-
.collect(),
35+
cursor: Cursor {
36+
buffer: event.position.buffer,
37+
sel: event
38+
.position
39+
.cursors
40+
.into_iter()
41+
.map(|x| Selection {
42+
start_row: x.start.row,
43+
start_col: x.start.col,
44+
end_row: x.end.row,
45+
end_col: x.end.col,
46+
})
47+
.collect(),
48+
}
4749
})
4850
} else {
4951
tracing::warn!("received cursor for unknown user {user_id}");

0 commit comments

Comments
 (0)