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