diff --git a/reconcile-js/src/index.ts b/reconcile-js/src/index.ts index 3b267ce..247db26 100644 --- a/reconcile-js/src/index.ts +++ b/reconcile-js/src/index.ts @@ -180,6 +180,20 @@ export function reconcile( return jsResult; } +/** + * Generates a compact diff representation between an original and changed text. + * + * These can be parsed and unpacked using Rust crate's EditedText::from_change_set. + * + * This function computes the differences between two versions of text and returns + * a compact string representation of those changes. The returned format is + * serialised JSON. + * + * @param original - The original/base version of the text + * @param changed - The modified version of the text (either string or TextWithCursors with cursor positions) + * @param tokenizer - The tokenisation strategy, which is the same as used in `reconcile`. + * @returns A compact string representation of the diff between original and changed text + */ export function getCompactDiff( original: string, changed: string | TextWithOptionalCursors, diff --git a/src/operation_transformation/transport.rs b/src/operation_transformation/transport.rs index 97212f9..67c25e5 100644 --- a/src/operation_transformation/transport.rs +++ b/src/operation_transformation/transport.rs @@ -12,9 +12,7 @@ use crate::{CursorPosition, Tokenizer, operation_transformation::Operation}; #[derive(Clone, PartialEq, Eq, Debug)] pub enum SimpleOperation { Equal { length: usize }, - Insert { text: String }, - Delete { length: usize }, } @@ -35,6 +33,7 @@ impl SimpleOperation { previous_equal = Some(*length); } } + Operation::Insert { text, .. } => { if let Some(prev_length) = previous_equal { result.push(SimpleOperation::Equal { @@ -49,6 +48,7 @@ impl SimpleOperation { .collect(); result.push(SimpleOperation::Insert { text }); } + Operation::Delete { deleted_character_count, .. @@ -76,6 +76,7 @@ impl SimpleOperation { result } + // This is similar to `crate::operation_transformation::utils::cook_operations` pub fn to_operations( simple_operations: Vec, original_text: &str, @@ -85,7 +86,6 @@ impl SimpleOperation { T: PartialEq + Clone + Debug, { let mut operations: Vec> = Vec::with_capacity(simple_operations.len()); - let mut order = 0; for simple_operation in simple_operations { @@ -101,10 +101,12 @@ impl SimpleOperation { order += token.get_original_length(); } } + SimpleOperation::Insert { text } => { let tokens = tokenizer(&text); operations.push(Operation::create_insert(order, tokens)); } + SimpleOperation::Delete { length } => { operations.push(Operation::create_delete(order, length)); order += length;