Fix cursor movement on shortened deletes and refactor
This commit is contained in:
parent
e2edc076b9
commit
35bb7f2405
4 changed files with 35 additions and 42 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
);
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue