@@ -5,8 +5,11 @@ use ropey::iter::Chars;
55use crate::{
66 chars::{categorize_char, char_is_line_ending, CharCategory},
77 coords_at_pos,
8− graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary},
9− line_ending::{get_line_ending, line_end_char_index},
8+ graphemes::{
9+ next_grapheme_boundary, nth_next_grapheme_boundary, nth_prev_grapheme_boundary,
10+ prev_grapheme_boundary,
11+ },
12+ line_ending::get_line_ending,
1013 pos_at_coords, Position, Range, RopeSlice,
1114};
1215
@@ -29,25 +32,60 @@ pub fn move_horizontally(
2932 count: usize,
3033 behaviour: Movement,
3134) -> Range {
32− let pos = range.head;
33− let line = slice.char_to_line(pos);
34− // TODO: we can optimize clamping by passing in RopeSlice limited to current line. that way
35− // we stop calculating past start/end of line.
36− let pos = match dir {
37− Direction::Backward => {
38− let start = slice.line_to_char(line);
39− nth_prev_grapheme_boundary(slice, pos, count).max(start)
35+ match (behaviour, dir) {
36+ (Movement::Move, Direction::Backward) => {
37+ let count = if range.anchor < range.head {
38+ count + 1
39+ } else {
40+ count
41+ };
42+ let pos = nth_prev_grapheme_boundary(slice, range.head, count);
43+ Range::new(pos, pos)
4044 }
41− Direction::Forward => {
42− let end_char_idx = line_end_char_index(&slice, line);
43− nth_next_grapheme_boundary(slice, pos, count).min(end_char_idx)
45+ (Movement::Move, Direction::Forward) => {
46+ let count = if range.anchor < range.head {
47+ count - 1
48+ } else {
49+ count
50+ };
51+ let pos = nth_next_grapheme_boundary(slice, range.head, count);
52+ Range::new(pos, pos)
4453 }
45− };
46− let anchor = match behaviour {
47− Movement::Extend => range.anchor,
48− Movement::Move => pos,
49− };
50− Range::new(anchor, pos)
54+ (Movement::Extend, Direction::Backward) => {
55+ // Ensure a valid initial selection state.
56+ let range = range.min_width_1(slice);
57+
58+ // Do the main movement.
59+ let mut head = nth_prev_grapheme_boundary(slice, range.head, count);
60+ let mut anchor = range.anchor;
61+
62+ // If the head and anchor crossed over each other, we need to
63+ // fiddle around to make it behave like a 1-wide cursor.
64+ if head <= anchor && range.head > range.anchor {
65+ anchor = next_grapheme_boundary(slice, anchor);
66+ head = prev_grapheme_boundary(slice, head);
67+ }
68+
69+ Range::new(anchor, head)
70+ }
71+ (Movement::Extend, Direction::Forward) => {
72+ // Ensure a valid initial selection state.
73+ let range = range.min_width_1(slice);
74+
75+ // Do the main movement.
76+ let mut head = nth_next_grapheme_boundary(slice, range.head, count);
77+ let mut anchor = range.anchor;
78+
79+ // If the head and anchor crossed over each other, we need to
80+ // fiddle around to make it behave like a 1-wide cursor.
81+ if head >= anchor && range.head < range.anchor {
82+ anchor = prev_grapheme_boundary(slice, anchor);
83+ head = next_grapheme_boundary(slice, head);
84+ }
85+
86+ Range::new(anchor, head)
87+ }
88+ }
5189}
5290
5391pub fn move_vertically(
@@ -323,7 +361,7 @@ mod test {
323361 }
324362
325363 #[test]
326− fn horizontal_moves_through_single_line_in_single_line_text() {
364+ fn horizontal_moves_through_single_line_text() {
327365 let text = Rope::from(SINGLE_LINE_SAMPLE);
328366 let slice = text.slice(..);
329367 let position = pos_at_coords(slice, (0, 0).into());
@@ -346,7 +384,7 @@ mod test {
346384 }
347385
348386 #[test]
349− fn horizontal_moves_through_single_line_in_multiline_text() {
387+ fn horizontal_moves_through_multiline_text() {
350388 let text = Rope::from(MULTILINE_SAMPLE);
351389 let slice = text.slice(..);
352390 let position = pos_at_coords(slice, (0, 0).into());
@@ -354,15 +392,15 @@ mod test {
354392 let mut range = Range::point(position);
355393
356394 let moves_and_expected_coordinates = IntoIter::new([
357− ((Direction::Forward, 1usize), (0, 1)), // M|ultiline\n
358− ((Direction::Forward, 2usize), (0, 3)), // Mul|tiline\n
359− ((Direction::Backward, 6usize), (0, 0)), // |Multiline\n
360− ((Direction::Backward, 999usize), (0, 0)), // |Multiline\n
361− ((Direction::Forward, 3usize), (0, 3)), // Mul|tiline\n
362− ((Direction::Forward, 0usize), (0, 3)), // Mul|tiline\n
363− ((Direction::Backward, 0usize), (0, 3)), // Mul|tiline\n
364− ((Direction::Forward, 999usize), (0, 9)), // Multiline|\n
365− ((Direction::Forward, 999usize), (0, 9)), // Multiline|\n
395+ ((Direction::Forward, 11usize), (1, 1)), // Multiline\nt|ext sample\n...
396+ ((Direction::Backward, 1usize), (1, 0)), // Multiline\n|text sample\n...
397+ ((Direction::Backward, 5usize), (0, 5)), // Multi|line\ntext sample\n...
398+ ((Direction::Backward, 999usize), (0, 0)), // |Multiline\ntext sample\n...
399+ ((Direction::Forward, 3usize), (0, 3)), // Mul|tiline\ntext sample\n...
400+ ((Direction::Forward, 0usize), (0, 3)), // Mul|tiline\ntext sample\n...
401+ ((Direction::Backward, 0usize), (0, 3)), // Mul|tiline\ntext sample\n...
402+ ((Direction::Forward, 999usize), (5, 0)), // ...and whitespaced\n|
403+ ((Direction::Forward, 999usize), (5, 0)), // ...and whitespaced\n|
366404 ]);
367405
368406 for ((direction, amount), coordinates) in moves_and_expected_coordinates {