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 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};

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))
}
/// 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)

View file

@ -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)]

View file

@ -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);

View file

@ -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,