Simplify proto::Buffer to express its fields in terms of operations

This commit is contained in:
Antonio Scandurra 2022-08-23 12:40:20 +02:00
parent 01e3f118b2
commit ec48ffc9da
3 changed files with 25 additions and 49 deletions

View File

@ -70,6 +70,7 @@ pub struct Buffer {
diagnostics_timestamp: clock::Lamport,
file_update_count: usize,
completion_triggers: Vec<String>,
completion_triggers_timestamp: clock::Lamport,
deferred_ops: OperationQueue<Operation>,
}
@ -375,34 +376,6 @@ impl Buffer {
.collect::<Result<Vec<_>>>()?;
this.apply_ops(ops, cx)?;
for selection_set in message.selections {
let lamport_timestamp = clock::Lamport {
replica_id: selection_set.replica_id as ReplicaId,
value: selection_set.lamport_timestamp,
};
this.remote_selections.insert(
selection_set.replica_id as ReplicaId,
SelectionSet {
line_mode: selection_set.line_mode,
selections: proto::deserialize_selections(selection_set.selections),
lamport_timestamp,
},
);
this.text.lamport_clock.observe(lamport_timestamp);
}
let snapshot = this.snapshot();
let entries = proto::deserialize_diagnostics(message.diagnostics);
this.apply_diagnostic_update(
DiagnosticSet::from_sorted_entries(entries.iter().cloned(), &snapshot),
clock::Lamport {
replica_id: 0,
value: message.diagnostics_timestamp,
},
cx,
);
this.completion_triggers = message.completion_triggers;
Ok(this)
}
@ -412,6 +385,25 @@ impl Buffer {
.history()
.map(|op| proto::serialize_operation(&Operation::Buffer(op.clone())))
.chain(self.deferred_ops.iter().map(proto::serialize_operation))
.chain(self.remote_selections.iter().map(|(_, set)| {
proto::serialize_operation(&Operation::UpdateSelections {
selections: set.selections.clone(),
lamport_timestamp: set.lamport_timestamp,
line_mode: set.line_mode,
})
}))
.chain(Some(proto::serialize_operation(
&Operation::UpdateDiagnostics {
diagnostics: self.diagnostics.iter().cloned().collect(),
lamport_timestamp: self.diagnostics_timestamp,
},
)))
.chain(Some(proto::serialize_operation(
&Operation::UpdateCompletionTriggers {
triggers: self.completion_triggers.clone(),
lamport_timestamp: self.completion_triggers_timestamp,
},
)))
.collect::<Vec<_>>();
operations.sort_unstable_by_key(proto::lamport_timestamp_for_operation);
proto::Buffer {
@ -419,19 +411,6 @@ impl Buffer {
file: self.file.as_ref().map(|f| f.to_proto()),
base_text: self.base_text().to_string(),
operations,
selections: self
.remote_selections
.iter()
.map(|(replica_id, set)| proto::SelectionSet {
replica_id: *replica_id as u32,
selections: proto::serialize_selections(&set.selections),
lamport_timestamp: set.lamport_timestamp.value,
line_mode: set.line_mode,
})
.collect(),
diagnostics: proto::serialize_diagnostics(self.diagnostics.iter()),
diagnostics_timestamp: self.diagnostics_timestamp.value,
completion_triggers: self.completion_triggers.clone(),
line_ending: proto::serialize_line_ending(self.line_ending()) as i32,
}
}
@ -470,6 +449,7 @@ impl Buffer {
diagnostics_timestamp: Default::default(),
file_update_count: 0,
completion_triggers: Default::default(),
completion_triggers_timestamp: Default::default(),
deferred_ops: OperationQueue::new(),
}
}
@ -1517,11 +1497,11 @@ impl Buffer {
pub fn set_completion_triggers(&mut self, triggers: Vec<String>, cx: &mut ModelContext<Self>) {
self.completion_triggers = triggers.clone();
let lamport_timestamp = self.text.lamport_clock.tick();
self.completion_triggers_timestamp = self.text.lamport_clock.tick();
self.send_operation(
Operation::UpdateCompletionTriggers {
triggers,
lamport_timestamp,
lamport_timestamp: self.completion_triggers_timestamp,
},
cx,
);

View File

@ -813,11 +813,7 @@ message Buffer {
optional File file = 2;
string base_text = 3;
repeated Operation operations = 4;
repeated SelectionSet selections = 5;
repeated Diagnostic diagnostics = 6;
uint32 diagnostics_timestamp = 7;
repeated string completion_triggers = 8;
LineEnding line_ending = 9;
LineEnding line_ending = 5;
}
enum LineEnding {

View File

@ -6,4 +6,4 @@ pub use conn::Connection;
pub use peer::*;
mod macros;
pub const PROTOCOL_VERSION: u32 = 30;
pub const PROTOCOL_VERSION: u32 = 31;