From 8634e04a31f8f761b3d0505528295d31d63c7918 Mon Sep 17 00:00:00 2001 From: Jan Hrastnik Date: Sun, 20 Jun 2021 02:22:10 +0200 Subject: [PATCH] added the line_end helper function --- helix-core/src/lib.rs | 2 +- helix-core/src/line_ending.rs | 6 ++++++ helix-core/src/movement.rs | 6 ++---- helix-term/src/commands.rs | 21 ++++----------------- 4 files changed, 13 insertions(+), 22 deletions(-) diff --git a/helix-core/src/lib.rs b/helix-core/src/lib.rs index 9ac506a63..d99bb66d1 100644 --- a/helix-core/src/lib.rs +++ b/helix-core/src/lib.rs @@ -114,6 +114,6 @@ pub use state::State; pub use line_ending::{ auto_detect_line_ending, get_line_ending, rope_slice_to_line_ending, LineEnding, - DEFAULT_LINE_ENDING, + DEFAULT_LINE_ENDING, line_end }; pub use transaction::{Assoc, Change, ChangeSet, Operation, Transaction}; diff --git a/helix-core/src/line_ending.rs b/helix-core/src/line_ending.rs index 33f8d0783..f6118493d 100644 --- a/helix-core/src/line_ending.rs +++ b/helix-core/src/line_ending.rs @@ -111,6 +111,12 @@ pub fn get_line_ending(line: &RopeSlice) -> Option { str_to_line_ending(g2).or_else(|| str_to_line_ending(g1)) } +pub fn line_end(slice: &RopeSlice, line: usize) -> usize { + slice.line_to_char(line + 1).saturating_sub(get_line_ending(&slice.line(line)) + .map(|le| le.len_chars()) + .unwrap_or(0)) +} + #[cfg(target_os = "windows")] pub const DEFAULT_LINE_ENDING: LineEnding = LineEnding::Crlf; #[cfg(not(target_os = "windows"))] diff --git a/helix-core/src/movement.rs b/helix-core/src/movement.rs index cf7ea8548..a3cd9b96f 100644 --- a/helix-core/src/movement.rs +++ b/helix-core/src/movement.rs @@ -5,7 +5,7 @@ use ropey::iter::Chars; use crate::{ coords_at_pos, graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary}, - pos_at_coords, Position, Range, RopeSlice, get_line_ending + pos_at_coords, Position, Range, RopeSlice, get_line_ending, line_end }; #[derive(Debug, Copy, Clone, PartialEq, Eq)] @@ -38,9 +38,7 @@ pub fn move_horizontally( } Direction::Forward => { // Line end is pos at the start of next line - 1 - let end = slice.line_to_char(line + 1).saturating_sub(get_line_ending(&slice.line(line)) - .map(|le| le.len_chars()) - .unwrap_or(0)); + let end = line_end(&slice, line); nth_next_grapheme_boundary(slice, pos, count).min(end) } }; diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index cee0e752f..8124c17af 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1,6 +1,6 @@ use helix_core::{ comment, coords_at_pos, find_first_non_whitespace_char, find_root, get_line_ending, graphemes, - indent, match_brackets, + indent, line_end, match_brackets, movement::{self, Direction}, object, pos_at_coords, regex::{self, Regex}, @@ -342,11 +342,7 @@ fn move_line_end(cx: &mut Context) { let text = doc.text(); let line = text.char_to_line(range.head); - let pos = text.line_to_char(line + 1).saturating_sub( - get_line_ending(&text.line(line)) - .map(|le| le.len_chars()) - .unwrap_or(0), - ); + let pos = line_end(&text.slice(..), line); Range::new(pos, pos) }); @@ -767,11 +763,7 @@ fn extend_line_end(cx: &mut Context) { let text = doc.text(); let line = text.char_to_line(range.head); - let pos = text.line_to_char(line + 1).saturating_sub( - get_line_ending(&text.line(line)) - .map(|le| le.len_chars()) - .unwrap_or(0), - ); + let pos = line_end(&text.slice(..), line); Range::new(range.anchor, pos) }); @@ -1650,12 +1642,7 @@ fn append_to_line(cx: &mut Context) { let selection = doc.selection(view.id).transform(|range| { let text = doc.text(); let line = text.char_to_line(range.head); - // we can't use line_to_char(line + 1) - 2 because the last line might not contain a newline - let pos = (text.line_to_char(line) + text.line(line).len_chars()).saturating_sub( - get_line_ending(&text.line(line)) - .map(|le| le.len_chars()) - .unwrap_or(0), - ); + let pos = line_end(&text.slice(..), line); Range::new(pos, pos) }); doc.set_selection(view.id, selection);