reconcile-text 0.12.0

Intelligent 3-way text merging with automated conflict resolution
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
use std::fmt::Debug;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::{
    BuiltinTokenizer, CursorPosition, TextWithCursors, Token,
    operation_transformation::{
        DiffError, Operation,
        utils::{cook_operations::cook_operations, elongate_operations::elongate_operations},
    },
    raw_operation::RawOperation,
    tokenizer::Tokenizer,
    types::{
        history::History, number_or_text::NumberOrText, side::Side,
        span_with_history::SpanWithHistory,
    },
    utils::string_builder::StringBuilder,
};

/// A text document with a sequence of operations derived from diffing it
/// against an updated version. Supports merging two `EditedText` instances
/// (from the same original) via Operational Transformation.
///
/// Created via `from_strings`, `from_strings_with_tokenizer`, or `from_diff`,
/// then merged with another `EditedText` and applied to get the reconciled
/// text.
///
/// Also tracks cursor positions from the updated text, repositioning them
/// when operations are applied.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct EditedText<'a, T>
where
    T: PartialEq + Clone + Debug,
{
    text: &'a str,
    operations: Vec<Operation<T>>,
    operation_sides: Vec<Side>,
    cursors: Vec<CursorPosition>,
}

impl<'a> EditedText<'a, String> {
    /// Create an `EditedText` from the given original and updated strings.
    /// Uses the default word tokenizer (splits on word boundaries).
    #[must_use]
    pub fn from_strings(original: &'a str, updated: &TextWithCursors) -> Self {
        Self::from_strings_with_tokenizer(original, updated, &*BuiltinTokenizer::Word)
    }
}

