on master help sign in
Misc fixes and clean up of line ending detect code.
5d22e3cNathan Vegdahl
helix-core/src/lib.rs+1−2
@@ -113,7 +113,6 @@ pub use diagnostic::Diagnostic;
113pub use state::State;
113pub use state::State;
114
114
115pub use line_ending::{
115pub use line_ending::{
116− auto_detect_line_ending, get_line_ending, rope_slice_to_line_ending, LineEnding,
116+ auto_detect_line_ending, get_line_ending, line_end, LineEnding, DEFAULT_LINE_ENDING,
117− DEFAULT_LINE_ENDING, line_end
118};
117};
119pub use transaction::{Assoc, Change, ChangeSet, Operation, Transaction};
118pub use transaction::{Assoc, Change, ChangeSet, Operation, Transaction};
helix-core/src/line_ending.rs+43−56
@@ -5,11 +5,11 @@ use crate::{Rope, RopeGraphemes, RopeSlice};
5pub enum LineEnding {
5pub enum LineEnding {
6 Crlf, // CarriageReturn followed by LineFeed
6 Crlf, // CarriageReturn followed by LineFeed
7 LF, // U+000A -- LineFeed
7 LF, // U+000A -- LineFeed
8+ VT, // U+000B -- VerticalTab
9+ FF, // U+000C -- FormFeed
8 CR, // U+000D -- CarriageReturn
10 CR, // U+000D -- CarriageReturn
9 Nel, // U+0085 -- NextLine
11 Nel, // U+0085 -- NextLine
10 LS, // U+2028 -- Line Separator
12 LS, // U+2028 -- Line Separator
11− VT, // U+000B -- VerticalTab
12− FF, // U+000C -- FormFeed
13 PS, // U+2029 -- ParagraphSeparator
13 PS, // U+2029 -- ParagraphSeparator
14}
14}
15
15
@@ -21,74 +21,58 @@ impl LineEnding {
21 }
21 }
22 }
22 }
23
23
24− pub fn as_str(&self) -> &str {
24+ pub fn as_str(&self) -> &'static str {
25 match self {
25 match self {
26 Self::Crlf => "\u{000D}\u{000A}",
26 Self::Crlf => "\u{000D}\u{000A}",
27 Self::LF => "\u{000A}",
27 Self::LF => "\u{000A}",
28− Self::Nel => "\u{0085}",
29− Self::LS => "\u{2028}",
30− Self::CR => "\u{000D}",
31 Self::VT => "\u{000B}",
28 Self::VT => "\u{000B}",
32 Self::FF => "\u{000C}",
29 Self::FF => "\u{000C}",
30+ Self::CR => "\u{000D}",
31+ Self::Nel => "\u{0085}",
32+ Self::LS => "\u{2028}",
33 Self::PS => "\u{2029}",
33 Self::PS => "\u{2029}",
34 }
34 }
35 }
35 }
36−}
37
36
38−pub fn rope_slice_to_line_ending(g: &RopeSlice) -> Option<LineEnding> {
37+ pub fn from_str(g: &str) -> Option<LineEnding> {
39− if let Some(text) = g.as_str() {
38+ match g {
40− str_to_line_ending(text)
39+ "\u{000D}\u{000A}" => Some(LineEnding::Crlf),
41− }
40+ "\u{000A}" => Some(LineEnding::LF),
42− else {
41+ "\u{000B}" => Some(LineEnding::VT),
42+ "\u{000C}" => Some(LineEnding::FF),
43+ "\u{000D}" => Some(LineEnding::CR),
44+ "\u{0085}" => Some(LineEnding::Nel),
45+ "\u{2028}" => Some(LineEnding::LS),
46+ "\u{2029}" => Some(LineEnding::PS),
43− // Not a line ending
47+ // Not a line ending
44− None
48+ _ => None,
49+ }
45 }
50 }
46−}
47
51
48−pub fn str_to_line_ending(g: &str) -> Option<LineEnding> {
52+ pub fn from_rope_slice(g: &RopeSlice) -> Option<LineEnding> {
49− match g {
53+ if let Some(text) = g.as_str() {
50− "\u{000D}\u{000A}" => Some(LineEnding::Crlf),
51− "\u{000A}" => Some(LineEnding::LF),
52− "\u{000D}" => Some(LineEnding::CR),
54+ LineEnding::from_str(text)
53− "\u{0085}" => Some(LineEnding::Nel),
55+ } else {
54− "\u{2028}" => Some(LineEnding::LS),
55− "\u{000B}" => Some(LineEnding::VT),
56− "\u{000C}" => Some(LineEnding::FF),
57− "\u{2029}" => Some(LineEnding::PS),
58− // Not a line ending
56+ // Non-contiguous, so it can't be a line ending.
57+ // Specifically, Ropey guarantees that CRLF is always
58+ // contiguous. And the remaining line endings are all
59+ // single `char`s, and therefore trivially contiguous.
59− _ => None,
60+ None
61+ }
60 }
62 }
61}
63}
62
64
65+/// Attempts to detect what line ending the passed document uses.
63pub fn auto_detect_line_ending(doc: &Rope) -> Option<LineEnding> {
66pub fn auto_detect_line_ending(doc: &Rope) -> Option<LineEnding> {
64− // based on https://github.com/cessen/led/blob/27572c8838a1c664ee378a19358604063881cc1d/src/editor/mod.rs#L88-L162
65−
66− let mut ending = None;
67− // return first matched line ending. Not all possible line endings are being matched, as they might be special-use only
67+ // Return first matched line ending. Not all possible line endings
68+ // are being matched, as they might be special-use only
68 for line in doc.lines().take(100) {
69 for line in doc.lines().take(100) {
69− ending = match line.len_chars() {
70+ match get_line_ending(&line) {
70− 1 => {
71− let g = RopeGraphemes::new(line.slice((line.len_chars() - 1)..))
72− .last()
73− .unwrap();
74− rope_slice_to_line_ending(&g)
75− }
76− n if n > 1 => {
77− let g = RopeGraphemes::new(line.slice((line.len_chars() - 2)..))
78− .last()
79− .unwrap();
80− rope_slice_to_line_ending(&g)
81− }
82− _ => None,
83− };
84− if ending.is_some() {
85− match ending {
86− Some(LineEnding::VT) | Some(LineEnding::FF) | Some(LineEnding::PS) => {}
71+ None | Some(LineEnding::VT) | Some(LineEnding::FF) | Some(LineEnding::PS) => {}
87− _ => return ending,
72+ ending => return ending,
88− }
89 }
73 }
90 }
74 }
91− ending
75+ None
92}
76}
93
77
94/// Returns the passed line's line ending, if any.
78/// Returns the passed line's line ending, if any.
@@ -108,13 +92,16 @@ pub fn get_line_ending(line: &RopeSlice) -> Option<LineEnding> {
108 .unwrap_or("");
92 .unwrap_or("");
109
93
110 // First check the two-character case for CRLF, then check the single-character case.
94 // First check the two-character case for CRLF, then check the single-character case.
111− str_to_line_ending(g2).or_else(|| str_to_line_ending(g1))
95+ LineEnding::from_str(g2).or_else(|| LineEnding::from_str(g1))
112}
96}
113
97
98+/// Returns the char index of the end of the given line, not including its line ending.
114pub fn line_end(slice: &RopeSlice, line: usize) -> usize {
99pub fn line_end(slice: &RopeSlice, line: usize) -> usize {
115− slice.line_to_char(line + 1).saturating_sub(get_line_ending(&slice.line(line))
100+ slice.line_to_char(line + 1).saturating_sub(
101+ get_line_ending(&slice.line(line))
116− .map(|le| le.len_chars())
102+ .map(|le| le.len_chars())
117− .unwrap_or(0))
103+ .unwrap_or(0),
104+ )
118}
105}
119
106
120#[cfg(target_os = "windows")]
107#[cfg(target_os = "windows")]
helix-core/src/movement.rs+2−2
@@ -3,9 +3,9 @@ use std::iter::{self, from_fn, Peekable, SkipWhile};
3use ropey::iter::Chars;
3use ropey::iter::Chars;
4
4
5use crate::{
5use crate::{
6− coords_at_pos,
6+ coords_at_pos, get_line_ending,
7 graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary},
7 graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary},
8− pos_at_coords, Position, Range, RopeSlice, get_line_ending, line_end
8+ line_end, pos_at_coords, Position, Range, RopeSlice,
9};
9};
10
10
11#[derive(Debug, Copy, Clone, PartialEq, Eq)]
11#[derive(Debug, Copy, Clone, PartialEq, Eq)]
helix-term/src/ui/editor.rs+3−3
@@ -7,9 +7,9 @@ use crate::{
7};
7};
8
8
9use helix_core::{
9use helix_core::{
10− coords_at_pos, rope_slice_to_line_ending,
10+ coords_at_pos,
11 syntax::{self, HighlightEvent},
11 syntax::{self, HighlightEvent},
12− Position, Range,
12+ LineEnding, Position, Range,
13};
13};
14use helix_view::input::{KeyCode, KeyEvent, KeyModifiers};
14use helix_view::input::{KeyCode, KeyEvent, KeyModifiers};
15use helix_view::{document::Mode, Document, Editor, Theme, View};
15use helix_view::{document::Mode, Document, Editor, Theme, View};
@@ -177,7 +177,7 @@ impl EditorView {
177
177
178 // iterate over range char by char
178 // iterate over range char by char
179 for grapheme in RopeGraphemes::new(text) {
179 for grapheme in RopeGraphemes::new(text) {
180− if rope_slice_to_line_ending(&grapheme).is_some() {
180+ if LineEnding::from_rope_slice(&grapheme).is_some() {
181 visual_x = 0;
181 visual_x = 0;
182 line += 1;
182 line += 1;
183
183
helix-view/src/document.rs+1−1
@@ -255,7 +255,7 @@ impl Document {
255 use std::{fs::File, io::BufReader};
255 use std::{fs::File, io::BufReader};
256
256
257 let doc = if !path.exists() {
257 let doc = if !path.exists() {
258− Rope::from(DEFAULT_LINE_ENDING.as_str())
258+ Rope::from(DEFAULT_LINE_ENDING.as_str())
259 } else {
259 } else {
260 let file = File::open(&path).context(format!("unable to open {:?}", path))?;
260 let file = File::open(&path).context(format!("unable to open {:?}", path))?;
261 let mut doc = Rope::from_reader(BufReader::new(file))?;
261 let mut doc = Rope::from_reader(BufReader::new(file))?;