@@ -10,12 +10,18 @@ pub const DEFAULT_LINE_ENDING: LineEnding = LineEnding::LF;
10pub enum LineEnding {
10pub enum LineEnding {
11 Crlf, // CarriageReturn followed by LineFeed
11 Crlf, // CarriageReturn followed by LineFeed
12 LF, // U+000A -- LineFeed
12 LF, // U+000A -- LineFeed
13+ #[cfg(feature = "unicode-lines")]
13− VT, // U+000B -- VerticalTab
14+ VT, // U+000B -- VerticalTab
15+ #[cfg(feature = "unicode-lines")]
14− FF, // U+000C -- FormFeed
16+ FF, // U+000C -- FormFeed
17+ #[cfg(feature = "unicode-lines")]
15− CR, // U+000D -- CarriageReturn
18+ CR, // U+000D -- CarriageReturn
19+ #[cfg(feature = "unicode-lines")]
16− Nel, // U+0085 -- NextLine
20+ Nel, // U+0085 -- NextLine
21+ #[cfg(feature = "unicode-lines")]
17− LS, // U+2028 -- Line Separator
22+ LS, // U+2028 -- Line Separator
23+ #[cfg(feature = "unicode-lines")]
18− PS, // U+2029 -- ParagraphSeparator
24+ PS, // U+2029 -- ParagraphSeparator
21impl LineEnding {
27impl LineEnding {
@@ -32,11 +38,17 @@ impl LineEnding {
32 match self {
38 match self {
33 Self::Crlf => "\u{000D}\u{000A}",
39 Self::Crlf => "\u{000D}\u{000A}",
34 Self::LF => "\u{000A}",
40 Self::LF => "\u{000A}",
41+ #[cfg(feature = "unicode-lines")]
35 Self::VT => "\u{000B}",
42 Self::VT => "\u{000B}",
43+ #[cfg(feature = "unicode-lines")]
36 Self::FF => "\u{000C}",
44 Self::FF => "\u{000C}",
45+ #[cfg(feature = "unicode-lines")]
37 Self::CR => "\u{000D}",
46 Self::CR => "\u{000D}",
47+ #[cfg(feature = "unicode-lines")]
38 Self::Nel => "\u{0085}",
48 Self::Nel => "\u{0085}",
49+ #[cfg(feature = "unicode-lines")]
39 Self::LS => "\u{2028}",
50 Self::LS => "\u{2028}",
51+ #[cfg(feature = "unicode-lines")]
40 Self::PS => "\u{2029}",
52 Self::PS => "\u{2029}",
@@ -45,11 +57,17 @@ impl LineEnding {
45 pub const fn from_char(ch: char) -> Option<LineEnding> {
57 pub const fn from_char(ch: char) -> Option<LineEnding> {
46 match ch {
58 match ch {
47 '\u{000A}' => Some(LineEnding::LF),
59 '\u{000A}' => Some(LineEnding::LF),
60+ #[cfg(feature = "unicode-lines")]
48 '\u{000B}' => Some(LineEnding::VT),
61 '\u{000B}' => Some(LineEnding::VT),
62+ #[cfg(feature = "unicode-lines")]
49 '\u{000C}' => Some(LineEnding::FF),
63 '\u{000C}' => Some(LineEnding::FF),
64+ #[cfg(feature = "unicode-lines")]
50 '\u{000D}' => Some(LineEnding::CR),
65 '\u{000D}' => Some(LineEnding::CR),
66+ #[cfg(feature = "unicode-lines")]
51 '\u{0085}' => Some(LineEnding::Nel),
67 '\u{0085}' => Some(LineEnding::Nel),
68+ #[cfg(feature = "unicode-lines")]
52 '\u{2028}' => Some(LineEnding::LS),
69 '\u{2028}' => Some(LineEnding::LS),
70+ #[cfg(feature = "unicode-lines")]
53 '\u{2029}' => Some(LineEnding::PS),
71 '\u{2029}' => Some(LineEnding::PS),
54 // Not a line ending
72 // Not a line ending
55 _ => None,
73 _ => None,
@@ -65,11 +83,17 @@ impl LineEnding {
66 "\u{000D}\u{000A}" => Some(LineEnding::Crlf),
84 "\u{000D}\u{000A}" => Some(LineEnding::Crlf),
67 "\u{000A}" => Some(LineEnding::LF),
85 "\u{000A}" => Some(LineEnding::LF),
86+ #[cfg(feature = "unicode-lines")]
68 "\u{000B}" => Some(LineEnding::VT),
87 "\u{000B}" => Some(LineEnding::VT),
88+ #[cfg(feature = "unicode-lines")]
69 "\u{000C}" => Some(LineEnding::FF),
89 "\u{000C}" => Some(LineEnding::FF),
90+ #[cfg(feature = "unicode-lines")]
70 "\u{000D}" => Some(LineEnding::CR),
91 "\u{000D}" => Some(LineEnding::CR),
92+ #[cfg(feature = "unicode-lines")]
71 "\u{0085}" => Some(LineEnding::Nel),
93 "\u{0085}" => Some(LineEnding::Nel),
94+ #[cfg(feature = "unicode-lines")]
72 "\u{2028}" => Some(LineEnding::LS),
95 "\u{2028}" => Some(LineEnding::LS),
96+ #[cfg(feature = "unicode-lines")]
73 "\u{2029}" => Some(LineEnding::PS),
97 "\u{2029}" => Some(LineEnding::PS),
74 // Not a line ending
98 // Not a line ending
75 _ => None,
99 _ => None,
@@ -101,7 +125,9 @@ pub fn auto_detect_line_ending(doc: &Rope) -> Option<LineEnding> {
101 // are being matched, as they might be special-use only
125 // are being matched, as they might be special-use only
102 for line in doc.lines().take(100) {
126 for line in doc.lines().take(100) {
103 match get_line_ending(&line) {
127 match get_line_ending(&line) {
129+ #[cfg(feature = "unicode-lines")]
104− None | Some(LineEnding::VT) | Some(LineEnding::FF) | Some(LineEnding::PS) => {}
130+ Some(LineEnding::VT) | Some(LineEnding::FF) | Some(LineEnding::PS) => {}
105 ending => return ending,
131 ending => return ending,
@@ -128,6 +154,19 @@ pub fn get_line_ending(line: &RopeSlice) -> Option<LineEnding> {
128 LineEnding::from_str(g2).or_else(|| LineEnding::from_str(g1))
154 LineEnding::from_str(g2).or_else(|| LineEnding::from_str(g1))
157+#[cfg(not(feature = "unicode-lines"))]
158+/// Returns the passed line's line ending, if any.
159+pub fn get_line_ending_of_str(line: &str) -> Option<LineEnding> {
160+ if line.ends_with("\u{000D}\u{000A}") {
161+ Some(LineEnding::Crlf)
162+ } else if line.ends_with('\u{000A}') {
163+ Some(LineEnding::LF)
169+#[cfg(feature = "unicode-lines")]
131/// Returns the passed line's line ending, if any.
170/// Returns the passed line's line ending, if any.
132pub fn get_line_ending_of_str(line: &str) -> Option<LineEnding> {
171pub fn get_line_ending_of_str(line: &str) -> Option<LineEnding> {
133 if line.ends_with("\u{000D}\u{000A}") {
172 if line.ends_with("\u{000D}\u{000A}") {
@@ -211,6 +250,7 @@ mod line_ending_tests {
213 fn str_to_line_ending() {
252 fn str_to_line_ending() {
253+ #[cfg(feature = "unicode-lines")]
214 assert_eq!(LineEnding::from_str("\r"), Some(LineEnding::CR));
254 assert_eq!(LineEnding::from_str("\r"), Some(LineEnding::CR));
215 assert_eq!(LineEnding::from_str("\n"), Some(LineEnding::LF));
255 assert_eq!(LineEnding::from_str("\n"), Some(LineEnding::LF));
216 assert_eq!(LineEnding::from_str("\r\n"), Some(LineEnding::Crlf));
256 assert_eq!(LineEnding::from_str("\r\n"), Some(LineEnding::Crlf));
@@ -220,6 +260,7 @@ mod line_ending_tests {
221 fn rope_slice_to_line_ending() {
261 fn rope_slice_to_line_ending() {
222 let r = Rope::from_str("hello\r\n");
262 let r = Rope::from_str("hello\r\n");
263+ #[cfg(feature = "unicode-lines")]
223 assert_eq!(
264 assert_eq!(
224 LineEnding::from_rope_slice(&r.slice(5..6)),
265 LineEnding::from_rope_slice(&r.slice(5..6)),
225 Some(LineEnding::CR)
266 Some(LineEnding::CR)
@@ -238,6 +279,7 @@ mod line_ending_tests {
239 fn get_line_ending_rope_slice() {
280 fn get_line_ending_rope_slice() {
240 let r = Rope::from_str("Hello\rworld\nhow\r\nare you?");
281 let r = Rope::from_str("Hello\rworld\nhow\r\nare you?");
282+ #[cfg(feature = "unicode-lines")]
241 assert_eq!(get_line_ending(&r.slice(..6)), Some(LineEnding::CR));
283 assert_eq!(get_line_ending(&r.slice(..6)), Some(LineEnding::CR));
242 assert_eq!(get_line_ending(&r.slice(..12)), Some(LineEnding::LF));
284 assert_eq!(get_line_ending(&r.slice(..12)), Some(LineEnding::LF));
243 assert_eq!(get_line_ending(&r.slice(..17)), Some(LineEnding::Crlf));
285 assert_eq!(get_line_ending(&r.slice(..17)), Some(LineEnding::Crlf));
@@ -247,6 +289,7 @@ mod line_ending_tests {
248 fn get_line_ending_str() {
290 fn get_line_ending_str() {
249 let text = "Hello\rworld\nhow\r\nare you?";
291 let text = "Hello\rworld\nhow\r\nare you?";
292+ #[cfg(feature = "unicode-lines")]
250 assert_eq!(get_line_ending_of_str(&text[..6]), Some(LineEnding::CR));
293 assert_eq!(get_line_ending_of_str(&text[..6]), Some(LineEnding::CR));
251 assert_eq!(get_line_ending_of_str(&text[..12]), Some(LineEnding::LF));
294 assert_eq!(get_line_ending_of_str(&text[..12]), Some(LineEnding::LF));
252 assert_eq!(get_line_ending_of_str(&text[..17]), Some(LineEnding::Crlf));
295 assert_eq!(get_line_ending_of_str(&text[..17]), Some(LineEnding::Crlf));