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