1//! Handles conversions of `language` items to and from the [`rpc`] protocol.
2
3use crate::{diagnostic_set::DiagnosticEntry, CursorShape, Diagnostic};
4use anyhow::{anyhow, Context as _, 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
234pub fn serialize_anchor_range(range: Range<Anchor>) -> proto::AnchorRange {
235 proto::AnchorRange {
236 start: Some(serialize_anchor(&range.start)),
237 end: Some(serialize_anchor(&range.end)),
238 }
239}
240
241/// Deserializes an [`Range<Anchor>`] from the RPC representation.
242pub fn deserialize_anchor_range(range: proto::AnchorRange) -> Result<Range<Anchor>> {
243 Ok(
244 deserialize_anchor(range.start.context("invalid anchor")?).context("invalid anchor")?
245 ..deserialize_anchor(range.end.context("invalid anchor")?).context("invalid anchor")?,
246 )
247}
248
249// This behavior is currently copied in the collab database, for snapshotting channel notes
250/// Deserializes an [`crate::Operation`] from the RPC representation.
251pub fn deserialize_operation(message: proto::Operation) -> Result<crate::Operation> {
252 Ok(
253 match message
254 .variant
255 .ok_or_else(|| anyhow!("missing operation variant"))?
256 {
257 proto::operation::Variant::Edit(edit) => {
258 crate::Operation::Buffer(text::Operation::Edit(deserialize_edit_operation(edit)))
259 }
260 proto::operation::Variant::Undo(undo) => {
261 crate::Operation::Buffer(text::Operation::Undo(UndoOperation {
262 timestamp: clock::Lamport {
263 replica_id: undo.replica_id as ReplicaId,
264 value: undo.lamport_timestamp,
265 },
266 version: deserialize_version(&undo.version),
267 counts: undo
268 .counts
269 .into_iter()
270 .map(|c| {
271 (
272 clock::Lamport {
273 replica_id: c.replica_id as ReplicaId,
274 value: c.lamport_timestamp,
275 },
276 c.count,
277 )
278 })
279 .collect(),
280 }))
281 }
282 proto::operation::Variant::UpdateSelections(message) => {
283 let selections = message
284 .selections
285 .into_iter()
286 .filter_map(|selection| {
287 Some(Selection {
288 id: selection.id as usize,
289 start: deserialize_anchor(selection.start?.anchor?)?,
290 end: deserialize_anchor(selection.end?.anchor?)?,
291 reversed: selection.reversed,
292 goal: SelectionGoal::None,
293 })
294 })
295 .collect::<Vec<_>>();
296
297 crate::Operation::UpdateSelections {
298 lamport_timestamp: clock::Lamport {
299 replica_id: message.replica_id as ReplicaId,
300 value: message.lamport_timestamp,
301 },
302 selections: Arc::from(selections),
303 line_mode: message.line_mode,
304 cursor_shape: deserialize_cursor_shape(
305 proto::CursorShape::from_i32(message.cursor_shape)
306 .ok_or_else(|| anyhow!("Missing cursor shape"))?,
307 ),
308 }
309 }
310 proto::operation::Variant::UpdateDiagnostics(message) => {
311 crate::Operation::UpdateDiagnostics {
312 lamport_timestamp: clock::Lamport {
313 replica_id: message.replica_id as ReplicaId,
314 value: message.lamport_timestamp,
315 },
316 server_id: LanguageServerId(message.server_id as usize),
317 diagnostics: deserialize_diagnostics(message.diagnostics),
318 }
319 }
320 proto::operation::Variant::UpdateCompletionTriggers(message) => {
321 crate::Operation::UpdateCompletionTriggers {
322 triggers: message.triggers,
323 lamport_timestamp: clock::Lamport {
324 replica_id: message.replica_id as ReplicaId,
325 value: message.lamport_timestamp,
326 },
327 }
328 }
329 },
330 )
331}
332
333/// Deserializes an [`EditOperation`] from the RPC representation.
334pub fn deserialize_edit_operation(edit: proto::operation::Edit) -> EditOperation {
335 EditOperation {
336 timestamp: clock::Lamport {
337 replica_id: edit.replica_id as ReplicaId,
338 value: edit.lamport_timestamp,
339 },
340 version: deserialize_version(&edit.version),
341 ranges: edit.ranges.into_iter().map(deserialize_range).collect(),
342 new_text: edit.new_text.into_iter().map(Arc::from).collect(),
343 }
344}
345
346/// Deserializes an entry in the undo map from the RPC representation.
347pub fn deserialize_undo_map_entry(
348 entry: proto::UndoMapEntry,
349) -> (clock::Lamport, Vec<(clock::Lamport, u32)>) {
350 (
351 clock::Lamport {
352 replica_id: entry.replica_id as u16,
353 value: entry.local_timestamp,
354 },
355 entry
356 .counts
357 .into_iter()
358 .map(|undo_count| {
359 (
360 clock::Lamport {
361 replica_id: undo_count.replica_id as u16,
362 value: undo_count.lamport_timestamp,
363 },
364 undo_count.count,
365 )
366 })
367 .collect(),
368 )
369}
370
371/// Deserializes selections from the RPC representation.
372pub fn deserialize_selections(selections: Vec<proto::Selection>) -> Arc<[Selection<Anchor>]> {
373 Arc::from(
374 selections
375 .into_iter()
376 .filter_map(deserialize_selection)
377 .collect::<Vec<_>>(),
378 )
379}
380
381/// Deserializes a [`Selection`] from the RPC representation.
382pub fn deserialize_selection(selection: proto::Selection) -> Option<Selection<Anchor>> {
383 Some(Selection {
384 id: selection.id as usize,
385 start: deserialize_anchor(selection.start?.anchor?)?,
386 end: deserialize_anchor(selection.end?.anchor?)?,
387 reversed: selection.reversed,
388 goal: SelectionGoal::None,
389 })
390}
391
392/// Deserializes a list of diagnostics from the RPC representation.
393pub fn deserialize_diagnostics(
394 diagnostics: Vec<proto::Diagnostic>,
395) -> Arc<[DiagnosticEntry<Anchor>]> {
396 diagnostics
397 .into_iter()
398 .filter_map(|diagnostic| {
399 Some(DiagnosticEntry {
400 range: deserialize_anchor(diagnostic.start?)?..deserialize_anchor(diagnostic.end?)?,
401 diagnostic: Diagnostic {
402 source: diagnostic.source,
403 severity: match proto::diagnostic::Severity::from_i32(diagnostic.severity)? {
404 proto::diagnostic::Severity::Error => DiagnosticSeverity::ERROR,
405 proto::diagnostic::Severity::Warning => DiagnosticSeverity::WARNING,
406 proto::diagnostic::Severity::Information => DiagnosticSeverity::INFORMATION,
407 proto::diagnostic::Severity::Hint => DiagnosticSeverity::HINT,
408 proto::diagnostic::Severity::None => return None,
409 },
410 message: diagnostic.message,
411 group_id: diagnostic.group_id as usize,
412 code: diagnostic.code,
413 is_primary: diagnostic.is_primary,
414 is_disk_based: diagnostic.is_disk_based,
415 is_unnecessary: diagnostic.is_unnecessary,
416 },
417 })
418 })
419 .collect()
420}
421
422/// Deserializes an [`Anchor`] from the RPC representation.
423pub fn deserialize_anchor(anchor: proto::Anchor) -> Option<Anchor> {
424 let buffer_id = if let Some(id) = anchor.buffer_id {
425 Some(BufferId::new(id).ok()?)
426 } else {
427 None
428 };
429 Some(Anchor {
430 timestamp: clock::Lamport {
431 replica_id: anchor.replica_id as ReplicaId,
432 value: anchor.timestamp,
433 },
434 offset: anchor.offset as usize,
435 bias: match proto::Bias::from_i32(anchor.bias)? {
436 proto::Bias::Left => Bias::Left,
437 proto::Bias::Right => Bias::Right,
438 },
439 buffer_id,
440 })
441}
442
443/// Returns a `[clock::Lamport`] timestamp for the given [`proto::Operation`].
444pub fn lamport_timestamp_for_operation(operation: &proto::Operation) -> Option<clock::Lamport> {
445 let replica_id;
446 let value;
447 match operation.variant.as_ref()? {
448 proto::operation::Variant::Edit(op) => {
449 replica_id = op.replica_id;
450 value = op.lamport_timestamp;
451 }
452 proto::operation::Variant::Undo(op) => {
453 replica_id = op.replica_id;
454 value = op.lamport_timestamp;
455 }
456 proto::operation::Variant::UpdateDiagnostics(op) => {
457 replica_id = op.replica_id;
458 value = op.lamport_timestamp;
459 }
460 proto::operation::Variant::UpdateSelections(op) => {
461 replica_id = op.replica_id;
462 value = op.lamport_timestamp;
463 }
464 proto::operation::Variant::UpdateCompletionTriggers(op) => {
465 replica_id = op.replica_id;
466 value = op.lamport_timestamp;
467 }
468 }
469
470 Some(clock::Lamport {
471 replica_id: replica_id as ReplicaId,
472 value,
473 })
474}
475
476/// Serializes a [`Transaction`] to be sent over RPC.
477pub fn serialize_transaction(transaction: &Transaction) -> proto::Transaction {
478 proto::Transaction {
479 id: Some(serialize_timestamp(transaction.id)),
480 edit_ids: transaction
481 .edit_ids
482 .iter()
483 .copied()
484 .map(serialize_timestamp)
485 .collect(),
486 start: serialize_version(&transaction.start),
487 }
488}
489
490/// Deserializes a [`Transaction`] from the RPC representation.
491pub fn deserialize_transaction(transaction: proto::Transaction) -> Result<Transaction> {
492 Ok(Transaction {
493 id: deserialize_timestamp(
494 transaction
495 .id
496 .ok_or_else(|| anyhow!("missing transaction id"))?,
497 ),
498 edit_ids: transaction
499 .edit_ids
500 .into_iter()
501 .map(deserialize_timestamp)
502 .collect(),
503 start: deserialize_version(&transaction.start),
504 })
505}
506
507/// Serializes a [`clock::Lamport`] timestamp to be sent over RPC.
508pub fn serialize_timestamp(timestamp: clock::Lamport) -> proto::LamportTimestamp {
509 proto::LamportTimestamp {
510 replica_id: timestamp.replica_id as u32,
511 value: timestamp.value,
512 }
513}
514
515/// Deserializes a [`clock::Lamport`] timestamp from the RPC representation.
516pub fn deserialize_timestamp(timestamp: proto::LamportTimestamp) -> clock::Lamport {
517 clock::Lamport {
518 replica_id: timestamp.replica_id as ReplicaId,
519 value: timestamp.value,
520 }
521}
522
523/// Serializes a range of [`FullOffset`]s to be sent over RPC.
524pub fn serialize_range(range: &Range<FullOffset>) -> proto::Range {
525 proto::Range {
526 start: range.start.0 as u64,
527 end: range.end.0 as u64,
528 }
529}
530
531/// Deserializes a range of [`FullOffset`]s from the RPC representation.
532pub fn deserialize_range(range: proto::Range) -> Range<FullOffset> {
533 FullOffset(range.start as usize)..FullOffset(range.end as usize)
534}
535
536/// Deserializes a clock version from the RPC representation.
537pub fn deserialize_version(message: &[proto::VectorClockEntry]) -> clock::Global {
538 let mut version = clock::Global::new();
539 for entry in message {
540 version.observe(clock::Lamport {
541 replica_id: entry.replica_id as ReplicaId,
542 value: entry.timestamp,
543 });
544 }
545 version
546}
547
548/// Serializes a clock version to be sent over RPC.
549pub fn serialize_version(version: &clock::Global) -> Vec<proto::VectorClockEntry> {
550 version
551 .iter()
552 .map(|entry| proto::VectorClockEntry {
553 replica_id: entry.replica_id as u32,
554 timestamp: entry.value,
555 })
556 .collect()
557}