Skip to main content

lb_rs/model/text/
buffer.rs

1use super::offset_types::{DocByteOffset, DocCharOffset, RangeExt, RelCharOffset};
2use super::operation_types::{InverseOperation, Operation, Replace};
3use super::unicode_segs::UnicodeSegs;
4use super::{diff, unicode_segs};
5use std::ops::Index;
6use std::time::{Duration, Instant};
7use unicode_segmentation::UnicodeSegmentation;
8
9/// Long-lived state of the editor's text buffer. Factored into sub-structs for borrow-checking.
10/// # Operation algebra
11/// Operations are created based on a version of the buffer. This version is called the operation's base and is
12/// identified with a sequence number. When the base of an operation is equal to the buffer's current sequence number,
13/// the operation can be applied and increments the buffer's sequence number.
14///
15/// When multiple operations are created based on the same version of the buffer, such as when a user types a few
16/// keystrokes in one frame or issues a command like indenting multiple list items, the operations all have the same
17/// base. Once the first operation is applied and the buffer's sequence number is incremented, the base of the
18/// remaining operations must be incremented by using the first operation to transform them before they can be applied.
19/// This corresponds to the reality that the buffer state has changed since the operation was created and the operation
20/// must be re-interpreted. For example, if text is typed at the beginning then end of a buffer in one frame, the
21/// position of the text typed at the end of the buffer is greater when it is applied than it was when it was typed.
22///
23/// External changes are merged into the buffer by creating a set of operations that would transform the buffer from
24/// the last external state to the current state. These operations, based on the version of the buffer at the last
25/// successful save or load, must be transformed by all operations that have been applied since (this means we must
26/// preserve the undo history for at least that long; if this presents performance issues, we can always save). Each
27/// operation that is transforming the new operations will match the base of the new operations at the time of
28/// transformation. Finally, the operations will need to transform each other just like any other set of operations
29/// made in a single frame/made based on the same version of the buffer.
30///
31/// # Undo (work in progress)
32/// Undo should revert local changes only, leaving external changes in-tact, so that when all local changes are undone,
33/// the buffer is in a state reflecting external changes only. This is complicated by the fact that external changes
34/// may have been based on local changes that were synced to another client. To undo an operation that had an external
35/// change based on it, we have to interpret the external change in the absence of local changes that were present when
36/// it was created. This is the opposite of interpreting the external change in the presence of local changes that were
37/// not present when it was created i.e. the normal flow of merging external changes. Here, we are removing a local
38/// operation from the middle of the chain of operations that led to the current state of the buffer.
39///
40/// To do this, we perform the dance of transforming operations in reverse, taking a chain of operations each based on
41/// the prior and transforming them into a set of operations based on the same base as the operation to be undone. Then
42/// we remove the operation to be undone and apply the remaining operations with the forward transformation flow.
43///
44/// Operations are not invertible i.e. you cannot construct an inverse operation that will perfectly cancel out the
45/// effect of another operation regardless of the time of interpretation. For example, with a text replacement, you can
46/// construct an inverse text replacement that replaces the new range with the original text, but when operations are
47/// undone from the middle of the chain, it may affect the original text. The operation will be re-interpreted based on
48/// a new state of the buffer at its time of application. The replaced text has no fixed value by design.
49///
50/// However, it is possible to undo the specific application of an operation in the context of the state of the buffer
51/// when it was applied. We store information necessary to undo applied operations alongside the operations themselves
52/// i.e. the text replaced in the application. When the operation is transformed for any reason, this undo information
53/// is invalidated.
54#[derive(Default)]
55pub struct Buffer {
56    /// Current contents of the buffer (what should be displayed in the editor). Todo: hide behind a read-only accessor
57    pub current: Snapshot,
58
59    /// Snapshot of the buffer at the earliest undoable state. Operations are compacted into this as they overflow the
60    /// undo limit.
61    base: Snapshot,
62
63    /// Operations received by the buffer. Used for undo/redo and for merging external changes.
64    ops: Ops,
65
66    /// State for tracking out-of-editor changes
67    external: External,
68}
69
70#[derive(Debug, Default)]
71pub struct Snapshot {
72    pub text: String,
73    pub segs: UnicodeSegs,
74    pub selection: (DocCharOffset, DocCharOffset),
75    pub seq: usize,
76}
77
78impl Snapshot {
79    fn apply_select(&mut self, range: (DocCharOffset, DocCharOffset)) -> Response {
80        self.selection = range;
81        Response { text_updated: false, open_camera: false }
82    }
83
84    fn apply_replace(&mut self, replace: &Replace) -> Response {
85        let Replace { range, text } = replace;
86        let byte_range = self.segs.range_to_byte(*range);
87
88        self.text
89            .replace_range(byte_range.start().0..byte_range.end().0, text);
90        self.segs = unicode_segs::calc(&self.text);
91        adjust_subsequent_range(
92            *range,
93            text.graphemes(true).count().into(),
94            false,
95            &mut self.selection,
96        );
97
98        Response { text_updated: true, open_camera: false }
99    }
100
101    fn invert(&self, op: &Operation) -> InverseOperation {
102        let mut inverse = InverseOperation { replace: None, select: self.selection };
103        if let Operation::Replace(replace) = op {
104            inverse.replace = Some(self.invert_replace(replace));
105        }
106        inverse
107    }
108
109    fn invert_replace(&self, replace: &Replace) -> Replace {
110        let Replace { range, text } = replace;
111        let byte_range = self.segs.range_to_byte(*range);
112        let replaced_text = self[byte_range].into();
113        let replacement_range = (range.start(), range.start() + text.graphemes(true).count());
114        Replace { range: replacement_range, text: replaced_text }
115    }
116}
117
118#[derive(Default)]
119struct Ops {
120    /// Operations that have been received by the buffer
121    all: Vec<Operation>,
122    meta: Vec<OpMeta>,
123
124    /// Sequence number of the first unapplied operation. Operations starting with this one are queued for processing.
125    processed_seq: usize,
126
127    /// Operations that have been applied to the buffer and already transformed, in order of application. Each of these
128    /// operations is based on the previous operation in this list, with the first based on the history base. Derived
129    /// from other data and invalidated by some undo/redo flows.
130    transformed: Vec<Operation>,
131
132    /// Operations representing the inverse of the operations in `transformed_ops`, in order of application. Useful for
133    /// undoing operations. The data model differs because an operation that replaces text containing the cursor needs
134    /// two operations to revert the text and cursor. Derived from other data and invalidated by some undo/redo flows.
135    transformed_inverted: Vec<InverseOperation>,
136}
137
138impl Ops {
139    fn len(&self) -> usize {
140        self.all.len()
141    }
142
143    fn is_undo_checkpoint(&self, idx: usize) -> bool {
144        // start and end of undo history are checkpoints
145        if idx == 0 {
146            return true;
147        }
148        if idx == self.len() {
149            return true;
150        }
151
152        // events separated by enough time are checkpoints
153        let meta = &self.meta[idx];
154        let prev_meta = &self.meta[idx - 1];
155        if meta.timestamp - prev_meta.timestamp > Duration::from_millis(500) {
156            return true;
157        }
158
159        // immediately after a standalone selection is a checkpoint
160        let mut prev_op_standalone = meta.base != prev_meta.base;
161        if idx > 1 {
162            let prev_prev_meta = &self.meta[idx - 2];
163            prev_op_standalone &= prev_meta.base != prev_prev_meta.base;
164        }
165        let prev_op_selection = matches!(&self.all[idx - 1], Operation::Select(..));
166        if prev_op_standalone && prev_op_selection {
167            return true;
168        }
169
170        false
171    }
172}
173
174#[derive(Default)]
175struct External {
176    /// Text last loaded into the editor. Used as a reference point for merging out-of-editor changes with in-editor
177    /// changes, similar to a base in a 3-way merge. May be a state that never appears in the buffer's history.
178    text: String,
179
180    /// Index of the last external operation referenced when merging changes. May be ahead of current.seq if there has
181    /// not been a call to `update()` (updates current.seq) since the last call to `reload()` (assigns new greatest seq
182    /// to `external_seq`).
183    seq: usize,
184}
185
186#[derive(Default)]
187pub struct Response {
188    pub text_updated: bool,
189    pub open_camera: bool,
190}
191
192impl std::ops::BitOrAssign for Response {
193    fn bitor_assign(&mut self, other: Response) {
194        self.text_updated |= other.text_updated;
195        self.open_camera |= other.open_camera;
196    }
197}
198
199/// Additional metadata tracked alongside operations internally.
200#[derive(Clone, Debug)]
201struct OpMeta {
202    /// At what time was this operation applied? Affects undo units.
203    pub timestamp: Instant,
204
205    /// What version of the buffer was the modifier looking at when they made this operation? Used for operational
206    /// transformation, both when applying multiple operations in one frame and when merging out-of-editor changes.
207    /// The magic happens here.
208    pub base: usize,
209}
210
211impl Buffer {
212    /// Push a series of operations onto the buffer's input queue; operations will be undone/redone atomically. Useful
213    /// for batches of internal operations produced from a single input event e.g. multi-line list identation.
214    pub fn queue(&mut self, mut ops: Vec<Operation>) {
215        let timestamp = Instant::now();
216        let base = self.current.seq;
217
218        // combine adjacent replacements
219        let mut combined_ops = Vec::new();
220        ops.sort_by_key(|op| match op {
221            Operation::Select(range) | Operation::Replace(Replace { range, .. }) => range.start(),
222        });
223        for op in ops.into_iter() {
224            match &op {
225                Operation::Replace(Replace { range: op_range, text: op_text }) => {
226                    if let Some(Operation::Replace(Replace {
227                        range: last_op_range,
228                        text: last_op_text,
229                    })) = combined_ops.last_mut()
230                    {
231                        if last_op_range.end() == op_range.start() {
232                            last_op_range.1 = op_range.1;
233                            last_op_text.push_str(op_text);
234                        } else {
235                            combined_ops.push(op);
236                        }
237                    } else {
238                        combined_ops.push(op);
239                    }
240                }
241                Operation::Select(_) => combined_ops.push(op),
242            }
243        }
244
245        self.ops
246            .meta
247            .extend(combined_ops.iter().map(|_| OpMeta { timestamp, base }));
248        self.ops.all.extend(combined_ops);
249    }
250
251    /// Loads a new string into the buffer, merging out-of-editor changes made since last load with in-editor changes
252    /// made since last load. The buffer's undo history is preserved; undo'ing will revert in-editor changes only.
253    /// Exercising undo's may put the buffer in never-before-seen states and exercising all undo's will revert the
254    /// buffer to the most recently loaded state (undo limit permitting).
255    /// Note: undo behavior described here is aspirational and not yet implemented.
256    pub fn reload(&mut self, text: String) {
257        let timestamp = Instant::now();
258        let base = self.external.seq;
259        let ops = diff(&self.external.text, &text);
260
261        self.ops
262            .meta
263            .extend(ops.iter().map(|_| OpMeta { timestamp, base }));
264        self.ops.all.extend(ops.into_iter().map(Operation::Replace));
265
266        self.external.text = text;
267        self.external.seq = self.base.seq + self.ops.all.len();
268    }
269
270    /// Indicates to the buffer the changes that have been saved outside the editor. This will serve as the new base
271    /// for merging external changes. The sequence number should be taken from `current.seq` of the buffer when the
272    /// buffer's contents are read for saving.
273    pub fn saved(&mut self, external_seq: usize, external_text: String) {
274        self.external.text = external_text;
275        self.external.seq = external_seq;
276    }
277
278    pub fn merge(mut self, external_text_a: String, external_text_b: String) -> String {
279        let ops_a = diff(&self.external.text, &external_text_a);
280        let ops_b = diff(&self.external.text, &external_text_b);
281
282        let timestamp = Instant::now();
283        let base = self.external.seq;
284        self.ops
285            .meta
286            .extend(ops_a.iter().map(|_| OpMeta { timestamp, base }));
287        self.ops
288            .meta
289            .extend(ops_b.iter().map(|_| OpMeta { timestamp, base }));
290
291        self.ops
292            .all
293            .extend(ops_a.into_iter().map(Operation::Replace));
294        self.ops
295            .all
296            .extend(ops_b.into_iter().map(Operation::Replace));
297
298        self.update();
299        self.current.text
300    }
301
302    /// Applies all operations in the buffer's input queue
303    pub fn update(&mut self) -> Response {
304        // clear redo stack
305        //             v base        v current    v processed
306        // ops before: |<- applied ->|<- undone ->|<- queued ->|
307        // ops after:  |<- applied ->|<- queued ->|
308        let queue_len = self.base.seq + self.ops.len() - self.ops.processed_seq;
309        if queue_len > 0 {
310            let drain_range = self.current.seq..self.ops.processed_seq;
311            self.ops.all.drain(drain_range.clone());
312            self.ops.meta.drain(drain_range.clone());
313            self.ops.transformed.drain(drain_range.clone());
314            self.ops.transformed_inverted.drain(drain_range.clone());
315            self.ops.processed_seq = self.current.seq;
316        } else {
317            return Response::default();
318        }
319
320        // transform & apply
321        let mut result = Response::default();
322        for idx in self.current_idx()..self.current_idx() + queue_len {
323            let mut op = self.ops.all[idx].clone();
324            let meta = &self.ops.meta[idx];
325            self.transform(&mut op, meta);
326            self.ops.transformed_inverted.push(self.current.invert(&op));
327            self.ops.transformed.push(op.clone());
328            self.ops.processed_seq += 1;
329
330            result |= self.redo();
331        }
332
333        result
334    }
335
336    fn transform(&self, op: &mut Operation, meta: &OpMeta) {
337        let base_idx = meta.base - self.base.seq;
338        for transforming_idx in base_idx..self.ops.processed_seq {
339            let preceding_op = &self.ops.transformed[transforming_idx];
340            if let Operation::Replace(Replace {
341                range: preceding_replaced_range,
342                text: preceding_replacement_text,
343            }) = preceding_op
344            {
345                if let Operation::Replace(Replace { range: transformed_range, text }) = op {
346                    if preceding_replaced_range.intersects(transformed_range, true)
347                        && !(preceding_replaced_range.is_empty() && transformed_range.is_empty())
348                    {
349                        // concurrent replacements to intersecting ranges choose the first/local edit as the winner
350                        // this doesn't create self-conflicts during merge because merge combines adjacent replacements
351                        // this doesn't create self-conflicts for same-frame editor changes because our final condition
352                        // is that we don't simultaneously insert text for both operations, which creates un-ideal
353                        // behavior (see test buffer_merge_insert)
354                        *text = "".into();
355                        transformed_range.1 = transformed_range.0;
356                    }
357                }
358
359                match op {
360                    Operation::Replace(Replace { range: transformed_range, .. })
361                    | Operation::Select(transformed_range) => {
362                        adjust_subsequent_range(
363                            *preceding_replaced_range,
364                            preceding_replacement_text.graphemes(true).count().into(),
365                            true,
366                            transformed_range,
367                        );
368                    }
369                }
370            }
371        }
372    }
373
374    pub fn can_redo(&self) -> bool {
375        self.current.seq < self.ops.processed_seq
376    }
377
378    pub fn can_undo(&self) -> bool {
379        self.current.seq > self.base.seq
380    }
381
382    pub fn redo(&mut self) -> Response {
383        let mut response = Response::default();
384        while self.can_redo() {
385            let op = &self.ops.transformed[self.current_idx()];
386
387            self.current.seq += 1;
388
389            response |= match op {
390                Operation::Replace(replace) => self.current.apply_replace(replace),
391                Operation::Select(range) => self.current.apply_select(*range),
392            };
393
394            if self.ops.is_undo_checkpoint(self.current_idx()) {
395                break;
396            }
397        }
398        response
399    }
400
401    pub fn undo(&mut self) -> Response {
402        let mut response = Response::default();
403        while self.can_undo() {
404            self.current.seq -= 1;
405            let op = &self.ops.transformed_inverted[self.current_idx()];
406
407            if let Some(replace) = &op.replace {
408                response |= self.current.apply_replace(replace);
409            }
410            response |= self.current.apply_select(op.select);
411
412            if self.ops.is_undo_checkpoint(self.current_idx()) {
413                break;
414            }
415        }
416        response
417    }
418
419    fn current_idx(&self) -> usize {
420        self.current.seq - self.base.seq
421    }
422
423    /// Reports whether the buffer's current text is empty.
424    pub fn is_empty(&self) -> bool {
425        self.current.text.is_empty()
426    }
427
428    pub fn selection_text(&self) -> String {
429        self[self.current.selection].to_string()
430    }
431}
432
433impl From<&str> for Buffer {
434    fn from(value: &str) -> Self {
435        let mut result = Self::default();
436        result.current.text = value.to_string();
437        result.current.segs = unicode_segs::calc(value);
438        result.external.text = value.to_string();
439        result
440    }
441}
442
443/// Adjust a range based on a text replacement. Positions before the replacement generally are not adjusted,
444/// positions after the replacement generally are, and positions within the replacement are adjusted to the end of
445/// the replacement if `prefer_advance` is true or are adjusted to the start of the replacement otherwise.
446pub fn adjust_subsequent_range(
447    replaced_range: (DocCharOffset, DocCharOffset), replacement_len: RelCharOffset,
448    prefer_advance: bool, range: &mut (DocCharOffset, DocCharOffset),
449) {
450    for position in [&mut range.0, &mut range.1] {
451        adjust_subsequent_position(replaced_range, replacement_len, prefer_advance, position);
452    }
453}
454
455/// Adjust a position based on a text replacement. Positions before the replacement generally are not adjusted,
456/// positions after the replacement generally are, and positions within the replacement are adjusted to the end of
457/// the replacement if `prefer_advance` is true or are adjusted to the start of the replacement otherwise.
458fn adjust_subsequent_position(
459    replaced_range: (DocCharOffset, DocCharOffset), replacement_len: RelCharOffset,
460    prefer_advance: bool, position: &mut DocCharOffset,
461) {
462    let replaced_len = replaced_range.len();
463    let replacement_start = replaced_range.start();
464    let replacement_end = replacement_start + replacement_len;
465
466    enum Mode {
467        Insert,
468        Replace,
469    }
470    let mode = if replaced_range.is_empty() { Mode::Insert } else { Mode::Replace };
471
472    let sorted_bounds = {
473        let mut bounds = vec![replaced_range.start(), replaced_range.end(), *position];
474        bounds.sort();
475        bounds
476    };
477    let bind = |start: &DocCharOffset, end: &DocCharOffset, pos: &DocCharOffset| {
478        start == &replaced_range.start() && end == &replaced_range.end() && pos == &*position
479    };
480
481    *position = match (mode, &sorted_bounds[..]) {
482        // case 1: position at point of text insertion
483        //                       text before replacement: * *
484        //                        range of replaced text:  |
485        //          range of subsequent cursor selection:  |
486        //                        text after replacement: * X *
487        // advance:
488        // adjusted range of subsequent cursor selection:    |
489        // don't advance:
490        // adjusted range of subsequent cursor selection:  |
491        (Mode::Insert, [start, end, pos]) if bind(start, end, pos) && end == pos => {
492            if prefer_advance {
493                replacement_end
494            } else {
495                replacement_start
496            }
497        }
498
499        // case 2: position at start of text replacement
500        //                       text before replacement: * * * *
501        //                        range of replaced text:  |<->|
502        //          range of subsequent cursor selection:  |
503        //                        text after replacement: * X *
504        // adjusted range of subsequent cursor selection:  |
505        (Mode::Replace, [start, pos, end]) if bind(start, end, pos) && start == pos => {
506            if prefer_advance {
507                replacement_end
508            } else {
509                replacement_start
510            }
511        }
512
513        // case 3: position at end of text replacement
514        //                       text before replacement: * * * *
515        //                        range of replaced text:  |<->|
516        //          range of subsequent cursor selection:      |
517        //                        text after replacement: * X *
518        // adjusted range of subsequent cursor selection:    |
519        (Mode::Replace, [start, end, pos]) if bind(start, end, pos) && end == pos => {
520            if prefer_advance {
521                replacement_end
522            } else {
523                replacement_start
524            }
525        }
526
527        // case 4: position before point/start of text insertion/replacement
528        //                       text before replacement: * * * * *
529        //       (possibly empty) range of replaced text:    |<->|
530        //          range of subsequent cursor selection:  |
531        //                        text after replacement: * * X *
532        // adjusted range of subsequent cursor selection:  |
533        (_, [pos, start, end]) if bind(start, end, pos) => *position,
534
535        // case 5: position within text replacement
536        //                       text before replacement: * * * *
537        //                        range of replaced text:  |<->|
538        //          range of subsequent cursor selection:    |
539        //                        text after replacement: * X *
540        // advance:
541        // adjusted range of subsequent cursor selection:    |
542        // don't advance:
543        // adjusted range of subsequent cursor selection:  |
544        (Mode::Replace, [start, pos, end]) if bind(start, end, pos) => {
545            if prefer_advance {
546                replacement_end
547            } else {
548                replacement_start
549            }
550        }
551
552        // case 6: position after point/end of text insertion/replacement
553        //                       text before replacement: * * * * *
554        //       (possibly empty) range of replaced text:  |<->|
555        //          range of subsequent cursor selection:        |
556        //                        text after replacement: * X * *
557        // adjusted range of subsequent cursor selection:      |
558        (_, [start, end, pos]) if bind(start, end, pos) => {
559            *position + replacement_len - replaced_len
560        }
561        _ => unreachable!(),
562    }
563}
564
565impl Index<(DocByteOffset, DocByteOffset)> for Snapshot {
566    type Output = str;
567
568    fn index(&self, index: (DocByteOffset, DocByteOffset)) -> &Self::Output {
569        &self.text[index.start().0..index.end().0]
570    }
571}
572
573impl Index<(DocCharOffset, DocCharOffset)> for Snapshot {
574    type Output = str;
575
576    fn index(&self, index: (DocCharOffset, DocCharOffset)) -> &Self::Output {
577        let index = self.segs.range_to_byte(index);
578        &self.text[index.start().0..index.end().0]
579    }
580}
581
582impl Index<(DocByteOffset, DocByteOffset)> for Buffer {
583    type Output = str;
584
585    fn index(&self, index: (DocByteOffset, DocByteOffset)) -> &Self::Output {
586        &self.current[index]
587    }
588}
589
590impl Index<(DocCharOffset, DocCharOffset)> for Buffer {
591    type Output = str;
592
593    fn index(&self, index: (DocCharOffset, DocCharOffset)) -> &Self::Output {
594        &self.current[index]
595    }
596}
597
598#[cfg(test)]
599mod test {
600    use super::Buffer;
601
602    #[test]
603    fn buffer_merge_nonintersecting_replace() {
604        let base_content = "base content base";
605        let local_content = "local content base";
606        let remote_content = "base content remote";
607
608        assert_eq!(
609            Buffer::from(base_content).merge(local_content.into(), remote_content.into()),
610            "local content remote"
611        );
612        assert_eq!(
613            Buffer::from(base_content).merge(remote_content.into(), local_content.into()),
614            "local content remote"
615        );
616    }
617
618    #[test]
619    fn buffer_merge_prefix_replace() {
620        let base_content = "base content";
621        let local_content = "local content";
622        let remote_content = "remote content";
623
624        assert_eq!(
625            Buffer::from(base_content).merge(local_content.into(), remote_content.into()),
626            "local content"
627        );
628    }
629
630    #[test]
631    fn buffer_merge_infix_replace() {
632        let base_content = "con base tent";
633        let local_content = "con local tent";
634        let remote_content = "con remote tent";
635
636        assert_eq!(
637            Buffer::from(base_content).merge(local_content.into(), remote_content.into()),
638            "con local tent"
639        );
640        assert_eq!(
641            Buffer::from(base_content).merge(remote_content.into(), local_content.into()),
642            "con remote tent"
643        );
644    }
645
646    #[test]
647    fn buffer_merge_postfix_replace() {
648        let base_content = "content base";
649        let local_content = "content local";
650        let remote_content = "content remote";
651
652        assert_eq!(
653            Buffer::from(base_content).merge(local_content.into(), remote_content.into()),
654            "content local"
655        );
656        assert_eq!(
657            Buffer::from(base_content).merge(remote_content.into(), local_content.into()),
658            "content remote"
659        );
660    }
661
662    #[test]
663    fn buffer_merge_insert() {
664        let base_content = "content";
665        let local_content = "content local";
666        let remote_content = "content remote";
667
668        assert_eq!(
669            Buffer::from(base_content).merge(local_content.into(), remote_content.into()),
670            "content local remote"
671        );
672        assert_eq!(
673            Buffer::from(base_content).merge(remote_content.into(), local_content.into()),
674            "content remote local"
675        );
676    }
677
678    #[test]
679    // this test case documents behavior moreso than asserting target state
680    fn buffer_merge_insert_replace() {
681        let base_content = "content";
682        let local_content = "content local";
683        let remote_content = "remote";
684
685        assert_eq!(
686            Buffer::from(base_content).merge(local_content.into(), remote_content.into()),
687            "content local"
688        );
689        assert_eq!(
690            Buffer::from(base_content).merge(remote_content.into(), local_content.into()),
691            "remote"
692        );
693    }
694
695    #[test]
696    // this test case used to crash `merge`
697    fn buffer_merge_crash() {
698        let base_content = "con tent";
699        let local_content = "cont tent locallocal";
700        let remote_content = "cont remote tent";
701
702        let _ = Buffer::from(base_content).merge(local_content.into(), remote_content.into());
703        let _ = Buffer::from(base_content).merge(remote_content.into(), local_content.into());
704    }
705}