Add function to get the line ending of a str slice.

This is needed in some places.
This commit is contained in:
Nathan Vegdahl 2021-06-21 10:29:29 -07:00
parent 714002048c
commit 07e28802f6
5 changed files with 33 additions and 10 deletions

View File

@ -116,7 +116,5 @@ pub fn cache_dir() -> std::path::PathBuf {
pub use diagnostic::Diagnostic; pub use diagnostic::Diagnostic;
pub use state::State; pub use state::State;
pub use line_ending::{ pub use line_ending::{LineEnding, DEFAULT_LINE_ENDING};
auto_detect_line_ending, get_line_ending, line_end_char_index, LineEnding, DEFAULT_LINE_ENDING,
};
pub use transaction::{Assoc, Change, ChangeSet, Operation, Transaction}; pub use transaction::{Assoc, Change, ChangeSet, Operation, Transaction};

View File

@ -128,6 +128,29 @@ pub fn get_line_ending(line: &RopeSlice) -> Option<LineEnding> {
LineEnding::from_str(g2).or_else(|| LineEnding::from_str(g1)) 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. /// 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 { pub fn line_end_char_index(slice: &RopeSlice, line: usize) -> usize {
slice.line_to_char(line + 1) slice.line_to_char(line + 1)

View File

@ -7,9 +7,10 @@
categorize_char, char_is_line_ending, char_is_punctuation, char_is_whitespace, categorize_char, char_is_line_ending, char_is_punctuation, char_is_whitespace,
char_is_word, CharCategory, char_is_word, CharCategory,
}, },
coords_at_pos, get_line_ending, coords_at_pos,
graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary}, 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)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]

View File

@ -1,6 +1,7 @@
use helix_core::{ use helix_core::{
comment, coords_at_pos, find_first_non_whitespace_char, find_root, get_line_ending, graphemes, comment, coords_at_pos, find_first_non_whitespace_char, find_root, graphemes, indent,
indent, line_end_char_index, match_brackets, line_ending::{get_line_ending, get_line_ending_of_str, line_end_char_index},
match_brackets,
movement::{self, Direction}, movement::{self, Direction},
object, pos_at_coords, object, pos_at_coords,
regex::{self, Regex}, regex::{self, Regex},
@ -2534,10 +2535,10 @@ fn paste_impl(
.unwrap(), .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 let linewise = values
.iter() .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); let mut values = values.iter().cloned().map(Tendril::from).chain(repeat);

View File

@ -7,9 +7,9 @@
use std::sync::Arc; use std::sync::Arc;
use helix_core::{ use helix_core::{
auto_detect_line_ending,
chars::{char_is_line_ending, char_is_whitespace}, chars::{char_is_line_ending, char_is_whitespace},
history::History, history::History,
line_ending::auto_detect_line_ending,
syntax::{self, LanguageConfiguration}, syntax::{self, LanguageConfiguration},
ChangeSet, Diagnostic, LineEnding, Rope, Selection, State, Syntax, Transaction, ChangeSet, Diagnostic, LineEnding, Rope, Selection, State, Syntax, Transaction,
DEFAULT_LINE_ENDING, DEFAULT_LINE_ENDING,