|
|
@ -32,60 +32,31 @@ pub fn move_horizontally(
|
|
|
|
count: usize,
|
|
|
|
count: usize,
|
|
|
|
behaviour: Movement,
|
|
|
|
behaviour: Movement,
|
|
|
|
) -> Range {
|
|
|
|
) -> Range {
|
|
|
|
match (behaviour, dir) {
|
|
|
|
use Movement::Extend;
|
|
|
|
(Movement::Move, Direction::Backward) => {
|
|
|
|
|
|
|
|
let count = if range.anchor < range.head {
|
|
|
|
// Shift back one grapheme if needed, to account for
|
|
|
|
count + 1
|
|
|
|
// the cursor being visually 1-width.
|
|
|
|
|
|
|
|
let pos = if range.head > range.anchor {
|
|
|
|
|
|
|
|
prev_grapheme_boundary(slice, range.head)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
count
|
|
|
|
range.head
|
|
|
|
};
|
|
|
|
};
|
|
|
|
let pos = nth_prev_grapheme_boundary(slice, range.head, count);
|
|
|
|
|
|
|
|
Range::new(pos, pos)
|
|
|
|
// Compute the new position.
|
|
|
|
}
|
|
|
|
let mut new_pos = if dir == Direction::Backward {
|
|
|
|
(Movement::Move, Direction::Forward) => {
|
|
|
|
nth_prev_grapheme_boundary(slice, pos, count)
|
|
|
|
let count = if range.anchor < range.head {
|
|
|
|
|
|
|
|
count - 1
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
count
|
|
|
|
nth_next_grapheme_boundary(slice, pos, count)
|
|
|
|
};
|
|
|
|
};
|
|
|
|
let pos = nth_next_grapheme_boundary(slice, range.head, count);
|
|
|
|
|
|
|
|
Range::new(pos, pos)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
(Movement::Extend, Direction::Backward) => {
|
|
|
|
|
|
|
|
// Ensure a valid initial selection state.
|
|
|
|
|
|
|
|
let range = range.min_width_1(slice);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Do the main movement.
|
|
|
|
|
|
|
|
let mut head = nth_prev_grapheme_boundary(slice, range.head, count);
|
|
|
|
|
|
|
|
let mut anchor = range.anchor;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If the head and anchor crossed over each other, we need to
|
|
|
|
|
|
|
|
// fiddle around to make it behave like a 1-wide cursor.
|
|
|
|
|
|
|
|
if head <= anchor && range.head > range.anchor {
|
|
|
|
|
|
|
|
anchor = next_grapheme_boundary(slice, anchor);
|
|
|
|
|
|
|
|
head = prev_grapheme_boundary(slice, head);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Range::new(anchor, head)
|
|
|
|
// Shift forward one grapheme if needed, for the
|
|
|
|
}
|
|
|
|
// visual 1-width cursor.
|
|
|
|
(Movement::Extend, Direction::Forward) => {
|
|
|
|
if behaviour == Extend && new_pos >= range.anchor {
|
|
|
|
// Ensure a valid initial selection state.
|
|
|
|
new_pos = next_grapheme_boundary(slice, new_pos);
|
|
|
|
let range = range.min_width_1(slice);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Do the main movement.
|
|
|
|
|
|
|
|
let mut head = nth_next_grapheme_boundary(slice, range.head, count);
|
|
|
|
|
|
|
|
let mut anchor = range.anchor;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If the head and anchor crossed over each other, we need to
|
|
|
|
|
|
|
|
// fiddle around to make it behave like a 1-wide cursor.
|
|
|
|
|
|
|
|
if head >= anchor && range.head < range.anchor {
|
|
|
|
|
|
|
|
anchor = prev_grapheme_boundary(slice, anchor);
|
|
|
|
|
|
|
|
head = next_grapheme_boundary(slice, head);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Range::new(anchor, head)
|
|
|
|
// Compute the final new range.
|
|
|
|
}
|
|
|
|
range.put(slice, behaviour == Extend, new_pos)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn move_vertically(
|
|
|
|
pub fn move_vertically(
|
|
|
@ -135,19 +106,9 @@ pub fn move_vertically(
|
|
|
|
new_pos
|
|
|
|
new_pos
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let new_anchor = if range.anchor <= range.head && range.anchor > new_head {
|
|
|
|
let mut new_range = range.put(slice, true, new_head);
|
|
|
|
next_grapheme_boundary(slice, range.anchor)
|
|
|
|
new_range.horiz = Some(horiz);
|
|
|
|
} else if range.anchor > range.head && range.anchor < new_head {
|
|
|
|
new_range
|
|
|
|
prev_grapheme_boundary(slice, range.anchor)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
range.anchor
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Range {
|
|
|
|
|
|
|
|
anchor: new_anchor,
|
|
|
|
|
|
|
|
head: new_head,
|
|
|
|
|
|
|
|
horiz: Some(horiz),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|