Fix wrapping

This commit is contained in:
Andras Schmelczer 2025-07-04 02:16:17 +01:00
parent f0ff720577
commit 8004ac3742
3 changed files with 16 additions and 8 deletions

View file

@ -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)),
);
}
}

View file

@ -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
);
}

View file

@ -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|