Remove merge context

This commit is contained in:
Andras Schmelczer 2025-06-22 09:48:56 +01:00
parent 46d2e0c485
commit a4f1a496bd
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
3 changed files with 6 additions and 94 deletions

View file

@ -1,6 +1,5 @@
mod cursor;
mod edited_text;
mod merge_context;
mod operation;
mod ordered_operation;
mod utils;

View file

@ -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

View file

@ -1,75 +0,0 @@
use core::fmt::Debug;
use crate::operation_transformation::Operation;
#[derive(Clone, Debug)]
pub struct MergeContext<T>
where
T: PartialEq + Clone + std::fmt::Debug,
{
last_operation: Option<Operation<T>>,
pub shift: i64,
}
impl<T> Default for MergeContext<T>
where
T: PartialEq + Clone + std::fmt::Debug,
{
fn default() -> Self {
MergeContext {
last_operation: None,
shift: 0,
}
}
}
impl<T> MergeContext<T>
where
T: PartialEq + Clone + std::fmt::Debug,
{
pub fn last_operation(&self) -> Option<&Operation<T>> { self.last_operation.as_ref() }
pub fn replace_last_operation(&mut self, operation: Option<Operation<T>>) {
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<Operation<T>>) {
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;
}
}
_ => {}
}
}
}