From a4f1a496bd8535919a02540196337d44d3276345 Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Sun, 22 Jun 2025 09:48:56 +0100 Subject: [PATCH] Remove merge context --- src/operation_transformation.rs | 1 - src/operation_transformation/edited_text.rs | 24 ++---- src/operation_transformation/merge_context.rs | 75 ------------------- 3 files changed, 6 insertions(+), 94 deletions(-) delete mode 100644 src/operation_transformation/merge_context.rs diff --git a/src/operation_transformation.rs b/src/operation_transformation.rs index 9fd5a5d..c971229 100644 --- a/src/operation_transformation.rs +++ b/src/operation_transformation.rs @@ -1,6 +1,5 @@ mod cursor; mod edited_text; -mod merge_context; mod operation; mod ordered_operation; mod utils; diff --git a/src/operation_transformation/edited_text.rs b/src/operation_transformation/edited_text.rs index 6aea48e..4190157 100644 --- a/src/operation_transformation/edited_text.rs +++ b/src/operation_transformation/edited_text.rs @@ -4,9 +4,8 @@ use serde::{Deserialize, Serialize}; use super::{CursorPosition, Operation, TextWithCursors, ordered_operation::OrderedOperation}; use crate::{ diffs::{myers::diff, raw_operation::RawOperation}, - operation_transformation::{ - merge_context::MergeContext, - utils::{cook_operations::cook_operations, elongate_operations::elongate_operations}, + operation_transformation::utils::{ + cook_operations::cook_operations, elongate_operations::elongate_operations, }, tokenizer::{Tokenizer, word_tokenizer::word_tokenizer}, utils::{side::Side, string_builder::StringBuilder}, @@ -99,9 +98,6 @@ where "`EditedText`-s must be derived from the same text to be mergable" ); - let mut left_merge_context = MergeContext::default(); - let mut right_merge_context = MergeContext::default(); - 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(); @@ -148,12 +144,8 @@ where let original_length = operation.len() as i64; let result = match side { Side::Left => { - let result = operation.merge_operations_with_context( - order, - &mut right_merge_context, - &mut left_merge_context, - maybe_other_operation, - ); + let result = + operation.merge_operations_with_context(order, maybe_other_operation); if let Some(ref op @ (Operation::Insert { .. } | Operation::Equal { .. })) = result @@ -182,12 +174,8 @@ where result } Side::Right => { - let result = operation.merge_operations_with_context( - order, - &mut left_merge_context, - &mut right_merge_context, - maybe_other_operation, - ); + let result = + operation.merge_operations_with_context(order, maybe_other_operation); if let Some(ref op @ (Operation::Insert { .. } | Operation::Equal { .. })) = result diff --git a/src/operation_transformation/merge_context.rs b/src/operation_transformation/merge_context.rs deleted file mode 100644 index b40d2f3..0000000 --- a/src/operation_transformation/merge_context.rs +++ /dev/null @@ -1,75 +0,0 @@ -use core::fmt::Debug; - -use crate::operation_transformation::Operation; - -#[derive(Clone, Debug)] -pub struct MergeContext -where - T: PartialEq + Clone + std::fmt::Debug, -{ - last_operation: Option>, - pub shift: i64, -} - -impl Default for MergeContext -where - T: PartialEq + Clone + std::fmt::Debug, -{ - fn default() -> Self { - MergeContext { - last_operation: None, - shift: 0, - } - } -} - -impl MergeContext -where - T: PartialEq + Clone + std::fmt::Debug, -{ - pub fn last_operation(&self) -> Option<&Operation> { self.last_operation.as_ref() } - - pub fn replace_last_operation(&mut self, operation: Option>) { - self.last_operation = operation; - } - - /// Replace the last delete operation (if there was one) with a new one - /// while applying it to the `shift` in case the last operation - /// was a delete. - pub fn consume_and_replace_last_operation(&mut self, operation: Option>) { - if let Some(Operation::Delete { - deleted_character_count, - .. - }) = self.last_operation.take() - { - self.shift -= deleted_character_count as i64; - } - - self.last_operation = operation; - } - - /// Remove the last operation (if there was one) in case it is behind the - /// threshold operation. This updates the `shift` in case the last operation - /// was a delete. - pub fn consume_last_operation_if_it_is_too_behind(&mut self, threshold_index: i64) { - match self.last_operation.as_ref() { - Some( - op @ Operation::Delete { - deleted_character_count, - .. - }, - ) => { - if threshold_index + self.shift >= op.end_index() as i64 { - self.shift -= *deleted_character_count as i64; - self.last_operation = None; - } - } - Some(op @ Operation::Insert { .. }) | Some(op @ Operation::Equal { .. }) => { - if threshold_index + self.shift - op.len() as i64 >= op.end_index() as i64 { - self.last_operation = None; - } - } - _ => {} - } - } -}