2024-09-04 21:37:35 +02:00
|
|
|
//! ### Cursor
|
2024-09-05 01:45:48 +02:00
|
|
|
//! Represents the position of a remote user's cursor.
|
2024-08-05 19:15:30 +02:00
|
|
|
|
|
|
|
use codemp_proto as proto;
|
2024-08-05 22:44:46 +02:00
|
|
|
use uuid::Uuid;
|
2024-08-05 19:15:30 +02:00
|
|
|
|
2024-08-09 00:00:21 +02:00
|
|
|
#[cfg(feature = "python")]
|
|
|
|
use pyo3::prelude::*;
|
|
|
|
|
2024-09-04 21:37:35 +02:00
|
|
|
/// User cursor position in a buffer
|
2024-08-05 19:15:30 +02:00
|
|
|
#[derive(Clone, Debug, Default)]
|
2024-08-09 00:00:21 +02:00
|
|
|
#[cfg_attr(feature = "python", pyclass)]
|
|
|
|
// #[cfg_attr(feature = "python", pyo3(crate = "reexported::pyo3"))]
|
2024-08-05 19:15:30 +02:00
|
|
|
pub struct Cursor {
|
2024-09-05 01:45:48 +02:00
|
|
|
/// Cursor start position in buffer, as 0-indexed row-column tuple.
|
2024-08-05 19:15:30 +02:00
|
|
|
pub start: (i32, i32),
|
2024-09-05 01:45:48 +02:00
|
|
|
/// Cursor end position in buffer, as 0-indexed row-column tuple.
|
2024-08-05 19:15:30 +02:00
|
|
|
pub end: (i32, i32),
|
2024-09-05 01:45:48 +02:00
|
|
|
/// Path of buffer this cursor is on.
|
2024-08-05 19:15:30 +02:00
|
|
|
pub buffer: String,
|
2024-09-05 01:45:48 +02:00
|
|
|
/// User display name, if provided.
|
|
|
|
pub user: Option<Uuid>, // TODO this should be a string, not the UUID!
|
2024-08-05 19:15:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<proto::cursor::CursorPosition> for Cursor {
|
|
|
|
fn from(value: proto::cursor::CursorPosition) -> Self {
|
|
|
|
Self {
|
|
|
|
start: (value.start.row, value.start.col),
|
|
|
|
end: (value.end.row, value.end.col),
|
|
|
|
buffer: value.buffer.path,
|
|
|
|
user: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Cursor> for proto::cursor::CursorPosition {
|
|
|
|
fn from(value: Cursor) -> Self {
|
|
|
|
Self {
|
|
|
|
buffer: proto::files::BufferNode { path: value.buffer },
|
2024-08-05 22:44:46 +02:00
|
|
|
start: proto::cursor::RowCol {
|
|
|
|
row: value.start.0,
|
|
|
|
col: value.start.1,
|
|
|
|
},
|
|
|
|
end: proto::cursor::RowCol {
|
|
|
|
row: value.end.0,
|
|
|
|
col: value.end.1,
|
|
|
|
},
|
2024-08-05 19:15:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<proto::cursor::CursorEvent> for Cursor {
|
|
|
|
fn from(value: proto::cursor::CursorEvent) -> Self {
|
|
|
|
Self {
|
|
|
|
start: (value.position.start.row, value.position.start.col),
|
|
|
|
end: (value.position.end.row, value.position.end.col),
|
|
|
|
buffer: value.position.buffer.path,
|
2024-08-22 00:57:24 +02:00
|
|
|
user: Some(value.user.uuid()),
|
2024-08-05 19:15:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Cursor> for proto::cursor::CursorEvent {
|
|
|
|
fn from(value: Cursor) -> Self {
|
|
|
|
Self {
|
2024-08-22 00:57:24 +02:00
|
|
|
user: value.user.unwrap_or_default().into(),
|
2024-08-05 19:15:30 +02:00
|
|
|
position: proto::cursor::CursorPosition {
|
|
|
|
buffer: proto::files::BufferNode { path: value.buffer },
|
2024-08-05 22:44:46 +02:00
|
|
|
start: proto::cursor::RowCol {
|
|
|
|
row: value.start.0,
|
|
|
|
col: value.start.1,
|
|
|
|
},
|
|
|
|
end: proto::cursor::RowCol {
|
|
|
|
row: value.end.0,
|
|
|
|
col: value.end.1,
|
|
|
|
},
|
|
|
|
},
|
2024-08-05 19:15:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|