on master help sign in
Address PR comments.

- Move char functions into their own module under helix_core. - Use matches!() macro where appropriate. - Use a static lifetime on indent_unit() now that we can.

commit0a5580aa21b55947859191a4f33244d77fb794ed authorNathan Vegdahl <cessen@cessen.com> dateSun 13 Jun 2021, 19:13 (5 years ago) parent358ea6a
3 files changed+44−50
helix-core/src/chars.rs+41 helix-core/src/lib.rs+1 helix-view/src/document.rs+2−50
helix-core/src/chars.rsnew file+41
@@ -0,0 +1,41 @@
1+/// Determine whether a character is a line break.
2+pub fn char_is_linebreak(c: char) -> bool {
3+ matches!(
4+ c,
5+ '\u{000A}' | // LineFeed
6+ '\u{000B}' | // VerticalTab
7+ '\u{000C}' | // FormFeed
8+ '\u{000D}' | // CarriageReturn
9+ '\u{0085}' | // NextLine
10+ '\u{2028}' | // Line Separator
11+ '\u{2029}' // ParagraphSeparator
12+ )
13+}
14+
15+/// Determine whether a character qualifies as (non-line-break)
16+/// whitespace.
17+pub fn char_is_whitespace(c: char) -> bool {
18+ // TODO: this is a naive binary categorization of whitespace
19+ // characters. For display, word wrapping, etc. we'll need a better
20+ // categorization based on e.g. breaking vs non-breaking spaces
21+ // and whether they're zero-width or not.
22+ match c {
23+ //'\u{1680}' | // Ogham Space Mark (here for completeness, but usually displayed as a dash, not as whitespace)
24+ '\u{0009}' | // Character Tabulation
25+ '\u{0020}' | // Space
26+ '\u{00A0}' | // No-break Space
27+ '\u{180E}' | // Mongolian Vowel Separator
28+ '\u{202F}' | // Narrow No-break Space
29+ '\u{205F}' | // Medium Mathematical Space
30+ '\u{3000}' | // Ideographic Space
31+ '\u{FEFF}' // Zero Width No-break Space
32+ => true,
33+
34+ // En Quad, Em Quad, En Space, Em Space, Three-per-em Space,
35+ // Four-per-em Space, Six-per-em Space, Figure Space,
36+ // Punctuation Space, Thin Space, Hair Space, Zero Width Space.
37+ c if ('\u{2000}' ..= '\u{200B}').contains(&c) => true,
38+
39+ _ => false,
40+ }
41+}
helix-core/src/lib.rs+1
@@ -1,5 +1,6 @@
11#![allow(unused)]
22pub mod auto_pairs;
3+pub mod chars;
34pub mod comment;
45pub mod diagnostic;
56pub mod graphemes;
helix-view/src/document.rs+2−50
@@ -5,6 +5,7 @@ use std::path::{Component, Path, PathBuf};
55use std::sync::Arc;
66
77use helix_core::{
8+ chars::{char_is_linebreak, char_is_whitespace},
89 history::History,
910 syntax::{LanguageConfiguration, LOADER},
1011 ChangeSet, Diagnostic, Rope, Selection, State, Syntax, Transaction,
@@ -277,55 +278,6 @@ impl Document {
277278 }
278279
279280 fn detect_indent_style(&mut self) {
280− // Determine whether a character is a line break.
281− //
282− // TODO: this is probably a generally useful utility function. Where
283− // should we put it?
284− fn char_is_linebreak(c: char) -> bool {
285− [
286− '\u{000A}', // LineFeed
287− '\u{000B}', // VerticalTab
288− '\u{000C}', // FormFeed
289− '\u{000D}', // CarriageReturn
290− '\u{0085}', // NextLine
291− '\u{2028}', // Line Separator
292− '\u{2029}', // ParagraphSeparator
293− ]
294− .contains(&c)
295− }
296−
297− // Determine whether a character qualifies as (non-line-break)
298− // whitespace.
299− //
300− // TODO: this is probably a generally useful utility function. Where
301− // should we put it?
302− //
303− // TODO: this is a naive binary categorization of whitespace
304− // characters. For display, word wrapping, etc. we'll need a better
305− // categorization based on e.g. breaking vs non-breaking spaces
306− // and whether they're zero-width or not.
307− pub fn char_is_whitespace(c: char) -> bool {
308− match c {
309− //'\u{1680}' | // Ogham Space Mark (here for completeness, but usually displayed as a dash, not as whitespace)
310− '\u{0009}' | // Character Tabulation
311− '\u{0020}' | // Space
312− '\u{00A0}' | // No-break Space
313− '\u{180E}' | // Mongolian Vowel Separator
314− '\u{202F}' | // Narrow No-break Space
315− '\u{205F}' | // Medium Mathematical Space
316− '\u{3000}' | // Ideographic Space
317− '\u{FEFF}' // Zero Width No-break Space
318− => true,
319−
320− // En Quad, Em Quad, En Space, Em Space, Three-per-em Space,
321− // Four-per-em Space, Six-per-em Space, Figure Space,
322− // Punctuation Space, Thin Space, Hair Space, Zero Width Space.
323− c if ('\u{2000}' ..= '\u{200B}').contains(&c) => true,
324−
325− _ => false,
326− }
327− }
328−
329281 // Build a histogram of the indentation *increases* between
330282 // subsequent lines, ignoring lines that are all whitespace.
331283 //
@@ -689,7 +641,7 @@ impl Document {
689641 ///
690642 /// TODO: we might not need this function anymore, since the information
691643 /// is conveniently available in `Document::indent_style` now.
692− pub fn indent_unit(&self) -> &str {
644+ pub fn indent_unit(&self) -> &'static str {
693645 match self.indent_style {
694646 IndentStyle::Tabs => "\t",
695647 IndentStyle::Spaces(1) => " ",