on master help sign in
rebase on branch line_ending_detection
commit3756c21baefa6182beaa9a3d5ced9d720cf9adcb authorJan Hrastnik <jan.hrastnik2@gmail.com> dateWed 16 Jun 2021, 17:00 (5 years ago) parenta364d6c
5 files changed+79−6
helix-core/src/lib.rs+1 helix-term/src/ui/editor.rs+1−1 helix-view/src/document.rs+74−3 helix-view/src/editor.rs+2−2 helix-view/src/lib.rs+1
helix-core/src/lib.rs+1
@@ -104,6 +104,7 @@ pub use position::{coords_at_pos, pos_at_coords, Position};
104104pub use selection::{Range, Selection};
105105pub use smallvec::SmallVec;
106106pub use syntax::Syntax;
107+pub use graphemes::RopeGraphemes;
107108
108109pub use diagnostic::Diagnostic;
109110pub use state::State;
helix-term/src/ui/editor.rs+1−1
@@ -179,7 +179,7 @@ impl EditorView {
179179
180180 // iterate over range char by char
181181 for grapheme in RopeGraphemes::new(text) {
182− if grapheme == "\n" {
182+ if grapheme == "\n" || grapheme == "\r\n" {
183183 visual_x = 0;
184184 line += 1;
185185
helix-view/src/document.rs+74−3
@@ -8,7 +8,7 @@ use helix_core::{
88 chars::{char_is_linebreak, char_is_whitespace},
99 history::History,
1010 syntax::{LanguageConfiguration, LOADER},
11− ChangeSet, Diagnostic, Rope, Selection, State, Syntax, Transaction,
11+ ChangeSet, Diagnostic, History, Rope, RopeSlice, RopeGraphemes, Selection, State, Syntax, Transaction,
1212};
1313
1414use crate::{DocumentId, ViewId};
@@ -22,12 +22,28 @@ pub enum Mode {
2222 Insert,
2323}
2424
25+
2526#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
2627pub enum IndentStyle {
2728 Tabs,
2829 Spaces(u8),
2930}
3031
32+/// Represents one of the valid Unicode line endings.
33+/// Also acts as an index into `LINE_ENDINGS`.
34+#[derive(PartialEq, Copy, Clone, Debug)]
35+pub enum LineEnding {
36+ None = 0, // No line ending
37+ CRLF = 1, // CarriageReturn followed by LineFeed
38+ LF = 2, // U+000A -- LineFeed
39+ VT = 3, // U+000B -- VerticalTab
40+ FF = 4, // U+000C -- FormFeed
41+ CR = 5, // U+000D -- CarriageReturn
42+ NEL = 6, // U+0085 -- NextLine
43+ LS = 7, // U+2028 -- Line Separator
44+ PS = 8, // U+2029 -- ParagraphSeparator
45+}
46+
3147pub struct Document {
3248 // rope + selection
3349 pub(crate) id: DocumentId,
@@ -61,6 +77,7 @@ pub struct Document {
6177
6278 diagnostics: Vec<Diagnostic>,
6379 language_server: Option<Arc<helix_lsp::Client>>,
80+ line_ending: LineEnding
6481}
6582
6683use std::fmt;
@@ -146,11 +163,61 @@ pub fn canonicalize_path(path: &Path) -> std::io::Result<PathBuf> {
146163 std::env::current_dir().map(|current_dir| normalize_path(&current_dir.join(path)))
147164}
148165
166+pub fn auto_detect_line_ending(doc: &Rope) -> LineEnding {
167+ // based on https://github.com/cessen/led/blob/27572c8838a1c664ee378a19358604063881cc1d/src/editor/mod.rs#L88-L162
168+
169+ let mut ending = LineEnding::None;
170+ for line in doc.lines().take(1) { // check first line only - unsure how sound this is
171+ // Get the line ending
172+ ending = if line.len_chars() == 1 {
173+ let g = RopeGraphemes::new(line.slice((line.len_chars() - 1)..))
174+ .last()
175+ .unwrap();
176+ rope_slice_to_line_ending(&g)
177+ } else if line.len_chars() > 1 {
178+ let g = RopeGraphemes::new(line.slice((line.len_chars() - 2)..))
179+ .last()
180+ .unwrap();
181+ rope_slice_to_line_ending(&g)
182+ } else {
183+ LineEnding::None
184+ };
185+ }
186+ ending
187+}
188+
189+pub fn rope_slice_to_line_ending(g: &RopeSlice) -> LineEnding {
190+ if let Some(text) = g.as_str() {
191+ str_to_line_ending(text)
192+ } else if g == "\u{000D}\u{000A}" {
193+ LineEnding::CRLF
194+ } else {
195+ // Not a line ending
196+ LineEnding::None
197+ }
198+}
199+
200+pub fn str_to_line_ending(g: &str) -> LineEnding {
201+ match g {
202+ "\u{000D}\u{000A}" => LineEnding::CRLF,
203+ "\u{000A}" => LineEnding::LF,
204+ "\u{000B}" => LineEnding::VT,
205+ "\u{000C}" => LineEnding::FF,
206+ "\u{000D}" => LineEnding::CR,
207+ "\u{0085}" => LineEnding::NEL,
208+ "\u{2028}" => LineEnding::LS,
209+ "\u{2029}" => LineEnding::PS,
210+
211+ // Not a line ending
212+ _ => LineEnding::None,
213+ }
214+}
215+
149216use helix_lsp::lsp;
150217use url::Url;
151218
152219impl Document {
153− pub fn new(text: Rope) -> Self {
220+ pub fn new(text: Rope, line_ending: LineEnding) -> Self {
154221 let changes = ChangeSet::new(&text);
155222 let old_state = None;
156223
@@ -171,6 +238,7 @@ impl Document {
171238 history: Cell::new(History::default()),
172239 last_saved_revision: 0,
173240 language_server: None,
241+ line_ending: line_ending
174242 }
175243 }
176244
@@ -190,7 +258,10 @@ impl Document {
190258 doc
191259 };
192260
193− let mut doc = Self::new(doc);
261+ // search for line endings
262+ let line_ending = auto_detect_line_ending(&doc);
263+
264+ let mut doc = Self::new(doc, line_ending);
194265 // set the path and try detecting the language
195266 doc.set_path(&path)?;
196267 doc.detect_indent_style();
helix-view/src/editor.rs+2−2
@@ -1,4 +1,4 @@
1−use crate::{theme::Theme, tree::Tree, Document, DocumentId, RegisterSelection, View, ViewId};
1+use crate::{theme::Theme, tree::Tree, Document, DocumentId, RegisterSelection, View, ViewId, LineEnding};
22use tui::layout::Rect;
33use tui::terminal::CursorKind;
44
@@ -148,7 +148,7 @@ impl Editor {
148148
149149 pub fn new_file(&mut self, action: Action) -> DocumentId {
150150 use helix_core::Rope;
151− let doc = Document::new(Rope::from("\n"));
151+ let doc = Document::new(Rope::from("\n"), LineEnding::LF);
152152 let id = self.documents.insert(doc);
153153 self.documents[id].id = id;
154154 self.switch(id, action);
helix-view/src/lib.rs+1
@@ -10,6 +10,7 @@ new_key_type! { pub struct DocumentId; }
1010new_key_type! { pub struct ViewId; }
1111
1212pub use document::Document;
13+pub use document::LineEnding;
1314pub use editor::Editor;
1415pub use register_selection::RegisterSelection;
1516pub use theme::Theme;