1mod anchor;
2pub mod locator;
3pub mod operation_queue;
4mod patch;
5mod point;
6mod point_utf16;
7#[cfg(any(test, feature = "test-support"))]
8pub mod random_char_iter;
9pub mod rope;
10mod selection;
11pub mod subscription;
12#[cfg(test)]
13mod tests;
14
15pub use anchor::*;
16use anyhow::Result;
17use clock::ReplicaId;
18use collections::{HashMap, HashSet};
19use locator::Locator;
20use operation_queue::OperationQueue;
21pub use patch::Patch;
22pub use point::*;
23pub use point_utf16::*;
24#[cfg(any(test, feature = "test-support"))]
25pub use random_char_iter::*;
26use rope::TextDimension;
27pub use rope::{Chunks, Rope, TextSummary};
28pub use selection::*;
29use std::{
30 cmp::{self, Ordering},
31 iter::Iterator,
32 ops::{self, Deref, Range, Sub},
33 str,
34 sync::Arc,
35 time::{Duration, Instant},
36};
37pub use subscription::*;
38pub use sum_tree::Bias;
39use sum_tree::{FilterCursor, SumTree};
40
41pub type TransactionId = usize;
42
43pub struct Buffer {
44 snapshot: BufferSnapshot,
45 last_edit: clock::Local,
46 history: History,
47 deferred_ops: OperationQueue<Operation>,
48 deferred_replicas: HashSet<ReplicaId>,
49 replica_id: ReplicaId,
50 remote_id: u64,
51 local_clock: clock::Local,
52 pub lamport_clock: clock::Lamport,
53 subscriptions: Topic,
54}
55
56#[derive(Clone, Debug)]
57pub struct BufferSnapshot {
58 replica_id: ReplicaId,
59 visible_text: Rope,
60 deleted_text: Rope,
61 undo_map: UndoMap,
62 fragments: SumTree<Fragment>,
63 insertions: SumTree<InsertionFragment>,
64 pub version: clock::Global,
65}
66
67#[derive(Clone, Debug)]
68pub struct Transaction {
69 id: TransactionId,
70 start: clock::Global,
71 end: clock::Global,
72 edits: Vec<clock::Local>,
73 ranges: Vec<Range<FullOffset>>,
74 first_edit_at: Instant,
75 last_edit_at: Instant,
76}
77
78impl Transaction {
79 fn push_edit(&mut self, edit: &EditOperation) {
80 self.edits.push(edit.timestamp.local());
81 self.end.observe(edit.timestamp.local());
82
83 let mut other_ranges = edit.ranges.iter().peekable();
84 let mut new_ranges = Vec::new();
85 let insertion_len = edit.new_text.as_ref().map_or(0, |t| t.len());
86 let mut delta = 0;
87
88 for mut self_range in self.ranges.iter().cloned() {
89 self_range.start += delta;
90 self_range.end += delta;
91
92 while let Some(other_range) = other_ranges.peek() {
93 let mut other_range = (*other_range).clone();
94 other_range.start += delta;
95 other_range.end += delta;
96
97 if other_range.start <= self_range.end {
98 other_ranges.next().unwrap();
99 delta += insertion_len;
100
101 if other_range.end < self_range.start {
102 new_ranges.push(other_range.start..other_range.end + insertion_len);
103 self_range.start += insertion_len;
104 self_range.end += insertion_len;
105 } else {
106 self_range.start = cmp::min(self_range.start, other_range.start);
107 self_range.end = cmp::max(self_range.end, other_range.end) + insertion_len;
108 }
109 } else {
110 break;
111 }
112 }
113
114 new_ranges.push(self_range);
115 }
116
117 for other_range in other_ranges {
118 new_ranges.push(other_range.start + delta..other_range.end + delta + insertion_len);
119 delta += insertion_len;
120 }
121
122 self.ranges = new_ranges;
123 }
124}
125
126#[derive(Clone)]
127pub struct History {
128 // TODO: Turn this into a String or Rope, maybe.
129 pub base_text: Arc<str>,
130 ops: HashMap<clock::Local, EditOperation>,
131 undo_stack: Vec<Transaction>,
132 redo_stack: Vec<Transaction>,
133 transaction_depth: usize,
134 group_interval: Duration,
135 next_transaction_id: TransactionId,
136}
137
138impl History {
139 pub fn new(base_text: Arc<str>) -> Self {
140 Self {
141 base_text,
142 ops: Default::default(),
143 undo_stack: Vec::new(),
144 redo_stack: Vec::new(),
145 transaction_depth: 0,
146 group_interval: Duration::from_millis(300),
147 next_transaction_id: 0,
148 }
149 }
150
151 fn push(&mut self, op: EditOperation) {
152 self.ops.insert(op.timestamp.local(), op);
153 }
154
155 fn start_transaction(&mut self, start: clock::Global, now: Instant) -> Option<TransactionId> {
156 self.transaction_depth += 1;
157 if self.transaction_depth == 1 {
158 let id = self.next_transaction_id;
159 self.next_transaction_id += 1;
160 self.undo_stack.push(Transaction {
161 id,
162 start: start.clone(),
163 end: start,
164 edits: Vec::new(),
165 ranges: Vec::new(),
166 first_edit_at: now,
167 last_edit_at: now,
168 });
169 Some(id)
170 } else {
171 None
172 }
173 }
174
175 fn end_transaction(&mut self, now: Instant) -> Option<&Transaction> {
176 assert_ne!(self.transaction_depth, 0);
177 self.transaction_depth -= 1;
178 if self.transaction_depth == 0 {
179 if self.undo_stack.last().unwrap().ranges.is_empty() {
180 self.undo_stack.pop();
181 None
182 } else {
183 let transaction = self.undo_stack.last_mut().unwrap();
184 transaction.last_edit_at = now;
185 Some(transaction)
186 }
187 } else {
188 None
189 }
190 }
191
192 fn group(&mut self) -> Option<TransactionId> {
193 let mut new_len = self.undo_stack.len();
194 let mut transactions = self.undo_stack.iter_mut();
195
196 if let Some(mut transaction) = transactions.next_back() {
197 while let Some(prev_transaction) = transactions.next_back() {
198 if transaction.first_edit_at - prev_transaction.last_edit_at <= self.group_interval
199 && transaction.start == prev_transaction.end
200 {
201 transaction = prev_transaction;
202 new_len -= 1;
203 } else {
204 break;
205 }
206 }
207 }
208
209 let (transactions_to_keep, transactions_to_merge) = self.undo_stack.split_at_mut(new_len);
210 if let Some(last_transaction) = transactions_to_keep.last_mut() {
211 for transaction in &*transactions_to_merge {
212 for edit_id in &transaction.edits {
213 last_transaction.push_edit(&self.ops[edit_id]);
214 }
215 }
216
217 if let Some(transaction) = transactions_to_merge.last_mut() {
218 last_transaction.last_edit_at = transaction.last_edit_at;
219 last_transaction.end = transaction.end.clone();
220 }
221 }
222
223 self.undo_stack.truncate(new_len);
224 self.undo_stack.last().map(|t| t.id)
225 }
226
227 fn push_undo(&mut self, edit_id: clock::Local) {
228 assert_ne!(self.transaction_depth, 0);
229 let last_transaction = self.undo_stack.last_mut().unwrap();
230 last_transaction.push_edit(&self.ops[&edit_id]);
231 }
232
233 fn pop_undo(&mut self) -> Option<&Transaction> {
234 assert_eq!(self.transaction_depth, 0);
235 if let Some(transaction) = self.undo_stack.pop() {
236 self.redo_stack.push(transaction);
237 self.redo_stack.last()
238 } else {
239 None
240 }
241 }
242
243 fn pop_redo(&mut self) -> Option<&Transaction> {
244 assert_eq!(self.transaction_depth, 0);
245 if let Some(transaction) = self.redo_stack.pop() {
246 self.undo_stack.push(transaction);
247 self.undo_stack.last()
248 } else {
249 None
250 }
251 }
252}
253
254#[derive(Clone, Default, Debug)]
255struct UndoMap(HashMap<clock::Local, Vec<(clock::Local, u32)>>);
256
257impl UndoMap {
258 fn insert(&mut self, undo: &UndoOperation) {
259 for (edit_id, count) in &undo.counts {
260 self.0.entry(*edit_id).or_default().push((undo.id, *count));
261 }
262 }
263
264 fn is_undone(&self, edit_id: clock::Local) -> bool {
265 self.undo_count(edit_id) % 2 == 1
266 }
267
268 fn was_undone(&self, edit_id: clock::Local, version: &clock::Global) -> bool {
269 let undo_count = self
270 .0
271 .get(&edit_id)
272 .unwrap_or(&Vec::new())
273 .iter()
274 .filter(|(undo_id, _)| version.observed(*undo_id))
275 .map(|(_, undo_count)| *undo_count)
276 .max()
277 .unwrap_or(0);
278 undo_count % 2 == 1
279 }
280
281 fn undo_count(&self, edit_id: clock::Local) -> u32 {
282 self.0
283 .get(&edit_id)
284 .unwrap_or(&Vec::new())
285 .iter()
286 .map(|(_, undo_count)| *undo_count)
287 .max()
288 .unwrap_or(0)
289 }
290}
291
292struct Edits<'a, D: TextDimension, F: FnMut(&FragmentSummary) -> bool> {
293 visible_cursor: rope::Cursor<'a>,
294 deleted_cursor: rope::Cursor<'a>,
295 fragments_cursor: Option<FilterCursor<'a, F, Fragment, FragmentTextSummary>>,
296 undos: &'a UndoMap,
297 since: &'a clock::Global,
298 old_end: D,
299 new_end: D,
300 range: Range<(&'a Locator, usize)>,
301}
302
303#[derive(Clone, Debug, Default, Eq, PartialEq)]
304pub struct Edit<D> {
305 pub old: Range<D>,
306 pub new: Range<D>,
307}
308
309impl<D> Edit<D>
310where
311 D: Sub<D, Output = D> + PartialEq + Copy,
312{
313 pub fn old_len(&self) -> D {
314 self.old.end - self.old.start
315 }
316
317 pub fn new_len(&self) -> D {
318 self.new.end - self.new.start
319 }
320
321 pub fn is_empty(&self) -> bool {
322 self.old.start == self.old.end && self.new.start == self.new.end
323 }
324}
325
326impl<D1, D2> Edit<(D1, D2)> {
327 pub fn flatten(self) -> (Edit<D1>, Edit<D2>) {
328 (
329 Edit {
330 old: self.old.start.0..self.old.end.0,
331 new: self.new.start.0..self.new.end.0,
332 },
333 Edit {
334 old: self.old.start.1..self.old.end.1,
335 new: self.new.start.1..self.new.end.1,
336 },
337 )
338 }
339}
340
341#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
342pub struct InsertionTimestamp {
343 pub replica_id: ReplicaId,
344 pub local: clock::Seq,
345 pub lamport: clock::Seq,
346}
347
348impl InsertionTimestamp {
349 fn local(&self) -> clock::Local {
350 clock::Local {
351 replica_id: self.replica_id,
352 value: self.local,
353 }
354 }
355
356 fn lamport(&self) -> clock::Lamport {
357 clock::Lamport {
358 replica_id: self.replica_id,
359 value: self.lamport,
360 }
361 }
362}
363
364#[derive(Eq, PartialEq, Clone, Debug)]
365struct Fragment {
366 id: Locator,
367 insertion_timestamp: InsertionTimestamp,
368 insertion_offset: usize,
369 len: usize,
370 visible: bool,
371 deletions: HashSet<clock::Local>,
372 max_undos: clock::Global,
373}
374
375#[derive(Eq, PartialEq, Clone, Debug)]
376pub struct FragmentSummary {
377 text: FragmentTextSummary,
378 max_id: Locator,
379 max_version: clock::Global,
380 min_insertion_version: clock::Global,
381 max_insertion_version: clock::Global,
382}
383
384#[derive(Copy, Default, Clone, Debug, PartialEq, Eq)]
385struct FragmentTextSummary {
386 visible: usize,
387 deleted: usize,
388}
389
390impl<'a> sum_tree::Dimension<'a, FragmentSummary> for FragmentTextSummary {
391 fn add_summary(&mut self, summary: &'a FragmentSummary, _: &Option<clock::Global>) {
392 self.visible += summary.text.visible;
393 self.deleted += summary.text.deleted;
394 }
395}
396
397#[derive(Eq, PartialEq, Clone, Debug)]
398struct InsertionFragment {
399 timestamp: clock::Local,
400 split_offset: usize,
401 fragment_id: Locator,
402}
403
404#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
405struct InsertionFragmentKey {
406 timestamp: clock::Local,
407 split_offset: usize,
408}
409
410#[derive(Clone, Debug, Eq, PartialEq)]
411pub enum Operation {
412 Edit(EditOperation),
413 Undo {
414 undo: UndoOperation,
415 lamport_timestamp: clock::Lamport,
416 },
417}
418
419#[derive(Clone, Debug, Eq, PartialEq)]
420pub struct EditOperation {
421 pub timestamp: InsertionTimestamp,
422 pub version: clock::Global,
423 pub ranges: Vec<Range<FullOffset>>,
424 pub new_text: Option<String>,
425}
426
427#[derive(Clone, Debug, Eq, PartialEq)]
428pub struct UndoOperation {
429 pub id: clock::Local,
430 pub counts: HashMap<clock::Local, u32>,
431 pub ranges: Vec<Range<FullOffset>>,
432 pub version: clock::Global,
433}
434
435impl Buffer {
436 pub fn new(replica_id: u16, remote_id: u64, history: History) -> Buffer {
437 let mut fragments = SumTree::new();
438 let mut insertions = SumTree::new();
439
440 let mut local_clock = clock::Local::new(replica_id);
441 let mut lamport_clock = clock::Lamport::new(replica_id);
442 let mut version = clock::Global::new();
443 let visible_text = Rope::from(history.base_text.as_ref());
444 if visible_text.len() > 0 {
445 let insertion_timestamp = InsertionTimestamp {
446 replica_id: 0,
447 local: 1,
448 lamport: 1,
449 };
450 local_clock.observe(insertion_timestamp.local());
451 lamport_clock.observe(insertion_timestamp.lamport());
452 version.observe(insertion_timestamp.local());
453 let fragment_id = Locator::between(&Locator::min(), &Locator::max());
454 let fragment = Fragment {
455 id: fragment_id,
456 insertion_timestamp,
457 insertion_offset: 0,
458 len: visible_text.len(),
459 visible: true,
460 deletions: Default::default(),
461 max_undos: Default::default(),
462 };
463 insertions.push(InsertionFragment::new(&fragment), &());
464 fragments.push(fragment, &None);
465 }
466
467 Buffer {
468 snapshot: BufferSnapshot {
469 replica_id,
470 visible_text,
471 deleted_text: Rope::new(),
472 fragments,
473 insertions,
474 version,
475 undo_map: Default::default(),
476 },
477 last_edit: clock::Local::default(),
478 history,
479 deferred_ops: OperationQueue::new(),
480 deferred_replicas: HashSet::default(),
481 replica_id,
482 remote_id,
483 local_clock,
484 lamport_clock,
485 subscriptions: Default::default(),
486 }
487 }
488
489 pub fn version(&self) -> clock::Global {
490 self.version.clone()
491 }
492
493 pub fn snapshot(&self) -> BufferSnapshot {
494 self.snapshot.clone()
495 }
496
497 pub fn replica_id(&self) -> ReplicaId {
498 self.local_clock.replica_id
499 }
500
501 pub fn remote_id(&self) -> u64 {
502 self.remote_id
503 }
504
505 pub fn deferred_ops_len(&self) -> usize {
506 self.deferred_ops.len()
507 }
508
509 pub fn transaction_group_interval(&self) -> Duration {
510 self.history.group_interval
511 }
512
513 pub fn edit<R, I, S, T>(&mut self, ranges: R, new_text: T) -> EditOperation
514 where
515 R: IntoIterator<IntoIter = I>,
516 I: ExactSizeIterator<Item = Range<S>>,
517 S: ToOffset,
518 T: Into<String>,
519 {
520 let new_text = new_text.into();
521 let new_text_len = new_text.len();
522 let new_text = if new_text_len > 0 {
523 Some(new_text)
524 } else {
525 None
526 };
527
528 self.start_transaction();
529 let timestamp = InsertionTimestamp {
530 replica_id: self.replica_id,
531 local: self.local_clock.tick().value,
532 lamport: self.lamport_clock.tick().value,
533 };
534 let edit = self.apply_local_edit(ranges.into_iter(), new_text, timestamp);
535
536 self.history.push(edit.clone());
537 self.history.push_undo(edit.timestamp.local());
538 self.last_edit = edit.timestamp.local();
539 self.snapshot.version.observe(edit.timestamp.local());
540 self.end_transaction();
541 edit
542 }
543
544 fn apply_local_edit<S: ToOffset>(
545 &mut self,
546 ranges: impl ExactSizeIterator<Item = Range<S>>,
547 new_text: Option<String>,
548 timestamp: InsertionTimestamp,
549 ) -> EditOperation {
550 let mut edits = Patch::default();
551 let mut edit_op = EditOperation {
552 timestamp,
553 version: self.version(),
554 ranges: Vec::with_capacity(ranges.len()),
555 new_text: None,
556 };
557 let mut new_insertions = Vec::new();
558 let mut insertion_offset = 0;
559
560 let mut ranges = ranges
561 .map(|range| range.start.to_offset(&*self)..range.end.to_offset(&*self))
562 .peekable();
563
564 let mut new_ropes =
565 RopeBuilder::new(self.visible_text.cursor(0), self.deleted_text.cursor(0));
566 let mut old_fragments = self.fragments.cursor::<FragmentTextSummary>();
567 let mut new_fragments =
568 old_fragments.slice(&ranges.peek().unwrap().start, Bias::Right, &None);
569 new_ropes.push_tree(new_fragments.summary().text);
570
571 let mut fragment_start = old_fragments.start().visible;
572 for range in ranges {
573 let fragment_end = old_fragments.end(&None).visible;
574
575 // If the current fragment ends before this range, then jump ahead to the first fragment
576 // that extends past the start of this range, reusing any intervening fragments.
577 if fragment_end < range.start {
578 // If the current fragment has been partially consumed, then consume the rest of it
579 // and advance to the next fragment before slicing.
580 if fragment_start > old_fragments.start().visible {
581 if fragment_end > fragment_start {
582 let mut suffix = old_fragments.item().unwrap().clone();
583 suffix.len = fragment_end - fragment_start;
584 suffix.insertion_offset += fragment_start - old_fragments.start().visible;
585 new_insertions.push(InsertionFragment::insert_new(&suffix));
586 new_ropes.push_fragment(&suffix, suffix.visible);
587 new_fragments.push(suffix, &None);
588 }
589 old_fragments.next(&None);
590 }
591
592 let slice = old_fragments.slice(&range.start, Bias::Right, &None);
593 new_ropes.push_tree(slice.summary().text);
594 new_fragments.push_tree(slice, &None);
595 fragment_start = old_fragments.start().visible;
596 }
597
598 let full_range_start = FullOffset(range.start + old_fragments.start().deleted);
599
600 // Preserve any portion of the current fragment that precedes this range.
601 if fragment_start < range.start {
602 let mut prefix = old_fragments.item().unwrap().clone();
603 prefix.len = range.start - fragment_start;
604 prefix.insertion_offset += fragment_start - old_fragments.start().visible;
605 prefix.id = Locator::between(&new_fragments.summary().max_id, &prefix.id);
606 new_insertions.push(InsertionFragment::insert_new(&prefix));
607 new_ropes.push_fragment(&prefix, prefix.visible);
608 new_fragments.push(prefix, &None);
609 fragment_start = range.start;
610 }
611
612 // Insert the new text before any existing fragments within the range.
613 if let Some(new_text) = new_text.as_deref() {
614 let new_start = new_fragments.summary().text.visible;
615 edits.push(Edit {
616 old: fragment_start..fragment_start,
617 new: new_start..new_start + new_text.len(),
618 });
619 let fragment = Fragment {
620 id: Locator::between(
621 &new_fragments.summary().max_id,
622 old_fragments
623 .item()
624 .map_or(&Locator::max(), |old_fragment| &old_fragment.id),
625 ),
626 insertion_timestamp: timestamp,
627 insertion_offset,
628 len: new_text.len(),
629 deletions: Default::default(),
630 max_undos: Default::default(),
631 visible: true,
632 };
633 new_insertions.push(InsertionFragment::insert_new(&fragment));
634 new_ropes.push_str(new_text);
635 new_fragments.push(fragment, &None);
636 insertion_offset += new_text.len();
637 }
638
639 // Advance through every fragment that intersects this range, marking the intersecting
640 // portions as deleted.
641 while fragment_start < range.end {
642 let fragment = old_fragments.item().unwrap();
643 let fragment_end = old_fragments.end(&None).visible;
644 let mut intersection = fragment.clone();
645 let intersection_end = cmp::min(range.end, fragment_end);
646 if fragment.visible {
647 intersection.len = intersection_end - fragment_start;
648 intersection.insertion_offset += fragment_start - old_fragments.start().visible;
649 intersection.id =
650 Locator::between(&new_fragments.summary().max_id, &intersection.id);
651 intersection.deletions.insert(timestamp.local());
652 intersection.visible = false;
653 }
654 if intersection.len > 0 {
655 if fragment.visible && !intersection.visible {
656 let new_start = new_fragments.summary().text.visible;
657 edits.push(Edit {
658 old: fragment_start..intersection_end,
659 new: new_start..new_start,
660 });
661 }
662 new_insertions.push(InsertionFragment::insert_new(&intersection));
663 new_ropes.push_fragment(&intersection, fragment.visible);
664 new_fragments.push(intersection, &None);
665 fragment_start = intersection_end;
666 }
667 if fragment_end <= range.end {
668 old_fragments.next(&None);
669 }
670 }
671
672 let full_range_end = FullOffset(range.end + old_fragments.start().deleted);
673 edit_op.ranges.push(full_range_start..full_range_end);
674 }
675
676 // If the current fragment has been partially consumed, then consume the rest of it
677 // and advance to the next fragment before slicing.
678 if fragment_start > old_fragments.start().visible {
679 let fragment_end = old_fragments.end(&None).visible;
680 if fragment_end > fragment_start {
681 let mut suffix = old_fragments.item().unwrap().clone();
682 suffix.len = fragment_end - fragment_start;
683 suffix.insertion_offset += fragment_start - old_fragments.start().visible;
684 new_insertions.push(InsertionFragment::insert_new(&suffix));
685 new_ropes.push_fragment(&suffix, suffix.visible);
686 new_fragments.push(suffix, &None);
687 }
688 old_fragments.next(&None);
689 }
690
691 let suffix = old_fragments.suffix(&None);
692 new_ropes.push_tree(suffix.summary().text);
693 new_fragments.push_tree(suffix, &None);
694 let (visible_text, deleted_text) = new_ropes.finish();
695 drop(old_fragments);
696
697 self.snapshot.fragments = new_fragments;
698 self.snapshot.insertions.edit(new_insertions, &());
699 self.snapshot.visible_text = visible_text;
700 self.snapshot.deleted_text = deleted_text;
701 self.subscriptions.publish_mut(&edits);
702 edit_op.new_text = new_text;
703 edit_op
704 }
705
706 pub fn apply_ops<I: IntoIterator<Item = Operation>>(&mut self, ops: I) -> Result<()> {
707 let mut deferred_ops = Vec::new();
708 for op in ops {
709 if self.can_apply_op(&op) {
710 self.apply_op(op)?;
711 } else {
712 self.deferred_replicas.insert(op.replica_id());
713 deferred_ops.push(op);
714 }
715 }
716 self.deferred_ops.insert(deferred_ops);
717 self.flush_deferred_ops()?;
718 Ok(())
719 }
720
721 fn apply_op(&mut self, op: Operation) -> Result<()> {
722 match op {
723 Operation::Edit(edit) => {
724 if !self.version.observed(edit.timestamp.local()) {
725 self.apply_remote_edit(
726 &edit.version,
727 &edit.ranges,
728 edit.new_text.as_deref(),
729 edit.timestamp,
730 );
731 self.snapshot.version.observe(edit.timestamp.local());
732 self.history.push(edit);
733 }
734 }
735 Operation::Undo {
736 undo,
737 lamport_timestamp,
738 } => {
739 if !self.version.observed(undo.id) {
740 self.apply_undo(&undo)?;
741 self.snapshot.version.observe(undo.id);
742 self.lamport_clock.observe(lamport_timestamp);
743 }
744 }
745 }
746 Ok(())
747 }
748
749 fn apply_remote_edit(
750 &mut self,
751 version: &clock::Global,
752 ranges: &[Range<FullOffset>],
753 new_text: Option<&str>,
754 timestamp: InsertionTimestamp,
755 ) {
756 if ranges.is_empty() {
757 return;
758 }
759
760 let mut edits = Patch::default();
761 let cx = Some(version.clone());
762 let mut new_insertions = Vec::new();
763 let mut insertion_offset = 0;
764 let mut new_ropes =
765 RopeBuilder::new(self.visible_text.cursor(0), self.deleted_text.cursor(0));
766 let mut old_fragments = self.fragments.cursor::<(VersionedFullOffset, usize)>();
767 let mut new_fragments = old_fragments.slice(
768 &VersionedFullOffset::Offset(ranges[0].start),
769 Bias::Left,
770 &cx,
771 );
772 new_ropes.push_tree(new_fragments.summary().text);
773
774 let mut fragment_start = old_fragments.start().0.full_offset();
775 for range in ranges {
776 let fragment_end = old_fragments.end(&cx).0.full_offset();
777
778 // If the current fragment ends before this range, then jump ahead to the first fragment
779 // that extends past the start of this range, reusing any intervening fragments.
780 if fragment_end < range.start {
781 // If the current fragment has been partially consumed, then consume the rest of it
782 // and advance to the next fragment before slicing.
783 if fragment_start > old_fragments.start().0.full_offset() {
784 if fragment_end > fragment_start {
785 let mut suffix = old_fragments.item().unwrap().clone();
786 suffix.len = fragment_end.0 - fragment_start.0;
787 suffix.insertion_offset +=
788 fragment_start - old_fragments.start().0.full_offset();
789 new_insertions.push(InsertionFragment::insert_new(&suffix));
790 new_ropes.push_fragment(&suffix, suffix.visible);
791 new_fragments.push(suffix, &None);
792 }
793 old_fragments.next(&cx);
794 }
795
796 let slice =
797 old_fragments.slice(&VersionedFullOffset::Offset(range.start), Bias::Left, &cx);
798 new_ropes.push_tree(slice.summary().text);
799 new_fragments.push_tree(slice, &None);
800 fragment_start = old_fragments.start().0.full_offset();
801 }
802
803 // If we are at the end of a non-concurrent fragment, advance to the next one.
804 let fragment_end = old_fragments.end(&cx).0.full_offset();
805 if fragment_end == range.start && fragment_end > fragment_start {
806 let mut fragment = old_fragments.item().unwrap().clone();
807 fragment.len = fragment_end.0 - fragment_start.0;
808 fragment.insertion_offset += fragment_start - old_fragments.start().0.full_offset();
809 new_insertions.push(InsertionFragment::insert_new(&fragment));
810 new_ropes.push_fragment(&fragment, fragment.visible);
811 new_fragments.push(fragment, &None);
812 old_fragments.next(&cx);
813 fragment_start = old_fragments.start().0.full_offset();
814 }
815
816 // Skip over insertions that are concurrent to this edit, but have a lower lamport
817 // timestamp.
818 while let Some(fragment) = old_fragments.item() {
819 if fragment_start == range.start
820 && fragment.insertion_timestamp.lamport() > timestamp.lamport()
821 {
822 new_ropes.push_fragment(fragment, fragment.visible);
823 new_fragments.push(fragment.clone(), &None);
824 old_fragments.next(&cx);
825 debug_assert_eq!(fragment_start, range.start);
826 } else {
827 break;
828 }
829 }
830 debug_assert!(fragment_start <= range.start);
831
832 // Preserve any portion of the current fragment that precedes this range.
833 if fragment_start < range.start {
834 let mut prefix = old_fragments.item().unwrap().clone();
835 prefix.len = range.start.0 - fragment_start.0;
836 prefix.insertion_offset += fragment_start - old_fragments.start().0.full_offset();
837 prefix.id = Locator::between(&new_fragments.summary().max_id, &prefix.id);
838 new_insertions.push(InsertionFragment::insert_new(&prefix));
839 fragment_start = range.start;
840 new_ropes.push_fragment(&prefix, prefix.visible);
841 new_fragments.push(prefix, &None);
842 }
843
844 // Insert the new text before any existing fragments within the range.
845 if let Some(new_text) = new_text {
846 let mut old_start = old_fragments.start().1;
847 if old_fragments.item().map_or(false, |f| f.visible) {
848 old_start += fragment_start.0 - old_fragments.start().0.full_offset().0;
849 }
850 let new_start = new_fragments.summary().text.visible;
851 edits.push(Edit {
852 old: old_start..old_start,
853 new: new_start..new_start + new_text.len(),
854 });
855 let fragment = Fragment {
856 id: Locator::between(
857 &new_fragments.summary().max_id,
858 old_fragments
859 .item()
860 .map_or(&Locator::max(), |old_fragment| &old_fragment.id),
861 ),
862 insertion_timestamp: timestamp,
863 insertion_offset,
864 len: new_text.len(),
865 deletions: Default::default(),
866 max_undos: Default::default(),
867 visible: true,
868 };
869 new_insertions.push(InsertionFragment::insert_new(&fragment));
870 new_ropes.push_str(new_text);
871 new_fragments.push(fragment, &None);
872 insertion_offset += new_text.len();
873 }
874
875 // Advance through every fragment that intersects this range, marking the intersecting
876 // portions as deleted.
877 while fragment_start < range.end {
878 let fragment = old_fragments.item().unwrap();
879 let fragment_end = old_fragments.end(&cx).0.full_offset();
880 let mut intersection = fragment.clone();
881 let intersection_end = cmp::min(range.end, fragment_end);
882 if fragment.was_visible(version, &self.undo_map) {
883 intersection.len = intersection_end.0 - fragment_start.0;
884 intersection.insertion_offset +=
885 fragment_start - old_fragments.start().0.full_offset();
886 intersection.id =
887 Locator::between(&new_fragments.summary().max_id, &intersection.id);
888 intersection.deletions.insert(timestamp.local());
889 intersection.visible = false;
890 }
891 if intersection.len > 0 {
892 if fragment.visible && !intersection.visible {
893 let old_start = old_fragments.start().1
894 + (fragment_start.0 - old_fragments.start().0.full_offset().0);
895 let new_start = new_fragments.summary().text.visible;
896 edits.push(Edit {
897 old: old_start..old_start + intersection.len,
898 new: new_start..new_start,
899 });
900 }
901 new_insertions.push(InsertionFragment::insert_new(&intersection));
902 new_ropes.push_fragment(&intersection, fragment.visible);
903 new_fragments.push(intersection, &None);
904 fragment_start = intersection_end;
905 }
906 if fragment_end <= range.end {
907 old_fragments.next(&cx);
908 }
909 }
910 }
911
912 // If the current fragment has been partially consumed, then consume the rest of it
913 // and advance to the next fragment before slicing.
914 if fragment_start > old_fragments.start().0.full_offset() {
915 let fragment_end = old_fragments.end(&cx).0.full_offset();
916 if fragment_end > fragment_start {
917 let mut suffix = old_fragments.item().unwrap().clone();
918 suffix.len = fragment_end.0 - fragment_start.0;
919 suffix.insertion_offset += fragment_start - old_fragments.start().0.full_offset();
920 new_insertions.push(InsertionFragment::insert_new(&suffix));
921 new_ropes.push_fragment(&suffix, suffix.visible);
922 new_fragments.push(suffix, &None);
923 }
924 old_fragments.next(&cx);
925 }
926
927 let suffix = old_fragments.suffix(&cx);
928 new_ropes.push_tree(suffix.summary().text);
929 new_fragments.push_tree(suffix, &None);
930 let (visible_text, deleted_text) = new_ropes.finish();
931 drop(old_fragments);
932
933 self.snapshot.fragments = new_fragments;
934 self.snapshot.visible_text = visible_text;
935 self.snapshot.deleted_text = deleted_text;
936 self.snapshot.insertions.edit(new_insertions, &());
937 self.local_clock.observe(timestamp.local());
938 self.lamport_clock.observe(timestamp.lamport());
939 self.subscriptions.publish_mut(&edits);
940 }
941
942 fn apply_undo(&mut self, undo: &UndoOperation) -> Result<()> {
943 let mut edits = Patch::default();
944 self.snapshot.undo_map.insert(undo);
945
946 let mut cx = undo.version.clone();
947 for edit_id in undo.counts.keys().copied() {
948 cx.observe(edit_id);
949 }
950 let cx = Some(cx);
951
952 let mut old_fragments = self.fragments.cursor::<(VersionedFullOffset, usize)>();
953 let mut new_fragments = old_fragments.slice(
954 &VersionedFullOffset::Offset(undo.ranges[0].start),
955 Bias::Right,
956 &cx,
957 );
958 let mut new_ropes =
959 RopeBuilder::new(self.visible_text.cursor(0), self.deleted_text.cursor(0));
960 new_ropes.push_tree(new_fragments.summary().text);
961
962 for range in &undo.ranges {
963 let mut end_offset = old_fragments.end(&cx).0.full_offset();
964
965 if end_offset < range.start {
966 let preceding_fragments = old_fragments.slice(
967 &VersionedFullOffset::Offset(range.start),
968 Bias::Right,
969 &cx,
970 );
971 new_ropes.push_tree(preceding_fragments.summary().text);
972 new_fragments.push_tree(preceding_fragments, &None);
973 }
974
975 while end_offset <= range.end {
976 if let Some(fragment) = old_fragments.item() {
977 let mut fragment = fragment.clone();
978 let fragment_was_visible = fragment.visible;
979
980 if fragment.was_visible(&undo.version, &self.undo_map)
981 || undo
982 .counts
983 .contains_key(&fragment.insertion_timestamp.local())
984 {
985 fragment.visible = fragment.is_visible(&self.undo_map);
986 fragment.max_undos.observe(undo.id);
987 }
988
989 let old_start = old_fragments.start().1;
990 let new_start = new_fragments.summary().text.visible;
991 if fragment_was_visible && !fragment.visible {
992 edits.push(Edit {
993 old: old_start..old_start + fragment.len,
994 new: new_start..new_start,
995 });
996 } else if !fragment_was_visible && fragment.visible {
997 edits.push(Edit {
998 old: old_start..old_start,
999 new: new_start..new_start + fragment.len,
1000 });
1001 }
1002 new_ropes.push_fragment(&fragment, fragment_was_visible);
1003 new_fragments.push(fragment, &None);
1004
1005 old_fragments.next(&cx);
1006 if end_offset == old_fragments.end(&cx).0.full_offset() {
1007 let unseen_fragments = old_fragments.slice(
1008 &VersionedFullOffset::Offset(end_offset),
1009 Bias::Right,
1010 &cx,
1011 );
1012 new_ropes.push_tree(unseen_fragments.summary().text);
1013 new_fragments.push_tree(unseen_fragments, &None);
1014 }
1015 end_offset = old_fragments.end(&cx).0.full_offset();
1016 } else {
1017 break;
1018 }
1019 }
1020 }
1021
1022 let suffix = old_fragments.suffix(&cx);
1023 new_ropes.push_tree(suffix.summary().text);
1024 new_fragments.push_tree(suffix, &None);
1025
1026 drop(old_fragments);
1027 let (visible_text, deleted_text) = new_ropes.finish();
1028 self.snapshot.fragments = new_fragments;
1029 self.snapshot.visible_text = visible_text;
1030 self.snapshot.deleted_text = deleted_text;
1031 self.subscriptions.publish_mut(&edits);
1032 Ok(())
1033 }
1034
1035 fn flush_deferred_ops(&mut self) -> Result<()> {
1036 self.deferred_replicas.clear();
1037 let mut deferred_ops = Vec::new();
1038 for op in self.deferred_ops.drain().iter().cloned() {
1039 if self.can_apply_op(&op) {
1040 self.apply_op(op)?;
1041 } else {
1042 self.deferred_replicas.insert(op.replica_id());
1043 deferred_ops.push(op);
1044 }
1045 }
1046 self.deferred_ops.insert(deferred_ops);
1047 Ok(())
1048 }
1049
1050 fn can_apply_op(&self, op: &Operation) -> bool {
1051 if self.deferred_replicas.contains(&op.replica_id()) {
1052 false
1053 } else {
1054 match op {
1055 Operation::Edit(edit) => self.version.ge(&edit.version),
1056 Operation::Undo { undo, .. } => self.version.ge(&undo.version),
1057 }
1058 }
1059 }
1060
1061 pub fn can_resolve(&self, anchor: &Anchor) -> bool {
1062 *anchor == Anchor::min()
1063 || *anchor == Anchor::max()
1064 || self.version.observed(anchor.timestamp)
1065 }
1066
1067 pub fn peek_undo_stack(&self) -> Option<&Transaction> {
1068 self.history.undo_stack.last()
1069 }
1070
1071 pub fn start_transaction(&mut self) -> Option<TransactionId> {
1072 self.start_transaction_at(Instant::now())
1073 }
1074
1075 pub fn start_transaction_at(&mut self, now: Instant) -> Option<TransactionId> {
1076 self.history.start_transaction(self.version.clone(), now)
1077 }
1078
1079 pub fn end_transaction(&mut self) -> Option<(TransactionId, clock::Global)> {
1080 self.end_transaction_at(Instant::now())
1081 }
1082
1083 pub fn end_transaction_at(&mut self, now: Instant) -> Option<(TransactionId, clock::Global)> {
1084 if let Some(transaction) = self.history.end_transaction(now) {
1085 let since = transaction.start.clone();
1086 let id = self.history.group().unwrap();
1087 Some((id, since))
1088 } else {
1089 None
1090 }
1091 }
1092
1093 pub fn base_text(&self) -> &Arc<str> {
1094 &self.history.base_text
1095 }
1096
1097 pub fn history(&self) -> impl Iterator<Item = &EditOperation> {
1098 self.history.ops.values()
1099 }
1100
1101 pub fn undo(&mut self) -> Option<(TransactionId, Operation)> {
1102 if let Some(transaction) = self.history.pop_undo().cloned() {
1103 let transaction_id = transaction.id;
1104 let op = self.undo_or_redo(transaction).unwrap();
1105 Some((transaction_id, op))
1106 } else {
1107 None
1108 }
1109 }
1110
1111 pub fn redo(&mut self) -> Option<(TransactionId, Operation)> {
1112 if let Some(transaction) = self.history.pop_redo().cloned() {
1113 let transaction_id = transaction.id;
1114 let op = self.undo_or_redo(transaction).unwrap();
1115 Some((transaction_id, op))
1116 } else {
1117 None
1118 }
1119 }
1120
1121 fn undo_or_redo(&mut self, transaction: Transaction) -> Result<Operation> {
1122 let mut counts = HashMap::default();
1123 for edit_id in transaction.edits {
1124 counts.insert(edit_id, self.undo_map.undo_count(edit_id) + 1);
1125 }
1126
1127 let undo = UndoOperation {
1128 id: self.local_clock.tick(),
1129 counts,
1130 ranges: transaction.ranges,
1131 version: transaction.start.clone(),
1132 };
1133 self.apply_undo(&undo)?;
1134 self.snapshot.version.observe(undo.id);
1135
1136 Ok(Operation::Undo {
1137 undo,
1138 lamport_timestamp: self.lamport_clock.tick(),
1139 })
1140 }
1141
1142 pub fn subscribe(&mut self) -> Subscription {
1143 self.subscriptions.subscribe()
1144 }
1145}
1146
1147#[cfg(any(test, feature = "test-support"))]
1148impl Buffer {
1149 fn random_byte_range(&mut self, start_offset: usize, rng: &mut impl rand::Rng) -> Range<usize> {
1150 let end = self.clip_offset(rng.gen_range(start_offset..=self.len()), Bias::Right);
1151 let start = self.clip_offset(rng.gen_range(start_offset..=end), Bias::Right);
1152 start..end
1153 }
1154
1155 pub fn randomly_edit<T>(
1156 &mut self,
1157 rng: &mut T,
1158 old_range_count: usize,
1159 ) -> (Vec<Range<usize>>, String, Operation)
1160 where
1161 T: rand::Rng,
1162 {
1163 let mut old_ranges: Vec<Range<usize>> = Vec::new();
1164 for _ in 0..old_range_count {
1165 let last_end = old_ranges.last().map_or(0, |last_range| last_range.end + 1);
1166 if last_end > self.len() {
1167 break;
1168 }
1169 old_ranges.push(self.random_byte_range(last_end, rng));
1170 }
1171 let new_text_len = rng.gen_range(0..10);
1172 let new_text: String = crate::random_char_iter::RandomCharIter::new(&mut *rng)
1173 .take(new_text_len)
1174 .collect();
1175 log::info!(
1176 "mutating buffer {} at {:?}: {:?}",
1177 self.replica_id,
1178 old_ranges,
1179 new_text
1180 );
1181 let op = self.edit(old_ranges.iter().cloned(), new_text.as_str());
1182 (old_ranges, new_text, Operation::Edit(op))
1183 }
1184
1185 pub fn randomly_undo_redo(&mut self, rng: &mut impl rand::Rng) -> Vec<Operation> {
1186 use rand::prelude::*;
1187
1188 let mut ops = Vec::new();
1189 for _ in 0..rng.gen_range(1..=5) {
1190 if let Some(transaction) = self.history.undo_stack.choose(rng).cloned() {
1191 log::info!(
1192 "undoing buffer {} transaction {:?}",
1193 self.replica_id,
1194 transaction
1195 );
1196 ops.push(self.undo_or_redo(transaction).unwrap());
1197 }
1198 }
1199 ops
1200 }
1201}
1202
1203impl Deref for Buffer {
1204 type Target = BufferSnapshot;
1205
1206 fn deref(&self) -> &Self::Target {
1207 &self.snapshot
1208 }
1209}
1210
1211impl BufferSnapshot {
1212 pub fn as_rope(&self) -> &Rope {
1213 &self.visible_text
1214 }
1215
1216 pub fn replica_id(&self) -> ReplicaId {
1217 self.replica_id
1218 }
1219
1220 pub fn row_count(&self) -> u32 {
1221 self.max_point().row + 1
1222 }
1223
1224 pub fn len(&self) -> usize {
1225 self.visible_text.len()
1226 }
1227
1228 pub fn chars(&self) -> impl Iterator<Item = char> + '_ {
1229 self.chars_at(0)
1230 }
1231
1232 pub fn chars_for_range<T: ToOffset>(&self, range: Range<T>) -> impl Iterator<Item = char> + '_ {
1233 self.text_for_range(range).flat_map(str::chars)
1234 }
1235
1236 pub fn contains_str_at<T>(&self, position: T, needle: &str) -> bool
1237 where
1238 T: ToOffset,
1239 {
1240 let position = position.to_offset(self);
1241 position == self.clip_offset(position, Bias::Left)
1242 && self
1243 .bytes_in_range(position..self.len())
1244 .flatten()
1245 .copied()
1246 .take(needle.len())
1247 .eq(needle.bytes())
1248 }
1249
1250 pub fn text(&self) -> String {
1251 self.text_for_range(0..self.len()).collect()
1252 }
1253
1254 pub fn text_summary(&self) -> TextSummary {
1255 self.visible_text.summary()
1256 }
1257
1258 pub fn max_point(&self) -> Point {
1259 self.visible_text.max_point()
1260 }
1261
1262 pub fn point_to_offset(&self, point: Point) -> usize {
1263 self.visible_text.point_to_offset(point)
1264 }
1265
1266 pub fn point_utf16_to_offset(&self, point: PointUtf16) -> usize {
1267 self.visible_text.point_utf16_to_offset(point)
1268 }
1269
1270 pub fn offset_to_point(&self, offset: usize) -> Point {
1271 self.visible_text.offset_to_point(offset)
1272 }
1273
1274 pub fn offset_to_point_utf16(&self, offset: usize) -> PointUtf16 {
1275 self.visible_text.offset_to_point_utf16(offset)
1276 }
1277
1278 pub fn version(&self) -> &clock::Global {
1279 &self.version
1280 }
1281
1282 pub fn chars_at<'a, T: ToOffset>(&'a self, position: T) -> impl Iterator<Item = char> + 'a {
1283 let offset = position.to_offset(self);
1284 self.visible_text.chars_at(offset)
1285 }
1286
1287 pub fn reversed_chars_at<'a, T: ToOffset>(
1288 &'a self,
1289 position: T,
1290 ) -> impl Iterator<Item = char> + 'a {
1291 let offset = position.to_offset(self);
1292 self.visible_text.reversed_chars_at(offset)
1293 }
1294
1295 pub fn bytes_in_range<'a, T: ToOffset>(&'a self, range: Range<T>) -> rope::Bytes<'a> {
1296 let start = range.start.to_offset(self);
1297 let end = range.end.to_offset(self);
1298 self.visible_text.bytes_in_range(start..end)
1299 }
1300
1301 pub fn text_for_range<'a, T: ToOffset>(&'a self, range: Range<T>) -> Chunks<'a> {
1302 let start = range.start.to_offset(self);
1303 let end = range.end.to_offset(self);
1304 self.visible_text.chunks_in_range(start..end)
1305 }
1306
1307 pub fn line_len(&self, row: u32) -> u32 {
1308 let row_start_offset = Point::new(row, 0).to_offset(self);
1309 let row_end_offset = if row >= self.max_point().row {
1310 self.len()
1311 } else {
1312 Point::new(row + 1, 0).to_offset(self) - 1
1313 };
1314 (row_end_offset - row_start_offset) as u32
1315 }
1316
1317 pub fn is_line_blank(&self, row: u32) -> bool {
1318 self.text_for_range(Point::new(row, 0)..Point::new(row, self.line_len(row)))
1319 .all(|chunk| chunk.matches(|c: char| !c.is_whitespace()).next().is_none())
1320 }
1321
1322 pub fn indent_column_for_line(&self, row: u32) -> u32 {
1323 let mut result = 0;
1324 for c in self.chars_at(Point::new(row, 0)) {
1325 if c == ' ' {
1326 result += 1;
1327 } else {
1328 break;
1329 }
1330 }
1331 result
1332 }
1333
1334 pub fn text_summary_for_range<'a, D, O: ToOffset>(&'a self, range: Range<O>) -> D
1335 where
1336 D: TextDimension,
1337 {
1338 self.visible_text
1339 .cursor(range.start.to_offset(self))
1340 .summary(range.end.to_offset(self))
1341 }
1342
1343 pub fn summaries_for_anchors<'a, D, A>(&'a self, anchors: A) -> impl 'a + Iterator<Item = D>
1344 where
1345 D: 'a + TextDimension,
1346 A: 'a + IntoIterator<Item = &'a Anchor>,
1347 {
1348 let anchors = anchors.into_iter();
1349 let mut insertion_cursor = self.insertions.cursor::<InsertionFragmentKey>();
1350 let mut fragment_cursor = self.fragments.cursor::<(Option<&Locator>, usize)>();
1351 let mut text_cursor = self.visible_text.cursor(0);
1352 let mut position = D::default();
1353
1354 anchors.map(move |anchor| {
1355 if *anchor == Anchor::min() {
1356 return D::default();
1357 } else if *anchor == Anchor::max() {
1358 return D::from_text_summary(&self.visible_text.summary());
1359 }
1360
1361 let anchor_key = InsertionFragmentKey {
1362 timestamp: anchor.timestamp,
1363 split_offset: anchor.offset,
1364 };
1365 insertion_cursor.seek(&anchor_key, anchor.bias, &());
1366 if let Some(insertion) = insertion_cursor.item() {
1367 let comparison = sum_tree::KeyedItem::key(insertion).cmp(&anchor_key);
1368 if comparison == Ordering::Greater
1369 || (anchor.bias == Bias::Left
1370 && comparison == Ordering::Equal
1371 && anchor.offset > 0)
1372 {
1373 insertion_cursor.prev(&());
1374 }
1375 } else {
1376 insertion_cursor.prev(&());
1377 }
1378 let insertion = insertion_cursor.item().expect("invalid insertion");
1379 debug_assert_eq!(insertion.timestamp, anchor.timestamp, "invalid insertion");
1380
1381 fragment_cursor.seek_forward(&Some(&insertion.fragment_id), Bias::Left, &None);
1382 let fragment = fragment_cursor.item().unwrap();
1383 let mut fragment_offset = fragment_cursor.start().1;
1384 if fragment.visible {
1385 fragment_offset += anchor.offset - insertion.split_offset;
1386 }
1387
1388 position.add_assign(&text_cursor.summary(fragment_offset));
1389 position.clone()
1390 })
1391 }
1392
1393 fn summary_for_anchor<'a, D>(&'a self, anchor: &Anchor) -> D
1394 where
1395 D: TextDimension,
1396 {
1397 if *anchor == Anchor::min() {
1398 D::default()
1399 } else if *anchor == Anchor::max() {
1400 D::from_text_summary(&self.visible_text.summary())
1401 } else {
1402 let anchor_key = InsertionFragmentKey {
1403 timestamp: anchor.timestamp,
1404 split_offset: anchor.offset,
1405 };
1406 let mut insertion_cursor = self.insertions.cursor::<InsertionFragmentKey>();
1407 insertion_cursor.seek(&anchor_key, anchor.bias, &());
1408 if let Some(insertion) = insertion_cursor.item() {
1409 let comparison = sum_tree::KeyedItem::key(insertion).cmp(&anchor_key);
1410 if comparison == Ordering::Greater
1411 || (anchor.bias == Bias::Left
1412 && comparison == Ordering::Equal
1413 && anchor.offset > 0)
1414 {
1415 insertion_cursor.prev(&());
1416 }
1417 } else {
1418 insertion_cursor.prev(&());
1419 }
1420 let insertion = insertion_cursor.item().expect("invalid insertion");
1421 debug_assert_eq!(insertion.timestamp, anchor.timestamp, "invalid insertion");
1422
1423 let mut fragment_cursor = self.fragments.cursor::<(Option<&Locator>, usize)>();
1424 fragment_cursor.seek(&Some(&insertion.fragment_id), Bias::Left, &None);
1425 let fragment = fragment_cursor.item().unwrap();
1426 let mut fragment_offset = fragment_cursor.start().1;
1427 if fragment.visible {
1428 fragment_offset += anchor.offset - insertion.split_offset;
1429 }
1430 self.text_summary_for_range(0..fragment_offset)
1431 }
1432 }
1433
1434 fn fragment_id_for_anchor(&self, anchor: &Anchor) -> &Locator {
1435 if *anchor == Anchor::min() {
1436 &locator::MIN
1437 } else if *anchor == Anchor::max() {
1438 &locator::MAX
1439 } else {
1440 let anchor_key = InsertionFragmentKey {
1441 timestamp: anchor.timestamp,
1442 split_offset: anchor.offset,
1443 };
1444 let mut insertion_cursor = self.insertions.cursor::<InsertionFragmentKey>();
1445 insertion_cursor.seek(&anchor_key, anchor.bias, &());
1446 if let Some(insertion) = insertion_cursor.item() {
1447 let comparison = sum_tree::KeyedItem::key(insertion).cmp(&anchor_key);
1448 if comparison == Ordering::Greater
1449 || (anchor.bias == Bias::Left
1450 && comparison == Ordering::Equal
1451 && anchor.offset > 0)
1452 {
1453 insertion_cursor.prev(&());
1454 }
1455 } else {
1456 insertion_cursor.prev(&());
1457 }
1458 let insertion = insertion_cursor.item().expect("invalid insertion");
1459 debug_assert_eq!(insertion.timestamp, anchor.timestamp, "invalid insertion");
1460 &insertion.fragment_id
1461 }
1462 }
1463
1464 pub fn anchor_before<T: ToOffset>(&self, position: T) -> Anchor {
1465 self.anchor_at(position, Bias::Left)
1466 }
1467
1468 pub fn anchor_after<T: ToOffset>(&self, position: T) -> Anchor {
1469 self.anchor_at(position, Bias::Right)
1470 }
1471
1472 pub fn anchor_at<T: ToOffset>(&self, position: T, bias: Bias) -> Anchor {
1473 let offset = position.to_offset(self);
1474 if bias == Bias::Left && offset == 0 {
1475 Anchor::min()
1476 } else if bias == Bias::Right && offset == self.len() {
1477 Anchor::max()
1478 } else {
1479 let mut fragment_cursor = self.fragments.cursor::<usize>();
1480 fragment_cursor.seek(&offset, bias, &None);
1481 let fragment = fragment_cursor.item().unwrap();
1482 let overshoot = offset - *fragment_cursor.start();
1483 Anchor {
1484 timestamp: fragment.insertion_timestamp.local(),
1485 offset: fragment.insertion_offset + overshoot,
1486 bias,
1487 }
1488 }
1489 }
1490
1491 pub fn clip_offset(&self, offset: usize, bias: Bias) -> usize {
1492 self.visible_text.clip_offset(offset, bias)
1493 }
1494
1495 pub fn clip_point(&self, point: Point, bias: Bias) -> Point {
1496 self.visible_text.clip_point(point, bias)
1497 }
1498
1499 pub fn clip_point_utf16(&self, point: PointUtf16, bias: Bias) -> PointUtf16 {
1500 self.visible_text.clip_point_utf16(point, bias)
1501 }
1502
1503 // pub fn point_for_offset(&self, offset: usize) -> Result<Point> {
1504 // if offset <= self.len() {
1505 // Ok(self.text_summary_for_range(0..offset))
1506 // } else {
1507 // Err(anyhow!("offset out of bounds"))
1508 // }
1509 // }
1510
1511 pub fn edits_since<'a, D>(
1512 &'a self,
1513 since: &'a clock::Global,
1514 ) -> impl 'a + Iterator<Item = Edit<D>>
1515 where
1516 D: TextDimension + Ord,
1517 {
1518 self.edits_since_in_range(since, Anchor::min()..Anchor::max())
1519 }
1520
1521 pub fn edits_since_in_range<'a, D>(
1522 &'a self,
1523 since: &'a clock::Global,
1524 range: Range<Anchor>,
1525 ) -> impl 'a + Iterator<Item = Edit<D>>
1526 where
1527 D: TextDimension + Ord,
1528 {
1529 let fragments_cursor = if *since == self.version {
1530 None
1531 } else {
1532 Some(
1533 self.fragments
1534 .filter(move |summary| !since.ge(&summary.max_version), &None),
1535 )
1536 };
1537 let mut cursor = self
1538 .fragments
1539 .cursor::<(Option<&Locator>, FragmentTextSummary)>();
1540
1541 let start_fragment_id = self.fragment_id_for_anchor(&range.start);
1542 cursor.seek(&Some(start_fragment_id), Bias::Left, &None);
1543 let mut visible_start = cursor.start().1.visible;
1544 let mut deleted_start = cursor.start().1.deleted;
1545 if let Some(fragment) = cursor.item() {
1546 let overshoot = range.start.offset - fragment.insertion_offset;
1547 if fragment.visible {
1548 visible_start += overshoot;
1549 } else {
1550 deleted_start += overshoot;
1551 }
1552 }
1553 let end_fragment_id = self.fragment_id_for_anchor(&range.end);
1554
1555 Edits {
1556 visible_cursor: self.visible_text.cursor(visible_start),
1557 deleted_cursor: self.deleted_text.cursor(deleted_start),
1558 fragments_cursor,
1559 undos: &self.undo_map,
1560 since,
1561 old_end: Default::default(),
1562 new_end: Default::default(),
1563 range: (start_fragment_id, range.start.offset)..(end_fragment_id, range.end.offset),
1564 }
1565 }
1566}
1567
1568struct RopeBuilder<'a> {
1569 old_visible_cursor: rope::Cursor<'a>,
1570 old_deleted_cursor: rope::Cursor<'a>,
1571 new_visible: Rope,
1572 new_deleted: Rope,
1573}
1574
1575impl<'a> RopeBuilder<'a> {
1576 fn new(old_visible_cursor: rope::Cursor<'a>, old_deleted_cursor: rope::Cursor<'a>) -> Self {
1577 Self {
1578 old_visible_cursor,
1579 old_deleted_cursor,
1580 new_visible: Rope::new(),
1581 new_deleted: Rope::new(),
1582 }
1583 }
1584
1585 fn push_tree(&mut self, len: FragmentTextSummary) {
1586 self.push(len.visible, true, true);
1587 self.push(len.deleted, false, false);
1588 }
1589
1590 fn push_fragment(&mut self, fragment: &Fragment, was_visible: bool) {
1591 debug_assert!(fragment.len > 0);
1592 self.push(fragment.len, was_visible, fragment.visible)
1593 }
1594
1595 fn push(&mut self, len: usize, was_visible: bool, is_visible: bool) {
1596 let text = if was_visible {
1597 self.old_visible_cursor
1598 .slice(self.old_visible_cursor.offset() + len)
1599 } else {
1600 self.old_deleted_cursor
1601 .slice(self.old_deleted_cursor.offset() + len)
1602 };
1603 if is_visible {
1604 self.new_visible.append(text);
1605 } else {
1606 self.new_deleted.append(text);
1607 }
1608 }
1609
1610 fn push_str(&mut self, text: &str) {
1611 self.new_visible.push(text);
1612 }
1613
1614 fn finish(mut self) -> (Rope, Rope) {
1615 self.new_visible.append(self.old_visible_cursor.suffix());
1616 self.new_deleted.append(self.old_deleted_cursor.suffix());
1617 (self.new_visible, self.new_deleted)
1618 }
1619}
1620
1621impl<'a, D: TextDimension + Ord, F: FnMut(&FragmentSummary) -> bool> Iterator for Edits<'a, D, F> {
1622 type Item = Edit<D>;
1623
1624 fn next(&mut self) -> Option<Self::Item> {
1625 let mut pending_edit: Option<Edit<D>> = None;
1626 let cursor = self.fragments_cursor.as_mut()?;
1627
1628 while let Some(fragment) = cursor.item() {
1629 if fragment.id < *self.range.start.0 {
1630 cursor.next(&None);
1631 continue;
1632 } else if fragment.id > *self.range.end.0 {
1633 break;
1634 }
1635
1636 if cursor.start().visible > self.visible_cursor.offset() {
1637 let summary = self.visible_cursor.summary(cursor.start().visible);
1638 self.old_end.add_assign(&summary);
1639 self.new_end.add_assign(&summary);
1640 }
1641
1642 if pending_edit
1643 .as_ref()
1644 .map_or(false, |change| change.new.end < self.new_end)
1645 {
1646 break;
1647 }
1648
1649 if !fragment.was_visible(&self.since, &self.undos) && fragment.visible {
1650 let mut visible_end = cursor.end(&None).visible;
1651 if fragment.id == *self.range.end.0 {
1652 visible_end = cmp::min(
1653 visible_end,
1654 cursor.start().visible + (self.range.end.1 - fragment.insertion_offset),
1655 );
1656 }
1657
1658 let fragment_summary = self.visible_cursor.summary(visible_end);
1659 let mut new_end = self.new_end.clone();
1660 new_end.add_assign(&fragment_summary);
1661 if let Some(pending_edit) = pending_edit.as_mut() {
1662 pending_edit.new.end = new_end.clone();
1663 } else {
1664 pending_edit = Some(Edit {
1665 old: self.old_end.clone()..self.old_end.clone(),
1666 new: self.new_end.clone()..new_end.clone(),
1667 });
1668 }
1669
1670 self.new_end = new_end;
1671 } else if fragment.was_visible(&self.since, &self.undos) && !fragment.visible {
1672 let mut deleted_end = cursor.end(&None).deleted;
1673 if fragment.id == *self.range.end.0 {
1674 deleted_end = cmp::min(
1675 deleted_end,
1676 cursor.start().deleted + (self.range.end.1 - fragment.insertion_offset),
1677 );
1678 }
1679
1680 if cursor.start().deleted > self.deleted_cursor.offset() {
1681 self.deleted_cursor.seek_forward(cursor.start().deleted);
1682 }
1683 let fragment_summary = self.deleted_cursor.summary(deleted_end);
1684 let mut old_end = self.old_end.clone();
1685 old_end.add_assign(&fragment_summary);
1686 if let Some(pending_edit) = pending_edit.as_mut() {
1687 pending_edit.old.end = old_end.clone();
1688 } else {
1689 pending_edit = Some(Edit {
1690 old: self.old_end.clone()..old_end.clone(),
1691 new: self.new_end.clone()..self.new_end.clone(),
1692 });
1693 }
1694
1695 self.old_end = old_end;
1696 }
1697
1698 cursor.next(&None);
1699 }
1700
1701 pending_edit
1702 }
1703}
1704
1705impl Fragment {
1706 fn is_visible(&self, undos: &UndoMap) -> bool {
1707 !undos.is_undone(self.insertion_timestamp.local())
1708 && self.deletions.iter().all(|d| undos.is_undone(*d))
1709 }
1710
1711 fn was_visible(&self, version: &clock::Global, undos: &UndoMap) -> bool {
1712 (version.observed(self.insertion_timestamp.local())
1713 && !undos.was_undone(self.insertion_timestamp.local(), version))
1714 && self
1715 .deletions
1716 .iter()
1717 .all(|d| !version.observed(*d) || undos.was_undone(*d, version))
1718 }
1719}
1720
1721impl sum_tree::Item for Fragment {
1722 type Summary = FragmentSummary;
1723
1724 fn summary(&self) -> Self::Summary {
1725 let mut max_version = clock::Global::new();
1726 max_version.observe(self.insertion_timestamp.local());
1727 for deletion in &self.deletions {
1728 max_version.observe(*deletion);
1729 }
1730 max_version.join(&self.max_undos);
1731
1732 let mut min_insertion_version = clock::Global::new();
1733 min_insertion_version.observe(self.insertion_timestamp.local());
1734 let max_insertion_version = min_insertion_version.clone();
1735 if self.visible {
1736 FragmentSummary {
1737 max_id: self.id.clone(),
1738 text: FragmentTextSummary {
1739 visible: self.len,
1740 deleted: 0,
1741 },
1742 max_version,
1743 min_insertion_version,
1744 max_insertion_version,
1745 }
1746 } else {
1747 FragmentSummary {
1748 max_id: self.id.clone(),
1749 text: FragmentTextSummary {
1750 visible: 0,
1751 deleted: self.len,
1752 },
1753 max_version,
1754 min_insertion_version,
1755 max_insertion_version,
1756 }
1757 }
1758 }
1759}
1760
1761impl sum_tree::Summary for FragmentSummary {
1762 type Context = Option<clock::Global>;
1763
1764 fn add_summary(&mut self, other: &Self, _: &Self::Context) {
1765 self.max_id.assign(&other.max_id);
1766 self.text.visible += &other.text.visible;
1767 self.text.deleted += &other.text.deleted;
1768 self.max_version.join(&other.max_version);
1769 self.min_insertion_version
1770 .meet(&other.min_insertion_version);
1771 self.max_insertion_version
1772 .join(&other.max_insertion_version);
1773 }
1774}
1775
1776impl Default for FragmentSummary {
1777 fn default() -> Self {
1778 FragmentSummary {
1779 max_id: Locator::min(),
1780 text: FragmentTextSummary::default(),
1781 max_version: clock::Global::new(),
1782 min_insertion_version: clock::Global::new(),
1783 max_insertion_version: clock::Global::new(),
1784 }
1785 }
1786}
1787
1788impl sum_tree::Item for InsertionFragment {
1789 type Summary = InsertionFragmentKey;
1790
1791 fn summary(&self) -> Self::Summary {
1792 InsertionFragmentKey {
1793 timestamp: self.timestamp,
1794 split_offset: self.split_offset,
1795 }
1796 }
1797}
1798
1799impl sum_tree::KeyedItem for InsertionFragment {
1800 type Key = InsertionFragmentKey;
1801
1802 fn key(&self) -> Self::Key {
1803 sum_tree::Item::summary(self)
1804 }
1805}
1806
1807impl InsertionFragment {
1808 fn new(fragment: &Fragment) -> Self {
1809 Self {
1810 timestamp: fragment.insertion_timestamp.local(),
1811 split_offset: fragment.insertion_offset,
1812 fragment_id: fragment.id.clone(),
1813 }
1814 }
1815
1816 fn insert_new(fragment: &Fragment) -> sum_tree::Edit<Self> {
1817 sum_tree::Edit::Insert(Self::new(fragment))
1818 }
1819}
1820
1821impl sum_tree::Summary for InsertionFragmentKey {
1822 type Context = ();
1823
1824 fn add_summary(&mut self, summary: &Self, _: &()) {
1825 *self = *summary;
1826 }
1827}
1828
1829#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
1830pub struct FullOffset(pub usize);
1831
1832impl ops::AddAssign<usize> for FullOffset {
1833 fn add_assign(&mut self, rhs: usize) {
1834 self.0 += rhs;
1835 }
1836}
1837
1838impl ops::Add<usize> for FullOffset {
1839 type Output = Self;
1840
1841 fn add(mut self, rhs: usize) -> Self::Output {
1842 self += rhs;
1843 self
1844 }
1845}
1846
1847impl ops::Sub for FullOffset {
1848 type Output = usize;
1849
1850 fn sub(self, rhs: Self) -> Self::Output {
1851 self.0 - rhs.0
1852 }
1853}
1854
1855impl<'a> sum_tree::Dimension<'a, FragmentSummary> for usize {
1856 fn add_summary(&mut self, summary: &FragmentSummary, _: &Option<clock::Global>) {
1857 *self += summary.text.visible;
1858 }
1859}
1860
1861impl<'a> sum_tree::Dimension<'a, FragmentSummary> for FullOffset {
1862 fn add_summary(&mut self, summary: &FragmentSummary, _: &Option<clock::Global>) {
1863 self.0 += summary.text.visible + summary.text.deleted;
1864 }
1865}
1866
1867impl<'a> sum_tree::Dimension<'a, FragmentSummary> for Option<&'a Locator> {
1868 fn add_summary(&mut self, summary: &'a FragmentSummary, _: &Option<clock::Global>) {
1869 *self = Some(&summary.max_id);
1870 }
1871}
1872
1873impl<'a> sum_tree::SeekTarget<'a, FragmentSummary, FragmentTextSummary> for usize {
1874 fn cmp(
1875 &self,
1876 cursor_location: &FragmentTextSummary,
1877 _: &Option<clock::Global>,
1878 ) -> cmp::Ordering {
1879 Ord::cmp(self, &cursor_location.visible)
1880 }
1881}
1882
1883#[derive(Copy, Clone, Debug, Eq, PartialEq)]
1884enum VersionedFullOffset {
1885 Offset(FullOffset),
1886 Invalid,
1887}
1888
1889impl VersionedFullOffset {
1890 fn full_offset(&self) -> FullOffset {
1891 if let Self::Offset(position) = self {
1892 *position
1893 } else {
1894 panic!("invalid version")
1895 }
1896 }
1897}
1898
1899impl Default for VersionedFullOffset {
1900 fn default() -> Self {
1901 Self::Offset(Default::default())
1902 }
1903}
1904
1905impl<'a> sum_tree::Dimension<'a, FragmentSummary> for VersionedFullOffset {
1906 fn add_summary(&mut self, summary: &'a FragmentSummary, cx: &Option<clock::Global>) {
1907 if let Self::Offset(offset) = self {
1908 let version = cx.as_ref().unwrap();
1909 if version.ge(&summary.max_insertion_version) {
1910 *offset += summary.text.visible + summary.text.deleted;
1911 } else if version.observed_any(&summary.min_insertion_version) {
1912 *self = Self::Invalid;
1913 }
1914 }
1915 }
1916}
1917
1918impl<'a> sum_tree::SeekTarget<'a, FragmentSummary, Self> for VersionedFullOffset {
1919 fn cmp(&self, cursor_position: &Self, _: &Option<clock::Global>) -> cmp::Ordering {
1920 match (self, cursor_position) {
1921 (Self::Offset(a), Self::Offset(b)) => Ord::cmp(a, b),
1922 (Self::Offset(_), Self::Invalid) => cmp::Ordering::Less,
1923 (Self::Invalid, _) => unreachable!(),
1924 }
1925 }
1926}
1927
1928impl Operation {
1929 fn replica_id(&self) -> ReplicaId {
1930 operation_queue::Operation::lamport_timestamp(self).replica_id
1931 }
1932
1933 pub fn is_edit(&self) -> bool {
1934 match self {
1935 Operation::Edit { .. } => true,
1936 _ => false,
1937 }
1938 }
1939}
1940
1941impl operation_queue::Operation for Operation {
1942 fn lamport_timestamp(&self) -> clock::Lamport {
1943 match self {
1944 Operation::Edit(edit) => edit.timestamp.lamport(),
1945 Operation::Undo {
1946 lamport_timestamp, ..
1947 } => *lamport_timestamp,
1948 }
1949 }
1950}
1951
1952pub trait ToOffset {
1953 fn to_offset<'a>(&self, snapshot: &BufferSnapshot) -> usize;
1954}
1955
1956impl ToOffset for Point {
1957 fn to_offset<'a>(&self, snapshot: &BufferSnapshot) -> usize {
1958 snapshot.point_to_offset(*self)
1959 }
1960}
1961
1962impl ToOffset for PointUtf16 {
1963 fn to_offset<'a>(&self, snapshot: &BufferSnapshot) -> usize {
1964 snapshot.point_utf16_to_offset(*self)
1965 }
1966}
1967
1968impl ToOffset for usize {
1969 fn to_offset<'a>(&self, snapshot: &BufferSnapshot) -> usize {
1970 assert!(*self <= snapshot.len(), "offset is out of range");
1971 *self
1972 }
1973}
1974
1975impl ToOffset for Anchor {
1976 fn to_offset<'a>(&self, snapshot: &BufferSnapshot) -> usize {
1977 snapshot.summary_for_anchor(self)
1978 }
1979}
1980
1981impl<'a, T: ToOffset> ToOffset for &'a T {
1982 fn to_offset(&self, content: &BufferSnapshot) -> usize {
1983 (*self).to_offset(content)
1984 }
1985}
1986
1987pub trait ToPoint {
1988 fn to_point<'a>(&self, snapshot: &BufferSnapshot) -> Point;
1989}
1990
1991impl ToPoint for Anchor {
1992 fn to_point<'a>(&self, snapshot: &BufferSnapshot) -> Point {
1993 snapshot.summary_for_anchor(self)
1994 }
1995}
1996
1997impl ToPoint for usize {
1998 fn to_point<'a>(&self, snapshot: &BufferSnapshot) -> Point {
1999 snapshot.offset_to_point(*self)
2000 }
2001}
2002
2003impl ToPoint for Point {
2004 fn to_point<'a>(&self, _: &BufferSnapshot) -> Point {
2005 *self
2006 }
2007}
2008
2009pub trait FromAnchor {
2010 fn from_anchor(anchor: &Anchor, snapshot: &BufferSnapshot) -> Self;
2011}
2012
2013impl FromAnchor for Point {
2014 fn from_anchor(anchor: &Anchor, snapshot: &BufferSnapshot) -> Self {
2015 anchor.to_point(snapshot)
2016 }
2017}
2018
2019impl FromAnchor for usize {
2020 fn from_anchor(anchor: &Anchor, snapshot: &BufferSnapshot) -> Self {
2021 anchor.to_offset(snapshot)
2022 }
2023}