impl<'a, T> EditedText<'a, T>
where
    T: PartialEq + Clone + Debug,
{
    /// Create an `EditedText` from the given original and updated strings
    /// using the provided tokenizer
    #[must_use]
    pub fn from_strings_with_tokenizer(
        original: &'a str,
        updated: &TextWithCursors,
        tokenizer: &Tokenizer<T>,
    ) -> Self {
        let original_tokens = (tokenizer)(original);
        let updated_tokens = (tokenizer)(&updated.text());

        let diff: Vec<RawOperation<T>> = RawOperation::vec_from(&original_tokens, &updated_tokens);
        let operations: Vec<Operation<T>> = cook_operations(elongate_operations(diff)).collect();
        let operation_count = operations.len();

        Self::new(
            original,
            operations,
            vec![Side::Left; operation_count],
            updated.cursors(),
        )
    }

    /// Create a new `EditedText` with the given operations.
    /// The operations must be in the order in which they are meant to be
    /// applied. The operations must not overlap.
    fn new(
        text: &'a str,
        operations: Vec<Operation<T>>,
        operation_sides: Vec<Side>,
        mut cursors: Vec<CursorPosition>,
    ) -> Self {
        cursors.sort_by_key(|cursor| cursor.char_index);

        Self {
            text,
            operations,
            operation_sides,
            cursors,
        }
    }

    /// Merge two `EditedText` instances. The two instances must be derived
    /// from the same original text. The operations are merged using the
    /// principles of Operational Transformation. The cursors are updated
    /// accordingly to reflect the changes made by the merged operations.
    ///
    /// # Panics
    ///
    /// Panics if there's an integer overflow (in isize) when calculating new
    /// cursor positions.
    #[must_use]
    #[allow(clippy::too_many_lines)]
    pub fn merge(self, other: Self) -> Self {
        debug_assert_eq!(
            self.text, other.text,
            "`EditedText`-s must be derived from the same text to be mergable"
        );

        let mut merged_cursors = Vec::with_capacity(self.cursors.len() + other.cursors.len());
        let mut left_cursors = self.cursors.into_iter().peekable();
        let mut right_cursors = other.cursors.into_iter().peekable();

        let mut merged_operations: Vec<Operation<T>> =
            Vec::with_capacity(self.operations.len() + other.operations.len());
        let mut merged_operation_sides: Vec<Side> =
            Vec::with_capacity(self.operations.len() + other.operations.len());

        let mut left_iter = self.operations.into_iter();
        let mut right_iter = other.operations.into_iter();

        let mut maybe_left_op = left_iter.next();
        let mut maybe_right_op = right_iter.next();

        let mut seen_left_length: usize = 0;
        let mut seen_right_length: usize = 0;
        let mut merged_length: usize = 0;

        let mut last_left_op = None;
        let mut last_right_op = None;

        loop {
            let (side, operation) = match (maybe_left_op.as_ref(), maybe_right_op.as_ref()) {
                (Some(left_op), Some(right_op)) => {
                    if left_op.cmp_priority(seen_left_length, right_op, seen_right_length)
                        == std::cmp::Ordering::Less
                    {
                        (Side::Left, maybe_left_op.take().unwrap())
                    } else {
                        (Side::Right, maybe_right_op.take().unwrap())
                    }
                }

                (Some(_), None) => (Side::Left, maybe_left_op.take().unwrap()),
                (None, Some(_)) => (Side::Right, maybe_right_op.take().unwrap()),
                (None, None) => break,
            };

            let is_advancing_operation = matches!(
                operation,
                Operation::Insert { .. } | Operation::Equal { .. }
            );

            let original_length = operation.len();
            let (side, result) = match side {
                Side::Left => {
                    let result = operation.merge_operations(last_right_op.as_ref());

                    if let ref op @ (Operation::Insert { .. } | Operation::Equal { .. }) = result {
                        let merged_length_signed = isize::try_from(merged_length)
                            .expect("merged_length must fit in isize");
                        let seen_left_length_signed = isize::try_from(seen_left_length)
                            .expect("seen_left_length must fit in isize");
                        let op_len_signed =
                            isize::try_from(op.len()).expect("op.len() must fit in isize");
                        let original_length_signed = isize::try_from(original_length)
                            .expect("original_length must fit in isize");

                        let shift = merged_length_signed - seen_left_length_signed + op_len_signed
                            - original_length_signed;

                        while let Some(cursor) = left_cursors.next_if(|cursor| {
                            cursor.char_index <= seen_left_length + original_length
                        }) {
                            merged_cursors.push(
                                cursor.with_index(cursor.char_index.saturating_add_signed(shift)),
                            );
                        }
                    }

                    if is_advancing_operation {
                        seen_left_length += original_length;
                    }

                    maybe_left_op = left_iter.next();
                    last_left_op = Some(result.clone());

                    (Side::Left, result)
                }
                Side::Right => {
                    let result = operation.merge_operations(last_left_op.as_ref());

                    if let ref op @ (Operation::Insert { .. } | Operation::Equal { .. }) = result {
                        let merged_length_signed = isize::try_from(merged_length)
                            .expect("merged_length must fit in isize");
                        let seen_right_length_signed = isize::try_from(seen_right_length)
                            .expect("seen_right_length must fit in isize");
                        let op_len_signed =
                            isize::try_from(op.len()).expect("op.len() must fit in isize");
                        let original_length_signed = isize::try_from(original_length)
                            .expect("original_length must fit in isize");

                        let shift = merged_length_signed - seen_right_length_signed + op_len_signed
                            - original_length_signed;

                        while let Some(cursor) = right_cursors.next_if(|cursor| {
                            cursor.char_index <= seen_right_length + original_length
                        }) {
                            merged_cursors.push(
                                cursor.with_index(cursor.char_index.saturating_add_signed(shift)),
                            );
                        }
                    }

                    if is_advancing_operation {
                        seen_right_length += original_length;
                    }

                    maybe_right_op = right_iter.next();
                    last_right_op = Some(result.clone());

                    (Side::Right, result)
                }
            };

            if result.len() == 0 {
                continue;
            }

            if is_advancing_operation {
                merged_length += result.len();
            }

            merged_operations.push(result);
            merged_operation_sides.push(side);
        }

        for cursor in left_cursors.chain(right_cursors) {
            merged_cursors.push(cursor.with_index(merged_length));
        }

        debug_assert_eq!(merged_operations.len(), merged_operation_sides.len());

        Self::new(
            self.text,
            merged_operations,
            merged_operation_sides,
            merged_cursors,
        )
    }

    /// Apply the operations to the text and return the resulting text
    #[must_use]
    pub fn apply(&self) -> TextWithCursors {
        let mut builder: StringBuilder<'_> = StringBuilder::new(self.text);

        for operation in &self.operations {
            builder = operation.apply(builder);
        }

        TextWithCursors::new(builder.take(), self.cursors.clone())
    }

    /// Apply the operations to the text and return the resulting text in chunks
    /// together with the provenance describing where each chunk came from.
    ///
    /// Returns all spans including deletions (not present in the merged text).
    ///
    /// ```
    ///  use reconcile_text::{History, SpanWithHistory, BuiltinTokenizer, reconcile};
    ///
    ///  let parent = "Merging text is hard!";
    ///  let left = "Merging text is easy!"; // Changed "hard" to "easy"
    ///  let right = "With reconcile, merging documents is hard!"; // Added prefix and changed word
    ///
    ///  let result = reconcile(
    ///      parent,
    ///      &left.into(),
    ///      &right.into(),
    ///      &*BuiltinTokenizer::Word,
    ///  );
    ///
    ///  assert_eq!(
    ///      result.apply_with_history(),
    ///      vec![
    ///          SpanWithHistory::new("Merging text".to_string(), History::RemovedFromRight,),
    ///          SpanWithHistory::new(
    ///              "With reconcile, merging documents".to_string(),
    ///              History::AddedFromRight,
    ///          ),
    ///          SpanWithHistory::new(" ".to_string(), History::Unchanged,),
    ///          SpanWithHistory::new("is".to_string(), History::Unchanged,),
    ///          SpanWithHistory::new(" hard!".to_string(), History::RemovedFromLeft,),
    ///          SpanWithHistory::new(" easy!".to_string(), History::AddedFromLeft,),
    ///      ]
    ///  );
    /// ```
    #[must_use]
    pub fn apply_with_history(&self) -> Vec<SpanWithHistory> {
        let chars: Vec<char> = self.text.chars().collect();
        let mut builder: StringBuilder<'_> = StringBuilder::new(self.text);

        let mut history = Vec::with_capacity(self.operations.len());

        for (operation, side) in self.operations.iter().zip(self.operation_sides.iter()) {
            builder = operation.apply(builder);

            match operation {
                Operation::Equal { .. } => {
                    history.push(SpanWithHistory::new(builder.take(), History::Unchanged));
                }
                Operation::Insert { .. } => {
                    let h = match side {
                        Side::Left => History::AddedFromLeft,
                        Side::Right => History::AddedFromRight,
                    };
                    history.push(SpanWithHistory::new(builder.take(), h));
                }
                Operation::Delete {
                    deleted_character_count,
                    order,
                    ..
                } => {
                    let deleted: String = chars[*order..*order + *deleted_character_count]
                        .iter()
                        .collect();
                    let h = match side {
                        Side::Left => History::RemovedFromLeft,
                        Side::Right => History::RemovedFromRight,
                    };
                    history.push(SpanWithHistory::new(deleted, h));
                }
            }
        }

        history
    }

    /// Apply the operations and return both the merged text with cursors and
    /// the provenance history in a single pass
    #[must_use]
    pub fn apply_with_all(&self) -> (TextWithCursors, Vec<SpanWithHistory>) {
        let chars: Vec<char> = self.text.chars().collect();
        let mut builder: StringBuilder<'_> = StringBuilder::new(self.text);
        let mut history = Vec::with_capacity(self.operations.len());
        let mut full_text = String::new();

        for (operation, side) in self.operations.iter().zip(self.operation_sides.iter()) {
            builder = operation.apply(builder);

            match operation {
                Operation::Equal { .. } => {
                    let span = builder.take();
                    full_text.push_str(&span);
                    history.push(SpanWithHistory::new(span, History::Unchanged));
                }
                Operation::Insert { .. } => {
                    let span = builder.take();
                    full_text.push_str(&span);
                    let h = match side {
                        Side::Left => History::AddedFromLeft,
                        Side::Right => History::AddedFromRight,
                    };
                    history.push(SpanWithHistory::new(span, h));
                }
                Operation::Delete {
                    deleted_character_count,
                    order,
                    ..
                } => {
                    let deleted: String = chars[*order..*order + *deleted_character_count]
                        .iter()
                        .collect();
                    let h = match side {
                        Side::Left => History::RemovedFromLeft,
                        Side::Right => History::RemovedFromRight,
                    };
                    history.push(SpanWithHistory::new(deleted, h));
                }
            }
        }

        (
            TextWithCursors::new(full_text, self.cursors.clone()),
            history,
        )
    }

    /// Convert the `EditedText` into a terse representation ready for
    /// serialization. The result omits cursor positions and the original text.
    /// This is useful for sending text diffs over the network if there's a
    /// clear consensus on the original text.
    ///
    /// Inserts are strings, deletes are negative integers (character count),
    /// and retained spans are positive integers (character count).
    ///
    /// # Errors
    ///
    /// Returns `DiffError::IntegerOverflow` if a character count exceeds
    /// `i64::MAX`.
    pub fn to_diff(&self) -> Result<Vec<NumberOrText>, DiffError> {
        let mut result: Vec<NumberOrText> = Vec::with_capacity(self.operations.len());
        let mut previous_equal: Option<usize> = None;

        for operation in &self.operations {
            match operation {
                Operation::Equal { length, .. } => {
                    if let Some(prev_length) = previous_equal {
                        previous_equal = Some(prev_length + *length);
                    } else {
                        previous_equal = Some(*length);
                    }
                }

                Operation::Insert { text, .. } => {
                    if let Some(prev_length) = previous_equal {
                        result
                            .push(NumberOrText::Number(i64::try_from(prev_length).map_err(
                                |_| DiffError::IntegerOverflow { value: prev_length },
                            )?));
                        previous_equal = None;
                    }

                    let text: String = text.iter().map(Token::original).collect();
                    result.push(NumberOrText::Text(text));
                }

                Operation::Delete {
                    deleted_character_count,
                    ..
                } => {
                    if let Some(prev_length) = previous_equal {
                        result
                            .push(NumberOrText::Number(i64::try_from(prev_length).map_err(
                                |_| DiffError::IntegerOverflow { value: prev_length },
                            )?));
                        previous_equal = None;
                    }

                    let count = i64::try_from(*deleted_character_count).map_err(|_| {
                        DiffError::IntegerOverflow {
                            value: *deleted_character_count,
                        }
                    })?;
                    result.push(NumberOrText::Number(-count));
                }
            }
        }

        if let Some(prev_length) = previous_equal {
            result
                .push(NumberOrText::Number(i64::try_from(prev_length).map_err(
                    |_| DiffError::IntegerOverflow { value: prev_length },
                )?));
        }

        Ok(result)
    }

    /// Reconstruct an `EditedText` from a diff and the original text.
    ///
    /// # Errors
    ///
    /// Returns `DiffError::LengthExceedsOriginal` if the diff references a
    /// range that exceeds the original text length.
    ///
    /// # Panics
    ///
    /// Panics if there's an integer overflow in i64.
    pub fn from_diff(
        original_text: &'a str,
        diff: Vec<NumberOrText>,
        tokenizer: &Tokenizer<T>,
    ) -> Result<EditedText<'a, T>, DiffError> {
        let mut operations: Vec<Operation<T>> = Vec::with_capacity(diff.len());
        let mut order = 0;
        let chars: Vec<char> = original_text.chars().collect();
        let text_length = chars.len();

        for item in diff {
            match item {
                NumberOrText::Number(length) => {
                    if length >= 0 {
                        let length = usize::try_from(length).expect("length must fit in usize");

                        // Validate that the range doesn't exceed the original text
                        if order + length > text_length {
                            return Err(DiffError::LengthExceedsOriginal {
                                position: order,
                                requested: length,
                                available: text_length.saturating_sub(order),
                            });
                        }

                        let original_characters: String =
                            chars[order..order + length].iter().collect();

                        let original_tokens = tokenizer(&original_characters);
                        for token in original_tokens {
                            operations
                                .push(Operation::create_equal(order, token.get_original_length()));
                            order += token.get_original_length();
                        }
                    } else {
                        let length =
                            usize::try_from(-length).expect("negative length must fit in usize");

                        // Validate that the delete range doesn't exceed the original text
                        if order + length > text_length {
                            return Err(DiffError::LengthExceedsOriginal {
                                position: order,
                                requested: length,
                                available: text_length.saturating_sub(order),
                            });
                        }

                        operations.push(Operation::create_delete(order, length));
                        order += length;
                    }
                }
                NumberOrText::Text(text) => {
                    let tokens = tokenizer(&text);
                    operations.push(Operation::create_insert(order, tokens));
                }
            }
        }

        let operation_count = operations.len();
        Ok(EditedText::new(
            original_text,
            operations,
            vec![Side::Left; operation_count],
            vec![],
        ))
    }
}

