From 07f3a5f7956891315fd8f55e08033732cfd11f03 Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Sun, 29 Jun 2025 15:29:56 +0100 Subject: [PATCH] Remove diff mod --- src/diffs.rs | 2 -- src/{diffs => }/raw_operation.rs | 16 ++++++++++---- src/utils.rs | 1 + src/{diffs/myers.rs => utils/myers_diff.rs} | 22 +++++++++---------- ...ils__myers_diff__tests__complex_diff.snap} | 3 +-- ...tils__myers_diff__tests__delete_only.snap} | 3 +-- ...myers_diff__tests__identical_content.snap} | 3 +-- ...tils__myers_diff__tests__insert_only.snap} | 3 +-- ...myers_diff__tests__prefix_and_suffix.snap} | 3 +-- 9 files changed, 29 insertions(+), 27 deletions(-) delete mode 100644 src/diffs.rs rename src/{diffs => }/raw_operation.rs (80%) rename src/{diffs/myers.rs => utils/myers_diff.rs} (95%) rename src/{diffs/snapshots/reconcile__diffs__myers__tests__complex_diff.snap => utils/snapshots/reconcile__utils__myers_diff__tests__complex_diff.snap} (95%) rename src/{diffs/snapshots/reconcile__diffs__myers__tests__delete_only.snap => utils/snapshots/reconcile__utils__myers_diff__tests__delete_only.snap} (89%) rename src/{diffs/snapshots/reconcile__diffs__myers__tests__identical_content.snap => utils/snapshots/reconcile__utils__myers_diff__tests__identical_content.snap} (92%) rename src/{diffs/snapshots/reconcile__diffs__myers__tests__insert_only.snap => utils/snapshots/reconcile__utils__myers_diff__tests__insert_only.snap} (89%) rename src/{diffs/snapshots/reconcile__diffs__myers__tests__prefix_and_suffix.snap => utils/snapshots/reconcile__utils__myers_diff__tests__prefix_and_suffix.snap} (95%) diff --git a/src/diffs.rs b/src/diffs.rs deleted file mode 100644 index b57139a..0000000 --- a/src/diffs.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod myers; -pub mod raw_operation; diff --git a/src/diffs/raw_operation.rs b/src/raw_operation.rs similarity index 80% rename from src/diffs/raw_operation.rs rename to src/raw_operation.rs index 2e4a0a7..6f87b84 100644 --- a/src/diffs/raw_operation.rs +++ b/src/raw_operation.rs @@ -1,9 +1,15 @@ -use crate::tokenizer::token::Token; +use std::fmt::Debug; +use crate::{tokenizer::token::Token, utils::myers_diff::myers_diff}; + +/// Text editing operation containing the to-be-changed `Tokens`-s. +/// +/// RawOperations can be joined together when the underlying tokens +/// allow for joining subseqeunt operations. #[derive(Debug, Clone, PartialEq)] pub enum RawOperation where - T: PartialEq + Clone + std::fmt::Debug, + T: PartialEq + Clone + Debug, { Insert(Vec>), Delete(Vec>), @@ -12,8 +18,10 @@ where impl RawOperation where - T: PartialEq + Clone + std::fmt::Debug, + T: PartialEq + Clone + Debug, { + pub fn vec_from(left: &[Token], right: &[Token]) -> Vec { myers_diff(left, right) } + pub fn tokens(&self) -> &Vec> { match self { RawOperation::Insert(tokens) @@ -41,7 +49,7 @@ where /// Extends the operation with another operation. Only operations of the /// same type as self can be used to extend self, otherwise the function /// will panic. - pub fn extend(self, other: RawOperation) -> RawOperation { + pub fn join(self, other: RawOperation) -> RawOperation { debug_assert!( std::mem::discriminant(&self) == std::mem::discriminant(&other), "Cannot extend operations of different types. This should have been handled before \ diff --git a/src/utils.rs b/src/utils.rs index 8d31e66..58d55b8 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -2,5 +2,6 @@ pub mod common_prefix_len; pub mod common_suffix_len; pub mod find_longest_prefix_contained_within; pub mod history; +pub mod myers_diff; pub mod side; pub mod string_builder; diff --git a/src/diffs/myers.rs b/src/utils/myers_diff.rs similarity index 95% rename from src/diffs/myers.rs rename to src/utils/myers_diff.rs index 501eca8..b94270c 100644 --- a/src/diffs/myers.rs +++ b/src/utils/myers_diff.rs @@ -24,8 +24,8 @@ use std::{ vec, }; -use super::raw_operation::RawOperation; use crate::{ + raw_operation::RawOperation, tokenizer::token::Token, utils::{common_prefix_len::common_prefix_len, common_suffix_len::common_suffix_len}, }; @@ -35,8 +35,8 @@ use crate::{ /// Diff `old`, between indices `old_range` and `new` between indices /// `new_range`. /// -/// The returned `RawOperations` all have a token count of 1. -pub fn diff(old: &[Token], new: &[Token]) -> Vec> +/// The returned `RawOperations` each wrap a single token. +pub fn myers_diff(old: &[Token], new: &[Token]) -> Vec> where T: PartialEq + Clone + std::fmt::Debug, { @@ -57,7 +57,7 @@ where debug_assert!( result.iter().all(|op| op.tokens().len() == 1), - "All operations should be of length 1" + "All operations must be of length 1" ); result @@ -80,7 +80,7 @@ where #[derive(Debug)] struct V { offset: isize, - v: Vec, // Look into initializing this to -1 and storing isize + v: Vec, } impl V { @@ -312,14 +312,14 @@ mod tests { fn test_empty_diff() { let old: Vec> = vec![]; let new: Vec> = vec![]; - let result = diff(&old, &new); + let result = myers_diff(&old, &new); assert_eq!(result.len(), 0); } #[test] fn test_identical_content() { let content = vec!["a".into(), "b".into(), "c".into()]; - let result = diff(&content, &content); + let result = myers_diff(&content, &content); assert_debug_snapshot!(result); } @@ -327,7 +327,7 @@ mod tests { fn test_insert_only() { let old: Vec> = vec![]; let new: Vec> = vec!["a".into(), "b".into()]; - let result = diff(&old, &new); + let result = myers_diff(&old, &new); assert_debug_snapshot!(result); } @@ -335,7 +335,7 @@ mod tests { fn test_delete_only() { let old = vec!["a".into(), "b".into()]; let new: Vec> = vec![]; - let result = diff(&old, &new); + let result = myers_diff(&old, &new); assert_debug_snapshot!(result); } @@ -343,7 +343,7 @@ mod tests { fn test_prefix_and_suffix() { let old = vec!["a".into(), "b".into(), "c".into(), "d".into()]; let new = vec!["a".into(), "x".into(), "d".into()]; - let result = diff(&old, &new); + let result = myers_diff(&old, &new); assert_debug_snapshot!(result); } @@ -351,7 +351,7 @@ mod tests { fn test_complex_diff() { let old = vec!["a".into(), "b".into(), "c".into(), "d".into()]; let new = vec!["a".into(), "x".into(), "c".into(), "y".into()]; - let result = diff(&old, &new); + let result = myers_diff(&old, &new); assert_debug_snapshot!(result); } } diff --git a/src/diffs/snapshots/reconcile__diffs__myers__tests__complex_diff.snap b/src/utils/snapshots/reconcile__utils__myers_diff__tests__complex_diff.snap similarity index 95% rename from src/diffs/snapshots/reconcile__diffs__myers__tests__complex_diff.snap rename to src/utils/snapshots/reconcile__utils__myers_diff__tests__complex_diff.snap index 57ee086..98ef83c 100644 --- a/src/diffs/snapshots/reconcile__diffs__myers__tests__complex_diff.snap +++ b/src/utils/snapshots/reconcile__utils__myers_diff__tests__complex_diff.snap @@ -1,7 +1,6 @@ --- -source: reconcile/src/diffs/myers.rs +source: src/utils/myers_diff.rs expression: result -snapshot_kind: text --- [ Equal( diff --git a/src/diffs/snapshots/reconcile__diffs__myers__tests__delete_only.snap b/src/utils/snapshots/reconcile__utils__myers_diff__tests__delete_only.snap similarity index 89% rename from src/diffs/snapshots/reconcile__diffs__myers__tests__delete_only.snap rename to src/utils/snapshots/reconcile__utils__myers_diff__tests__delete_only.snap index 93bb529..1c01e1a 100644 --- a/src/diffs/snapshots/reconcile__diffs__myers__tests__delete_only.snap +++ b/src/utils/snapshots/reconcile__utils__myers_diff__tests__delete_only.snap @@ -1,7 +1,6 @@ --- -source: reconcile/src/diffs/myers.rs +source: src/utils/myers_diff.rs expression: result -snapshot_kind: text --- [ Delete( diff --git a/src/diffs/snapshots/reconcile__diffs__myers__tests__identical_content.snap b/src/utils/snapshots/reconcile__utils__myers_diff__tests__identical_content.snap similarity index 92% rename from src/diffs/snapshots/reconcile__diffs__myers__tests__identical_content.snap rename to src/utils/snapshots/reconcile__utils__myers_diff__tests__identical_content.snap index f82d4ac..f942a17 100644 --- a/src/diffs/snapshots/reconcile__diffs__myers__tests__identical_content.snap +++ b/src/utils/snapshots/reconcile__utils__myers_diff__tests__identical_content.snap @@ -1,7 +1,6 @@ --- -source: reconcile/src/diffs/myers.rs +source: src/utils/myers_diff.rs expression: result -snapshot_kind: text --- [ Equal( diff --git a/src/diffs/snapshots/reconcile__diffs__myers__tests__insert_only.snap b/src/utils/snapshots/reconcile__utils__myers_diff__tests__insert_only.snap similarity index 89% rename from src/diffs/snapshots/reconcile__diffs__myers__tests__insert_only.snap rename to src/utils/snapshots/reconcile__utils__myers_diff__tests__insert_only.snap index 0f61f3c..0b40f12 100644 --- a/src/diffs/snapshots/reconcile__diffs__myers__tests__insert_only.snap +++ b/src/utils/snapshots/reconcile__utils__myers_diff__tests__insert_only.snap @@ -1,7 +1,6 @@ --- -source: reconcile/src/diffs/myers.rs +source: src/utils/myers_diff.rs expression: result -snapshot_kind: text --- [ Insert( diff --git a/src/diffs/snapshots/reconcile__diffs__myers__tests__prefix_and_suffix.snap b/src/utils/snapshots/reconcile__utils__myers_diff__tests__prefix_and_suffix.snap similarity index 95% rename from src/diffs/snapshots/reconcile__diffs__myers__tests__prefix_and_suffix.snap rename to src/utils/snapshots/reconcile__utils__myers_diff__tests__prefix_and_suffix.snap index e50984f..59f4997 100644 --- a/src/diffs/snapshots/reconcile__diffs__myers__tests__prefix_and_suffix.snap +++ b/src/utils/snapshots/reconcile__utils__myers_diff__tests__prefix_and_suffix.snap @@ -1,7 +1,6 @@ --- -source: reconcile/src/diffs/myers.rs +source: src/utils/myers_diff.rs expression: result -snapshot_kind: text --- [ Equal(