Merge crates
This commit is contained in:
parent
82e77eec89
commit
bcbac03228
60 changed files with 73 additions and 248 deletions
55
src/operation_transformation/utils/cook_operations.rs
Normal file
55
src/operation_transformation/utils/cook_operations.rs
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
use crate::{
|
||||
diffs::raw_operation::RawOperation,
|
||||
operation_transformation::{Operation, ordered_operation::OrderedOperation},
|
||||
};
|
||||
|
||||
/// Turn raw operations into ordered operations while keeping track of old & new
|
||||
/// 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| {
|
||||
let length = raw_operation.original_text_length();
|
||||
|
||||
match raw_operation {
|
||||
RawOperation::Equal(..) => {
|
||||
let op = if cfg!(debug_assertions) {
|
||||
Operation::create_equal_with_text(new_index, raw_operation.get_original_text())
|
||||
} else {
|
||||
Operation::create_equal(new_index, 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::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
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
127
src/operation_transformation/utils/elongate_operations.rs
Normal file
127
src/operation_transformation/utils/elongate_operations.rs
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
use core::iter;
|
||||
|
||||
use crate::diffs::raw_operation::RawOperation;
|
||||
|
||||
/// Elongates the operations by merging adjacent insertions and deletions that
|
||||
/// can be joined. This makes the subsequent merging of operations more
|
||||
/// intuitive.
|
||||
pub fn elongate_operations<I, T>(raw_operations: I) -> Vec<RawOperation<T>>
|
||||
where
|
||||
I: IntoIterator<Item = RawOperation<T>>,
|
||||
T: PartialEq + Clone + std::fmt::Debug,
|
||||
{
|
||||
// This might look bad, but this makes sense. The inserts and deltes can be
|
||||
// interleaved, such as: IDIDID and we need to turn this into IIIDDD.
|
||||
// So we need to keep track of both the last insert and delete operations, not
|
||||
// just the last one.
|
||||
let mut maybe_previous_insert: Option<RawOperation<T>> = None;
|
||||
let mut maybe_previous_delete: Option<RawOperation<T>> = None;
|
||||
|
||||
let mut result: Vec<RawOperation<T>> = raw_operations
|
||||
.into_iter()
|
||||
.flat_map(|next| match next {
|
||||
RawOperation::Insert(..) => match maybe_previous_insert.take() {
|
||||
Some(prev) if prev.is_right_joinable() && next.is_left_joinable() => {
|
||||
maybe_previous_insert = Some(prev.extend(next));
|
||||
Box::new(iter::empty()) as Box<dyn Iterator<Item = RawOperation<T>>>
|
||||
}
|
||||
prev => {
|
||||
maybe_previous_insert = Some(next);
|
||||
Box::new(prev.into_iter())
|
||||
}
|
||||
},
|
||||
RawOperation::Delete(..) => match maybe_previous_delete.take() {
|
||||
Some(prev) if prev.is_right_joinable() && next.is_left_joinable() => {
|
||||
maybe_previous_delete = Some(prev.extend(next));
|
||||
Box::new(iter::empty()) as Box<dyn Iterator<Item = RawOperation<T>>>
|
||||
}
|
||||
prev => {
|
||||
maybe_previous_delete = Some(next);
|
||||
Box::new(prev.into_iter())
|
||||
}
|
||||
},
|
||||
RawOperation::Equal(..) => Box::new(
|
||||
maybe_previous_insert
|
||||
.take()
|
||||
.into_iter()
|
||||
.chain(maybe_previous_delete.take())
|
||||
.chain(iter::once(next)),
|
||||
) as Box<dyn Iterator<Item = RawOperation<T>>>,
|
||||
})
|
||||
.collect();
|
||||
|
||||
if let Some(prev) = maybe_previous_insert {
|
||||
result.push(prev);
|
||||
}
|
||||
|
||||
if let Some(prev) = maybe_previous_delete {
|
||||
result.push(prev);
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
// #[cfg(test)]
|
||||
// mod tests {
|
||||
|
||||
// use super::*;
|
||||
|
||||
// #[test]
|
||||
// fn test_elongate_operations_empty() {
|
||||
// let operations: Vec<RawOperation<()>> = vec![];
|
||||
// let result = elongate_operations(operations);
|
||||
// assert_eq!(result, vec![]);
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn test_elongate_operations_single_operation() {
|
||||
// let operations = vec![RawOperation::Insert(vec!["test".into()])];
|
||||
// let result = elongate_operations(operations);
|
||||
// assert_eq!(result.len(), 1);
|
||||
// assert!(matches!(result[0], RawOperation::Insert(_)));
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn test_elongate_operations_interleaved() {
|
||||
// let operations = vec![
|
||||
// RawOperation::Insert(vec!["a".into()]),
|
||||
// RawOperation::Delete(vec!["b".into()]),
|
||||
// RawOperation::Insert(vec!["c".into()]),
|
||||
// RawOperation::Delete(vec!["d".into()]),
|
||||
// ];
|
||||
// let result = elongate_operations(operations);
|
||||
// assert_eq!(result.len(), 2);
|
||||
// assert!(matches!(result[0], RawOperation::Insert(_)));
|
||||
// assert!(matches!(result[1], RawOperation::Delete(_)));
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn test_elongate_operations_with_equal() {
|
||||
// let operations = vec![
|
||||
// RawOperation::Equal(vec!["a".into()]),
|
||||
// RawOperation::Equal(vec!["b".into()]),
|
||||
// RawOperation::Insert(vec!["c".into()]),
|
||||
// RawOperation::Insert(vec!["d".into()]),
|
||||
// ];
|
||||
// let result = elongate_operations(operations);
|
||||
// assert_eq!(result.len(), 2);
|
||||
// assert!(matches!(result[0], RawOperation::Equal(_)));
|
||||
// assert!(matches!(result[1], RawOperation::Insert(_)));
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn test_elongate_operations_mixed_sequence() {
|
||||
// let operations = vec![
|
||||
// RawOperation::Insert(vec!["a".into()]),
|
||||
// RawOperation::Equal(vec!["b".into()]),
|
||||
// RawOperation::Delete(vec!["c".into()]),
|
||||
// RawOperation::Equal(vec!["d".into()]),
|
||||
// ];
|
||||
// let result = elongate_operations(operations);
|
||||
// assert_eq!(result.len(), 4);
|
||||
// assert!(matches!(result[0], RawOperation::Insert(_)));
|
||||
// assert!(matches!(result[1], RawOperation::Equal(_)));
|
||||
// assert!(matches!(result[2], RawOperation::Delete(_)));
|
||||
// assert!(matches!(result[3], RawOperation::Equal(_)));
|
||||
// }
|
||||
// }
|
||||
Loading…
Add table
Add a link
Reference in a new issue