aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Vegdahl2021-06-21 17:29:29 +0000
committerNathan Vegdahl2021-06-21 17:29:29 +0000
commit07e28802f6b61a17e839d05b2a031575f323b9c9 (patch)
treecf28b772ad9b43a125a27dc3018ac09ba1bb2e16
parent714002048cc9601bf0981435c6d3ad43d1c765e8 (diff)
Add function to get the line ending of a str slice.
This is needed in some places.
-rw-r--r--helix-core/src/lib.rs4
-rw-r--r--helix-core/src/line_ending.rs23
-rw-r--r--helix-core/src/movement.rs5
-rw-r--r--helix-term/src/commands.rs9
-rw-r--r--helix-view/src/document.rs2
5 files changed, 33 insertions, 10 deletions
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> {
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<LineEnding> {
+ 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)]
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index 70441fcf..3d2a9028 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 945271ea..bd45db5a 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,