diff --git a/backend/reconcile/src/operation_transformation/cursor.rs b/backend/reconcile/src/operation_transformation/cursor.rs index c7fbaf5..f145273 100644 --- a/backend/reconcile/src/operation_transformation/cursor.rs +++ b/backend/reconcile/src/operation_transformation/cursor.rs @@ -14,7 +14,7 @@ pub struct CursorPosition { impl CursorPosition { #[must_use] - pub fn with_index(self, index: usize) -> Self { + pub fn with_index(&self, index: usize) -> Self { CursorPosition { id: self.id, char_index: index, diff --git a/backend/reconcile/src/operation_transformation/edited_text.rs b/backend/reconcile/src/operation_transformation/edited_text.rs index 0f136fe..b83441f 100644 --- a/backend/reconcile/src/operation_transformation/edited_text.rs +++ b/backend/reconcile/src/operation_transformation/edited_text.rs @@ -253,57 +253,50 @@ where .flat_map(|(OrderedOperation { order, operation }, side)| { let original_start = operation.start_index() as i64; let original_end = operation.end_index(); + let original_length = operation.len() as i64; - match side { - Side::Left => { - let result = operation.merge_operations_with_context( - &mut right_merge_context, - &mut left_merge_context, - ); + let result = match side { + Side::Left => operation.merge_operations_with_context( + &mut right_merge_context, + &mut left_merge_context, + ), + Side::Right => operation.merge_operations_with_context( + &mut left_merge_context, + &mut right_merge_context, + ), + }; - if let Some(ref op @ (Operation::Insert { .. } | Operation::Equal { .. })) = - result - { - while let Some(mut cursor) = + if let Some(ref op @ (Operation::Insert { .. } | Operation::Equal { .. })) = result + { + let shift = op.start_index() as i64 - original_start + op.len() as i64 + - original_length; + match side { + Side::Left => { + while let Some(cursor) = left_cursors.next_if(|cursor| cursor.char_index <= original_end + 1) { - let shift = op.start_index() as i64 - original_start; - - cursor.char_index = (op.start_index() as i64) - .max(cursor.char_index as i64 + shift) - as usize; - merged_cursors.push(cursor); + merged_cursors.push(cursor.with_index( + (op.start_index() as i64).max(cursor.char_index as i64 + shift) + as usize, + )); } } - - result - } - Side::Right => { - let result = operation.merge_operations_with_context( - &mut left_merge_context, - &mut right_merge_context, - ); - - if let Some(ref op @ (Operation::Insert { .. } | Operation::Equal { .. })) = - result - { - while let Some(mut cursor) = right_cursors + Side::Right => { + while let Some(cursor) = right_cursors .next_if(|cursor| cursor.char_index <= original_end + 1) { - let shift = op.start_index() as i64 - original_start; - - cursor.char_index = (op.start_index() as i64) - .max(cursor.char_index as i64 + shift) - as usize; - merged_cursors.push(cursor); + merged_cursors.push(cursor.with_index( + (op.start_index() as i64).max(cursor.char_index as i64 + shift) + as usize, + )); } } - - result } } - .map(|operation| OrderedOperation { order, operation }) - .into_iter() + + result + .map(|operation| OrderedOperation { order, operation }) + .into_iter() }) .collect(); diff --git a/backend/reconcile/tests/example_document.rs b/backend/reconcile/tests/example_document.rs index 10790c8..277e49e 100644 --- a/backend/reconcile/tests/example_document.rs +++ b/backend/reconcile/tests/example_document.rs @@ -65,7 +65,7 @@ impl ExampleDocument { for (i, cursor) in text.cursors.iter().enumerate() { assert!( cursor.char_index <= result.len(), // equals in case of insert at the end - "Cursor index out of bounds: {} > {}", + "Cursor index out of bounds: {} > {} when testing for '{result}'", cursor.char_index, result.len() ); diff --git a/backend/reconcile/tests/examples/various.yml b/backend/reconcile/tests/examples/various.yml index 7a87e4e..cfbeb42 100644 --- a/backend/reconcile/tests/examples/various.yml +++ b/backend/reconcile/tests/examples/various.yml @@ -67,7 +67,7 @@ expected: market| placemarket|space parent: A B C D left: A X B D| right: A B Y| -expected: A X B Y|| +expected: A X B |Y| --- parent: Please submit your assignment by Friday