Add cursor blink setting and replicate cursor shape to remote collaborators

This commit is contained in:
K Simmons 2022-10-15 15:08:21 -07:00
parent 318b923bac
commit 40c3e925ad
15 changed files with 128 additions and 63 deletions

View file

@ -111,9 +111,19 @@ pub enum IndentKind {
Tab,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
pub enum CursorShape {
#[default]
Bar,
Block,
Underscore,
Hollow,
}
#[derive(Clone, Debug)]
struct SelectionSet {
line_mode: bool,
cursor_shape: CursorShape,
selections: Arc<[Selection<Anchor>]>,
lamport_timestamp: clock::Lamport,
}
@ -161,6 +171,7 @@ pub enum Operation {
selections: Arc<[Selection<Anchor>]>,
lamport_timestamp: clock::Lamport,
line_mode: bool,
cursor_shape: CursorShape,
},
UpdateCompletionTriggers {
triggers: Vec<String>,
@ -395,6 +406,7 @@ impl Buffer {
selections: set.selections.clone(),
lamport_timestamp: set.lamport_timestamp,
line_mode: set.line_mode,
cursor_shape: set.cursor_shape,
})
}));
operations.push(proto::serialize_operation(&Operation::UpdateDiagnostics {
@ -1227,6 +1239,7 @@ impl Buffer {
&mut self,
selections: Arc<[Selection<Anchor>]>,
line_mode: bool,
cursor_shape: CursorShape,
cx: &mut ModelContext<Self>,
) {
let lamport_timestamp = self.text.lamport_clock.tick();
@ -1236,6 +1249,7 @@ impl Buffer {
selections: selections.clone(),
lamport_timestamp,
line_mode,
cursor_shape,
},
);
self.send_operation(
@ -1243,13 +1257,14 @@ impl Buffer {
selections,
line_mode,
lamport_timestamp,
cursor_shape,
},
cx,
);
}
pub fn remove_active_selections(&mut self, cx: &mut ModelContext<Self>) {
self.set_active_selections(Arc::from([]), false, cx);
self.set_active_selections(Arc::from([]), false, Default::default(), cx);
}
pub fn set_text<T>(&mut self, text: T, cx: &mut ModelContext<Self>) -> Option<clock::Local>
@ -1474,6 +1489,7 @@ impl Buffer {
selections,
lamport_timestamp,
line_mode,
cursor_shape,
} => {
if let Some(set) = self.remote_selections.get(&lamport_timestamp.replica_id) {
if set.lamport_timestamp > lamport_timestamp {
@ -1487,6 +1503,7 @@ impl Buffer {
selections,
lamport_timestamp,
line_mode,
cursor_shape,
},
);
self.text.lamport_clock.observe(lamport_timestamp);
@ -2236,6 +2253,7 @@ impl BufferSnapshot {
Item = (
ReplicaId,
bool,
CursorShape,
impl Iterator<Item = &Selection<Anchor>> + '_,
),
> + '_ {
@ -2259,6 +2277,7 @@ impl BufferSnapshot {
(
*replica_id,
set.line_mode,
set.cursor_shape,
set.selections[start_ix..end_ix].iter(),
)
})

View file

@ -1283,7 +1283,7 @@ fn test_random_collaboration(cx: &mut MutableAppContext, mut rng: StdRng) {
selections
);
active_selections.insert(replica_id, selections.clone());
buffer.set_active_selections(selections, false, cx);
buffer.set_active_selections(selections, false, Default::default(), cx);
});
mutation_count -= 1;
}
@ -1448,7 +1448,7 @@ fn test_random_collaboration(cx: &mut MutableAppContext, mut rng: StdRng) {
let buffer = buffer.read(cx).snapshot();
let actual_remote_selections = buffer
.remote_selections_in_range(Anchor::MIN..Anchor::MAX)
.map(|(replica_id, _, selections)| (replica_id, selections.collect::<Vec<_>>()))
.map(|(replica_id, _, _, selections)| (replica_id, selections.collect::<Vec<_>>()))
.collect::<Vec<_>>();
let expected_remote_selections = active_selections
.iter()

View file

@ -1,5 +1,6 @@
use crate::{
diagnostic_set::DiagnosticEntry, CodeAction, CodeLabel, Completion, Diagnostic, Language,
diagnostic_set::DiagnosticEntry, CodeAction, CodeLabel, Completion, CursorShape, Diagnostic,
Language,
};
use anyhow::{anyhow, Result};
use clock::ReplicaId;
@ -52,11 +53,13 @@ pub fn serialize_operation(operation: &crate::Operation) -> proto::Operation {
selections,
line_mode,
lamport_timestamp,
cursor_shape,
} => proto::operation::Variant::UpdateSelections(proto::operation::UpdateSelections {
replica_id: lamport_timestamp.replica_id as u32,
lamport_timestamp: lamport_timestamp.value,
selections: serialize_selections(selections),
line_mode: *line_mode,
cursor_shape: serialize_cursor_shape(cursor_shape) as i32,
}),
crate::Operation::UpdateDiagnostics {
diagnostics,
@ -125,6 +128,24 @@ pub fn serialize_selection(selection: &Selection<Anchor>) -> proto::Selection {
}
}
pub fn serialize_cursor_shape(cursor_shape: &CursorShape) -> proto::CursorShape {
match cursor_shape {
CursorShape::Bar => proto::CursorShape::CursorBar,
CursorShape::Block => proto::CursorShape::CursorBlock,
CursorShape::Underscore => proto::CursorShape::CursorUnderscore,
CursorShape::Hollow => proto::CursorShape::CursorHollow,
}
}
pub fn deserialize_cursor_shape(cursor_shape: proto::CursorShape) -> CursorShape {
match cursor_shape {
proto::CursorShape::CursorBar => CursorShape::Bar,
proto::CursorShape::CursorBlock => CursorShape::Block,
proto::CursorShape::CursorUnderscore => CursorShape::Underscore,
proto::CursorShape::CursorHollow => CursorShape::Hollow,
}
}
pub fn serialize_diagnostics<'a>(
diagnostics: impl IntoIterator<Item = &'a DiagnosticEntry<Anchor>>,
) -> Vec<proto::Diagnostic> {
@ -223,6 +244,10 @@ pub fn deserialize_operation(message: proto::Operation) -> Result<crate::Operati
},
selections: Arc::from(selections),
line_mode: message.line_mode,
cursor_shape: deserialize_cursor_shape(
proto::CursorShape::from_i32(message.cursor_shape)
.ok_or_else(|| anyhow!("Missing cursor shape"))?,
),
}
}
proto::operation::Variant::UpdateDiagnostics(message) => {