From 07e28802f6b61a17e839d05b2a031575f323b9c9 Mon Sep 17 00:00:00 2001 From: Nathan Vegdahl Date: Mon, 21 Jun 2021 10:29:29 -0700 Subject: Add function to get the line ending of a str slice. This is needed in some places. --- helix-core/src/lib.rs | 4 +--- helix-core/src/line_ending.rs | 23 +++++++++++++++++++++++ helix-core/src/movement.rs | 5 +++-- 3 files changed, 27 insertions(+), 5 deletions(-) (limited to 'helix-core') diff --git a/helix-core/src/lib.rs b/helix-core/src/lib.rs index 69294688..f697bc7f 100644 --- a/helix-core/src/lib.rs +++ b/helix-core/src/lib.rs @@ -116,7 +116,5 @@ pub use syntax::Syntax; pub use diagnostic::Diagnostic; pub use state::State; -pub use line_ending::{ - auto_detect_line_ending, get_line_ending, line_end_char_index, LineEnding, DEFAULT_LINE_ENDING, -}; +pub use line_ending::{LineEnding, DEFAULT_LINE_ENDING}; 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 3055c96e..19de2231 100644 --- a/helix-core/src/line_ending.rs +++ b/helix-core/src/line_ending.rs @@ -128,6 +128,29 @@ pub fn get_line_ending(line: &RopeSlice) -> Option { LineEnding::from_str(g2).or_else(|| LineEnding::from_str(g1)) } +/// Returns the passed line's line ending, if any. +pub fn get_line_ending_of_str(line: &str) -> Option { + if line.ends_with("\u{000D}\u{000A}") { + Some(LineEnding::Crlf) + } else if line.ends_with("\u{000A}") { + Some(LineEnding::LF) + } else if line.ends_with("\u{000B}") { + Some(LineEnding::VT) + } else if line.ends_with("\u{000C}") { + Some(LineEnding::FF) + } else if line.ends_with("\u{000D}") { + Some(LineEnding::CR) + } else if line.ends_with("\u{0085}") { + Some(LineEnding::Nel) + } else if line.ends_with("\u{2028}") { + Some(LineEnding::LS) + } else if line.ends_with("\u{2029}") { + Some(LineEnding::PS) + } else { + None + } +} + /// Returns the char index of the end of the given line, not including its line ending. pub fn line_end_char_index(slice: &RopeSlice, line: usize) -> usize { slice.line_to_char(line + 1) diff --git a/helix-core/src/movement.rs b/helix-core/src/movement.rs index d0023e9f..bfceb4ef 100644 --- a/helix-core/src/movement.rs +++ b/helix-core/src/movement.rs @@ -7,9 +7,10 @@ use crate::{ categorize_char, char_is_line_ending, char_is_punctuation, char_is_whitespace, char_is_word, CharCategory, }, - coords_at_pos, get_line_ending, + coords_at_pos, graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary}, - line_end_char_index, pos_at_coords, Position, Range, RopeSlice, + line_ending::{get_line_ending, line_end_char_index}, + pos_at_coords, Position, Range, RopeSlice, }; #[derive(Debug, Copy, Clone, PartialEq, Eq)] -- cgit v1.2.3-70-g09d2