on master help sign in
added the line_end helper function
8634e04Jan Hrastnik
helix-core/src/lib.rs+1−1
@@ -114,6 +114,6 @@ pub use state::State;
114
114
115pub use line_ending::{
115pub use line_ending::{
116 auto_detect_line_ending, get_line_ending, rope_slice_to_line_ending, LineEnding,
116 auto_detect_line_ending, get_line_ending, rope_slice_to_line_ending, LineEnding,
117− DEFAULT_LINE_ENDING,
117+ DEFAULT_LINE_ENDING, line_end
118};
118};
119pub use transaction::{Assoc, Change, ChangeSet, Operation, Transaction};
119pub use transaction::{Assoc, Change, ChangeSet, Operation, Transaction};
helix-core/src/line_ending.rs+6
@@ -111,6 +111,12 @@ pub fn get_line_ending(line: &RopeSlice) -> Option<LineEnding> {
111 str_to_line_ending(g2).or_else(|| str_to_line_ending(g1))
111 str_to_line_ending(g2).or_else(|| str_to_line_ending(g1))
112}
112}
113
113
114+pub fn line_end(slice: &RopeSlice, line: usize) -> usize {
115+ slice.line_to_char(line + 1).saturating_sub(get_line_ending(&slice.line(line))
116+ .map(|le| le.len_chars())
117+ .unwrap_or(0))
118+}
119+
114#[cfg(target_os = "windows")]
120#[cfg(target_os = "windows")]
115pub const DEFAULT_LINE_ENDING: LineEnding = LineEnding::Crlf;
121pub const DEFAULT_LINE_ENDING: LineEnding = LineEnding::Crlf;
116#[cfg(not(target_os = "windows"))]
122#[cfg(not(target_os = "windows"))]
helix-core/src/movement.rs+2−4
@@ -5,7 +5,7 @@ use ropey::iter::Chars;
5use crate::{
5use crate::{
6 coords_at_pos,
6 coords_at_pos,
7 graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary},
7 graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary},
8− pos_at_coords, Position, Range, RopeSlice, get_line_ending
8+ pos_at_coords, Position, Range, RopeSlice, get_line_ending, line_end
9};
9};
10
10
11#[derive(Debug, Copy, Clone, PartialEq, Eq)]
11#[derive(Debug, Copy, Clone, PartialEq, Eq)]
@@ -38,9 +38,7 @@ pub fn move_horizontally(
38 }
38 }
39 Direction::Forward => {
39 Direction::Forward => {
40 // Line end is pos at the start of next line - 1
40 // Line end is pos at the start of next line - 1
41− let end = slice.line_to_char(line + 1).saturating_sub(get_line_ending(&slice.line(line))
41+ let end = line_end(&slice, line);
42− .map(|le| le.len_chars())
43− .unwrap_or(0));
44 nth_next_grapheme_boundary(slice, pos, count).min(end)
42 nth_next_grapheme_boundary(slice, pos, count).min(end)
45 }
43 }
46 };
44 };
helix-term/src/commands.rs+4−17
@@ -1,6 +1,6 @@
1use helix_core::{
1use helix_core::{
2 comment, coords_at_pos, find_first_non_whitespace_char, find_root, get_line_ending, graphemes,
2 comment, coords_at_pos, find_first_non_whitespace_char, find_root, get_line_ending, graphemes,
3− indent, match_brackets,
3+ indent, line_end, match_brackets,
4 movement::{self, Direction},
4 movement::{self, Direction},
5 object, pos_at_coords,
5 object, pos_at_coords,
6 regex::{self, Regex},
6 regex::{self, Regex},
@@ -342,11 +342,7 @@ fn move_line_end(cx: &mut Context) {
342 let text = doc.text();
342 let text = doc.text();
343 let line = text.char_to_line(range.head);
343 let line = text.char_to_line(range.head);
344
344
345− let pos = text.line_to_char(line + 1).saturating_sub(
345+ let pos = line_end(&text.slice(..), line);
346− get_line_ending(&text.line(line))
347− .map(|le| le.len_chars())
348− .unwrap_or(0),
349− );
350
346
351 Range::new(pos, pos)
347 Range::new(pos, pos)
352 });
348 });
@@ -767,11 +763,7 @@ fn extend_line_end(cx: &mut Context) {
767 let text = doc.text();
763 let text = doc.text();
768 let line = text.char_to_line(range.head);
764 let line = text.char_to_line(range.head);
769
765
770− let pos = text.line_to_char(line + 1).saturating_sub(
766+ let pos = line_end(&text.slice(..), line);
771− get_line_ending(&text.line(line))
772− .map(|le| le.len_chars())
773− .unwrap_or(0),
774− );
775
767
776 Range::new(range.anchor, pos)
768 Range::new(range.anchor, pos)
777 });
769 });
@@ -1650,12 +1642,7 @@ fn append_to_line(cx: &mut Context) {
1650 let selection = doc.selection(view.id).transform(|range| {
1642 let selection = doc.selection(view.id).transform(|range| {
1651 let text = doc.text();
1643 let text = doc.text();
1652 let line = text.char_to_line(range.head);
1644 let line = text.char_to_line(range.head);
1653− // we can't use line_to_char(line + 1) - 2 because the last line might not contain a newline
1645+ let pos = line_end(&text.slice(..), line);
1654− let pos = (text.line_to_char(line) + text.line(line).len_chars()).saturating_sub(
1655− get_line_ending(&text.line(line))
1656− .map(|le| le.len_chars())
1657− .unwrap_or(0),
1658− );
1659 Range::new(pos, pos)
1646 Range::new(pos, pos)
1660 });
1647 });
1661 doc.set_selection(view.id, selection);
1648 doc.set_selection(view.id, selection);