aboutsummaryrefslogtreecommitdiff
path: root/helix-core/src/movement.rs
diff options
context:
space:
mode:
Diffstat (limited to 'helix-core/src/movement.rs')
-rw-r--r--helix-core/src/movement.rs146
1 files changed, 86 insertions, 60 deletions
diff --git a/helix-core/src/movement.rs b/helix-core/src/movement.rs
index f9e5deb4..2cb4b40d 100644
--- a/helix-core/src/movement.rs
+++ b/helix-core/src/movement.rs
@@ -5,8 +5,11 @@ use ropey::iter::Chars;
use crate::{
chars::{categorize_char, char_is_line_ending, CharCategory},
coords_at_pos,
- graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary},
- line_ending::{get_line_ending, line_end_char_index},
+ graphemes::{
+ next_grapheme_boundary, nth_next_grapheme_boundary, nth_prev_grapheme_boundary,
+ prev_grapheme_boundary, RopeGraphemes,
+ },
+ line_ending::line_without_line_ending,
pos_at_coords, Position, Range, RopeSlice,
};
@@ -29,25 +32,31 @@ pub fn move_horizontally(
count: usize,
behaviour: Movement,
) -> Range {
- let pos = range.head;
- let line = slice.char_to_line(pos);
- // TODO: we can optimize clamping by passing in RopeSlice limited to current line. that way
- // we stop calculating past start/end of line.
- let pos = match dir {
- Direction::Backward => {
- let start = slice.line_to_char(line);
- nth_prev_grapheme_boundary(slice, pos, count).max(start)
- }
- Direction::Forward => {
- let end_char_idx = line_end_char_index(&slice, line);
- nth_next_grapheme_boundary(slice, pos, count).min(end_char_idx)
- }
+ use Movement::Extend;
+
+ // Shift back one grapheme if needed, to account for
+ // the cursor being visually 1-width.
+ let pos = if range.head > range.anchor {
+ prev_grapheme_boundary(slice, range.head)
+ } else {
+ range.head
};
- let anchor = match behaviour {
- Movement::Extend => range.anchor,
- Movement::Move => pos,
+
+ // Compute the new position.
+ let mut new_pos = if dir == Direction::Backward {
+ nth_prev_grapheme_boundary(slice, pos, count)
+ } else {
+ nth_next_grapheme_boundary(slice, pos, count)
+ };
+
+ // Shift forward one grapheme if needed, for the
+ // visual 1-width cursor.
+ if behaviour == Extend && new_pos >= range.anchor {
+ new_pos = next_grapheme_boundary(slice, new_pos);
};
- Range::new(anchor, pos)
+
+ // Compute the final new range.
+ range.put(slice, new_pos, behaviour == Extend)
}
pub fn move_vertically(
@@ -57,36 +66,51 @@ pub fn move_vertically(
count: usize,
behaviour: Movement,
) -> Range {
- let Position { row, col } = coords_at_pos(slice, range.head);
+ // Shift back one grapheme if needed, to account for
+ // the cursor being visually 1-width.
+ let pos = if range.head > range.anchor {
+ prev_grapheme_boundary(slice, range.head)
+ } else {
+ range.head
+ };
+ // Compute the current position's 2d coordinates.
+ let Position { row, col } = coords_at_pos(slice, pos);
let horiz = range.horiz.unwrap_or(col as u32);
- let new_line = match dir {
- Direction::Backward => row.saturating_sub(count),
- Direction::Forward => std::cmp::min(
- row.saturating_add(count),
- slice.len_lines().saturating_sub(2),
- ),
- };
+ // Compute the new position.
+ let new_pos = {
+ let new_row = if dir == Direction::Backward {
+ row.saturating_sub(count)
+ } else {
+ (row + count).min(slice.len_lines().saturating_sub(1))
+ };
+ let max_col = RopeGraphemes::new(line_without_line_ending(&slice, new_row)).count();
+ let new_col = col.max(horiz as usize).min(max_col);
- // Length of the line sans line-ending.
- let new_line_len = {
- let line = slice.line(new_line);
- line.len_chars() - get_line_ending(&line).map(|le| le.len_chars()).unwrap_or(0)
+ pos_at_coords(slice, Position::new(new_row, new_col))
};
- let new_col = std::cmp::min(horiz as usize, new_line_len);
-
- let pos = pos_at_coords(slice, Position::new(new_line, new_col));
-
- let anchor = match behaviour {
- Movement::Extend => range.anchor,
- Movement::Move => pos,
- };
+ // Compute the new range according to the type of movement.
+ match behaviour {
+ Movement::Move => Range {
+ anchor: new_pos,
+ head: new_pos,
+ horiz: Some(horiz),
+ },
+
+ Movement::Extend => {
+ let new_head = if new_pos >= range.anchor {
+ next_grapheme_boundary(slice, new_pos)
+ } else {
+ new_pos
+ };
- let mut range = Range::new(anchor, pos);
- range.horiz = Some(horiz);
- range
+ let mut new_range = range.put(slice, new_head, true);
+ new_range.horiz = Some(horiz);
+ new_range
+ }
+ }
}
pub fn move_next_word_start(slice: RopeSlice, range: Range, count: usize) -> Range {
@@ -330,7 +354,7 @@ mod test {
}
#[test]
- fn horizontal_moves_through_single_line_in_single_line_text() {
+ fn horizontal_moves_through_single_line_text() {
let text = Rope::from(SINGLE_LINE_SAMPLE);
let slice = text.slice(..);
let position = pos_at_coords(slice, (0, 0).into());
@@ -353,7 +377,7 @@ mod test {
}
#[test]
- fn horizontal_moves_through_single_line_in_multiline_text() {
+ fn horizontal_moves_through_multiline_text() {
let text = Rope::from(MULTILINE_SAMPLE);
let slice = text.slice(..);
let position = pos_at_coords(slice, (0, 0).into());
@@ -361,15 +385,15 @@ mod test {
let mut range = Range::point(position);
let moves_and_expected_coordinates = IntoIter::new([
- ((Direction::Forward, 1usize), (0, 1)), // M|ultiline\n
- ((Direction::Forward, 2usize), (0, 3)), // Mul|tiline\n
- ((Direction::Backward, 6usize), (0, 0)), // |Multiline\n
- ((Direction::Backward, 999usize), (0, 0)), // |Multiline\n
- ((Direction::Forward, 3usize), (0, 3)), // Mul|tiline\n
- ((Direction::Forward, 0usize), (0, 3)), // Mul|tiline\n
- ((Direction::Backward, 0usize), (0, 3)), // Mul|tiline\n
- ((Direction::Forward, 999usize), (0, 9)), // Multiline|\n
- ((Direction::Forward, 999usize), (0, 9)), // Multiline|\n
+ ((Direction::Forward, 11usize), (1, 1)), // Multiline\nt|ext sample\n...
+ ((Direction::Backward, 1usize), (1, 0)), // Multiline\n|text sample\n...
+ ((Direction::Backward, 5usize), (0, 5)), // Multi|line\ntext sample\n...
+ ((Direction::Backward, 999usize), (0, 0)), // |Multiline\ntext sample\n...
+ ((Direction::Forward, 3usize), (0, 3)), // Mul|tiline\ntext sample\n...
+ ((Direction::Forward, 0usize), (0, 3)), // Mul|tiline\ntext sample\n...
+ ((Direction::Backward, 0usize), (0, 3)), // Mul|tiline\ntext sample\n...
+ ((Direction::Forward, 999usize), (5, 0)), // ...and whitespaced\n|
+ ((Direction::Forward, 999usize), (5, 0)), // ...and whitespaced\n|
]);
for ((direction, amount), coordinates) in moves_and_expected_coordinates {
@@ -403,18 +427,19 @@ mod test {
#[test]
fn vertical_moves_in_single_column() {
let text = Rope::from(MULTILINE_SAMPLE);
- let slice = dbg!(&text).slice(..);
+ let slice = text.slice(..);
let position = pos_at_coords(slice, (0, 0).into());
let mut range = Range::point(position);
let moves_and_expected_coordinates = IntoIter::new([
((Direction::Forward, 1usize), (1, 0)),
((Direction::Forward, 2usize), (3, 0)),
+ ((Direction::Forward, 1usize), (4, 0)),
((Direction::Backward, 999usize), (0, 0)),
- ((Direction::Forward, 3usize), (3, 0)),
- ((Direction::Forward, 0usize), (3, 0)),
- ((Direction::Backward, 0usize), (3, 0)),
- ((Direction::Forward, 5), (4, 0)),
- ((Direction::Forward, 999usize), (4, 0)),
+ ((Direction::Forward, 4usize), (4, 0)),
+ ((Direction::Forward, 0usize), (4, 0)),
+ ((Direction::Backward, 0usize), (4, 0)),
+ ((Direction::Forward, 5), (5, 0)),
+ ((Direction::Forward, 999usize), (5, 0)),
]);
for ((direction, amount), coordinates) in moves_and_expected_coordinates {
@@ -446,7 +471,8 @@ mod test {
((Axis::V, Direction::Forward, 1usize), (3, 8)),
// Behaviour is preserved even through long jumps
((Axis::V, Direction::Backward, 999usize), (0, 8)),
- ((Axis::V, Direction::Forward, 999usize), (4, 8)),
+ ((Axis::V, Direction::Forward, 4usize), (4, 8)),
+ ((Axis::V, Direction::Forward, 999usize), (5, 0)),
]);
for ((axis, direction, amount), coordinates) in moves_and_expected_coordinates {