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 proto::Anchor {
255 replica_id: anchor.timestamp.replica_id.as_u16() as u32,
256 timestamp: anchor.timestamp.value,
257 offset: anchor.offset as u64,
258 bias: match anchor.bias {
259 Bias::Left => proto::Bias::Left as i32,
260 Bias::Right => proto::Bias::Right as i32,
261 },
262 buffer_id: anchor.buffer_id.map(Into::into),
263 }
264}
265
266pub fn serialize_anchor_range(range: Range<Anchor>) -> proto::AnchorRange {
267 proto::AnchorRange {
268 start: Some(serialize_anchor(&range.start)),
269 end: Some(serialize_anchor(&range.end)),
270 }
271}
272
273/// Deserializes an [`Range<Anchor>`] from the RPC representation.
274pub fn deserialize_anchor_range(range: proto::AnchorRange) -> Result<Range<Anchor>> {
275 Ok(
276 deserialize_anchor(range.start.context("invalid anchor")?).context("invalid anchor")?
277 ..deserialize_anchor(range.end.context("invalid anchor")?).context("invalid anchor")?,
278 )
279}
280
281// This behavior is currently copied in the collab database, for snapshotting channel notes
282/// Deserializes an [`crate::Operation`] from the RPC representation.
283pub fn deserialize_operation(message: proto::Operation) -> Result<crate::Operation> {
284 Ok(
285 match message.variant.context("missing operation variant")? {
286 proto::operation::Variant::Edit(edit) => {
287 crate::Operation::Buffer(text::Operation::Edit(deserialize_edit_operation(edit)))
288 }
289 proto::operation::Variant::Undo(undo) => {
290 crate::Operation::Buffer(text::Operation::Undo(UndoOperation {
291 timestamp: clock::Lamport {
292 replica_id: ReplicaId::new(undo.replica_id as u16),
293 value: undo.lamport_timestamp,
294 },
295 version: deserialize_version(&undo.version),
296 counts: undo
297 .counts
298 .into_iter()
299 .map(|c| {
300 (
301 clock::Lamport {
302 replica_id: ReplicaId::new(c.replica_id as u16),
303 value: c.lamport_timestamp,
304 },
305 c.count,
306 )
307 })
308 .collect(),
309 }))
310 }
311 proto::operation::Variant::UpdateSelections(message) => {
312 let selections = message
313 .selections
314 .into_iter()
315 .filter_map(|selection| {
316 Some(Selection {
317 id: selection.id as usize,
318 start: deserialize_anchor(selection.start?.anchor?)?,
319 end: deserialize_anchor(selection.end?.anchor?)?,
320 reversed: selection.reversed,
321 goal: SelectionGoal::None,
322 })
323 })
324 .collect::<Vec<_>>();
325
326 crate::Operation::UpdateSelections {
327 lamport_timestamp: clock::Lamport {
328 replica_id: ReplicaId::new(message.replica_id as u16),
329 value: message.lamport_timestamp,
330 },
331 selections: Arc::from(selections),
332 line_mode: message.line_mode,
333 cursor_shape: deserialize_cursor_shape(
334 proto::CursorShape::from_i32(message.cursor_shape)
335 .context("Missing cursor shape")?,
336 ),
337 }
338 }
339 proto::operation::Variant::UpdateDiagnostics(message) => {
340 crate::Operation::UpdateDiagnostics {
341 lamport_timestamp: clock::Lamport {
342 replica_id: ReplicaId::new(message.replica_id as u16),
343 value: message.lamport_timestamp,
344 },
345 server_id: LanguageServerId(message.server_id as usize),
346 diagnostics: deserialize_diagnostics(message.diagnostics),
347 }
348 }
349 proto::operation::Variant::UpdateCompletionTriggers(message) => {
350 crate::Operation::UpdateCompletionTriggers {
351 triggers: message.triggers,
352 lamport_timestamp: clock::Lamport {
353 replica_id: ReplicaId::new(message.replica_id as u16),
354 value: message.lamport_timestamp,
355 },
356 server_id: LanguageServerId::from_proto(message.language_server_id),
357 }
358 }
359 proto::operation::Variant::UpdateLineEnding(message) => {
360 crate::Operation::UpdateLineEnding {
361 lamport_timestamp: clock::Lamport {
362 replica_id: ReplicaId::new(message.replica_id as u16),
363 value: message.lamport_timestamp,
364 },
365 line_ending: deserialize_line_ending(
366 proto::LineEnding::from_i32(message.line_ending)
367 .context("missing line_ending")?,
368 ),
369 }
370 }
371 },
372 )
373}
374
375/// Deserializes an [`EditOperation`] from the RPC representation.
376pub fn deserialize_edit_operation(edit: proto::operation::Edit) -> EditOperation {
377 EditOperation {
378 timestamp: clock::Lamport {
379 replica_id: ReplicaId::new(edit.replica_id as u16),
380 value: edit.lamport_timestamp,
381 },
382 version: deserialize_version(&edit.version),
383 ranges: edit.ranges.into_iter().map(deserialize_range).collect(),
384 new_text: edit.new_text.into_iter().map(Arc::from).collect(),
385 }
386}
387
388/// Deserializes an entry in the undo map from the RPC representation.
389pub fn deserialize_undo_map_entry(
390 entry: proto::UndoMapEntry,
391) -> (clock::Lamport, Vec<(clock::Lamport, u32)>) {
392 (
393 clock::Lamport {
394 replica_id: ReplicaId::new(entry.replica_id as u16),
395 value: entry.local_timestamp,
396 },
397 entry
398 .counts
399 .into_iter()
400 .map(|undo_count| {
401 (
402 clock::Lamport {
403 replica_id: ReplicaId::new(undo_count.replica_id as u16),
404 value: undo_count.lamport_timestamp,
405 },
406 undo_count.count,
407 )
408 })
409 .collect(),
410 )
411}
412
413/// Deserializes selections from the RPC representation.
414pub fn deserialize_selections(selections: Vec<proto::Selection>) -> Arc<[Selection<Anchor>]> {
415 selections
416 .into_iter()
417 .filter_map(deserialize_selection)
418 .collect()
419}
420
421/// Deserializes a [`Selection`] from the RPC representation.
422pub fn deserialize_selection(selection: proto::Selection) -> Option<Selection<Anchor>> {
423 Some(Selection {
424 id: selection.id as usize,
425 start: deserialize_anchor(selection.start?.anchor?)?,
426 end: deserialize_anchor(selection.end?.anchor?)?,
427 reversed: selection.reversed,
428 goal: SelectionGoal::None,
429 })
430}
431
432/// Deserializes a list of diagnostics from the RPC representation.
433pub fn deserialize_diagnostics(
434 diagnostics: Vec<proto::Diagnostic>,
435) -> Arc<[DiagnosticEntry<Anchor>]> {
436 diagnostics
437 .into_iter()
438 .filter_map(|diagnostic| {
439 let data = if let Some(data) = diagnostic.data {
440 Some(Value::from_str(&data).ok()?)
441 } else {
442 None
443 };
444 Some(DiagnosticEntry {
445 range: deserialize_anchor(diagnostic.start?)?..deserialize_anchor(diagnostic.end?)?,
446 diagnostic: Diagnostic {
447 source: diagnostic.source,
448 severity: match proto::diagnostic::Severity::from_i32(diagnostic.severity)? {
449 proto::diagnostic::Severity::Error => DiagnosticSeverity::ERROR,
450 proto::diagnostic::Severity::Warning => DiagnosticSeverity::WARNING,
451 proto::diagnostic::Severity::Information => DiagnosticSeverity::INFORMATION,
452 proto::diagnostic::Severity::Hint => DiagnosticSeverity::HINT,
453 proto::diagnostic::Severity::None => return None,
454 },
455 message: diagnostic.message,
456 markdown: diagnostic.markdown,
457 group_id: diagnostic.group_id as usize,
458 code: diagnostic.code.map(lsp::NumberOrString::from_string),
459 code_description: diagnostic
460 .code_description
461 .and_then(|s| lsp::Uri::from_str(&s).ok()),
462 is_primary: diagnostic.is_primary,
463 is_disk_based: diagnostic.is_disk_based,
464 is_unnecessary: diagnostic.is_unnecessary,
465 underline: diagnostic.underline,
466 registration_id: diagnostic.registration_id.map(SharedString::from),
467 source_kind: match proto::diagnostic::SourceKind::from_i32(
468 diagnostic.source_kind,
469 )? {
470 proto::diagnostic::SourceKind::Pulled => DiagnosticSourceKind::Pulled,
471 proto::diagnostic::SourceKind::Pushed => DiagnosticSourceKind::Pushed,
472 proto::diagnostic::SourceKind::Other => DiagnosticSourceKind::Other,
473 },
474 data,
475 },
476 })
477 })
478 .collect()
479}
480
481/// Deserializes an [`Anchor`] from the RPC representation.
482pub fn deserialize_anchor(anchor: proto::Anchor) -> Option<Anchor> {
483 let buffer_id = if let Some(id) = anchor.buffer_id {
484 Some(BufferId::new(id).ok()?)
485 } else {
486 None
487 };
488 Some(Anchor {
489 timestamp: clock::Lamport {
490 replica_id: ReplicaId::new(anchor.replica_id as u16),
491 value: anchor.timestamp,
492 },
493 offset: anchor.offset as usize,
494 bias: match proto::Bias::from_i32(anchor.bias)? {
495 proto::Bias::Left => Bias::Left,
496 proto::Bias::Right => Bias::Right,
497 },
498 buffer_id,
499 })
500}
501
502/// Returns a `[clock::Lamport`] timestamp for the given [`proto::Operation`].
503pub fn lamport_timestamp_for_operation(operation: &proto::Operation) -> Option<clock::Lamport> {
504 let replica_id;
505 let value;
506 match operation.variant.as_ref()? {
507 proto::operation::Variant::Edit(op) => {
508 replica_id = op.replica_id;
509 value = op.lamport_timestamp;
510 }
511 proto::operation::Variant::Undo(op) => {
512 replica_id = op.replica_id;
513 value = op.lamport_timestamp;
514 }
515 proto::operation::Variant::UpdateDiagnostics(op) => {
516 replica_id = op.replica_id;
517 value = op.lamport_timestamp;
518 }
519 proto::operation::Variant::UpdateSelections(op) => {
520 replica_id = op.replica_id;
521 value = op.lamport_timestamp;
522 }
523 proto::operation::Variant::UpdateCompletionTriggers(op) => {
524 replica_id = op.replica_id;
525 value = op.lamport_timestamp;
526 }
527 proto::operation::Variant::UpdateLineEnding(op) => {
528 replica_id = op.replica_id;
529 value = op.lamport_timestamp;
530 }
531 }
532
533 Some(clock::Lamport {
534 replica_id: ReplicaId::new(replica_id as u16),
535 value,
536 })
537}
538
539/// Serializes a [`Transaction`] to be sent over RPC.
540pub fn serialize_transaction(transaction: &Transaction) -> proto::Transaction {
541 proto::Transaction {
542 id: Some(serialize_timestamp(transaction.id)),
543 edit_ids: transaction
544 .edit_ids
545 .iter()
546 .copied()
547 .map(serialize_timestamp)
548 .collect(),
549 start: serialize_version(&transaction.start),
550 }
551}
552
553/// Deserializes a [`Transaction`] from the RPC representation.
554pub fn deserialize_transaction(transaction: proto::Transaction) -> Result<Transaction> {
555 Ok(Transaction {
556 id: deserialize_timestamp(transaction.id.context("missing transaction id")?),
557 edit_ids: transaction
558 .edit_ids
559 .into_iter()
560 .map(deserialize_timestamp)
561 .collect(),
562 start: deserialize_version(&transaction.start),
563 })
564}
565
566/// Serializes a [`clock::Lamport`] timestamp to be sent over RPC.
567pub fn serialize_timestamp(timestamp: clock::Lamport) -> proto::LamportTimestamp {
568 proto::LamportTimestamp {
569 replica_id: timestamp.replica_id.as_u16() as u32,
570 value: timestamp.value,
571 }
572}
573
574/// Deserializes a [`clock::Lamport`] timestamp from the RPC representation.
575pub fn deserialize_timestamp(timestamp: proto::LamportTimestamp) -> clock::Lamport {
576 clock::Lamport {
577 replica_id: ReplicaId::new(timestamp.replica_id as u16),
578 value: timestamp.value,
579 }
580}
581
582/// Serializes a range of [`FullOffset`]s to be sent over RPC.
583pub fn serialize_range(range: &Range<FullOffset>) -> proto::Range {
584 proto::Range {
585 start: range.start.0 as u64,
586 end: range.end.0 as u64,
587 }
588}
589
590/// Deserializes a range of [`FullOffset`]s from the RPC representation.
591pub fn deserialize_range(range: proto::Range) -> Range<FullOffset> {
592 FullOffset(range.start as usize)..FullOffset(range.end as usize)
593}
594
595/// Deserializes a clock version from the RPC representation.
596pub fn deserialize_version(message: &[proto::VectorClockEntry]) -> clock::Global {
597 let mut version = clock::Global::new();
598 for entry in message {
599 version.observe(clock::Lamport {
600 replica_id: ReplicaId::new(entry.replica_id as u16),
601 value: entry.timestamp,
602 });
603 }
604 version
605}
606
607/// Serializes a clock version to be sent over RPC.
608pub fn serialize_version(version: &clock::Global) -> Vec<proto::VectorClockEntry> {
609 version
610 .iter()
611 .map(|entry| proto::VectorClockEntry {
612 replica_id: entry.replica_id.as_u16() as u32,
613 timestamp: entry.value,
614 })
615 .collect()
616}
617
618pub fn serialize_lsp_edit(edit: lsp::TextEdit) -> proto::TextEdit {
619 let start = point_from_lsp(edit.range.start).0;
620 let end = point_from_lsp(edit.range.end).0;
621 proto::TextEdit {
622 new_text: edit.new_text,
623 lsp_range_start: Some(proto::PointUtf16 {
624 row: start.row,
625 column: start.column,
626 }),
627 lsp_range_end: Some(proto::PointUtf16 {
628 row: end.row,
629 column: end.column,
630 }),
631 }
632}
633
634pub fn deserialize_lsp_edit(edit: proto::TextEdit) -> Option<lsp::TextEdit> {
635 let start = edit.lsp_range_start?;
636 let start = PointUtf16::new(start.row, start.column);
637 let end = edit.lsp_range_end?;
638 let end = PointUtf16::new(end.row, end.column);
639 Some(lsp::TextEdit {
640 range: lsp::Range {
641 start: point_to_lsp(start),
642 end: point_to_lsp(end),
643 },
644 new_text: edit.new_text,
645 })
646}