1//! Handles conversions of `language` items to and from the [`rpc`] protocol.
2
3use crate::{diagnostic_set::DiagnosticEntry, CursorShape, Diagnostic};
4use anyhow::{anyhow, Result};
5use clock::ReplicaId;
6use lsp::{DiagnosticSeverity, LanguageServerId};
7use rpc::proto;
8use std::{ops::Range, sync::Arc};
9use text::*;
10
11pub use proto::{BufferState, Operation};
12
13/// Deserializes a `[text::LineEnding]` from the RPC representation.
14pub fn deserialize_line_ending(message: proto::LineEnding) -> text::LineEnding {
15 match message {
16 proto::LineEnding::Unix => text::LineEnding::Unix,
17 proto::LineEnding::Windows => text::LineEnding::Windows,
18 }
19}
20
21/// Serializes a [`text::LineEnding`] to be sent over RPC.
22pub fn serialize_line_ending(message: text::LineEnding) -> proto::LineEnding {
23 match message {
24 text::LineEnding::Unix => proto::LineEnding::Unix,
25 text::LineEnding::Windows => proto::LineEnding::Windows,
26 }
27}
28
29/// Serializes a [`crate::Operation`] to be sent over RPC.
30pub fn serialize_operation(operation: &crate::Operation) -> proto::Operation {
31 proto::Operation {
32 variant: Some(match operation {
33 crate::Operation::Buffer(text::Operation::Edit(edit)) => {
34 proto::operation::Variant::Edit(serialize_edit_operation(edit))
35 }
36
37 crate::Operation::Buffer(text::Operation::Undo(undo)) => {
38 proto::operation::Variant::Undo(proto::operation::Undo {
39 replica_id: undo.timestamp.replica_id as u32,
40 lamport_timestamp: undo.timestamp.value,
41 version: serialize_version(&undo.version),
42 counts: undo
43 .counts
44 .iter()
45 .map(|(edit_id, count)| proto::UndoCount {
46 replica_id: edit_id.replica_id as u32,
47 lamport_timestamp: edit_id.value,
48 count: *count,
49 })
50 .collect(),
51 })
52 }
53
54 crate::Operation::UpdateSelections {
55 selections,
56 line_mode,
57 lamport_timestamp,
58 cursor_shape,
59 } => proto::operation::Variant::UpdateSelections(proto::operation::UpdateSelections {
60 replica_id: lamport_timestamp.replica_id as u32,
61 lamport_timestamp: lamport_timestamp.value,
62 selections: serialize_selections(selections),
63 line_mode: *line_mode,
64 cursor_shape: serialize_cursor_shape(cursor_shape) as i32,
65 }),
66
67 crate::Operation::UpdateDiagnostics {
68 lamport_timestamp,
69 server_id,
70 diagnostics,
71 } => proto::operation::Variant::UpdateDiagnostics(proto::UpdateDiagnostics {
72 replica_id: lamport_timestamp.replica_id as u32,
73 lamport_timestamp: lamport_timestamp.value,
74 server_id: server_id.0 as u64,
75 diagnostics: serialize_diagnostics(diagnostics.iter()),
76 }),
77
78 crate::Operation::UpdateCompletionTriggers {
79 triggers,
80 lamport_timestamp,
81 } => proto::operation::Variant::UpdateCompletionTriggers(
82 proto::operation::UpdateCompletionTriggers {
83 replica_id: lamport_timestamp.replica_id as u32,
84 lamport_timestamp: lamport_timestamp.value,
85 triggers: triggers.clone(),
86 },
87 ),
88 }),
89 }
90}
91
92/// Serializes an [`EditOperation`] to be sent over RPC.
93pub fn serialize_edit_operation(operation: &EditOperation) -> proto::operation::Edit {
94 proto::operation::Edit {
95 replica_id: operation.timestamp.replica_id as u32,
96 lamport_timestamp: operation.timestamp.value,
97 version: serialize_version(&operation.version),
98 ranges: operation.ranges.iter().map(serialize_range).collect(),
99 new_text: operation
100 .new_text
101 .iter()
102 .map(|text| text.to_string())
103 .collect(),
104 }
105}
106
107/// Serializes an entry in the undo map to be sent over RPC.
108pub fn serialize_undo_map_entry(
109 (edit_id, counts): (&clock::Lamport, &[(clock::Lamport, u32)]),
110) -> proto::UndoMapEntry {
111 proto::UndoMapEntry {
112 replica_id: edit_id.replica_id as u32,
113 local_timestamp: edit_id.value,
114 counts: counts
115 .iter()
116 .map(|(undo_id, count)| proto::UndoCount {
117 replica_id: undo_id.replica_id as u32,
118 lamport_timestamp: undo_id.value,
119 count: *count,
120 })
121 .collect(),
122 }
123}
124
125/// Splits the given list of operations into chunks.
126pub fn split_operations(
127 mut operations: Vec<proto::Operation>,
128) -> impl Iterator<Item = Vec<proto::Operation>> {
129 #[cfg(any(test, feature = "test-support"))]
130 const CHUNK_SIZE: usize = 5;
131
132 #[cfg(not(any(test, feature = "test-support")))]
133 const CHUNK_SIZE: usize = 100;
134
135 let mut done = false;
136 std::iter::from_fn(move || {
137 if done {
138 return None;
139 }
140
141 let operations = operations
142 .drain(..std::cmp::min(CHUNK_SIZE, operations.len()))
143 .collect::<Vec<_>>();
144 if operations.is_empty() {
145 done = true;
146 }
147 Some(operations)
148 })
149}
150
151/// Serializes selections to be sent over RPC.
152pub fn serialize_selections(selections: &Arc<[Selection<Anchor>]>) -> Vec<proto::Selection> {
153 selections.iter().map(serialize_selection).collect()
154}
155
156/// Serializes a [`Selection`] to be sent over RPC.
157pub fn serialize_selection(selection: &Selection<Anchor>) -> proto::Selection {
158 proto::Selection {
159 id: selection.id as u64,
160 start: Some(proto::EditorAnchor {
161 anchor: Some(serialize_anchor(&selection.start)),
162 excerpt_id: 0,
163 }),
164 end: Some(proto::EditorAnchor {
165 anchor: Some(serialize_anchor(&selection.end)),
166 excerpt_id: 0,
167 }),
168 reversed: selection.reversed,
169 }
170}
171
172/// Serializes a [`CursorShape`] to be sent over RPC.
173pub fn serialize_cursor_shape(cursor_shape: &CursorShape) -> proto::CursorShape {
174 match cursor_shape {
175 CursorShape::Bar => proto::CursorShape::CursorBar,
176 CursorShape::Block => proto::CursorShape::CursorBlock,
177 CursorShape::Underscore => proto::CursorShape::CursorUnderscore,
178 CursorShape::Hollow => proto::CursorShape::CursorHollow,
179 }
180}
181
182/// Deserializes a [`CursorShape`] from the RPC representation.
183pub fn deserialize_cursor_shape(cursor_shape: proto::CursorShape) -> CursorShape {
184 match cursor_shape {
185 proto::CursorShape::CursorBar => CursorShape::Bar,
186 proto::CursorShape::CursorBlock => CursorShape::Block,
187 proto::CursorShape::CursorUnderscore => CursorShape::Underscore,
188 proto::CursorShape::CursorHollow => CursorShape::Hollow,
189 }
190}
191
192/// Serializes a list of diagnostics to be sent over RPC.
193pub fn serialize_diagnostics<'a>(
194 diagnostics: impl IntoIterator<Item = &'a DiagnosticEntry<Anchor>>,
195) -> Vec<proto::Diagnostic> {
196 diagnostics
197 .into_iter()
198 .map(|entry| proto::Diagnostic {
199 source: entry.diagnostic.source.clone(),
200 start: Some(serialize_anchor(&entry.range.start)),
201 end: Some(serialize_anchor(&entry.range.end)),
202 message: entry.diagnostic.message.clone(),
203 severity: match entry.diagnostic.severity {
204 DiagnosticSeverity::ERROR => proto::diagnostic::Severity::Error,
205 DiagnosticSeverity::WARNING => proto::diagnostic::Severity::Warning,
206 DiagnosticSeverity::INFORMATION => proto::diagnostic::Severity::Information,
207 DiagnosticSeverity::HINT => proto::diagnostic::Severity::Hint,
208 _ => proto::diagnostic::Severity::None,
209 } as i32,
210 group_id: entry.diagnostic.group_id as u64,
211 is_primary: entry.diagnostic.is_primary,
212 is_valid: true,
213 code: entry.diagnostic.code.clone(),
214 is_disk_based: entry.diagnostic.is_disk_based,
215 is_unnecessary: entry.diagnostic.is_unnecessary,
216 })
217 .collect()
218}
219
220/// Serializes an [`Anchor`] to be sent over RPC.
221pub fn serialize_anchor(anchor: &Anchor) -> proto::Anchor {
222 proto::Anchor {
223 replica_id: anchor.timestamp.replica_id as u32,
224 timestamp: anchor.timestamp.value,
225 offset: anchor.offset as u64,
226 bias: match anchor.bias {
227 Bias::Left => proto::Bias::Left as i32,
228 Bias::Right => proto::Bias::Right as i32,
229 },
230 buffer_id: anchor.buffer_id.map(Into::into),
231 }
232}
233
234// This behavior is currently copied in the collab database, for snapshotting channel notes
235/// Deserializes an [`crate::Operation`] from the RPC representation.
236pub fn deserialize_operation(message: proto::Operation) -> Result<crate::Operation> {
237 Ok(
238 match message
239 .variant
240 .ok_or_else(|| anyhow!("missing operation variant"))?
241 {
242 proto::operation::Variant::Edit(edit) => {
243 crate::Operation::Buffer(text::Operation::Edit(deserialize_edit_operation(edit)))
244 }
245 proto::operation::Variant::Undo(undo) => {
246 crate::Operation::Buffer(text::Operation::Undo(UndoOperation {
247 timestamp: clock::Lamport {
248 replica_id: undo.replica_id as ReplicaId,
249 value: undo.lamport_timestamp,
250 },
251 version: deserialize_version(&undo.version),
252 counts: undo
253 .counts
254 .into_iter()
255 .map(|c| {
256 (
257 clock::Lamport {
258 replica_id: c.replica_id as ReplicaId,
259 value: c.lamport_timestamp,
260 },
261 c.count,
262 )
263 })
264 .collect(),
265 }))
266 }
267 proto::operation::Variant::UpdateSelections(message) => {
268 let selections = message
269 .selections
270 .into_iter()
271 .filter_map(|selection| {
272 Some(Selection {
273 id: selection.id as usize,
274 start: deserialize_anchor(selection.start?.anchor?)?,
275 end: deserialize_anchor(selection.end?.anchor?)?,
276 reversed: selection.reversed,
277 goal: SelectionGoal::None,
278 })
279 })
280 .collect::<Vec<_>>();
281
282 crate::Operation::UpdateSelections {
283 lamport_timestamp: clock::Lamport {
284 replica_id: message.replica_id as ReplicaId,
285 value: message.lamport_timestamp,
286 },
287 selections: Arc::from(selections),
288 line_mode: message.line_mode,
289 cursor_shape: deserialize_cursor_shape(
290 proto::CursorShape::from_i32(message.cursor_shape)
291 .ok_or_else(|| anyhow!("Missing cursor shape"))?,
292 ),
293 }
294 }
295 proto::operation::Variant::UpdateDiagnostics(message) => {
296 crate::Operation::UpdateDiagnostics {
297 lamport_timestamp: clock::Lamport {
298 replica_id: message.replica_id as ReplicaId,
299 value: message.lamport_timestamp,
300 },
301 server_id: LanguageServerId(message.server_id as usize),
302 diagnostics: deserialize_diagnostics(message.diagnostics),
303 }
304 }
305 proto::operation::Variant::UpdateCompletionTriggers(message) => {
306 crate::Operation::UpdateCompletionTriggers {
307 triggers: message.triggers,
308 lamport_timestamp: clock::Lamport {
309 replica_id: message.replica_id as ReplicaId,
310 value: message.lamport_timestamp,
311 },
312 }
313 }
314 },
315 )
316}
317
318/// Deserializes an [`EditOperation`] from the RPC representation.
319pub fn deserialize_edit_operation(edit: proto::operation::Edit) -> EditOperation {
320 EditOperation {
321 timestamp: clock::Lamport {
322 replica_id: edit.replica_id as ReplicaId,
323 value: edit.lamport_timestamp,
324 },
325 version: deserialize_version(&edit.version),
326 ranges: edit.ranges.into_iter().map(deserialize_range).collect(),
327 new_text: edit.new_text.into_iter().map(Arc::from).collect(),
328 }
329}
330
331/// Deserializes an entry in the undo map from the RPC representation.
332pub fn deserialize_undo_map_entry(
333 entry: proto::UndoMapEntry,
334) -> (clock::Lamport, Vec<(clock::Lamport, u32)>) {
335 (
336 clock::Lamport {
337 replica_id: entry.replica_id as u16,
338 value: entry.local_timestamp,
339 },
340 entry
341 .counts
342 .into_iter()
343 .map(|undo_count| {
344 (
345 clock::Lamport {
346 replica_id: undo_count.replica_id as u16,
347 value: undo_count.lamport_timestamp,
348 },
349 undo_count.count,
350 )
351 })
352 .collect(),
353 )
354}
355
356/// Deserializes selections from the RPC representation.
357pub fn deserialize_selections(selections: Vec<proto::Selection>) -> Arc<[Selection<Anchor>]> {
358 Arc::from(
359 selections
360 .into_iter()
361 .filter_map(deserialize_selection)
362 .collect::<Vec<_>>(),
363 )
364}
365
366/// Deserializes a [`Selection`] from the RPC representation.
367pub fn deserialize_selection(selection: proto::Selection) -> Option<Selection<Anchor>> {
368 Some(Selection {
369 id: selection.id as usize,
370 start: deserialize_anchor(selection.start?.anchor?)?,
371 end: deserialize_anchor(selection.end?.anchor?)?,
372 reversed: selection.reversed,
373 goal: SelectionGoal::None,
374 })
375}
376
377/// Deserializes a list of diagnostics from the RPC representation.
378pub fn deserialize_diagnostics(
379 diagnostics: Vec<proto::Diagnostic>,
380) -> Arc<[DiagnosticEntry<Anchor>]> {
381 diagnostics
382 .into_iter()
383 .filter_map(|diagnostic| {
384 Some(DiagnosticEntry {
385 range: deserialize_anchor(diagnostic.start?)?..deserialize_anchor(diagnostic.end?)?,
386 diagnostic: Diagnostic {
387 source: diagnostic.source,
388 severity: match proto::diagnostic::Severity::from_i32(diagnostic.severity)? {
389 proto::diagnostic::Severity::Error => DiagnosticSeverity::ERROR,
390 proto::diagnostic::Severity::Warning => DiagnosticSeverity::WARNING,
391 proto::diagnostic::Severity::Information => DiagnosticSeverity::INFORMATION,
392 proto::diagnostic::Severity::Hint => DiagnosticSeverity::HINT,
393 proto::diagnostic::Severity::None => return None,
394 },
395 message: diagnostic.message,
396 group_id: diagnostic.group_id as usize,
397 code: diagnostic.code,
398 is_primary: diagnostic.is_primary,
399 is_disk_based: diagnostic.is_disk_based,
400 is_unnecessary: diagnostic.is_unnecessary,
401 },
402 })
403 })
404 .collect()
405}
406
407/// Deserializes an [`Anchor`] from the RPC representation.
408pub fn deserialize_anchor(anchor: proto::Anchor) -> Option<Anchor> {
409 let buffer_id = if let Some(id) = anchor.buffer_id {
410 Some(BufferId::new(id).ok()?)
411 } else {
412 None
413 };
414 Some(Anchor {
415 timestamp: clock::Lamport {
416 replica_id: anchor.replica_id as ReplicaId,
417 value: anchor.timestamp,
418 },
419 offset: anchor.offset as usize,
420 bias: match proto::Bias::from_i32(anchor.bias)? {
421 proto::Bias::Left => Bias::Left,
422 proto::Bias::Right => Bias::Right,
423 },
424 buffer_id,
425 })
426}
427
428/// Returns a `[clock::Lamport`] timestamp for the given [`proto::Operation`].
429pub fn lamport_timestamp_for_operation(operation: &proto::Operation) -> Option<clock::Lamport> {
430 let replica_id;
431 let value;
432 match operation.variant.as_ref()? {
433 proto::operation::Variant::Edit(op) => {
434 replica_id = op.replica_id;
435 value = op.lamport_timestamp;
436 }
437 proto::operation::Variant::Undo(op) => {
438 replica_id = op.replica_id;
439 value = op.lamport_timestamp;
440 }
441 proto::operation::Variant::UpdateDiagnostics(op) => {
442 replica_id = op.replica_id;
443 value = op.lamport_timestamp;
444 }
445 proto::operation::Variant::UpdateSelections(op) => {
446 replica_id = op.replica_id;
447 value = op.lamport_timestamp;
448 }
449 proto::operation::Variant::UpdateCompletionTriggers(op) => {
450 replica_id = op.replica_id;
451 value = op.lamport_timestamp;
452 }
453 }
454
455 Some(clock::Lamport {
456 replica_id: replica_id as ReplicaId,
457 value,
458 })
459}
460
461/// Serializes a [`Transaction`] to be sent over RPC.
462pub fn serialize_transaction(transaction: &Transaction) -> proto::Transaction {
463 proto::Transaction {
464 id: Some(serialize_timestamp(transaction.id)),
465 edit_ids: transaction
466 .edit_ids
467 .iter()
468 .copied()
469 .map(serialize_timestamp)
470 .collect(),
471 start: serialize_version(&transaction.start),
472 }
473}
474
475/// Deserializes a [`Transaction`] from the RPC representation.
476pub fn deserialize_transaction(transaction: proto::Transaction) -> Result<Transaction> {
477 Ok(Transaction {
478 id: deserialize_timestamp(
479 transaction
480 .id
481 .ok_or_else(|| anyhow!("missing transaction id"))?,
482 ),
483 edit_ids: transaction
484 .edit_ids
485 .into_iter()
486 .map(deserialize_timestamp)
487 .collect(),
488 start: deserialize_version(&transaction.start),
489 })
490}
491
492/// Serializes a [`clock::Lamport`] timestamp to be sent over RPC.
493pub fn serialize_timestamp(timestamp: clock::Lamport) -> proto::LamportTimestamp {
494 proto::LamportTimestamp {
495 replica_id: timestamp.replica_id as u32,
496 value: timestamp.value,
497 }
498}
499
500/// Deserializes a [`clock::Lamport`] timestamp from the RPC representation.
501pub fn deserialize_timestamp(timestamp: proto::LamportTimestamp) -> clock::Lamport {
502 clock::Lamport {
503 replica_id: timestamp.replica_id as ReplicaId,
504 value: timestamp.value,
505 }
506}
507
508/// Serializes a range of [`FullOffset`]s to be sent over RPC.
509pub fn serialize_range(range: &Range<FullOffset>) -> proto::Range {
510 proto::Range {
511 start: range.start.0 as u64,
512 end: range.end.0 as u64,
513 }
514}
515
516/// Deserializes a range of [`FullOffset`]s from the RPC representation.
517pub fn deserialize_range(range: proto::Range) -> Range<FullOffset> {
518 FullOffset(range.start as usize)..FullOffset(range.end as usize)
519}
520
521/// Deserializes a clock version from the RPC representation.
522pub fn deserialize_version(message: &[proto::VectorClockEntry]) -> clock::Global {
523 let mut version = clock::Global::new();
524 for entry in message {
525 version.observe(clock::Lamport {
526 replica_id: entry.replica_id as ReplicaId,
527 value: entry.timestamp,
528 });
529 }
530 version
531}
532
533/// Serializes a clock version to be sent over RPC.
534pub fn serialize_version(version: &clock::Global) -> Vec<proto::VectorClockEntry> {
535 version
536 .iter()
537 .map(|entry| proto::VectorClockEntry {
538 replica_id: entry.replica_id as u32,
539 timestamp: entry.value,
540 })
541 .collect()
542}