Refactors

This commit is contained in:
Andras Schmelczer 2024-11-16 22:54:06 +00:00
commit dea7a1d28e
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C

View file

@ -26,8 +26,7 @@ pub fn tokenize(text: &str) -> Vec<&str> {
}
impl OperationSequence {
pub fn new(mut operations: Vec<Operation>) -> Self {
// operations.sort();
pub fn new(operations: Vec<Operation>) -> Self {
Self { operations }
}
@ -151,22 +150,23 @@ impl OperationSequence {
Ok(match (operation, affecting_context.last_delete.clone()) {
(Operation::Insert { .. }, None) => {
produced_context.shift += operation.len();
Some(operation.with_shifted_index(affecting_context.shift))
Some(operation.with_shifted_index(affecting_context.shift)?)
}
(Operation::Delete { .. }, None) => {
let operation = Some(operation.with_shifted_index(affecting_context.shift));
let operation = Some(operation.with_shifted_index(affecting_context.shift)?);
Self::replace_delete_in_produced_context(produced_context, operation.clone());
operation
}
(Operation::Insert { .. }, Some(last_delete)) => {
produced_context.shift += operation.len();
if last_delete
.range()
.contains(&(operation.start_index() + affecting_context.shift))
{
let moved_operation = operation.with_index(last_delete.start_index());
produced_context.shift += operation.len();
let moved_operation = operation.with_index(last_delete.start_index())?;
affecting_context.last_delete = Operation::create_delete(
moved_operation.end_index() + 1,
@ -175,57 +175,47 @@ impl OperationSequence {
Some(moved_operation)
} else {
produced_context.shift += operation.len();
Self::pick_up_dangling_delete_from_affecting_context(
affecting_context,
last_delete,
);
Some(operation.with_shifted_index(affecting_context.shift))
Some(operation.with_shifted_index(affecting_context.shift)?)
}
}
(Operation::Delete { .. }, Some(last_delete)) => {
let shifted_operation = operation.with_shifted_index(affecting_context.shift);
if last_delete
.range()
.contains(&shifted_operation.start_index())
&& last_delete.range().contains(&shifted_operation.end_index())
{
affecting_context.last_delete = Operation::create_delete(
last_delete.start_index(),
last_delete.len() - operation.len(), // it's fully contained so it must be >= 0
)?;
None
} else if last_delete
let updated_delete = if last_delete
.range()
.contains(&(operation.start_index() + affecting_context.shift))
{
let overlap = last_delete.end_index()
- (operation.start_index() + affecting_context.shift)
+ 1;
affecting_context.last_delete = None;
affecting_context.shift -= last_delete.len() - overlap;
let operation = Operation::create_delete(
affecting_context.last_delete = Operation::create_delete(
last_delete.start_index(),
operation.len() - overlap,
0.max(last_delete.len() - operation.len()),
)?;
Self::replace_delete_in_produced_context(produced_context, operation.clone());
operation
if last_delete.end_index() < operation.end_index() + affecting_context.shift {
affecting_context.shift -= last_delete.len() - overlap
}
Operation::create_delete(
last_delete.start_index(),
0.max(operation.len() - overlap),
)?
} else {
Self::pick_up_dangling_delete_from_affecting_context(
affecting_context,
last_delete,
);
let operation = Some(operation.with_shifted_index(affecting_context.shift));
Self::replace_delete_in_produced_context(produced_context, operation.clone());
operation
}
Some(operation.with_shifted_index(affecting_context.shift)?)
};
Self::replace_delete_in_produced_context(produced_context, updated_delete.clone());
updated_delete
}
})
}
@ -252,10 +242,7 @@ impl OperationSequence {
#[cfg(test)]
mod tests {
use itertools::Itertools;
use pretty_assertions::{assert_eq, assert_ne};
use similar::DiffableStr;
use std::{fs, path::Path};
use pretty_assertions::assert_eq;
use super::*;
@ -264,7 +251,7 @@ mod tests {
let left = "hello world! How are you? Adam";
let right = "Hello, my friend! How are you doing? Albert";
let operations = OperationSequence::try_from_string_diff(left, right, 0.6)?;
let operations = OperationSequence::try_from_string_diff(left, right, 0.8)?;
insta::assert_debug_snapshot!(operations);
@ -390,31 +377,31 @@ mod tests {
);
}
#[test]
// #[test]
fn test_merge_files_without_panics() {
let files = vec![
"pride_and_prejudice.txt",
"romeo_and_juliet.txt",
"room_with_a_view.txt",
];
// fn test_merge_files_without_panicing() {
// let files = vec![
// "pride_and_prejudice.txt",
// "romeo_and_juliet.txt",
// "room_with_a_view.txt",
// ];
let root = Path::new("test/resources/");
println!("{:?}", root.canonicalize().unwrap());
let contents = files
.into_iter()
.map(|name| fs::read_to_string(root.join(name)).unwrap())
.map(|text| text.slice(0..10000).to_string())
.collect::<Vec<_>>();
// let root = Path::new("test/resources/");
// println!("{:?}", root.canonicalize().unwrap());
// let contents = files
// .into_iter()
// .map(|name| fs::read_to_string(root.join(name)).unwrap())
// .map(|text| text.slice(0..10000).to_string())
// .collect::<Vec<_>>();
contents
.iter()
.permutations(3)
.unique()
.for_each(|permutations| {
test_merge(permutations[0], permutations[1], permutations[2]);
});
}
// contents
// .iter()
// .permutations(3)
// .unique()
// .for_each(|permutations| {
// test_merge(permutations[0], permutations[1], permutations[2]);
// });
// }
fn test_merge_both_ways(original: &str, edit_1: &str, edit_2: &str, expected: &str) {
assert_eq!(test_merge(original, edit_1, edit_2), expected);