on master help sign in
Add function to get the line ending of a str slice.

This is needed in some places.

commit07e28802f6b61a17e839d05b2a031575f323b9c9 authorNathan Vegdahl <cessen@cessen.com> dateMon 21 Jun 2021, 10:29 (5 years ago) parent7140020
5 files changed+33−10
helix-core/src/lib.rs+1−3 helix-core/src/line_ending.rs+23 helix-core/src/movement.rs+3−2 helix-term/src/commands.rs+5−4 helix-view/src/document.rs+1−1
helix-core/src/lib.rs+1−3
@@ -116,7 +116,5 @@ pub use syntax::Syntax;
116116pub use diagnostic::Diagnostic;
117117pub use state::State;
118118
119−pub use line_ending::{
120− auto_detect_line_ending, get_line_ending, line_end_char_index, LineEnding, DEFAULT_LINE_ENDING,
121−};
119+pub use line_ending::{LineEnding, DEFAULT_LINE_ENDING};
122120pub use transaction::{Assoc, Change, ChangeSet, Operation, Transaction};
helix-core/src/line_ending.rs+23
@@ -128,6 +128,29 @@ pub fn get_line_ending(line: &RopeSlice) -> Option<LineEnding> {
128128 LineEnding::from_str(g2).or_else(|| LineEnding::from_str(g1))
129129}
130130
131+/// Returns the passed line's line ending, if any.
132+pub fn get_line_ending_of_str(line: &str) -> Option<LineEnding> {
133+ if line.ends_with("\u{000D}\u{000A}") {
134+ Some(LineEnding::Crlf)
135+ } else if line.ends_with("\u{000A}") {
136+ Some(LineEnding::LF)
137+ } else if line.ends_with("\u{000B}") {
138+ Some(LineEnding::VT)
139+ } else if line.ends_with("\u{000C}") {
140+ Some(LineEnding::FF)
141+ } else if line.ends_with("\u{000D}") {
142+ Some(LineEnding::CR)
143+ } else if line.ends_with("\u{0085}") {
144+ Some(LineEnding::Nel)
145+ } else if line.ends_with("\u{2028}") {
146+ Some(LineEnding::LS)
147+ } else if line.ends_with("\u{2029}") {
148+ Some(LineEnding::PS)
149+ } else {
150+ None
151+ }
152+}
153+
131154/// Returns the char index of the end of the given line, not including its line ending.
132155pub fn line_end_char_index(slice: &RopeSlice, line: usize) -> usize {
133156 slice.line_to_char(line + 1)
helix-core/src/movement.rs+3−2
@@ -7,9 +7,10 @@ use crate::{
77 categorize_char, char_is_line_ending, char_is_punctuation, char_is_whitespace,
88 char_is_word, CharCategory,
99 },
10− coords_at_pos, get_line_ending,
10+ coords_at_pos,
1111 graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary},
12− line_end_char_index, pos_at_coords, Position, Range, RopeSlice,
12+ line_ending::{get_line_ending, line_end_char_index},
13+ pos_at_coords, Position, Range, RopeSlice,
1314};
1415
1516#[derive(Debug, Copy, Clone, PartialEq, Eq)]
helix-term/src/commands.rs+5−4
@@ -1,6 +1,7 @@
11use helix_core::{
2− comment, coords_at_pos, find_first_non_whitespace_char, find_root, get_line_ending, graphemes,
3− indent, line_end_char_index, match_brackets,
2+ comment, coords_at_pos, find_first_non_whitespace_char, find_root, graphemes, indent,
3+ line_ending::{get_line_ending, get_line_ending_of_str, line_end_char_index},
4+ match_brackets,
45 movement::{self, Direction},
56 object, pos_at_coords,
67 regex::{self, Regex},
@@ -2534,10 +2535,10 @@ fn paste_impl(
25342535 .unwrap(),
25352536 );
25362537
2537− // if any of values ends \n it's linewise paste
2538+ // if any of values ends with a line ending, it's linewise paste
25382539 let linewise = values
25392540 .iter()
2540− .any(|value| value.ends_with(doc.line_ending.as_str()));
2541+ .any(|value| get_line_ending_of_str(value).is_some());
25412542
25422543 let mut values = values.iter().cloned().map(Tendril::from).chain(repeat);
25432544
helix-view/src/document.rs+1−1
@@ -7,9 +7,9 @@ use std::str::FromStr;
77use std::sync::Arc;
88
99use helix_core::{
10− auto_detect_line_ending,
1110 chars::{char_is_line_ending, char_is_whitespace},
1211 history::History,
12+ line_ending::auto_detect_line_ending,
1313 syntax::{self, LanguageConfiguration},
1414 ChangeSet, Diagnostic, LineEnding, Rope, Selection, State, Syntax, Transaction,
1515 DEFAULT_LINE_ENDING,