on master help sign in
Put esoteric line endings behind a feature flag
commitf67e1ee1723592a4e6279197317d46759cd05976 authorBlaž Hrastnik <blaz@mxxn.io> dateWed 16 Mar 2022, 18:02 (5 years ago) parentb4a282f
3 files changed+58−8
helix-core/Cargo.toml+1−1 helix-core/src/line_ending.rs+50−7 helix-term/src/commands/typed.rs+7
helix-core/Cargo.toml+1−1
@@ -17,7 +17,7 @@ unicode-lines = ["ropey/unicode_lines"]
1717helix-loader = { version = "0.6", path = "../helix-loader" }
1818
1919# 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 }
2121smallvec = "1.8"
2222smartstring = "1.0.0"
2323unicode-segmentation = "1.9"
helix-core/src/line_ending.rs+50−7
@@ -10,12 +10,18 @@ pub const DEFAULT_LINE_ENDING: LineEnding = LineEnding::LF;
1010pub enum LineEnding {
1111 Crlf, // CarriageReturn followed by LineFeed
1212 LF, // U+000A -- LineFeed
13− VT, // U+000B -- VerticalTab
14− FF, // U+000C -- FormFeed
15− CR, // U+000D -- CarriageReturn
16− Nel, // U+0085 -- NextLine
17− LS, // U+2028 -- Line Separator
18− PS, // U+2029 -- ParagraphSeparator
13+ #[cfg(feature = "unicode-lines")]
14+ VT, // U+000B -- VerticalTab
15+ #[cfg(feature = "unicode-lines")]
16+ FF, // U+000C -- FormFeed
17+ #[cfg(feature = "unicode-lines")]
18+ CR, // U+000D -- CarriageReturn
19+ #[cfg(feature = "unicode-lines")]
20+ Nel, // U+0085 -- NextLine
21+ #[cfg(feature = "unicode-lines")]
22+ LS, // U+2028 -- Line Separator
23+ #[cfg(feature = "unicode-lines")]
24+ PS, // U+2029 -- ParagraphSeparator
1925}
2026
2127impl LineEnding {
@@ -32,11 +38,17 @@ impl LineEnding {
3238 match self {
3339 Self::Crlf => "\u{000D}\u{000A}",
3440 Self::LF => "\u{000A}",
41+ #[cfg(feature = "unicode-lines")]
3542 Self::VT => "\u{000B}",
43+ #[cfg(feature = "unicode-lines")]
3644 Self::FF => "\u{000C}",
45+ #[cfg(feature = "unicode-lines")]
3746 Self::CR => "\u{000D}",
47+ #[cfg(feature = "unicode-lines")]
3848 Self::Nel => "\u{0085}",
49+ #[cfg(feature = "unicode-lines")]
3950 Self::LS => "\u{2028}",
51+ #[cfg(feature = "unicode-lines")]
4052 Self::PS => "\u{2029}",
4153 }
4254 }
@@ -45,11 +57,17 @@ impl LineEnding {
4557 pub const fn from_char(ch: char) -> Option<LineEnding> {
4658 match ch {
4759 '\u{000A}' => Some(LineEnding::LF),
60+ #[cfg(feature = "unicode-lines")]
4861 '\u{000B}' => Some(LineEnding::VT),
62+ #[cfg(feature = "unicode-lines")]
4963 '\u{000C}' => Some(LineEnding::FF),
64+ #[cfg(feature = "unicode-lines")]
5065 '\u{000D}' => Some(LineEnding::CR),
66+ #[cfg(feature = "unicode-lines")]
5167 '\u{0085}' => Some(LineEnding::Nel),
68+ #[cfg(feature = "unicode-lines")]
5269 '\u{2028}' => Some(LineEnding::LS),
70+ #[cfg(feature = "unicode-lines")]
5371 '\u{2029}' => Some(LineEnding::PS),
5472 // Not a line ending
5573 _ => None,
@@ -65,11 +83,17 @@ impl LineEnding {
6583 match g {
6684 "\u{000D}\u{000A}" => Some(LineEnding::Crlf),
6785 "\u{000A}" => Some(LineEnding::LF),
86+ #[cfg(feature = "unicode-lines")]
6887 "\u{000B}" => Some(LineEnding::VT),
88+ #[cfg(feature = "unicode-lines")]
6989 "\u{000C}" => Some(LineEnding::FF),
90+ #[cfg(feature = "unicode-lines")]
7091 "\u{000D}" => Some(LineEnding::CR),
92+ #[cfg(feature = "unicode-lines")]
7193 "\u{0085}" => Some(LineEnding::Nel),
94+ #[cfg(feature = "unicode-lines")]
7295 "\u{2028}" => Some(LineEnding::LS),
96+ #[cfg(feature = "unicode-lines")]
7397 "\u{2029}" => Some(LineEnding::PS),
7498 // Not a line ending
7599 _ => None,
@@ -101,7 +125,9 @@ pub fn auto_detect_line_ending(doc: &Rope) -> Option<LineEnding> {
101125 // are being matched, as they might be special-use only
102126 for line in doc.lines().take(100) {
103127 match get_line_ending(&line) {
104− None | Some(LineEnding::VT) | Some(LineEnding::FF) | Some(LineEnding::PS) => {}
128+ None => {}
129+ #[cfg(feature = "unicode-lines")]
130+ Some(LineEnding::VT) | Some(LineEnding::FF) | Some(LineEnding::PS) => {}
105131 ending => return ending,
106132 }
107133 }
@@ -128,6 +154,19 @@ pub fn get_line_ending(line: &RopeSlice) -> Option<LineEnding> {
128154 LineEnding::from_str(g2).or_else(|| LineEnding::from_str(g1))
129155}
130156
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")]
131170/// Returns the passed line's line ending, if any.
132171pub fn get_line_ending_of_str(line: &str) -> Option<LineEnding> {
133172 if line.ends_with("\u{000D}\u{000A}") {
@@ -211,6 +250,7 @@ mod line_ending_tests {
211250
212251 #[test]
213252 fn str_to_line_ending() {
253+ #[cfg(feature = "unicode-lines")]
214254 assert_eq!(LineEnding::from_str("\r"), Some(LineEnding::CR));
215255 assert_eq!(LineEnding::from_str("\n"), Some(LineEnding::LF));
216256 assert_eq!(LineEnding::from_str("\r\n"), Some(LineEnding::Crlf));
@@ -220,6 +260,7 @@ mod line_ending_tests {
220260 #[test]
221261 fn rope_slice_to_line_ending() {
222262 let r = Rope::from_str("hello\r\n");
263+ #[cfg(feature = "unicode-lines")]
223264 assert_eq!(
224265 LineEnding::from_rope_slice(&r.slice(5..6)),
225266 Some(LineEnding::CR)
@@ -238,6 +279,7 @@ mod line_ending_tests {
238279 #[test]
239280 fn get_line_ending_rope_slice() {
240281 let r = Rope::from_str("Hello\rworld\nhow\r\nare you?");
282+ #[cfg(feature = "unicode-lines")]
241283 assert_eq!(get_line_ending(&r.slice(..6)), Some(LineEnding::CR));
242284 assert_eq!(get_line_ending(&r.slice(..12)), Some(LineEnding::LF));
243285 assert_eq!(get_line_ending(&r.slice(..17)), Some(LineEnding::Crlf));
@@ -247,6 +289,7 @@ mod line_ending_tests {
247289 #[test]
248290 fn get_line_ending_str() {
249291 let text = "Hello\rworld\nhow\r\nare you?";
292+ #[cfg(feature = "unicode-lines")]
250293 assert_eq!(get_line_ending_of_str(&text[..6]), Some(LineEnding::CR));
251294 assert_eq!(get_line_ending_of_str(&text[..12]), Some(LineEnding::LF));
252295 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(
288288 cx.editor.set_status(match line_ending {
289289 Crlf => "crlf",
290290 LF => "line feed",
291+ #[cfg(feature = "unicode-lines")]
291292 FF => "form feed",
293+ #[cfg(feature = "unicode-lines")]
292294 CR => "carriage return",
295+ #[cfg(feature = "unicode-lines")]
293296 Nel => "next line",
294297
295298 // These should never be a document's default line ending.
299+ #[cfg(feature = "unicode-lines")]
296300 VT | LS | PS => "error",
297301 });
298302
@@ -307,10 +311,13 @@ fn set_line_ending(
307311 // Attempt to parse argument as a line ending.
308312 let line_ending = match arg {
309313 // We check for CR first because it shares a common prefix with CRLF.
314+ #[cfg(feature = "unicode-lines")]
310315 arg if arg.starts_with("cr") => CR,
311316 arg if arg.starts_with("crlf") => Crlf,
312317 arg if arg.starts_with("lf") => LF,
318+ #[cfg(feature = "unicode-lines")]
313319 arg if arg.starts_with("ff") => FF,
320+ #[cfg(feature = "unicode-lines")]
314321 arg if arg.starts_with("nel") => Nel,
315322 _ => bail!("invalid line ending"),
316323 };