on master help sign in
Simplify line ending calculation.
1bb01d2Blaž Hrastnik
helix-core/src/state.rs+3−4
@@ -159,10 +159,9 @@ impl State {
159 (Direction::Forward, Granularity::Character) => {
159 (Direction::Forward, Granularity::Character) => {
160 // Clamp to line
160 // Clamp to line
161 let line = text.char_to_line(pos);
161 let line = text.char_to_line(pos);
162− let start = text.line_to_char(line);
162+ // Line end is pos at the start of next line - 1
163− let len = text.line(line).len_chars();
164− // convert to 0-indexed, subtract another 1 because len_chars() counts \n
163+ // subtract another 1 because the line ends with \n
165− let end = start + len.saturating_sub(2);
164+ let end = text.line_to_char(line + 1).saturating_sub(2);
166 std::cmp::min(nth_next_grapheme_boundary(&text.slice(..), pos, count), end)
165 std::cmp::min(nth_next_grapheme_boundary(&text.slice(..), pos, count), end)
167 }
166 }
168 (Direction::Forward, Granularity::Word) => {
167 (Direction::Forward, Granularity::Word) => {
helix-view/src/commands.rs+6−7
@@ -50,9 +50,10 @@ pub fn move_line_end(view: &mut View, count: usize) {
50 .into_iter()
50 .into_iter()
51 .map(|index| {
51 .map(|index| {
52 // adjust all positions to the end of the line.
52 // adjust all positions to the end of the line.
53− let line = view.state.doc.line(index);
53+
54+ // Line end is pos at the start of next line - 1
55+ // subtract another 1 because the line ends with \n
54− let line_start = view.state.doc.line_to_char(index);
56+ view.state.doc.line_to_char(index + 1).saturating_sub(2)
55− line_start + line.len_chars() - 1
56 })
57 })
57 .map(|pos| Range::new(pos, pos));
58 .map(|pos| Range::new(pos, pos));
58
59
@@ -236,10 +237,8 @@ pub fn open_below(view: &mut View, _count: usize) {
236 let positions: Vec<_> = lines
237 let positions: Vec<_> = lines
237 .into_iter()
238 .into_iter()
238 .map(|index| {
239 .map(|index| {
239− // adjust all positions to the end of the line.
240+ // adjust all positions to the end of the line/start of the next one.
240− let line = view.state.doc.line(index);
241− let line_start = view.state.doc.line_to_char(index);
241+ view.state.doc.line_to_char(index + 1)
242− line_start + line.len_chars()
243 })
242 })
244 .collect();
243 .collect();
245
244