From 07e28802f6b61a17e839d05b2a031575f323b9c9 Mon Sep 17 00:00:00 2001 From: Nathan Vegdahl Date: Mon, 21 Jun 2021 10:29:29 -0700 Subject: [PATCH] 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 +++-- helix-term/src/commands.rs | 9 +++++---- helix-view/src/document.rs | 2 +- 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/helix-core/src/lib.rs b/helix-core/src/lib.rs index 692946880..f697bc7fd 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 3055c96ef..19de22317 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 d0023e9f8..bfceb4ef5 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)] diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 70441fcfa..3d2a90288 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1,6 +1,7 @@ use helix_core::{ - comment, coords_at_pos, find_first_non_whitespace_char, find_root, get_line_ending, graphemes, - indent, line_end_char_index, match_brackets, + comment, coords_at_pos, find_first_non_whitespace_char, find_root, graphemes, indent, + line_ending::{get_line_ending, get_line_ending_of_str, line_end_char_index}, + match_brackets, movement::{self, Direction}, object, pos_at_coords, regex::{self, Regex}, @@ -2534,10 +2535,10 @@ fn paste_impl( .unwrap(), ); - // if any of values ends \n it's linewise paste + // if any of values ends with a line ending, it's linewise paste let linewise = values .iter() - .any(|value| value.ends_with(doc.line_ending.as_str())); + .any(|value| get_line_ending_of_str(value).is_some()); let mut values = values.iter().cloned().map(Tendril::from).chain(repeat); diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 945271ead..bd45db5a1 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -7,9 +7,9 @@ use std::str::FromStr; use std::sync::Arc; use helix_core::{ - auto_detect_line_ending, chars::{char_is_line_ending, char_is_whitespace}, history::History, + line_ending::auto_detect_line_ending, syntax::{self, LanguageConfiguration}, ChangeSet, Diagnostic, LineEnding, Rope, Selection, State, Syntax, Transaction, DEFAULT_LINE_ENDING,