Remove merge context
This commit is contained in:
parent
46d2e0c485
commit
a4f1a496bd
3 changed files with 6 additions and 94 deletions
|
|
@ -1,6 +1,5 @@
|
||||||
mod cursor;
|
mod cursor;
|
||||||
mod edited_text;
|
mod edited_text;
|
||||||
mod merge_context;
|
|
||||||
mod operation;
|
mod operation;
|
||||||
mod ordered_operation;
|
mod ordered_operation;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,8 @@ use serde::{Deserialize, Serialize};
|
||||||
use super::{CursorPosition, Operation, TextWithCursors, ordered_operation::OrderedOperation};
|
use super::{CursorPosition, Operation, TextWithCursors, ordered_operation::OrderedOperation};
|
||||||
use crate::{
|
use crate::{
|
||||||
diffs::{myers::diff, raw_operation::RawOperation},
|
diffs::{myers::diff, raw_operation::RawOperation},
|
||||||
operation_transformation::{
|
operation_transformation::utils::{
|
||||||
merge_context::MergeContext,
|
cook_operations::cook_operations, elongate_operations::elongate_operations,
|
||||||
utils::{cook_operations::cook_operations, elongate_operations::elongate_operations},
|
|
||||||
},
|
},
|
||||||
tokenizer::{Tokenizer, word_tokenizer::word_tokenizer},
|
tokenizer::{Tokenizer, word_tokenizer::word_tokenizer},
|
||||||
utils::{side::Side, string_builder::StringBuilder},
|
utils::{side::Side, string_builder::StringBuilder},
|
||||||
|
|
@ -99,9 +98,6 @@ where
|
||||||
"`EditedText`-s must be derived from the same text to be mergable"
|
"`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 merged_cursors = Vec::with_capacity(self.cursors.len() + other.cursors.len());
|
||||||
let mut left_cursors = self.cursors.into_iter().peekable();
|
let mut left_cursors = self.cursors.into_iter().peekable();
|
||||||
let mut right_cursors = other.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 original_length = operation.len() as i64;
|
||||||
let result = match side {
|
let result = match side {
|
||||||
Side::Left => {
|
Side::Left => {
|
||||||
let result = operation.merge_operations_with_context(
|
let result =
|
||||||
order,
|
operation.merge_operations_with_context(order, maybe_other_operation);
|
||||||
&mut right_merge_context,
|
|
||||||
&mut left_merge_context,
|
|
||||||
maybe_other_operation,
|
|
||||||
);
|
|
||||||
|
|
||||||
if let Some(ref op @ (Operation::Insert { .. } | Operation::Equal { .. })) =
|
if let Some(ref op @ (Operation::Insert { .. } | Operation::Equal { .. })) =
|
||||||
result
|
result
|
||||||
|
|
@ -182,12 +174,8 @@ where
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
Side::Right => {
|
Side::Right => {
|
||||||
let result = operation.merge_operations_with_context(
|
let result =
|
||||||
order,
|
operation.merge_operations_with_context(order, maybe_other_operation);
|
||||||
&mut left_merge_context,
|
|
||||||
&mut right_merge_context,
|
|
||||||
maybe_other_operation,
|
|
||||||
);
|
|
||||||
|
|
||||||
if let Some(ref op @ (Operation::Insert { .. } | Operation::Equal { .. })) =
|
if let Some(ref op @ (Operation::Insert { .. } | Operation::Equal { .. })) =
|
||||||
result
|
result
|
||||||
|
|
|
||||||
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue