2022-02-03 02:14:30 +00:00
|
|
|
use crate::{
|
|
|
|
diagnostic_set::DiagnosticEntry, Completion, CompletionLabel, Diagnostic, Language, Operation,
|
|
|
|
};
|
2021-11-01 21:05:19 +00:00
|
|
|
use anyhow::{anyhow, Result};
|
|
|
|
use clock::ReplicaId;
|
2022-01-05 09:29:29 +00:00
|
|
|
use collections::HashSet;
|
2021-11-01 22:28:37 +00:00
|
|
|
use lsp::DiagnosticSeverity;
|
2021-11-01 21:05:19 +00:00
|
|
|
use rpc::proto;
|
2021-12-13 10:38:38 +00:00
|
|
|
use std::sync::Arc;
|
2021-11-30 19:26:12 +00:00
|
|
|
use text::*;
|
2021-11-01 21:05:19 +00:00
|
|
|
|
2022-01-24 17:47:41 +00:00
|
|
|
pub use proto::{Buffer, BufferState, SelectionSet};
|
2021-11-01 21:05:19 +00:00
|
|
|
|
|
|
|
pub fn serialize_operation(operation: &Operation) -> proto::Operation {
|
|
|
|
proto::Operation {
|
|
|
|
variant: Some(match operation {
|
2021-11-30 19:26:12 +00:00
|
|
|
Operation::Buffer(text::Operation::Edit(edit)) => {
|
2021-11-01 21:05:19 +00:00
|
|
|
proto::operation::Variant::Edit(serialize_edit_operation(edit))
|
|
|
|
}
|
2021-11-30 19:26:12 +00:00
|
|
|
Operation::Buffer(text::Operation::Undo {
|
2021-11-01 21:05:19 +00:00
|
|
|
undo,
|
|
|
|
lamport_timestamp,
|
2021-11-01 22:28:37 +00:00
|
|
|
}) => proto::operation::Variant::Undo(proto::operation::Undo {
|
2021-11-01 21:05:19 +00:00
|
|
|
replica_id: undo.id.replica_id as u32,
|
|
|
|
local_timestamp: undo.id.value,
|
|
|
|
lamport_timestamp: lamport_timestamp.value,
|
|
|
|
ranges: undo
|
|
|
|
.ranges
|
|
|
|
.iter()
|
|
|
|
.map(|r| proto::Range {
|
|
|
|
start: r.start.0 as u64,
|
|
|
|
end: r.end.0 as u64,
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
counts: undo
|
|
|
|
.counts
|
|
|
|
.iter()
|
2022-01-05 02:06:16 +00:00
|
|
|
.map(|(edit_id, count)| proto::UndoCount {
|
2021-11-01 21:05:19 +00:00
|
|
|
replica_id: edit_id.replica_id as u32,
|
|
|
|
local_timestamp: edit_id.value,
|
|
|
|
count: *count,
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
version: From::from(&undo.version),
|
|
|
|
}),
|
2021-12-11 07:29:34 +00:00
|
|
|
Operation::UpdateSelections {
|
|
|
|
replica_id,
|
2021-11-01 21:05:19 +00:00
|
|
|
selections,
|
|
|
|
lamport_timestamp,
|
2021-12-11 07:29:34 +00:00
|
|
|
} => proto::operation::Variant::UpdateSelections(proto::operation::UpdateSelections {
|
|
|
|
replica_id: *replica_id as u32,
|
2021-11-01 21:05:19 +00:00
|
|
|
lamport_timestamp: lamport_timestamp.value,
|
2021-12-13 10:38:38 +00:00
|
|
|
selections: serialize_selections(selections),
|
2021-11-01 21:05:19 +00:00
|
|
|
}),
|
2021-12-09 15:38:46 +00:00
|
|
|
Operation::UpdateDiagnostics {
|
2021-12-23 22:21:10 +00:00
|
|
|
diagnostics,
|
2021-12-09 15:38:46 +00:00
|
|
|
lamport_timestamp,
|
2022-01-04 15:11:29 +00:00
|
|
|
} => proto::operation::Variant::UpdateDiagnostics(proto::UpdateDiagnostics {
|
2021-12-09 15:38:46 +00:00
|
|
|
replica_id: lamport_timestamp.replica_id as u32,
|
|
|
|
lamport_timestamp: lamport_timestamp.value,
|
2022-01-04 15:11:29 +00:00
|
|
|
diagnostics: serialize_diagnostics(diagnostics.iter()),
|
2021-12-09 15:38:46 +00:00
|
|
|
}),
|
2022-02-02 13:07:41 +00:00
|
|
|
Operation::UpdateCompletionTriggers { triggers } => {
|
|
|
|
proto::operation::Variant::UpdateCompletionTriggers(
|
|
|
|
proto::operation::UpdateCompletionTriggers {
|
|
|
|
triggers: triggers.clone(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2021-11-01 21:05:19 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn serialize_edit_operation(operation: &EditOperation) -> proto::operation::Edit {
|
|
|
|
let ranges = operation
|
|
|
|
.ranges
|
|
|
|
.iter()
|
|
|
|
.map(|range| proto::Range {
|
|
|
|
start: range.start.0 as u64,
|
|
|
|
end: range.end.0 as u64,
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
proto::operation::Edit {
|
|
|
|
replica_id: operation.timestamp.replica_id as u32,
|
|
|
|
local_timestamp: operation.timestamp.local,
|
|
|
|
lamport_timestamp: operation.timestamp.lamport,
|
|
|
|
version: From::from(&operation.version),
|
|
|
|
ranges,
|
|
|
|
new_text: operation.new_text.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 02:06:16 +00:00
|
|
|
pub fn serialize_undo_map_entry(
|
|
|
|
(edit_id, counts): (&clock::Local, &[(clock::Local, u32)]),
|
|
|
|
) -> proto::UndoMapEntry {
|
|
|
|
proto::UndoMapEntry {
|
|
|
|
replica_id: edit_id.replica_id as u32,
|
|
|
|
local_timestamp: edit_id.value,
|
|
|
|
counts: counts
|
|
|
|
.iter()
|
|
|
|
.map(|(undo_id, count)| proto::UndoCount {
|
|
|
|
replica_id: undo_id.replica_id as u32,
|
|
|
|
local_timestamp: undo_id.value,
|
|
|
|
count: *count,
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn serialize_buffer_fragment(fragment: &text::Fragment) -> proto::BufferFragment {
|
|
|
|
proto::BufferFragment {
|
|
|
|
replica_id: fragment.insertion_timestamp.replica_id as u32,
|
|
|
|
local_timestamp: fragment.insertion_timestamp.local,
|
|
|
|
lamport_timestamp: fragment.insertion_timestamp.lamport,
|
|
|
|
insertion_offset: fragment.insertion_offset as u32,
|
|
|
|
len: fragment.len as u32,
|
|
|
|
visible: fragment.visible,
|
|
|
|
deletions: fragment
|
|
|
|
.deletions
|
|
|
|
.iter()
|
|
|
|
.map(|clock| proto::VectorClockEntry {
|
|
|
|
replica_id: clock.replica_id as u32,
|
|
|
|
timestamp: clock.value,
|
|
|
|
})
|
|
|
|
.collect(),
|
2022-01-05 09:29:29 +00:00
|
|
|
max_undos: From::from(&fragment.max_undos),
|
2022-01-05 02:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-11 07:29:34 +00:00
|
|
|
pub fn serialize_selections(selections: &Arc<[Selection<Anchor>]>) -> Vec<proto::Selection> {
|
|
|
|
selections
|
|
|
|
.iter()
|
|
|
|
.map(|selection| proto::Selection {
|
|
|
|
id: selection.id as u64,
|
|
|
|
start: Some(serialize_anchor(&selection.start)),
|
|
|
|
end: Some(serialize_anchor(&selection.end)),
|
|
|
|
reversed: selection.reversed,
|
|
|
|
})
|
|
|
|
.collect()
|
2021-11-01 21:05:19 +00:00
|
|
|
}
|
|
|
|
|
2022-01-04 15:11:29 +00:00
|
|
|
pub fn serialize_diagnostics<'a>(
|
2021-12-23 22:21:10 +00:00
|
|
|
diagnostics: impl IntoIterator<Item = &'a DiagnosticEntry<Anchor>>,
|
2022-01-04 15:11:29 +00:00
|
|
|
) -> Vec<proto::Diagnostic> {
|
|
|
|
diagnostics
|
|
|
|
.into_iter()
|
|
|
|
.map(|entry| proto::Diagnostic {
|
|
|
|
start: Some(serialize_anchor(&entry.range.start)),
|
|
|
|
end: Some(serialize_anchor(&entry.range.end)),
|
|
|
|
message: entry.diagnostic.message.clone(),
|
|
|
|
severity: match entry.diagnostic.severity {
|
|
|
|
DiagnosticSeverity::ERROR => proto::diagnostic::Severity::Error,
|
|
|
|
DiagnosticSeverity::WARNING => proto::diagnostic::Severity::Warning,
|
|
|
|
DiagnosticSeverity::INFORMATION => proto::diagnostic::Severity::Information,
|
|
|
|
DiagnosticSeverity::HINT => proto::diagnostic::Severity::Hint,
|
|
|
|
_ => proto::diagnostic::Severity::None,
|
|
|
|
} as i32,
|
|
|
|
group_id: entry.diagnostic.group_id as u64,
|
|
|
|
is_primary: entry.diagnostic.is_primary,
|
|
|
|
is_valid: entry.diagnostic.is_valid,
|
|
|
|
code: entry.diagnostic.code.clone(),
|
|
|
|
is_disk_based: entry.diagnostic.is_disk_based,
|
|
|
|
})
|
|
|
|
.collect()
|
2021-12-09 15:38:46 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 11:27:13 +00:00
|
|
|
pub fn serialize_anchor(anchor: &Anchor) -> proto::Anchor {
|
2021-12-09 15:38:46 +00:00
|
|
|
proto::Anchor {
|
|
|
|
replica_id: anchor.timestamp.replica_id as u32,
|
|
|
|
local_timestamp: anchor.timestamp.value,
|
|
|
|
offset: anchor.offset as u64,
|
|
|
|
bias: match anchor.bias {
|
|
|
|
Bias::Left => proto::Bias::Left as i32,
|
|
|
|
Bias::Right => proto::Bias::Right as i32,
|
|
|
|
},
|
2021-11-01 22:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-01 21:05:19 +00:00
|
|
|
pub fn deserialize_operation(message: proto::Operation) -> Result<Operation> {
|
|
|
|
Ok(
|
|
|
|
match message
|
|
|
|
.variant
|
|
|
|
.ok_or_else(|| anyhow!("missing operation variant"))?
|
|
|
|
{
|
|
|
|
proto::operation::Variant::Edit(edit) => {
|
2021-11-30 19:26:12 +00:00
|
|
|
Operation::Buffer(text::Operation::Edit(deserialize_edit_operation(edit)))
|
2021-11-01 21:05:19 +00:00
|
|
|
}
|
2021-11-30 19:26:12 +00:00
|
|
|
proto::operation::Variant::Undo(undo) => Operation::Buffer(text::Operation::Undo {
|
2021-11-01 21:05:19 +00:00
|
|
|
lamport_timestamp: clock::Lamport {
|
|
|
|
replica_id: undo.replica_id as ReplicaId,
|
|
|
|
value: undo.lamport_timestamp,
|
|
|
|
},
|
|
|
|
undo: UndoOperation {
|
|
|
|
id: clock::Local {
|
|
|
|
replica_id: undo.replica_id as ReplicaId,
|
|
|
|
value: undo.local_timestamp,
|
|
|
|
},
|
|
|
|
counts: undo
|
|
|
|
.counts
|
|
|
|
.into_iter()
|
|
|
|
.map(|c| {
|
|
|
|
(
|
|
|
|
clock::Local {
|
|
|
|
replica_id: c.replica_id as ReplicaId,
|
|
|
|
value: c.local_timestamp,
|
|
|
|
},
|
|
|
|
c.count,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
ranges: undo
|
|
|
|
.ranges
|
|
|
|
.into_iter()
|
|
|
|
.map(|r| FullOffset(r.start as usize)..FullOffset(r.end as usize))
|
|
|
|
.collect(),
|
|
|
|
version: undo.version.into(),
|
|
|
|
},
|
2021-11-01 22:28:37 +00:00
|
|
|
}),
|
2021-11-01 21:05:19 +00:00
|
|
|
proto::operation::Variant::UpdateSelections(message) => {
|
2021-12-09 15:38:46 +00:00
|
|
|
let selections = message
|
2021-11-01 21:05:19 +00:00
|
|
|
.selections
|
2021-12-09 15:38:46 +00:00
|
|
|
.into_iter()
|
|
|
|
.filter_map(|selection| {
|
|
|
|
Some(Selection {
|
2021-11-01 21:05:19 +00:00
|
|
|
id: selection.id as usize,
|
2021-12-09 15:38:46 +00:00
|
|
|
start: deserialize_anchor(selection.start?)?,
|
|
|
|
end: deserialize_anchor(selection.end?)?,
|
2021-11-01 21:05:19 +00:00
|
|
|
reversed: selection.reversed,
|
|
|
|
goal: SelectionGoal::None,
|
2021-12-09 15:38:46 +00:00
|
|
|
})
|
2021-11-01 21:05:19 +00:00
|
|
|
})
|
2021-12-09 15:38:46 +00:00
|
|
|
.collect::<Vec<_>>();
|
2021-11-01 21:05:19 +00:00
|
|
|
|
2021-12-11 07:29:34 +00:00
|
|
|
Operation::UpdateSelections {
|
|
|
|
replica_id: message.replica_id as ReplicaId,
|
2021-11-01 21:05:19 +00:00
|
|
|
lamport_timestamp: clock::Lamport {
|
|
|
|
replica_id: message.replica_id as ReplicaId,
|
|
|
|
value: message.lamport_timestamp,
|
|
|
|
},
|
|
|
|
selections: Arc::from(selections),
|
2021-12-11 07:29:34 +00:00
|
|
|
}
|
2021-11-01 22:28:37 +00:00
|
|
|
}
|
2022-01-04 15:11:29 +00:00
|
|
|
proto::operation::Variant::UpdateDiagnostics(message) => Operation::UpdateDiagnostics {
|
|
|
|
diagnostics: deserialize_diagnostics(message.diagnostics),
|
|
|
|
lamport_timestamp: clock::Lamport {
|
|
|
|
replica_id: message.replica_id as ReplicaId,
|
|
|
|
value: message.lamport_timestamp,
|
|
|
|
},
|
|
|
|
},
|
2022-02-02 13:07:41 +00:00
|
|
|
proto::operation::Variant::UpdateCompletionTriggers(message) => {
|
|
|
|
Operation::UpdateCompletionTriggers {
|
|
|
|
triggers: message.triggers,
|
|
|
|
}
|
|
|
|
}
|
2021-11-01 21:05:19 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deserialize_edit_operation(edit: proto::operation::Edit) -> EditOperation {
|
|
|
|
let ranges = edit
|
|
|
|
.ranges
|
|
|
|
.into_iter()
|
|
|
|
.map(|range| FullOffset(range.start as usize)..FullOffset(range.end as usize))
|
|
|
|
.collect();
|
|
|
|
EditOperation {
|
|
|
|
timestamp: InsertionTimestamp {
|
|
|
|
replica_id: edit.replica_id as ReplicaId,
|
|
|
|
local: edit.local_timestamp,
|
|
|
|
lamport: edit.lamport_timestamp,
|
|
|
|
},
|
|
|
|
version: edit.version.into(),
|
|
|
|
ranges,
|
|
|
|
new_text: edit.new_text,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 02:06:16 +00:00
|
|
|
pub fn deserialize_undo_map_entry(
|
|
|
|
entry: proto::UndoMapEntry,
|
|
|
|
) -> (clock::Local, Vec<(clock::Local, u32)>) {
|
|
|
|
(
|
|
|
|
clock::Local {
|
|
|
|
replica_id: entry.replica_id as u16,
|
|
|
|
value: entry.local_timestamp,
|
|
|
|
},
|
|
|
|
entry
|
|
|
|
.counts
|
|
|
|
.into_iter()
|
|
|
|
.map(|undo_count| {
|
|
|
|
(
|
|
|
|
clock::Local {
|
|
|
|
replica_id: undo_count.replica_id as u16,
|
|
|
|
value: undo_count.local_timestamp,
|
|
|
|
},
|
|
|
|
undo_count.count,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deserialize_buffer_fragment(
|
|
|
|
message: proto::BufferFragment,
|
|
|
|
ix: usize,
|
|
|
|
count: usize,
|
|
|
|
) -> Fragment {
|
2022-01-05 09:29:29 +00:00
|
|
|
Fragment {
|
|
|
|
id: locator::Locator::from_index(ix, count),
|
|
|
|
insertion_timestamp: InsertionTimestamp {
|
|
|
|
replica_id: message.replica_id as ReplicaId,
|
|
|
|
local: message.local_timestamp,
|
|
|
|
lamport: message.lamport_timestamp,
|
|
|
|
},
|
|
|
|
insertion_offset: message.insertion_offset as usize,
|
|
|
|
len: message.len as usize,
|
|
|
|
visible: message.visible,
|
|
|
|
deletions: HashSet::from_iter(message.deletions.into_iter().map(|entry| clock::Local {
|
|
|
|
replica_id: entry.replica_id as ReplicaId,
|
|
|
|
value: entry.timestamp,
|
|
|
|
})),
|
|
|
|
max_undos: From::from(message.max_undos),
|
|
|
|
}
|
2022-01-05 02:06:16 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 07:29:34 +00:00
|
|
|
pub fn deserialize_selections(selections: Vec<proto::Selection>) -> Arc<[Selection<Anchor>]> {
|
|
|
|
Arc::from(
|
|
|
|
selections
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|selection| {
|
|
|
|
Some(Selection {
|
|
|
|
id: selection.id as usize,
|
|
|
|
start: deserialize_anchor(selection.start?)?,
|
|
|
|
end: deserialize_anchor(selection.end?)?,
|
|
|
|
reversed: selection.reversed,
|
|
|
|
goal: SelectionGoal::None,
|
2021-11-01 21:05:19 +00:00
|
|
|
})
|
2021-12-11 07:29:34 +00:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
)
|
2021-11-01 21:05:19 +00:00
|
|
|
}
|
2021-11-01 22:28:37 +00:00
|
|
|
|
2022-01-04 15:11:29 +00:00
|
|
|
pub fn deserialize_diagnostics(
|
|
|
|
diagnostics: Vec<proto::Diagnostic>,
|
|
|
|
) -> Arc<[DiagnosticEntry<Anchor>]> {
|
|
|
|
diagnostics
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|diagnostic| {
|
|
|
|
Some(DiagnosticEntry {
|
|
|
|
range: deserialize_anchor(diagnostic.start?)?..deserialize_anchor(diagnostic.end?)?,
|
|
|
|
diagnostic: Diagnostic {
|
|
|
|
severity: match proto::diagnostic::Severity::from_i32(diagnostic.severity)? {
|
|
|
|
proto::diagnostic::Severity::Error => DiagnosticSeverity::ERROR,
|
|
|
|
proto::diagnostic::Severity::Warning => DiagnosticSeverity::WARNING,
|
|
|
|
proto::diagnostic::Severity::Information => DiagnosticSeverity::INFORMATION,
|
|
|
|
proto::diagnostic::Severity::Hint => DiagnosticSeverity::HINT,
|
|
|
|
proto::diagnostic::Severity::None => return None,
|
2021-11-01 22:28:37 +00:00
|
|
|
},
|
2022-01-04 15:11:29 +00:00
|
|
|
message: diagnostic.message,
|
|
|
|
group_id: diagnostic.group_id as usize,
|
|
|
|
code: diagnostic.code,
|
|
|
|
is_valid: diagnostic.is_valid,
|
|
|
|
is_primary: diagnostic.is_primary,
|
|
|
|
is_disk_based: diagnostic.is_disk_based,
|
|
|
|
},
|
2021-12-09 15:38:46 +00:00
|
|
|
})
|
2022-01-04 15:11:29 +00:00
|
|
|
})
|
|
|
|
.collect()
|
2021-12-09 15:38:46 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 11:27:13 +00:00
|
|
|
pub fn deserialize_anchor(anchor: proto::Anchor) -> Option<Anchor> {
|
2021-12-09 15:38:46 +00:00
|
|
|
Some(Anchor {
|
|
|
|
timestamp: clock::Local {
|
|
|
|
replica_id: anchor.replica_id as ReplicaId,
|
|
|
|
value: anchor.local_timestamp,
|
|
|
|
},
|
|
|
|
offset: anchor.offset as usize,
|
|
|
|
bias: match proto::Bias::from_i32(anchor.bias)? {
|
|
|
|
proto::Bias::Left => Bias::Left,
|
|
|
|
proto::Bias::Right => Bias::Right,
|
|
|
|
},
|
|
|
|
})
|
2021-11-01 22:28:37 +00:00
|
|
|
}
|
2022-02-02 15:22:38 +00:00
|
|
|
|
|
|
|
pub fn serialize_completion(completion: &Completion<Anchor>) -> proto::Completion {
|
|
|
|
proto::Completion {
|
|
|
|
old_start: Some(serialize_anchor(&completion.old_range.start)),
|
|
|
|
old_end: Some(serialize_anchor(&completion.old_range.end)),
|
|
|
|
new_text: completion.new_text.clone(),
|
|
|
|
lsp_completion: serde_json::to_vec(&completion.lsp_completion).unwrap(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-02 17:43:55 +00:00
|
|
|
pub fn deserialize_completion(
|
|
|
|
completion: proto::Completion,
|
|
|
|
language: Option<&Arc<Language>>,
|
|
|
|
) -> Result<Completion<Anchor>> {
|
2022-02-02 15:22:38 +00:00
|
|
|
let old_start = completion
|
|
|
|
.old_start
|
|
|
|
.and_then(deserialize_anchor)
|
|
|
|
.ok_or_else(|| anyhow!("invalid old start"))?;
|
|
|
|
let old_end = completion
|
|
|
|
.old_end
|
|
|
|
.and_then(deserialize_anchor)
|
|
|
|
.ok_or_else(|| anyhow!("invalid old end"))?;
|
2022-02-02 17:43:55 +00:00
|
|
|
let lsp_completion = serde_json::from_slice(&completion.lsp_completion)?;
|
2022-02-02 15:22:38 +00:00
|
|
|
Ok(Completion {
|
|
|
|
old_range: old_start..old_end,
|
|
|
|
new_text: completion.new_text,
|
2022-02-03 02:14:30 +00:00
|
|
|
label: language
|
|
|
|
.and_then(|l| l.label_for_completion(&lsp_completion))
|
|
|
|
.unwrap_or(CompletionLabel::plain(&lsp_completion)),
|
2022-02-02 17:43:55 +00:00
|
|
|
lsp_completion,
|
2022-02-02 15:22:38 +00:00
|
|
|
})
|
|
|
|
}
|