diff --git a/src/operation_transformation/edited_text.rs b/src/operation_transformation/edited_text.rs index 60f32da..c287903 100644 --- a/src/operation_transformation/edited_text.rs +++ b/src/operation_transformation/edited_text.rs @@ -144,21 +144,21 @@ where Operation::Insert { .. } | Operation::Equal { .. } ); - let original_length = operation.len() as i64; + let original_length = operation.len() as isize; let result = match side { Side::Left => { let result = operation.merge_operations(&mut last_other_op); if let ref op @ (Operation::Insert { .. } | Operation::Equal { .. }) = result { - let shift = merged_length as i64 - seen_left_length as i64 - + op.len() as i64 + let shift = merged_length as isize - seen_left_length as isize + + op.len() as isize - original_length; while let Some(cursor) = left_cursors.next_if(|cursor| { cursor.char_index <= seen_left_length + original_length as usize }) { merged_cursors.push( - cursor.with_index((cursor.char_index as i64 + shift) as usize), + cursor.with_index(cursor.char_index.saturating_add_signed(shift)), ); } } @@ -176,15 +176,15 @@ where let result = operation.merge_operations(&mut last_other_op); if let ref op @ (Operation::Insert { .. } | Operation::Equal { .. }) = result { - let shift = merged_length as i64 - seen_right_length as i64 - + op.len() as i64 + let shift = merged_length as isize - seen_right_length as isize + + op.len() as isize - original_length; while let Some(cursor) = right_cursors.next_if(|cursor| { cursor.char_index <= seen_right_length + original_length as usize }) { merged_cursors.push( - cursor.with_index((cursor.char_index as i64 + shift) as usize), + cursor.with_index(cursor.char_index.saturating_add_signed(shift)), ); } } diff --git a/src/types/text_with_cursors.rs b/src/types/text_with_cursors.rs index ac7c18f..c9dec89 100644 --- a/src/types/text_with_cursors.rs +++ b/src/types/text_with_cursors.rs @@ -20,7 +20,9 @@ impl TextWithCursors { debug_assert!( cursor.char_index <= length, // cursor.char_index == length means that the cursor is at the end - "Cursor positions must be contained within the text or just after the end" + "Cursor positions ({}) must be contained within the text (of length {length}) or \ + just after the end", + cursor.char_index ); } diff --git a/tests/examples/various.yml b/tests/examples/various.yml index 4576edc..4370178 100644 --- a/tests/examples/various.yml +++ b/tests/examples/various.yml @@ -9,6 +9,12 @@ left: Party C shall pay Party B right: Party A shall receive from Party B expected: Party C shall receive from Party B +--- +parent: hello +left: hel|lo +right: hi +expected: "|hi" + --- parent: left: hi my friend|