on master help sign in
Make vertical selection movement work properly.
6e15c9bNathan Vegdahl
helix-core/src/line_ending.rs+7
@@ -159,6 +159,13 @@ pub fn line_end_char_index(slice: &RopeSlice, line: usize) -> usize {
159 .unwrap_or(0)
159 .unwrap_or(0)
160}
160}
161
161
162+/// Fetches line `line_idx` from the passed rope slice, sans any line ending.
163+pub fn line_without_line_ending<'a>(slice: &'a RopeSlice, line_idx: usize) -> RopeSlice<'a> {
164+ let start = slice.line_to_char(line_idx);
165+ let end = line_end_char_index(slice, line_idx);
166+ slice.slice(start..end)
167+}
168+
162/// Returns the char index of the end of the given RopeSlice, not including
169/// Returns the char index of the end of the given RopeSlice, not including
163/// any final line ending.
170/// any final line ending.
164pub fn rope_end_without_line_ending(slice: &RopeSlice) -> usize {
171pub fn rope_end_without_line_ending(slice: &RopeSlice) -> usize {
helix-core/src/movement.rs+49−24
@@ -7,9 +7,9 @@ use crate::{
7 coords_at_pos,
7 coords_at_pos,
8 graphemes::{
8 graphemes::{
9 next_grapheme_boundary, nth_next_grapheme_boundary, nth_prev_grapheme_boundary,
9 next_grapheme_boundary, nth_next_grapheme_boundary, nth_prev_grapheme_boundary,
10− prev_grapheme_boundary,
10+ prev_grapheme_boundary, RopeGraphemes,
11 },
11 },
12− line_ending::get_line_ending,
12+ line_ending::line_without_line_ending,
13 pos_at_coords, Position, Range, RopeSlice,
13 pos_at_coords, Position, Range, RopeSlice,
14};
14};
15
15
@@ -95,36 +95,61 @@ pub fn move_vertically(
95 count: usize,
95 count: usize,
96 behaviour: Movement,
96 behaviour: Movement,
97) -> Range {
97) -> Range {
98− let Position { row, col } = coords_at_pos(slice, range.head);
98+ // Shift back one grapheme if needed, to account for
99+ // the cursor being visually 1-width.
100+ let pos = if range.head > range.anchor {
101+ prev_grapheme_boundary(slice, range.head)
102+ } else {
103+ range.head
104+ };
99
105
106+ // Compute the current position's 2d coordinates.
107+ let Position { row, col } = coords_at_pos(slice, pos);
100 let horiz = range.horiz.unwrap_or(col as u32);
108 let horiz = range.horiz.unwrap_or(col as u32);
101
109
102− let new_line = match dir {
110+ // Compute the new position.
111+ let new_pos = {
112+ let new_row = if dir == Direction::Backward {
103− Direction::Backward => row.saturating_sub(count),
113+ row.saturating_sub(count)
104− Direction::Forward => std::cmp::min(
114+ } else {
105− row.saturating_add(count),
106− slice.len_lines().saturating_sub(1),
115+ (row + count).min(slice.len_lines().saturating_sub(1))
107− ),
116+ };
108− };
117+ let max_col = RopeGraphemes::new(line_without_line_ending(&slice, new_row)).count();
118+ let new_col = col.max(horiz as usize).min(max_col);
109
119
110− // Length of the line sans line-ending.
120+ pos_at_coords(slice, Position::new(new_row, new_col))
111− let new_line_len = {
112− let line = slice.line(new_line);
113− line.len_chars() - get_line_ending(&line).map(|le| le.len_chars()).unwrap_or(0)
114 };
121 };
115
122
116− let new_col = std::cmp::min(horiz as usize, new_line_len);
123+ // Compute the new range according to the type of movement.
117−
124+ match behaviour {
118− let pos = pos_at_coords(slice, Position::new(new_line, new_col));
125+ Movement::Move => Range {
126+ anchor: new_pos,
127+ head: new_pos,
128+ horiz: Some(horiz),
129+ },
130+
131+ Movement::Extend => {
132+ let new_head = if new_pos >= range.anchor {
133+ next_grapheme_boundary(slice, new_pos)
134+ } else {
135+ new_pos
136+ };
119
137
120− let anchor = match behaviour {
138+ let new_anchor = if range.anchor <= range.head && range.anchor > new_head {
121− Movement::Extend => range.anchor,
139+ next_grapheme_boundary(slice, range.anchor)
122− Movement::Move => pos,
140+ } else if range.anchor > range.head && range.anchor < new_head {
141+ prev_grapheme_boundary(slice, range.anchor)
142+ } else {
143+ range.anchor
123− };
144+ };
124
145
125− let mut range = Range::new(anchor, pos);
146+ Range {
147+ anchor: new_anchor,
148+ head: new_head,
126− range.horiz = Some(horiz);
149+ horiz: Some(horiz),
127− range
150+ }
151+ }
152+ }
128}
153}
129
154
130pub fn move_next_word_start(slice: RopeSlice, range: Range, count: usize) -> Range {
155pub fn move_next_word_start(slice: RopeSlice, range: Range, count: usize) -> Range {
helix-core/src/position.rs+4
@@ -53,6 +53,8 @@ impl From<Position> for tree_sitter::Point {
53}
53}
54/// Convert a character index to (line, column) coordinates.
54/// Convert a character index to (line, column) coordinates.
55pub fn coords_at_pos(text: RopeSlice, pos: usize) -> Position {
55pub fn coords_at_pos(text: RopeSlice, pos: usize) -> Position {
56+ // TODO: this isn't correct. This needs to work in terms of
57+ // visual horizontal position, not graphemes.
56 let line = text.char_to_line(pos);
58 let line = text.char_to_line(pos);
57 let line_start = text.line_to_char(line);
59 let line_start = text.line_to_char(line);
58 let col = RopeGraphemes::new(text.slice(line_start..pos)).count();
60 let col = RopeGraphemes::new(text.slice(line_start..pos)).count();
@@ -61,6 +63,8 @@ pub fn coords_at_pos(text: RopeSlice, pos: usize) -> Position {
61
63
62/// Convert (line, column) coordinates to a character index.
64/// Convert (line, column) coordinates to a character index.
63pub fn pos_at_coords(text: RopeSlice, coords: Position) -> usize {
65pub fn pos_at_coords(text: RopeSlice, coords: Position) -> usize {
66+ // TODO: this isn't correct. This needs to work in terms of
67+ // visual horizontal position, not graphemes.
64 let Position { row, col } = coords;
68 let Position { row, col } = coords;
65 let line_start = text.line_to_char(row);
69 let line_start = text.line_to_char(row);
66 // line_start + col
70 // line_start + col