on master help sign in
Simplify line ending calculation.
commit1bb01d27aed8b046ff1cb41d4932c303d3b4ad0d authorBlaž Hrastnik <blaz@mxxn.io> dateTue 29 Sep 2020, 17:49 (6 years ago) parent13d1ea5
2 files changed+9−11
helix-core/src/state.rs+3−4 helix-view/src/commands.rs+6−7
helix-core/src/state.rs+3−4
@@ -159,10 +159,9 @@ impl State {
159159 (Direction::Forward, Granularity::Character) => {
160160 // Clamp to line
161161 let line = text.char_to_line(pos);
162− let start = text.line_to_char(line);
163− let len = text.line(line).len_chars();
164− // convert to 0-indexed, subtract another 1 because len_chars() counts \n
165− let end = start + len.saturating_sub(2);
162+ // Line end is pos at the start of next line - 1
163+ // subtract another 1 because the line ends with \n
164+ let end = text.line_to_char(line + 1).saturating_sub(2);
166165 std::cmp::min(nth_next_grapheme_boundary(&text.slice(..), pos, count), end)
167166 }
168167 (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) {
5050 .into_iter()
5151 .map(|index| {
5252 // adjust all positions to the end of the line.
53− let line = view.state.doc.line(index);
54− let line_start = view.state.doc.line_to_char(index);
55− line_start + line.len_chars() - 1
53+
54+ // Line end is pos at the start of next line - 1
55+ // subtract another 1 because the line ends with \n
56+ view.state.doc.line_to_char(index + 1).saturating_sub(2)
5657 })
5758 .map(|pos| Range::new(pos, pos));
5859
@@ -236,10 +237,8 @@ pub fn open_below(view: &mut View, _count: usize) {
236237 let positions: Vec<_> = lines
237238 .into_iter()
238239 .map(|index| {
239− // adjust all positions to the end of the line.
240− let line = view.state.doc.line(index);
241− let line_start = view.state.doc.line_to_char(index);
242− line_start + line.len_chars()
240+ // adjust all positions to the end of the line/start of the next one.
241+ view.state.doc.line_to_char(index + 1)
243242 })
244243 .collect();
245244