on master help sign in
Put esoteric line endings behind a feature flag
f67e1eeBlaž Hrastnik
helix-core/Cargo.toml+1−1
@@ -17,7 +17,7 @@ unicode-lines = ["ropey/unicode_lines"]
17helix-loader = { version = "0.6", path = "../helix-loader" }
17helix-loader = { version = "0.6", path = "../helix-loader" }
18
18
19# ropey = "1.3"
19# ropey = "1.3"
20−ropey = { git = "https://github.com/cessen/ropey", default-features = false, features = ["cr_lines"] }
20+ropey = { git = "https://github.com/cessen/ropey", default-features = false }
21smallvec = "1.8"
21smallvec = "1.8"
22smartstring = "1.0.0"
22smartstring = "1.0.0"
23unicode-segmentation = "1.9"
23unicode-segmentation = "1.9"
helix-core/src/line_ending.rs+50−7
@@ -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
19}
25}
20
26
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}",
41 }
53 }
42 }
54 }
@@ -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 {
65 match g {
83 match g {
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) {
128+ None => {}
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,
106 }
132 }
107 }
133 }
@@ -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))
129}
155}
130
156
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)
164+ } else {
165+ None
166+ }
167+}
168+
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 {
211
250
212 #[test]
251 #[test]
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 {
220 #[test]
260 #[test]
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 {
238 #[test]
279 #[test]
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 {
247 #[test]
289 #[test]
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));
helix-term/src/commands/typed.rs+7
@@ -288,11 +288,15 @@ fn set_line_ending(
288 cx.editor.set_status(match line_ending {
288 cx.editor.set_status(match line_ending {
289 Crlf => "crlf",
289 Crlf => "crlf",
290 LF => "line feed",
290 LF => "line feed",
291+ #[cfg(feature = "unicode-lines")]
291 FF => "form feed",
292 FF => "form feed",
293+ #[cfg(feature = "unicode-lines")]
292 CR => "carriage return",
294 CR => "carriage return",
295+ #[cfg(feature = "unicode-lines")]
293 Nel => "next line",
296 Nel => "next line",
294
297
295 // These should never be a document's default line ending.
298 // These should never be a document's default line ending.
299+ #[cfg(feature = "unicode-lines")]
296 VT | LS | PS => "error",
300 VT | LS | PS => "error",
297 });
301 });
298
302
@@ -307,10 +311,13 @@ fn set_line_ending(
307 // Attempt to parse argument as a line ending.
311 // Attempt to parse argument as a line ending.
308 let line_ending = match arg {
312 let line_ending = match arg {
309 // We check for CR first because it shares a common prefix with CRLF.
313 // We check for CR first because it shares a common prefix with CRLF.
314+ #[cfg(feature = "unicode-lines")]
310 arg if arg.starts_with("cr") => CR,
315 arg if arg.starts_with("cr") => CR,
311 arg if arg.starts_with("crlf") => Crlf,
316 arg if arg.starts_with("crlf") => Crlf,
312 arg if arg.starts_with("lf") => LF,
317 arg if arg.starts_with("lf") => LF,
318+ #[cfg(feature = "unicode-lines")]
313 arg if arg.starts_with("ff") => FF,
319 arg if arg.starts_with("ff") => FF,
320+ #[cfg(feature = "unicode-lines")]
314 arg if arg.starts_with("nel") => Nel,
321 arg if arg.starts_with("nel") => Nel,
315 _ => bail!("invalid line ending"),
322 _ => bail!("invalid line ending"),
316 };
323 };