on master help sign in
added the line_end helper function
commit8634e04a31f8f761b3d0505528295d31d63c7918 authorJan Hrastnik <jan.hrastnik2@gmail.com> dateSun 20 Jun 2021, 02:22 (5 years ago) parent701eb0d
4 files changed+13−22
helix-core/src/lib.rs+1−1 helix-core/src/line_ending.rs+6 helix-core/src/movement.rs+2−4 helix-term/src/commands.rs+4−17
helix-core/src/lib.rs+1−1
@@ -114,6 +114,6 @@ pub use state::State;
114114
115115pub use line_ending::{
116116 auto_detect_line_ending, get_line_ending, rope_slice_to_line_ending, LineEnding,
117− DEFAULT_LINE_ENDING,
117+ DEFAULT_LINE_ENDING, line_end
118118};
119119pub 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> {
111111 str_to_line_ending(g2).or_else(|| str_to_line_ending(g1))
112112}
113113
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+
114120#[cfg(target_os = "windows")]
115121pub const DEFAULT_LINE_ENDING: LineEnding = LineEnding::Crlf;
116122#[cfg(not(target_os = "windows"))]
helix-core/src/movement.rs+2−4
@@ -5,7 +5,7 @@ use ropey::iter::Chars;
55use crate::{
66 coords_at_pos,
77 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
99};
1010
1111#[derive(Debug, Copy, Clone, PartialEq, Eq)]
@@ -38,9 +38,7 @@ pub fn move_horizontally(
3838 }
3939 Direction::Forward => {
4040 // 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))
42− .map(|le| le.len_chars())
43− .unwrap_or(0));
41+ let end = line_end(&slice, line);
4442 nth_next_grapheme_boundary(slice, pos, count).min(end)
4543 }
4644 };
helix-term/src/commands.rs+4−17
@@ -1,6 +1,6 @@
11use helix_core::{
22 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,
44 movement::{self, Direction},
55 object, pos_at_coords,
66 regex::{self, Regex},
@@ -342,11 +342,7 @@ fn move_line_end(cx: &mut Context) {
342342 let text = doc.text();
343343 let line = text.char_to_line(range.head);
344344
345− let pos = text.line_to_char(line + 1).saturating_sub(
346− get_line_ending(&text.line(line))
347− .map(|le| le.len_chars())
348− .unwrap_or(0),
349− );
345+ let pos = line_end(&text.slice(..), line);
350346
351347 Range::new(pos, pos)
352348 });
@@ -767,11 +763,7 @@ fn extend_line_end(cx: &mut Context) {
767763 let text = doc.text();
768764 let line = text.char_to_line(range.head);
769765
770− let pos = text.line_to_char(line + 1).saturating_sub(
771− get_line_ending(&text.line(line))
772− .map(|le| le.len_chars())
773− .unwrap_or(0),
774− );
766+ let pos = line_end(&text.slice(..), line);
775767
776768 Range::new(range.anchor, pos)
777769 });
@@ -1650,12 +1642,7 @@ fn append_to_line(cx: &mut Context) {
16501642 let selection = doc.selection(view.id).transform(|range| {
16511643 let text = doc.text();
16521644 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
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− );
1645+ let pos = line_end(&text.slice(..), line);
16591646 Range::new(pos, pos)
16601647 });
16611648 doc.set_selection(view.id, selection);