Refactor
This commit is contained in:
parent
c02f84a476
commit
143883a899
12 changed files with 801 additions and 645 deletions
279
backend/reconcile/src/operation_transformation/edited_text.rs
Normal file
279
backend/reconcile/src/operation_transformation/edited_text.rs
Normal file
|
|
@ -0,0 +1,279 @@
|
|||
use std::borrow::BorrowMut;
|
||||
|
||||
use super::{operation, Operation};
|
||||
use crate::diffs::raw_operation::RawOperation;
|
||||
use crate::errors::SyncLibError;
|
||||
use crate::operation_transformation::merge_context::MergeContext;
|
||||
use crate::tokenizer::token::Token;
|
||||
use crate::utils::ordered_operation::OrderedOperation;
|
||||
use crate::utils::side::Side;
|
||||
use crate::{diffs::myers::diff, utils::merge_iters::MergeSorted};
|
||||
use ropey::Rope;
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// A sequence of operations that can be applied to a text document.
|
||||
/// EditedText supports merging two sequences of operations using the
|
||||
/// principle of Operational Transformation.
|
||||
///
|
||||
/// It's mainly created through the from_strings method, then merged with another
|
||||
/// EditedText derived from the same original text and then applied to the original text
|
||||
/// to get the reconciled text of concurrent edits.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
|
||||
pub struct EditedText<'a> {
|
||||
text: &'a str,
|
||||
operations: Vec<OrderedOperation>,
|
||||
}
|
||||
|
||||
impl<'a> EditedText<'a> {
|
||||
/// Create an EditedText from the given original (old) and updated (new) strings.
|
||||
/// The returned EditedText represents the changes from the original to the updated text.
|
||||
/// When the return value is applied to the original text, it will result in the updated text.
|
||||
pub fn from_strings(original: &'a str, updated: &str) -> Self {
|
||||
let original_tokens = Token::tokenize(original);
|
||||
let updated_tokens = Token::tokenize(updated);
|
||||
|
||||
let diff: Vec<RawOperation> = diff(&original_tokens, &updated_tokens);
|
||||
|
||||
Self::new(
|
||||
original,
|
||||
Self::elongate_operations(Self::cook_operations(diff)),
|
||||
)
|
||||
}
|
||||
|
||||
// Turn raw operations into ordered operations while keeping track of old & new indexes.
|
||||
fn cook_operations(raw_operations: Vec<RawOperation>) -> Vec<OrderedOperation> {
|
||||
let mut new_index = 0; // this is the start index of the operation on the new text
|
||||
let mut order = 0; // this is the start index of the operation on the original text
|
||||
|
||||
raw_operations
|
||||
.into_iter()
|
||||
.flat_map(|raw_operation| {
|
||||
let length = raw_operation.original_text_length();
|
||||
|
||||
let operation = match raw_operation {
|
||||
RawOperation::Equal(..) => {
|
||||
new_index += length;
|
||||
order += length;
|
||||
|
||||
None
|
||||
}
|
||||
RawOperation::Insert(..) => {
|
||||
let op =
|
||||
Operation::create_insert(new_index, raw_operation.get_original_text())
|
||||
.map(|operation| OrderedOperation { order, operation });
|
||||
|
||||
new_index += length;
|
||||
|
||||
op
|
||||
}
|
||||
RawOperation::Delete(..) => {
|
||||
let op = if cfg!(debug_assertions) {
|
||||
Operation::create_delete_with_text(
|
||||
new_index,
|
||||
raw_operation.get_original_text(),
|
||||
)
|
||||
} else {
|
||||
Operation::create_delete(new_index, length)
|
||||
}
|
||||
.map(|operation| OrderedOperation { order, operation });
|
||||
|
||||
order += length;
|
||||
|
||||
op
|
||||
}
|
||||
};
|
||||
|
||||
operation.into_iter()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
// TODO: shift ops befor compacting
|
||||
fn elongate_operations(operations: Vec<OrderedOperation>) -> Vec<OrderedOperation> {
|
||||
let mut maybe_previous: Option<OrderedOperation> = None;
|
||||
|
||||
let mut result: Vec<OrderedOperation> = operations
|
||||
.into_iter()
|
||||
.flat_map(|next| {
|
||||
if let Some(previous) = maybe_previous.take() {
|
||||
match (previous, next) {
|
||||
(
|
||||
previous @ OrderedOperation {
|
||||
operation: Operation::Insert { .. },
|
||||
..
|
||||
},
|
||||
next @ OrderedOperation {
|
||||
operation: Operation::Insert { .. },
|
||||
..
|
||||
},
|
||||
) if previous.operation.end_index() + 1 == next.operation.start_index() => {
|
||||
maybe_previous = Some(OrderedOperation {
|
||||
order: previous.order,
|
||||
operation: previous.operation.extend(&next.operation),
|
||||
});
|
||||
None
|
||||
}
|
||||
(
|
||||
previous @ OrderedOperation {
|
||||
operation: Operation::Delete { .. },
|
||||
..
|
||||
},
|
||||
next @ OrderedOperation {
|
||||
operation: Operation::Delete { .. },
|
||||
..
|
||||
},
|
||||
) if previous.operation.start_index() == next.operation.start_index() => {
|
||||
maybe_previous = Some(OrderedOperation {
|
||||
order: previous.order,
|
||||
operation: previous.operation.extend(&next.operation),
|
||||
});
|
||||
None
|
||||
}
|
||||
(previous, next) => {
|
||||
maybe_previous = Some(next);
|
||||
Some(previous)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
maybe_previous = Some(next.clone());
|
||||
None
|
||||
}
|
||||
.into_iter()
|
||||
})
|
||||
.collect();
|
||||
|
||||
if let Some(prev) = maybe_previous {
|
||||
result.push(prev);
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
/// 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<OrderedOperation>) -> Self {
|
||||
operations
|
||||
.iter()
|
||||
.zip(operations.iter().skip(1))
|
||||
.for_each(|(previous, next)| {
|
||||
debug_assert!(
|
||||
previous.operation.start_index() <= next.operation.start_index(),
|
||||
"{} must not come before {} yet it does",
|
||||
previous.operation,
|
||||
next.operation
|
||||
);
|
||||
});
|
||||
|
||||
Self { text, operations }
|
||||
}
|
||||
|
||||
pub fn merge(self, other: Self) -> Self {
|
||||
debug_assert_eq!(
|
||||
self.text, other.text,
|
||||
"EditedTexts must be derived from the same text to be mergable"
|
||||
);
|
||||
|
||||
let mut left_merge_context = MergeContext::default();
|
||||
let mut right_merge_context = MergeContext::default();
|
||||
|
||||
Self::new(
|
||||
self.text,
|
||||
self.operations
|
||||
.into_iter()
|
||||
.map(|op| (op, Side::Left))
|
||||
.merge_sorted_by_key(
|
||||
other.operations.into_iter().map(|op| (op, Side::Right)),
|
||||
|(operation, _)| operation.order,
|
||||
)
|
||||
.flat_map(|(OrderedOperation { order, operation }, side)| {
|
||||
match side {
|
||||
Side::Left => operation.merge_operations_with_context(
|
||||
&mut right_merge_context,
|
||||
&mut left_merge_context,
|
||||
),
|
||||
Side::Right => operation.merge_operations_with_context(
|
||||
&mut left_merge_context,
|
||||
&mut right_merge_context,
|
||||
),
|
||||
}
|
||||
.map(|operation| OrderedOperation { order, operation })
|
||||
.into_iter()
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Apply the operations to the text and return the resulting text.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns an SyncLibError::OperationError if the operations cannot be applied to the text.
|
||||
pub fn apply(&self) -> Result<String, SyncLibError> {
|
||||
let mut text = Rope::from_str(self.text);
|
||||
self.operations
|
||||
.iter()
|
||||
.try_fold(
|
||||
&mut text,
|
||||
|rope_text, OrderedOperation { operation, .. }| operation.apply(rope_text),
|
||||
)
|
||||
.map(|rope| rope.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::{env, fs, ops::Range, path::Path};
|
||||
|
||||
use pretty_assertions::assert_eq;
|
||||
use test_case::test_matrix;
|
||||
|
||||
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);
|
||||
|
||||
insta::assert_debug_snapshot!(operations);
|
||||
|
||||
let new_right = operations.apply().unwrap();
|
||||
|
||||
assert_eq!(new_right.to_string(), right);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_calculate_operations_with_no_diff() {
|
||||
let text = "hello world!";
|
||||
|
||||
let operations = EditedText::from_strings(text, text);
|
||||
|
||||
assert_eq!(operations.operations.len(), 0);
|
||||
|
||||
let new_right = operations.apply().unwrap();
|
||||
|
||||
assert_eq!(new_right.to_string(), text);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_calculate_operations_with_insert() {
|
||||
let original = "hello world! ...";
|
||||
let left = "Hello world! How are you?";
|
||||
let right = "hello world! I'm Andras.";
|
||||
let expected = "Hello world! I'm Andras. How are you?";
|
||||
|
||||
let operations_1 = EditedText::from_strings(original, left);
|
||||
println!("{:#?}", operations_1);
|
||||
let operations_2 = EditedText::from_strings(original, right);
|
||||
println!("{:#?}", operations_2);
|
||||
|
||||
let operations = operations_1.merge(operations_2);
|
||||
|
||||
assert_eq!(operations.apply().unwrap(), expected);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
use crate::operation_transformation::{operation, Operation};
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct MergeContext {
|
||||
pub last_delete: Option<Operation>,
|
||||
pub shift: i64,
|
||||
}
|
||||
|
||||
impl MergeContext {
|
||||
/// Replace the last delete operation (if there was one) with a new one while
|
||||
/// applying it to the shift.
|
||||
pub fn replace_delete(&mut self, delete: Option<Operation>) {
|
||||
if let Some(produced_last_delete) = self.last_delete.take() {
|
||||
self.shift -= produced_last_delete.len() as i64;
|
||||
}
|
||||
|
||||
self.last_delete = delete;
|
||||
}
|
||||
|
||||
/// Remove the last delete operation (if there was one) in case it is behind the
|
||||
/// threshold operation.
|
||||
pub fn consume_delete_if_behind_operation(&mut self, threshold_operation: &Operation) {
|
||||
match self.last_delete.as_ref() {
|
||||
Some(last_delete)
|
||||
if threshold_operation.start_index() as i64 + self.shift
|
||||
> last_delete.end_index() as i64 =>
|
||||
{
|
||||
self.shift -= last_delete.len() as i64;
|
||||
self.last_delete = None;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
169
backend/reconcile/src/operation_transformation/mod.rs
Normal file
169
backend/reconcile/src/operation_transformation/mod.rs
Normal file
|
|
@ -0,0 +1,169 @@
|
|||
mod edited_text;
|
||||
mod merge_context;
|
||||
mod operation;
|
||||
|
||||
pub use edited_text::EditedText;
|
||||
pub use operation::Operation;
|
||||
|
||||
use crate::errors::SyncLibError;
|
||||
|
||||
pub fn reconcile(original: &str, left: &str, right: &str) -> Result<String, SyncLibError> {
|
||||
let left_operations = EditedText::from_strings(original, left);
|
||||
let right_operations = EditedText::from_strings(original, right);
|
||||
|
||||
let merged_operations = left_operations.merge(right_operations);
|
||||
merged_operations.apply()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use std::{env, fs, ops::Range, path::Path};
|
||||
|
||||
use pretty_assertions::assert_eq;
|
||||
use ropey::Rope;
|
||||
use test_case::test_matrix;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_merges() {
|
||||
// Both replaced one token but different
|
||||
test_merge_both_ways(
|
||||
"original_1 original_2 original_3",
|
||||
"original_1 edit_1 original_3",
|
||||
"original_1 original_2 edit_2",
|
||||
"original_1 edit_1 edit_2",
|
||||
);
|
||||
|
||||
// Both replaced the same one token
|
||||
test_merge_both_ways(
|
||||
"original_1 original_2 original_3",
|
||||
"original_1 edit_1 original_3",
|
||||
"original_1 edit_1 original_3",
|
||||
"original_1 edit_1 edit_1 original_3",
|
||||
);
|
||||
|
||||
// One deleted a large range, the other deleted subranges and inserted as well
|
||||
test_merge_both_ways(
|
||||
"original_1 original_2 original_3 original_4 original_5",
|
||||
"original_1 original_5",
|
||||
"original_1 edit_1 original_3 edit_2 original_5",
|
||||
"original_1 edit_1 edit_2 original_5",
|
||||
);
|
||||
|
||||
// One deleted a large range, the other inserted and deleted a partially overlapping range
|
||||
test_merge_both_ways(
|
||||
"original_1 original_2 original_3 original_4 original_5",
|
||||
"original_1 original_5",
|
||||
"original_1 edit_1 original_3 edit_2",
|
||||
"original_1 edit_1 edit_2",
|
||||
);
|
||||
|
||||
// Merge a replace and an append
|
||||
test_merge_both_ways("a b ", "c d ", "a b c d ", "c d c d ");
|
||||
|
||||
test_merge_both_ways("a b c d e", "a e", "a c e", "a e");
|
||||
|
||||
test_merge_both_ways("a 0 1 2 b", "a b", "a E 1 F b", "a E F b");
|
||||
|
||||
test_merge_both_ways(
|
||||
"a this one delete b",
|
||||
"a b",
|
||||
"a my one change b",
|
||||
"a my change b",
|
||||
);
|
||||
|
||||
test_merge_both_ways(
|
||||
"this stays, this is one big delete, don't touch this",
|
||||
"this stays, don't touch this",
|
||||
"this stays, my one change, don't touch this",
|
||||
"this stays, my change, don't touch this",
|
||||
);
|
||||
|
||||
test_merge_both_ways("1 2 3 4 5 6", "1 6", "1 2 4 ", "1 ");
|
||||
|
||||
test_merge_both_ways(
|
||||
"hello world",
|
||||
"hi, world",
|
||||
"hello my friend!",
|
||||
"hi, my friend!",
|
||||
);
|
||||
|
||||
// test_merge_both_ways("hello world", "world !", "hi hello world", "hi world !");
|
||||
|
||||
test_merge_both_ways(
|
||||
"both delete the same word",
|
||||
"both the same word",
|
||||
"both the same word",
|
||||
"both the same word",
|
||||
);
|
||||
|
||||
test_merge_both_ways(" ", "it’s utf-8!", " ", "it’s utf-8!");
|
||||
|
||||
test_merge_both_ways(
|
||||
"both delete the same word but one a bit more",
|
||||
"both the same word",
|
||||
"both same word",
|
||||
"both same wordword",
|
||||
);
|
||||
|
||||
test_merge_both_ways(
|
||||
"long text with one big delete and many small",
|
||||
"long small",
|
||||
"long with big and small",
|
||||
"long small",
|
||||
);
|
||||
}
|
||||
|
||||
#[test_matrix( [
|
||||
"pride_and_prejudice.txt",
|
||||
"romeo_and_juliet.txt",
|
||||
"room_with_a_view.txt",
|
||||
"kun_lu.txt",
|
||||
|
||||
], [
|
||||
"pride_and_prejudice.txt",
|
||||
"romeo_and_juliet.txt",
|
||||
"room_with_a_view.txt",
|
||||
"kun_lu.txt"
|
||||
], [
|
||||
"pride_and_prejudice.txt",
|
||||
"romeo_and_juliet.txt",
|
||||
"room_with_a_view.txt",
|
||||
"kun_lu.txt"
|
||||
], [0..10000, 10000..20000], [0..10000, 10000..20000], [0..10000, 10000..20000])]
|
||||
fn test_merge_files_without_panic(
|
||||
file_name_1: &str,
|
||||
file_name_2: &str,
|
||||
file_name_3: &str,
|
||||
range_1: Range<usize>,
|
||||
range_2: Range<usize>,
|
||||
range_3: Range<usize>,
|
||||
) {
|
||||
let files = vec![file_name_1, file_name_2, file_name_3];
|
||||
let permutations = vec![range_1, range_2, range_3];
|
||||
|
||||
let root = Path::new("test/resources/");
|
||||
|
||||
let contents = files
|
||||
.iter()
|
||||
.zip(permutations.iter())
|
||||
.map(|(file, range)| {
|
||||
let path = root.join(file);
|
||||
fs::read_to_string(&path)
|
||||
.unwrap()
|
||||
.chars()
|
||||
.skip(range.start)
|
||||
.take(range.end)
|
||||
.collect::<String>()
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
reconcile(&contents[0], &contents[1], &contents[2]).unwrap();
|
||||
}
|
||||
|
||||
fn test_merge_both_ways(original: &str, edit_1: &str, edit_2: &str, expected: &str) {
|
||||
assert_eq!(reconcile(original, edit_1, edit_2).unwrap(), expected);
|
||||
assert_eq!(reconcile(original, edit_2, edit_1).unwrap(), expected);
|
||||
}
|
||||
}
|
||||
394
backend/reconcile/src/operation_transformation/operation.rs
Normal file
394
backend/reconcile/src/operation_transformation/operation.rs
Normal file
|
|
@ -0,0 +1,394 @@
|
|||
use ropey::Rope;
|
||||
use std::fmt::Display;
|
||||
|
||||
use crate::errors::SyncLibError;
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::merge_context::MergeContext;
|
||||
|
||||
/// Represents a change that can be applied to a text document.
|
||||
/// Operation is tied to a ropey::Rope and is mainly expected to be
|
||||
/// created by EditedText.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum Operation {
|
||||
Insert {
|
||||
index: usize,
|
||||
text: String,
|
||||
},
|
||||
|
||||
Delete {
|
||||
index: usize,
|
||||
deleted_character_count: usize,
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
deleted_text: Option<String>,
|
||||
},
|
||||
}
|
||||
|
||||
impl Operation {
|
||||
/// Creates an insert operation with the given index and text.
|
||||
/// If the text is empty (meaning that the operation would be a no-op), returns None.
|
||||
pub fn create_insert(index: usize, text: String) -> Option<Self> {
|
||||
if text.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(Operation::Insert { index, text })
|
||||
}
|
||||
|
||||
/// Creates a delete operation with the given index and number of to-be-deleted characters.
|
||||
/// If the operation would delete 0 (meaning that the operation would be a no-op), returns None.
|
||||
pub fn create_delete(index: usize, deleted_character_count: usize) -> Option<Self> {
|
||||
if deleted_character_count == 0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(Operation::Delete {
|
||||
index,
|
||||
deleted_character_count,
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
deleted_text: None,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn create_delete_with_text(index: usize, text: String) -> Option<Self> {
|
||||
if text.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(Operation::Delete {
|
||||
index,
|
||||
deleted_character_count: text.chars().count(),
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
deleted_text: Some(text),
|
||||
})
|
||||
}
|
||||
|
||||
/// Tries to apply the operation to the given ropey::Rope text, returning the modified text.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns a SyncLibError::OperationApplicationError if the operation cannot be applied.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// When compiled in debug mode, panics if a delete operation is attempted on a range
|
||||
/// of text that does not match the text to be deleted.
|
||||
pub fn apply<'a>(&self, rope_text: &'a mut Rope) -> Result<&'a mut Rope, SyncLibError> {
|
||||
match self {
|
||||
Operation::Insert { text, .. } => rope_text
|
||||
.try_insert(self.start_index(), text)
|
||||
.map_err(|err| {
|
||||
SyncLibError::OperationApplicationError(format!(
|
||||
"Failed to insert text: {}",
|
||||
err
|
||||
))
|
||||
}),
|
||||
Operation::Delete {
|
||||
#[cfg(debug_assertions)]
|
||||
deleted_text,
|
||||
..
|
||||
} => {
|
||||
debug_assert!(
|
||||
rope_text.get_slice(self.range()).is_some(),
|
||||
"Failed to get slice of text to delete"
|
||||
);
|
||||
|
||||
if let Some(text) = deleted_text {
|
||||
debug_assert_eq!(
|
||||
rope_text.get_slice(self.range()).unwrap().to_string(),
|
||||
*text,
|
||||
"Text to delete does not match the text in the rope"
|
||||
);
|
||||
}
|
||||
|
||||
rope_text.try_remove(self.range()).map_err(|err| {
|
||||
SyncLibError::OperationApplicationError(format!(
|
||||
"Failed to remove text: {}",
|
||||
err
|
||||
))
|
||||
})
|
||||
}
|
||||
}?;
|
||||
|
||||
Ok(rope_text)
|
||||
}
|
||||
|
||||
/// Returns the index of the first character that the operation affects.
|
||||
pub fn start_index(&self) -> usize {
|
||||
match self {
|
||||
Operation::Insert { index, .. } => *index,
|
||||
Operation::Delete { index, .. } => *index,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the index of the last character that the operation affects.
|
||||
pub fn end_index(&self) -> usize {
|
||||
// len() must be greater than 0 because operations must be non-empty
|
||||
self.start_index() + self.len() - 1
|
||||
}
|
||||
|
||||
/// Returns the range of indices of characters that the operation affects, inclusive.
|
||||
pub fn range(&self) -> std::ops::RangeInclusive<usize> {
|
||||
self.start_index()..=self.end_index()
|
||||
}
|
||||
|
||||
/// Returns the number of affected characters. It is always greater than 0 because empty operations cannot be created.
|
||||
pub fn len(&self) -> usize {
|
||||
match self {
|
||||
Operation::Insert { text, .. } => text.chars().count(),
|
||||
Operation::Delete {
|
||||
deleted_character_count,
|
||||
..
|
||||
} => *deleted_character_count,
|
||||
}
|
||||
}
|
||||
|
||||
/// The operation cannot be empty.
|
||||
pub fn is_empty(&self) -> bool {
|
||||
debug_assert!(self.len() > 0, "Operation cannot be empty");
|
||||
false
|
||||
}
|
||||
|
||||
/// Clones the operation while updating the index.
|
||||
pub fn with_index(self, index: usize) -> Self {
|
||||
match self {
|
||||
Operation::Insert { text, .. } => Operation::Insert { index, text },
|
||||
Operation::Delete {
|
||||
deleted_character_count,
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
deleted_text,
|
||||
..
|
||||
} => Operation::Delete {
|
||||
index,
|
||||
deleted_character_count,
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
deleted_text,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Clones the operation while shifting the index by the given offset.
|
||||
/// The offset can be negative but the resulting index must be non-negative.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// In debug mode, panics if the resulting index is negative.
|
||||
pub fn with_shifted_index(self, offset: i64) -> Self {
|
||||
let index = self.start_index() as i64 + offset;
|
||||
debug_assert!(index >= 0, "Shifted index must be non-negative");
|
||||
|
||||
self.with_index(index as usize)
|
||||
}
|
||||
|
||||
/// Merges the operation with the given context, producing a new operation and updating the context.
|
||||
/// This implements a comples FSM that handles the merging of operations in a way that is consistent with the text.
|
||||
/// The contexts are updated in-place.
|
||||
pub fn merge_operations_with_context(
|
||||
self,
|
||||
affecting_context: &mut MergeContext,
|
||||
produced_context: &mut MergeContext,
|
||||
) -> Option<Operation> {
|
||||
affecting_context.consume_delete_if_behind_operation(&self);
|
||||
|
||||
let operation = self.with_shifted_index(affecting_context.shift);
|
||||
|
||||
match (operation, affecting_context.last_delete.clone()) {
|
||||
(operation @ Operation::Insert { .. }, None) => {
|
||||
produced_context.shift += operation.len() as i64;
|
||||
Some(operation)
|
||||
}
|
||||
|
||||
(operation @ Operation::Delete { .. }, None) => {
|
||||
produced_context.replace_delete(Some(operation.clone()));
|
||||
Some(operation)
|
||||
}
|
||||
|
||||
(operation @ Operation::Insert { .. }, Some(last_delete)) => {
|
||||
produced_context.shift += operation.len() as i64;
|
||||
|
||||
debug_assert!(
|
||||
last_delete.range().contains(&operation.start_index()),
|
||||
"There is a last delete ({last_delete}) but the operation ({operation}) is not contained in it"
|
||||
);
|
||||
|
||||
let difference = operation.start_index() as i64 - last_delete.start_index() as i64;
|
||||
|
||||
let moved_operation = operation.with_index(last_delete.start_index());
|
||||
|
||||
affecting_context.last_delete = Operation::create_delete(
|
||||
moved_operation.end_index() + 1,
|
||||
(last_delete.len() as i64 - difference) as usize,
|
||||
);
|
||||
affecting_context.shift -= difference;
|
||||
|
||||
Some(moved_operation)
|
||||
}
|
||||
|
||||
(operation @ Operation::Delete { .. }, Some(last_delete)) => {
|
||||
debug_assert!(
|
||||
last_delete.range().contains(&operation.start_index()),
|
||||
"There is a last delete ({last_delete}) but the operation ({operation}) is not contained in it"
|
||||
);
|
||||
|
||||
let difference = operation.start_index() as i64 - last_delete.start_index() as i64;
|
||||
|
||||
let updated_delete = Operation::create_delete(
|
||||
last_delete.start_index(),
|
||||
0.max(operation.end_index() as i64 - last_delete.end_index() as i64) as usize,
|
||||
);
|
||||
|
||||
affecting_context.shift -= difference;
|
||||
affecting_context.last_delete = Operation::create_delete(
|
||||
last_delete.start_index(),
|
||||
0.max(last_delete.end_index() as i64 - operation.end_index() as i64) as usize,
|
||||
);
|
||||
|
||||
produced_context.replace_delete(updated_delete.clone());
|
||||
|
||||
updated_delete
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Merges the operation with another operation that is consequtive to this operation.
|
||||
/// The other operation must start where this operation ends.
|
||||
/// The two operations must be of the same type, otherwise panics.
|
||||
pub fn extend(self, other: &Self) -> Self {
|
||||
match (self, other) {
|
||||
(
|
||||
Operation::Insert { index, text },
|
||||
Operation::Insert {
|
||||
text: other_text, ..
|
||||
},
|
||||
) => {
|
||||
let end_index = index + text.chars().count();
|
||||
debug_assert!(
|
||||
end_index == other.start_index(),
|
||||
"Cannot merge non-consequtive inserts with index {} and {}",
|
||||
end_index,
|
||||
other.start_index()
|
||||
);
|
||||
|
||||
Operation::Insert {
|
||||
index,
|
||||
text: text + other_text,
|
||||
}
|
||||
}
|
||||
(
|
||||
Operation::Delete {
|
||||
index,
|
||||
deleted_character_count,
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
deleted_text,
|
||||
},
|
||||
Operation::Delete {
|
||||
index: other_index,
|
||||
deleted_character_count: other_deleted_character_count,
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
deleted_text: other_deleted_text,
|
||||
},
|
||||
) => {
|
||||
debug_assert!(
|
||||
index == *other_index,
|
||||
"Cannot merge non-consequtive deletes",
|
||||
);
|
||||
|
||||
Operation::Delete {
|
||||
index,
|
||||
deleted_character_count: deleted_character_count
|
||||
+ other_deleted_character_count,
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
deleted_text: deleted_text
|
||||
.into_iter()
|
||||
.flat_map(|t1| other_deleted_text.as_ref().map(|t2| t1 + t2).into_iter())
|
||||
.last(),
|
||||
}
|
||||
}
|
||||
(this, other) => panic!(
|
||||
"Cannot merge operations of different type: {:?} and {:?}",
|
||||
&this, &other
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Operation {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Operation::Insert { index, text } => {
|
||||
write!(f, "<insert '{}' from index {}>", text, index)
|
||||
}
|
||||
Operation::Delete {
|
||||
index,
|
||||
deleted_character_count,
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
deleted_text,
|
||||
} => {
|
||||
if cfg!(debug_assertions) && deleted_text.is_some() {
|
||||
write!(
|
||||
f,
|
||||
"<delete '{}' from index {}>",
|
||||
deleted_text.as_ref().unwrap_or(&"<unknown>".to_string()),
|
||||
index
|
||||
)
|
||||
} else {
|
||||
write!(
|
||||
f,
|
||||
"<delete {} characters () from index {}>",
|
||||
deleted_character_count, index
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_shifting_error() {
|
||||
insta::assert_debug_snapshot!(Operation::create_insert(1, "hi".to_string())
|
||||
.unwrap()
|
||||
.with_shifted_index(-2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_delete_with_create() -> Result<(), SyncLibError> {
|
||||
let mut rope = Rope::from_str("hello world");
|
||||
let operation = Operation::create_delete_with_text(5, " world".to_string()).unwrap();
|
||||
|
||||
operation.apply(&mut rope)?;
|
||||
|
||||
assert_eq!(rope.to_string(), "hello");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_insert() -> Result<(), SyncLibError> {
|
||||
let mut rope = Rope::from_str("hello");
|
||||
let operation = Operation::create_insert(5, " my friend".to_string()).unwrap();
|
||||
|
||||
operation.apply(&mut rope)?;
|
||||
|
||||
assert_eq!(rope.to_string(), "hello my friend");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
---
|
||||
source: reconcile/src/operation_transformation/edited_text.rs
|
||||
expression: operations
|
||||
snapshot_kind: text
|
||||
---
|
||||
EditedText {
|
||||
text: "hello world! How are you? Adam",
|
||||
operations: [
|
||||
OrderedOperation {
|
||||
order: 0,
|
||||
operation: Insert {
|
||||
index: 0,
|
||||
text: "Hello, my friend! ",
|
||||
},
|
||||
},
|
||||
OrderedOperation {
|
||||
order: 0,
|
||||
operation: Delete {
|
||||
index: 18,
|
||||
deleted_character_count: 13,
|
||||
deleted_text: Some(
|
||||
"hello world! ",
|
||||
),
|
||||
},
|
||||
},
|
||||
OrderedOperation {
|
||||
order: 21,
|
||||
operation: Delete {
|
||||
index: 26,
|
||||
deleted_character_count: 5,
|
||||
deleted_text: Some(
|
||||
"you? ",
|
||||
),
|
||||
},
|
||||
},
|
||||
OrderedOperation {
|
||||
order: 26,
|
||||
operation: Delete {
|
||||
index: 26,
|
||||
deleted_character_count: 5,
|
||||
deleted_text: Some(
|
||||
" Adam",
|
||||
),
|
||||
},
|
||||
},
|
||||
OrderedOperation {
|
||||
order: 31,
|
||||
operation: Insert {
|
||||
index: 26,
|
||||
text: "you ",
|
||||
},
|
||||
},
|
||||
OrderedOperation {
|
||||
order: 31,
|
||||
operation: Insert {
|
||||
index: 30,
|
||||
text: "doing? Albert",
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
---
|
||||
source: reconcile/src/operation_transformation/edited_text.rs
|
||||
assertion_line: 242
|
||||
expression: operations
|
||||
snapshot_kind: text
|
||||
---
|
||||
EditedText {
|
||||
text: "hello world! How are you? Adam",
|
||||
operations: [
|
||||
OrderedOperation {
|
||||
order: 0,
|
||||
operation: Insert {
|
||||
index: 0,
|
||||
text: "Hello, my friend! ",
|
||||
},
|
||||
},
|
||||
OrderedOperation {
|
||||
order: 0,
|
||||
operation: Delete {
|
||||
index: 18,
|
||||
deleted_character_count: 13,
|
||||
deleted_text: Some(
|
||||
"hello world! ",
|
||||
),
|
||||
},
|
||||
},
|
||||
OrderedOperation {
|
||||
order: 21,
|
||||
operation: Delete {
|
||||
index: 26,
|
||||
deleted_character_count: 10,
|
||||
deleted_text: Some(
|
||||
"you? Adam",
|
||||
),
|
||||
},
|
||||
},
|
||||
OrderedOperation {
|
||||
order: 31,
|
||||
operation: Insert {
|
||||
index: 26,
|
||||
text: "you doing? Albert",
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
---
|
||||
source: reconcile/src/operations/edited_text.rs
|
||||
expression: operations
|
||||
snapshot_kind: text
|
||||
---
|
||||
EditedText {
|
||||
text: "hello world! How are you? Adam",
|
||||
operations: [
|
||||
OrderedOperation {
|
||||
order: 0,
|
||||
operation: Insert {
|
||||
index: 0,
|
||||
text: "Hello, my friend! ",
|
||||
},
|
||||
},
|
||||
OrderedOperation {
|
||||
order: 0,
|
||||
operation: Delete {
|
||||
index: 18,
|
||||
deleted_character_count: 13,
|
||||
deleted_text: Some(
|
||||
"hello world! ",
|
||||
),
|
||||
},
|
||||
},
|
||||
OrderedOperation {
|
||||
order: 21,
|
||||
operation: Delete {
|
||||
index: 26,
|
||||
deleted_character_count: 5,
|
||||
deleted_text: Some(
|
||||
"you? ",
|
||||
),
|
||||
},
|
||||
},
|
||||
OrderedOperation {
|
||||
order: 26,
|
||||
operation: Delete {
|
||||
index: 26,
|
||||
deleted_character_count: 5,
|
||||
deleted_text: Some(
|
||||
" Adam",
|
||||
),
|
||||
},
|
||||
},
|
||||
OrderedOperation {
|
||||
order: 31,
|
||||
operation: Insert {
|
||||
index: 26,
|
||||
text: "you ",
|
||||
},
|
||||
},
|
||||
OrderedOperation {
|
||||
order: 31,
|
||||
operation: Insert {
|
||||
index: 30,
|
||||
text: "doing? Albert",
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
---
|
||||
source: reconcile/src/operations/operation_sequence.rs
|
||||
expression: operations
|
||||
snapshot_kind: text
|
||||
---
|
||||
EditedText {
|
||||
operations: [
|
||||
OrderedOperation {
|
||||
order: 0,
|
||||
operation: Insert {
|
||||
index: 0,
|
||||
text: "Hello, my friend! ",
|
||||
},
|
||||
},
|
||||
OrderedOperation {
|
||||
order: 0,
|
||||
operation: Delete {
|
||||
index: 18,
|
||||
deleted_character_count: 13,
|
||||
deleted_text: Some(
|
||||
"hello world! ",
|
||||
),
|
||||
},
|
||||
},
|
||||
OrderedOperation {
|
||||
order: 21,
|
||||
operation: Delete {
|
||||
index: 26,
|
||||
deleted_character_count: 5,
|
||||
deleted_text: Some(
|
||||
"you? ",
|
||||
),
|
||||
},
|
||||
},
|
||||
OrderedOperation {
|
||||
order: 26,
|
||||
operation: Delete {
|
||||
index: 26,
|
||||
deleted_character_count: 5,
|
||||
deleted_text: Some(
|
||||
" Adam",
|
||||
),
|
||||
},
|
||||
},
|
||||
OrderedOperation {
|
||||
order: 31,
|
||||
operation: Insert {
|
||||
index: 26,
|
||||
text: "you ",
|
||||
},
|
||||
},
|
||||
OrderedOperation {
|
||||
order: 31,
|
||||
operation: Insert {
|
||||
index: 30,
|
||||
text: "doing? Albert",
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue