Remove indexes from Operation

This commit is contained in:
Andras Schmelczer 2025-06-22 09:50:37 +01:00
parent a4f1a496bd
commit 917d47fbaa
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
3 changed files with 27 additions and 149 deletions

View file

@ -3,14 +3,12 @@ use crate::{
operation_transformation::{Operation, ordered_operation::OrderedOperation},
};
/// Turn raw operations into ordered operations while keeping track of old & new
/// indexes.
/// Turn raw operations into ordered operations while keeping track of indexes.
pub fn cook_operations<I, T>(raw_operations: I) -> impl Iterator<Item = OrderedOperation<T>>
where
I: IntoIterator<Item = RawOperation<T>>,
T: PartialEq + Clone + std::fmt::Debug,
{
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().filter_map(move |raw_operation| {
@ -19,30 +17,23 @@ where
match raw_operation {
RawOperation::Equal(..) => {
let op = if cfg!(debug_assertions) {
Operation::create_equal_with_text(new_index, raw_operation.get_original_text())
Operation::create_equal_with_text(raw_operation.get_original_text())
} else {
Operation::create_equal(new_index, length)
Operation::create_equal(length)
}
.map(|operation| OrderedOperation { order, operation });
new_index += length;
order += length;
op
}
RawOperation::Insert(tokens) => {
let op = Operation::create_insert(new_index, tokens)
.map(|operation| OrderedOperation { order, operation });
new_index += length;
op
}
RawOperation::Insert(tokens) => Operation::create_insert(tokens)
.map(|operation| OrderedOperation { order, operation }),
RawOperation::Delete(..) => {
let op = if cfg!(debug_assertions) {
Operation::create_delete_with_text(new_index, raw_operation.get_original_text())
Operation::create_delete_with_text(raw_operation.get_original_text())
} else {
Operation::create_delete(new_index, length)
Operation::create_delete(length)
}
.map(|operation| OrderedOperation { order, operation });