diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs index 19325d4341..22bb46d175 100644 --- a/crates/language/src/buffer.rs +++ b/crates/language/src/buffer.rs @@ -65,7 +65,7 @@ pub struct Buffer { syntax_tree: Mutex>, parsing_in_background: bool, parse_count: usize, - remote_selections: TreeMap]>>, + remote_selections: TreeMap, selections_update_count: usize, diagnostic_sets: Vec, diagnostics_update_count: usize, @@ -80,13 +80,19 @@ pub struct BufferSnapshot { tree: Option, diagnostic_sets: Vec, diagnostics_update_count: usize, - remote_selections: TreeMap]>>, + remote_selections: TreeMap, selections_update_count: usize, is_parsing: bool, language: Option>, parse_count: usize, } +#[derive(Clone, Debug)] +struct SelectionSet { + selections: Arc<[Selection]>, + lamport_timestamp: clock::Lamport, +} + #[derive(Clone, Debug, PartialEq, Eq)] pub struct GroupId { source: Arc, @@ -132,10 +138,6 @@ pub enum Operation { selections: Arc<[Selection]>, lamport_timestamp: clock::Lamport, }, - RemoveSelections { - replica_id: ReplicaId, - lamport_timestamp: clock::Lamport, - }, } #[derive(Clone, Debug, Eq, PartialEq)] @@ -311,7 +313,13 @@ impl Buffer { for selection_set in message.selections { this.remote_selections.insert( selection_set.replica_id as ReplicaId, - proto::deserialize_selections(selection_set.selections), + SelectionSet { + selections: proto::deserialize_selections(selection_set.selections), + lamport_timestamp: clock::Lamport { + replica_id: selection_set.replica_id as ReplicaId, + value: selection_set.lamport_timestamp, + }, + }, ); } let snapshot = this.snapshot(); @@ -357,9 +365,10 @@ impl Buffer { selections: self .remote_selections .iter() - .map(|(replica_id, selections)| proto::SelectionSet { + .map(|(replica_id, set)| proto::SelectionSet { replica_id: *replica_id as u32, - selections: proto::serialize_selections(selections), + selections: proto::serialize_selections(&set.selections), + lamport_timestamp: set.lamport_timestamp.value, }) .collect(), diagnostic_sets: self @@ -1134,8 +1143,13 @@ impl Buffer { cx: &mut ModelContext, ) { let lamport_timestamp = self.text.lamport_clock.tick(); - self.remote_selections - .insert(self.text.replica_id(), selections.clone()); + self.remote_selections.insert( + self.text.replica_id(), + SelectionSet { + selections: selections.clone(), + lamport_timestamp, + }, + ); self.send_operation( Operation::UpdateSelections { replica_id: self.text.replica_id(), @@ -1147,14 +1161,7 @@ impl Buffer { } pub fn remove_active_selections(&mut self, cx: &mut ModelContext) { - let lamport_timestamp = self.text.lamport_clock.tick(); - self.send_operation( - Operation::RemoveSelections { - replica_id: self.text.replica_id(), - lamport_timestamp, - }, - cx, - ); + self.set_active_selections(Arc::from([]), cx); } fn update_language_server(&mut self) { @@ -1378,7 +1385,6 @@ impl Buffer { Operation::UpdateSelections { selections, .. } => selections .iter() .all(|s| self.can_resolve(&s.start) && self.can_resolve(&s.end)), - Operation::RemoveSelections { .. } => true, } } @@ -1407,15 +1413,19 @@ impl Buffer { selections, lamport_timestamp, } => { - self.remote_selections.insert(replica_id, selections); - self.text.lamport_clock.observe(lamport_timestamp); - self.selections_update_count += 1; - } - Operation::RemoveSelections { - replica_id, - lamport_timestamp, - } => { - self.remote_selections.remove(&replica_id); + if let Some(set) = self.remote_selections.get(&replica_id) { + if set.lamport_timestamp > lamport_timestamp { + return; + } + } + + self.remote_selections.insert( + replica_id, + SelectionSet { + selections, + lamport_timestamp, + }, + ); self.text.lamport_clock.observe(lamport_timestamp); self.selections_update_count += 1; } @@ -1812,9 +1822,11 @@ impl BufferSnapshot { { self.remote_selections .iter() - .filter(|(replica_id, _)| **replica_id != self.text.replica_id()) - .map(move |(replica_id, selections)| { - let start_ix = match selections.binary_search_by(|probe| { + .filter(|(replica_id, set)| { + **replica_id != self.text.replica_id() && !set.selections.is_empty() + }) + .map(move |(replica_id, set)| { + let start_ix = match set.selections.binary_search_by(|probe| { probe .end .cmp(&range.start, self) @@ -1823,7 +1835,7 @@ impl BufferSnapshot { }) { Ok(ix) | Err(ix) => ix, }; - let end_ix = match selections.binary_search_by(|probe| { + let end_ix = match set.selections.binary_search_by(|probe| { probe .start .cmp(&range.end, self) @@ -1833,7 +1845,7 @@ impl BufferSnapshot { Ok(ix) | Err(ix) => ix, }; - (*replica_id, selections[start_ix..end_ix].iter()) + (*replica_id, set.selections[start_ix..end_ix].iter()) }) } @@ -2126,9 +2138,6 @@ impl operation_queue::Operation for Operation { } | Operation::UpdateSelections { lamport_timestamp, .. - } - | Operation::RemoveSelections { - lamport_timestamp, .. } => *lamport_timestamp, } } diff --git a/crates/language/src/proto.rs b/crates/language/src/proto.rs index 4898a7ccd4..cc283e6287 100644 --- a/crates/language/src/proto.rs +++ b/crates/language/src/proto.rs @@ -50,13 +50,6 @@ pub fn serialize_operation(operation: &Operation) -> proto::Operation { lamport_timestamp: lamport_timestamp.value, selections: serialize_selections(selections), }), - Operation::RemoveSelections { - replica_id, - lamport_timestamp, - } => proto::operation::Variant::RemoveSelections(proto::operation::RemoveSelections { - replica_id: *replica_id as u32, - lamport_timestamp: lamport_timestamp.value, - }), Operation::UpdateDiagnostics { provider_name, diagnostics, @@ -246,13 +239,6 @@ pub fn deserialize_operation(message: proto::Operation) -> Result { selections: Arc::from(selections), } } - proto::operation::Variant::RemoveSelections(message) => Operation::RemoveSelections { - replica_id: message.replica_id as ReplicaId, - lamport_timestamp: clock::Lamport { - replica_id: message.replica_id as ReplicaId, - value: message.lamport_timestamp, - }, - }, proto::operation::Variant::UpdateDiagnosticSet(message) => { let (provider_name, diagnostics) = deserialize_diagnostic_set( message diff --git a/crates/rpc/proto/zed.proto b/crates/rpc/proto/zed.proto index 6da6e6051b..43f648d972 100644 --- a/crates/rpc/proto/zed.proto +++ b/crates/rpc/proto/zed.proto @@ -287,6 +287,7 @@ message BufferFragment { message SelectionSet { uint32 replica_id = 1; repeated Selection selections = 2; + uint32 lamport_timestamp = 3; } message Selection { @@ -344,8 +345,7 @@ message Operation { Edit edit = 1; Undo undo = 2; UpdateSelections update_selections = 3; - RemoveSelections remove_selections = 4; - UpdateDiagnosticSet update_diagnostic_set = 5; + UpdateDiagnosticSet update_diagnostic_set = 4; } message Edit { @@ -371,11 +371,6 @@ message Operation { uint32 lamport_timestamp = 3; repeated Selection selections = 4; } - - message RemoveSelections { - uint32 replica_id = 1; - uint32 lamport_timestamp = 3; - } } message UndoMapEntry {