on master help sign in
Add function to get the line ending of a str slice.
07e2880Nathan Vegdahl
helix-core/src/lib.rs+1−3
@@ -116,7 +116,5 @@ pub use syntax::Syntax;
116pub use diagnostic::Diagnostic;
116pub use diagnostic::Diagnostic;
117pub use state::State;
117pub use state::State;
118
118
119−pub use line_ending::{
119+pub use line_ending::{LineEnding, DEFAULT_LINE_ENDING};
120− auto_detect_line_ending, get_line_ending, line_end_char_index, LineEnding, DEFAULT_LINE_ENDING,
121−};
122pub use transaction::{Assoc, Change, ChangeSet, Operation, Transaction};
120pub 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> {
128 LineEnding::from_str(g2).or_else(|| LineEnding::from_str(g1))
128 LineEnding::from_str(g2).or_else(|| LineEnding::from_str(g1))
129}
129}
130
130
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+
131/// Returns the char index of the end of the given line, not including its line ending.
154/// Returns the char index of the end of the given line, not including its line ending.
132pub fn line_end_char_index(slice: &RopeSlice, line: usize) -> usize {
155pub fn line_end_char_index(slice: &RopeSlice, line: usize) -> usize {
133 slice.line_to_char(line + 1)
156 slice.line_to_char(line + 1)
helix-core/src/movement.rs+3−2
@@ -7,9 +7,10 @@ use crate::{
7 categorize_char, char_is_line_ending, char_is_punctuation, char_is_whitespace,
7 categorize_char, char_is_line_ending, char_is_punctuation, char_is_whitespace,
8 char_is_word, CharCategory,
8 char_is_word, CharCategory,
9 },
9 },
10− coords_at_pos, get_line_ending,
10+ coords_at_pos,
11 graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary},
11 graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary},
12+ line_ending::{get_line_ending, line_end_char_index},
12− line_end_char_index, pos_at_coords, Position, Range, RopeSlice,
13+ pos_at_coords, Position, Range, RopeSlice,
13};
14};
14
15
15#[derive(Debug, Copy, Clone, PartialEq, Eq)]
16#[derive(Debug, Copy, Clone, PartialEq, Eq)]
helix-term/src/commands.rs+5−4
@@ -1,6 +1,7 @@
1use helix_core::{
1use helix_core::{
2− comment, coords_at_pos, find_first_non_whitespace_char, find_root, get_line_ending, graphemes,
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},
3− indent, line_end_char_index, match_brackets,
4+ match_brackets,
4 movement::{self, Direction},
5 movement::{self, Direction},
5 object, pos_at_coords,
6 object, pos_at_coords,
6 regex::{self, Regex},
7 regex::{self, Regex},
@@ -2534,10 +2535,10 @@ fn paste_impl(
2534 .unwrap(),
2535 .unwrap(),
2535 );
2536 );
2536
2537
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
2538 let linewise = values
2539 let linewise = values
2539 .iter()
2540 .iter()
2540− .any(|value| value.ends_with(doc.line_ending.as_str()));
2541+ .any(|value| get_line_ending_of_str(value).is_some());
2541
2542
2542 let mut values = values.iter().cloned().map(Tendril::from).chain(repeat);
2543 let mut values = values.iter().cloned().map(Tendril::from).chain(repeat);
2543
2544
helix-view/src/document.rs+1−1
@@ -7,9 +7,9 @@ use std::str::FromStr;
7use std::sync::Arc;
7use std::sync::Arc;
8
8
9use helix_core::{
9use helix_core::{
10− auto_detect_line_ending,
11 chars::{char_is_line_ending, char_is_whitespace},
10 chars::{char_is_line_ending, char_is_whitespace},
12 history::History,
11 history::History,
12+ line_ending::auto_detect_line_ending,
13 syntax::{self, LanguageConfiguration},
13 syntax::{self, LanguageConfiguration},
14 ChangeSet, Diagnostic, LineEnding, Rope, Selection, State, Syntax, Transaction,
14 ChangeSet, Diagnostic, LineEnding, Rope, Selection, State, Syntax, Transaction,
15 DEFAULT_LINE_ENDING,
15 DEFAULT_LINE_ENDING,