#[cfg(test)]
mod tests {
    use insta::assert_debug_snapshot;
    use pretty_assertions::assert_eq;

    use super::*;

    #[test]
    fn test_calculate_operations() {
        let left = "hello world! How are you?  Adam";
        let right = "Hello, my friend! How are you doing? Albert";

        let operations = EditedText::from_strings(left, &right.into());

        insta::assert_debug_snapshot!(operations);

        let new_right = operations.apply();
        assert_eq!(new_right.text(), right);
    }

    #[test]
    fn test_calculate_operations_with_no_diff() {
        let text = "hello world!";

        let operations = EditedText::from_strings(text, &text.into());

        assert_debug_snapshot!(operations);

        let new_right = operations.apply();
        assert_eq!(new_right.text(), text);
    }

    #[test]
    fn test_calculate_operations_with_insert() {
        let original = "hello world! ...";
        let left = "Hello world! I'm Andras.";
        let right = "Hello world! How are you?";
        let expected = "Hello world! How are you? I'm Andras.";

        let operations_1 = EditedText::from_strings(original, &left.into());
        let operations_2 = EditedText::from_strings(original, &right.into());

        let operations = operations_1.merge(operations_2);
        assert_eq!(operations.apply().text(), expected);
    }

    #[test]
    fn test_from_diff_length_exceeds_original() {
        let result = EditedText::from_diff(
            "hello",
            vec![
                10.into(), // too large equal span - should error
                " world".into(),
            ],
            &*BuiltinTokenizer::Word,
        );

        assert!(result.is_err());
        match result {
            Err(DiffError::LengthExceedsOriginal {
                position,
                requested,
                available,
            }) => {
                assert_eq!(position, 0);
                assert_eq!(requested, 10);
                assert_eq!(available, 5);
            }
            _ => panic!("Expected LengthExceedsOriginal error"),
        }
    }

