@@ -1,7 +1,6 @@
1use crate::{Rope, RopeGraphemes, RopeSlice};
1use crate::{Rope, RopeGraphemes, RopeSlice};
3/// Represents one of the valid Unicode line endings.
3/// Represents one of the valid Unicode line endings.
4−/// VT, FF and PS are excluded here, as we don't expect them to show up as a default line break
5#[derive(PartialEq, Copy, Clone, Debug)]
4#[derive(PartialEq, Copy, Clone, Debug)]
6pub enum LineEnding {
5pub enum LineEnding {
7 Crlf, // CarriageReturn followed by LineFeed
6 Crlf, // CarriageReturn followed by LineFeed
@@ -9,6 +8,9 @@ pub enum LineEnding {
9 CR, // U+000D -- CarriageReturn
8 CR, // U+000D -- CarriageReturn
10 Nel, // U+0085 -- NextLine
9 Nel, // U+0085 -- NextLine
11 LS, // U+2028 -- Line Separator
10 LS, // U+2028 -- Line Separator
11+ VT, // U+000B -- VerticalTab
12+ FF, // U+000C -- FormFeed
13+ PS, // U+2029 -- ParagraphSeparator
14pub fn rope_slice_to_line_ending(g: &RopeSlice) -> Option<LineEnding> {
16pub fn rope_slice_to_line_ending(g: &RopeSlice) -> Option<LineEnding> {
@@ -38,8 +40,8 @@ pub fn auto_detect_line_ending(doc: &Rope) -> Option<LineEnding> {
38 // based on https://github.com/cessen/led/blob/27572c8838a1c664ee378a19358604063881cc1d/src/editor/mod.rs#L88-L162
40 // based on https://github.com/cessen/led/blob/27572c8838a1c664ee378a19358604063881cc1d/src/editor/mod.rs#L88-L162
40 let mut ending = None;
42 let mut ending = None;
43+ // return first matched line ending. Not all possible line endings are being matched, as they might be special-use only
41− for line in doc.lines().take(1) {
44+ for line in doc.lines().take(100) {
42− // check first line only - unsure how sound this is
43 ending = match line.len_chars() {
45 ending = match line.len_chars() {
45 let g = RopeGraphemes::new(line.slice((line.len_chars() - 1)..))
47 let g = RopeGraphemes::new(line.slice((line.len_chars() - 1)..))
@@ -54,6 +56,9 @@ pub fn auto_detect_line_ending(doc: &Rope) -> Option<LineEnding> {
54 rope_slice_to_line_ending(&g)
56 rope_slice_to_line_ending(&g)
56 _ => None,
58 _ => None,
60+ if ending.is_some() {
@@ -62,4 +67,55 @@ pub fn auto_detect_line_ending(doc: &Rope) -> Option<LineEnding> {
62#[cfg(target_os = "windows")]
67#[cfg(target_os = "windows")]
63pub const DEFAULT_LINE_ENDING: LineEnding = LineEnding::Crlf;
68pub const DEFAULT_LINE_ENDING: LineEnding = LineEnding::Crlf;
64#[cfg(not(target_os = "windows"))]
69#[cfg(not(target_os = "windows"))]
65−pub const DEFAULT_LINE_ENDING: LineEnding = LineEnding::LF;
70+pub const DEFAULT_LINE_ENDING: LineEnding = LineEnding::LF;
73+mod line_ending_tests {
77+ fn test_autodetect() {
79+ auto_detect_line_ending(&Rope::from_str("\n")),
83+ auto_detect_line_ending(&Rope::from_str("\r\n")),
84+ Some(LineEnding::Crlf)
86+ assert_eq!(auto_detect_line_ending(&Rope::from_str("hello")), None);
87+ assert_eq!(auto_detect_line_ending(&Rope::from_str("")), None);
89+ auto_detect_line_ending(&Rope::from_str("hello\nhelix\r\n")),
93+ auto_detect_line_ending(&Rope::from_str("a formfeed\u{000C}")),
97+ auto_detect_line_ending(&Rope::from_str("\n\u{000A}\n \u{000A}")),
101+ auto_detect_line_ending(&Rope::from_str(
102+ "a formfeed\u{000C} with a\u{000C} linefeed\u{000A}"
104+ Some(LineEnding::LF)
106+ assert_eq!(auto_detect_line_ending(&Rope::from_str("a formfeed\u{000C} with a\u{000C} carriage return linefeed\u{000D}\u{000A} and a linefeed\u{000A}")), Some(LineEnding::Crlf));
110+ fn test_rope_slice_to_line_ending() {
111+ let r = Rope::from_str("\r\n");
113+ rope_slice_to_line_ending(&r.slice(1..2)),
114+ Some(LineEnding::LF)
117+ rope_slice_to_line_ending(&r.slice(0..2)),
118+ Some(LineEnding::Crlf)