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