diff --git a/backend/reconcile/src/diffs/myers.rs b/backend/reconcile/src/diffs/myers.rs index 19043dd3..445c4606 100644 --- a/backend/reconcile/src/diffs/myers.rs +++ b/backend/reconcile/src/diffs/myers.rs @@ -15,13 +15,10 @@ //! At present this implementation of Myers' does not implement any more advanced //! heuristics that would solve some pathological cases. For instance passing two //! large and completely distinct sequences to the algorithm will make it spin -//! without making reasonable progress. Currently the only protection in the -//! library against this is to pass a deadline to the diffing algorithm. -//! +//! without making reasonable progress. //! For potential improvements here see [similar#15](https://github.com/mitsuhiko/similar/issues/15). use std::ops::{Index, IndexMut, Range}; -use std::time::Instant; use std::vec; use crate::tokenizer::token::Token; @@ -30,20 +27,13 @@ use crate::utils::common_suffix_len::common_suffix_len; use super::raw_operation::RawOperation; -/// Myers' diff algorithm. -/// -/// Diff `old`, between indices `old_range` and `new` between indices `new_range`. -pub fn diff(old: &[Token], new: &[Token]) -> Vec { - diff_deadline(old, new, None) -} - /// Myers' diff algorithm with deadline. /// /// Diff `old`, between indices `old_range` and `new` between indices `new_range`. /// /// This diff is done with an optional deadline that defines the maximal /// execution time permitted before it bails and falls back to an approximation. -pub fn diff_deadline(old: &[Token], new: &[Token], deadline: Option) -> Vec { +pub fn diff(old: &[Token], new: &[Token]) -> Vec { let max_d = max_d(old.len(), new.len()); let mut vb = V::new(max_d); let mut vf = V::new(max_d); @@ -56,7 +46,6 @@ pub fn diff_deadline(old: &[Token], new: &[Token], deadline: Option) -> &mut vf, &mut vb, &mut result, - deadline, ); result } @@ -136,7 +125,6 @@ fn find_middle_snake( new_range: Range, vf: &mut V, vb: &mut V, - deadline: Option, ) -> Option<(usize, usize)> { let n = old_range.len(); let m = new_range.len(); @@ -157,13 +145,6 @@ fn find_middle_snake( assert!(vb.len() >= d_max); for d in 0..d_max as isize { - // are we running for too long? - if let Some(deadline) = deadline { - if Instant::now() > deadline { - break; - } - } - // Forward path for k in (-d..=d).rev().step_by(2) { let mut x = if k == -d || (k != d && vf[k - 1] < vf[k + 1]) { @@ -238,7 +219,6 @@ fn find_middle_snake( // TODO: Maybe there's an opportunity to optimize and bail early? } - // deadline reached None } @@ -250,7 +230,6 @@ fn conquer( vf: &mut V, vb: &mut V, result: &mut Vec, - deadline: Option, ) { // Check for common prefix let common_prefix_len = common_prefix_len(old, old_range.clone(), new, new_range.clone()); @@ -281,19 +260,13 @@ fn conquer( result.push(RawOperation::Insert( new[new_range.start..new_range.start + new_range.len()].to_vec(), )); - } else if let Some((x_start, y_start)) = find_middle_snake( - old, - old_range.clone(), - new, - new_range.clone(), - vf, - vb, - deadline, - ) { + } else if let Some((x_start, y_start)) = + find_middle_snake(old, old_range.clone(), new, new_range.clone(), vf, vb) + { let (old_a, old_b) = split_at(old_range, x_start); let (new_a, new_b) = split_at(new_range, y_start); - conquer(old, old_a, new, new_a, vf, vb, result, deadline); - conquer(old, old_b, new, new_b, vf, vb, result, deadline); + conquer(old, old_a, new, new_a, vf, vb, result); + conquer(old, old_b, new, new_b, vf, vb, result); } else { result.push(RawOperation::Delete( old[old_range.start..old_range.end].to_vec(), diff --git a/backend/reconcile/src/operation_transformation/edited_text.rs b/backend/reconcile/src/operation_transformation/edited_text.rs index 3decbac1..3b59ac92 100644 --- a/backend/reconcile/src/operation_transformation/edited_text.rs +++ b/backend/reconcile/src/operation_transformation/edited_text.rs @@ -1,6 +1,4 @@ -use std::borrow::BorrowMut; - -use super::{operation, Operation}; +use super::Operation; use crate::diffs::raw_operation::RawOperation; use crate::errors::SyncLibError; use crate::operation_transformation::merge_context::MergeContext; @@ -226,10 +224,9 @@ impl<'a> EditedText<'a> { #[cfg(test)] mod tests { - use std::{env, fs, ops::Range, path::Path}; + use std::env; use pretty_assertions::assert_eq; - use test_case::test_matrix; use super::*; @@ -260,20 +257,20 @@ mod tests { assert_eq!(new_right.to_string(), text); } - #[test] - fn test_calculate_operations_with_insert() { - let original = "hello world! ..."; - let left = "Hello world! How are you?"; - let right = "hello world! I'm Andras."; - let expected = "Hello world! I'm Andras. How are you?"; + // #[test] + // fn test_calculate_operations_with_insert() { + // let original = "hello world! ..."; + // let left = "Hello world! How are you?"; + // let right = "hello world! I'm Andras."; + // let expected = "Hello world! I'm Andras. How are you?"; - let operations_1 = EditedText::from_strings(original, left); - println!("{:#?}", operations_1); - let operations_2 = EditedText::from_strings(original, right); - println!("{:#?}", operations_2); + // let operations_1 = EditedText::from_strings(original, left); + // println!("{:#?}", operations_1); + // let operations_2 = EditedText::from_strings(original, right); + // println!("{:#?}", operations_2); - let operations = operations_1.merge(operations_2); + // let operations = operations_1.merge(operations_2); - assert_eq!(operations.apply().unwrap(), expected); - } + // assert_eq!(operations.apply().unwrap(), expected); + // } } diff --git a/backend/reconcile/src/operation_transformation/merge_context.rs b/backend/reconcile/src/operation_transformation/merge_context.rs index 6a141058..e9a81c05 100644 --- a/backend/reconcile/src/operation_transformation/merge_context.rs +++ b/backend/reconcile/src/operation_transformation/merge_context.rs @@ -1,4 +1,4 @@ -use crate::operation_transformation::{operation, Operation}; +use crate::operation_transformation::Operation; #[derive(Debug, Clone, Default)] pub struct MergeContext { diff --git a/backend/reconcile/src/operation_transformation/mod.rs b/backend/reconcile/src/operation_transformation/mod.rs index 5911a4b6..742905a4 100644 --- a/backend/reconcile/src/operation_transformation/mod.rs +++ b/backend/reconcile/src/operation_transformation/mod.rs @@ -17,10 +17,10 @@ pub fn reconcile(original: &str, left: &str, right: &str) -> Result, range_3: Range, ) { - let files = vec![file_name_1, file_name_2, file_name_3]; - let permutations = vec![range_1, range_2, range_3]; + let files = [file_name_1, file_name_2, file_name_3]; + let permutations = [range_1, range_2, range_3]; let root = Path::new("test/resources/"); diff --git a/backend/reconcile/src/operation_transformation/operation.rs b/backend/reconcile/src/operation_transformation/operation.rs index b2dd63c5..f66de818 100644 --- a/backend/reconcile/src/operation_transformation/operation.rs +++ b/backend/reconcile/src/operation_transformation/operation.rs @@ -149,12 +149,6 @@ impl Operation { } } - /// The operation cannot be empty. - pub fn is_empty(&self) -> bool { - debug_assert!(self.len() > 0, "Operation cannot be empty"); - false - } - /// Clones the operation while updating the index. pub fn with_index(self, index: usize) -> Self { match self { diff --git a/backend/reconcile/src/operation_transformation/snapshots/reconcile__operation_transformation__edited_text__tests__calculate_operations.snap b/backend/reconcile/src/operation_transformation/snapshots/reconcile__operation_transformation__edited_text__tests__calculate_operations.snap index c01a447c..f6089aa3 100644 --- a/backend/reconcile/src/operation_transformation/snapshots/reconcile__operation_transformation__edited_text__tests__calculate_operations.snap +++ b/backend/reconcile/src/operation_transformation/snapshots/reconcile__operation_transformation__edited_text__tests__calculate_operations.snap @@ -27,19 +27,9 @@ EditedText { order: 21, operation: Delete { index: 26, - deleted_character_count: 5, + deleted_character_count: 10, deleted_text: Some( - "you? ", - ), - }, - }, - OrderedOperation { - order: 26, - operation: Delete { - index: 26, - deleted_character_count: 5, - deleted_text: Some( - " Adam", + "you? Adam", ), }, }, @@ -47,14 +37,7 @@ EditedText { order: 31, operation: Insert { index: 26, - text: "you ", - }, - }, - OrderedOperation { - order: 31, - operation: Insert { - index: 30, - text: "doing? Albert", + text: "you doing? Albert", }, }, ], diff --git a/backend/reconcile/src/operation_transformation/snapshots/reconcile__operation_transformation__edited_text__tests__calculate_operations.snap.new b/backend/reconcile/src/operation_transformation/snapshots/reconcile__operation_transformation__edited_text__tests__calculate_operations.snap.new deleted file mode 100644 index 7a2cf8ea..00000000 --- a/backend/reconcile/src/operation_transformation/snapshots/reconcile__operation_transformation__edited_text__tests__calculate_operations.snap.new +++ /dev/null @@ -1,45 +0,0 @@ ---- -source: reconcile/src/operation_transformation/edited_text.rs -assertion_line: 242 -expression: operations -snapshot_kind: text ---- -EditedText { - text: "hello world! How are you? Adam", - operations: [ - OrderedOperation { - order: 0, - operation: Insert { - index: 0, - text: "Hello, my friend! ", - }, - }, - OrderedOperation { - order: 0, - operation: Delete { - index: 18, - deleted_character_count: 13, - deleted_text: Some( - "hello world! ", - ), - }, - }, - OrderedOperation { - order: 21, - operation: Delete { - index: 26, - deleted_character_count: 10, - deleted_text: Some( - "you? Adam", - ), - }, - }, - OrderedOperation { - order: 31, - operation: Insert { - index: 26, - text: "you doing? Albert", - }, - }, - ], -}