    #[test]
    fn test_from_diff_valid() {
        let edited_text = EditedText::from_diff(
            "hello",
            vec![
                5.into(), // exact length
                " world".into(),
            ],
            &*BuiltinTokenizer::Word,
        )
        .unwrap();

        let content = edited_text.apply().text();

        assert_eq!(content, "hello world");
    }

    #[cfg(feature = "serde")]
    #[test]
    fn test_changes_deserialisation() {
        let original = "Merging text is hard!";
        let changes = "Merging text is easy with reconcile!";
        let result = EditedText::from_strings(original, &changes.into());
        let serialized = serde_yaml::to_string(&result.to_diff().unwrap()).unwrap();

        let expected = concat!("- 15\n", "- -6\n", "- ' easy with reconcile!'\n",);
        assert_eq!(serialized, expected);
    }

    #[test]
    fn test_apply_with_history_utf8() {
        let parent = "こんにちは世界"; // "Hello World" in Japanese (7 chars, 21 bytes)
        let left = "こんにちは宇宙"; // Changed 世界 to 宇宙
        let right = parent;

        let result = crate::reconcile(
            parent,
            &left.into(),
            &right.into(),
            &*BuiltinTokenizer::Word,
        );

        let history = result.apply_with_history();
        assert!(!history.is_empty());
        assert_eq!(result.apply().text(), "こんにちは宇宙");
    }

    #[cfg(feature = "serde")]
    #[test]
    fn test_changes_serialization() {
        let original = "The quick brown fox jumps over the lazy dog.";
        let updated = "The quick red fox jumped over the very lazy dog!";

        let edited_text = EditedText::from_strings(original, &updated.into());

        let changes = edited_text.to_diff().unwrap();
        let deserialized_edited_text =
            EditedText::from_diff(original, changes, &*BuiltinTokenizer::Word).unwrap();

        assert_eq!(deserialized_edited_text.apply().text(), updated);
    }
}