From c2693db2077de9bfa511a9629ce602ba06c42c41 Mon Sep 17 00:00:00 2001 From: Jan Hrastnik Date: Sat, 12 Jun 2021 15:55:57 +0200 Subject: [PATCH] applied some of the changes suggested in the pr review of line_ending_detection --- helix-core/src/lib.rs | 2 + helix-core/src/line_ending.rs | 74 ++++++++++++++++++++++++++++++++ helix-view/src/document.rs | 81 ++++++----------------------------- helix-view/src/editor.rs | 8 ++-- helix-view/src/lib.rs | 1 - 5 files changed, 91 insertions(+), 75 deletions(-) create mode 100644 helix-core/src/line_ending.rs diff --git a/helix-core/src/lib.rs b/helix-core/src/lib.rs index c9a561aac83d..8bc901a73240 100644 --- a/helix-core/src/lib.rs +++ b/helix-core/src/lib.rs @@ -5,6 +5,7 @@ pub mod diagnostic; pub mod graphemes; mod history; pub mod indent; +pub mod line_ending; pub mod macros; pub mod match_brackets; pub mod movement; @@ -117,4 +118,5 @@ pub use diagnostic::Diagnostic; pub use history::History; pub use state::State; +pub use line_ending::{auto_detect_line_ending, default_line_ending, LineEnding}; pub use transaction::{Assoc, Change, ChangeSet, Operation, Transaction}; diff --git a/helix-core/src/line_ending.rs b/helix-core/src/line_ending.rs new file mode 100644 index 000000000000..8e9b2ec04daa --- /dev/null +++ b/helix-core/src/line_ending.rs @@ -0,0 +1,74 @@ +use crate::{Rope, RopeGraphemes, RopeSlice}; + +/// Represents one of the valid Unicode line endings. +#[derive(PartialEq, Copy, Clone, Debug)] +pub enum LineEnding { + Crlf, // CarriageReturn followed by LineFeed + LF, // U+000A -- LineFeed + VT, // U+000B -- VerticalTab + FF, // U+000C -- FormFeed + CR, // U+000D -- CarriageReturn + Nel, // U+0085 -- NextLine + LS, // U+2028 -- Line Separator + PS, // U+2029 -- ParagraphSeparator +} + +pub fn rope_slice_to_line_ending(g: &RopeSlice) -> Option { + if let Some(text) = g.as_str() { + str_to_line_ending(text) + } else if g == "\u{000D}\u{000A}" { + Some(LineEnding::Crlf) + } else { + // Not a line ending + None + } +} + +pub fn str_to_line_ending(g: &str) -> Option { + match g { + "\u{000D}\u{000A}" => Some(LineEnding::Crlf), + "\u{000A}" => Some(LineEnding::LF), + "\u{000B}" => Some(LineEnding::VT), + "\u{000C}" => Some(LineEnding::FF), + "\u{000D}" => Some(LineEnding::CR), + "\u{0085}" => Some(LineEnding::Nel), + "\u{2028}" => Some(LineEnding::LS), + "\u{2029}" => Some(LineEnding::PS), + + // Not a line ending + _ => None, + } +} + +pub fn auto_detect_line_ending(doc: &Rope) -> Option { + // based on https://github.com/cessen/led/blob/27572c8838a1c664ee378a19358604063881cc1d/src/editor/mod.rs#L88-L162 + + let mut ending = None; + for line in doc.lines().take(1) { + // check first line only - unsure how sound this is + ending = match line.len_chars() { + 1 => { + let g = RopeGraphemes::new(line.slice((line.len_chars() - 1)..)) + .last() + .unwrap(); + rope_slice_to_line_ending(&g) + } + n if n > 1 => { + let g = RopeGraphemes::new(line.slice((line.len_chars() - 2)..)) + .last() + .unwrap(); + rope_slice_to_line_ending(&g) + } + _ => None, + } + } + ending +} + +pub fn default_line_ending() -> Option { + if cfg!(windows) { + Some(LineEnding::Crlf) + } else { + Some(LineEnding::LF) + } +} diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 97a43d01025f..2b6bef3a4cf5 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -5,9 +5,9 @@ use std::path::{Component, Path, PathBuf}; use std::sync::Arc; use helix_core::{ + auto_detect_line_ending, default_line_ending, syntax::{LanguageConfiguration, LOADER}, - ChangeSet, Diagnostic, History, Rope, RopeGraphemes, RopeSlice, Selection, State, Syntax, - Transaction, + ChangeSet, Diagnostic, History, LineEnding, Rope, Selection, State, Syntax, Transaction, }; use crate::{DocumentId, ViewId}; @@ -21,21 +21,6 @@ pub enum Mode { Insert, } -/// Represents one of the valid Unicode line endings. -/// Also acts as an index into `LINE_ENDINGS`. -#[derive(PartialEq, Copy, Clone, Debug)] -pub enum LineEnding { - None = 0, // No line ending - Crlf = 1, // CarriageReturn followed by LineFeed - LF = 2, // U+000A -- LineFeed - VT = 3, // U+000B -- VerticalTab - FF = 4, // U+000C -- FormFeed - CR = 5, // U+000D -- CarriageReturn - Nel = 6, // U+0085 -- NextLine - LS = 7, // U+2028 -- Line Separator - PS = 8, // U+2029 -- ParagraphSeparator -} - pub struct Document { // rope + selection pub(crate) id: DocumentId, @@ -66,7 +51,7 @@ pub struct Document { diagnostics: Vec, language_server: Option>, - _line_ending: LineEnding, + line_ending: Option, } /// Like std::mem::replace() except it allows the replacement value to be mapped from the @@ -129,61 +114,14 @@ pub fn canonicalize_path(path: &Path) -> std::io::Result { std::env::current_dir().map(|current_dir| normalize_path(¤t_dir.join(path))) } -pub fn auto_detect_line_ending(doc: &Rope) -> LineEnding { - // based on https://github.com/cessen/led/blob/27572c8838a1c664ee378a19358604063881cc1d/src/editor/mod.rs#L88-L162 - - let mut ending = LineEnding::None; - for line in doc.lines().take(1) { // check first line only - unsure how sound this is - ending = match line.len_chars() { - 1 => { let g = RopeGraphemes::new(line.slice((line.len_chars() - 1)..)) - .last() - .unwrap(); - rope_slice_to_line_ending(&g)} - n if n > 1 => { let g = RopeGraphemes::new(line.slice((line.len_chars() - 2)..)) - .last() - .unwrap(); - rope_slice_to_line_ending(&g) } - _ => LineEnding::None - - } - } - ending -} - -pub fn rope_slice_to_line_ending(g: &RopeSlice) -> LineEnding { - if let Some(text) = g.as_str() { - str_to_line_ending(text) - } else if g == "\u{000D}\u{000A}" { - LineEnding::Crlf - } else { - // Not a line ending - LineEnding::None - } -} - -pub fn str_to_line_ending(g: &str) -> LineEnding { - match g { - "\u{000D}\u{000A}" => LineEnding::Crlf, - "\u{000A}" => LineEnding::LF, - "\u{000B}" => LineEnding::VT, - "\u{000C}" => LineEnding::FF, - "\u{000D}" => LineEnding::CR, - "\u{0085}" => LineEnding::Nel, - "\u{2028}" => LineEnding::LS, - "\u{2029}" => LineEnding::PS, - - // Not a line ending - _ => LineEnding::None, - } -} - use helix_lsp::lsp; use url::Url; impl Document { - pub fn new(text: Rope, _line_ending: LineEnding) -> Self { + pub fn new(text: Rope) -> Self { let changes = ChangeSet::new(&text); let old_state = None; + let line_ending = default_line_ending(); Self { id: DocumentId::default(), @@ -201,7 +139,7 @@ impl Document { history: Cell::new(History::default()), last_saved_revision: 0, language_server: None, - _line_ending + line_ending, } } @@ -219,9 +157,10 @@ impl Document { // search for line endings let line_ending = auto_detect_line_ending(&doc); - let mut doc = Self::new(doc, line_ending); + let mut doc = Self::new(doc); // set the path and try detecting the language doc.set_path(&path)?; + doc.set_line_ending(line_ending); Ok(doc) } @@ -354,6 +293,10 @@ impl Document { self.selections.insert(view_id, selection); } + pub fn set_line_ending(&mut self, line_ending: Option) { + self.line_ending = line_ending; + } + fn _apply(&mut self, transaction: &Transaction, view_id: ViewId) -> bool { let old_doc = self.text().clone(); diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 978774d9f6a0..a5fa47354192 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -1,6 +1,4 @@ -use crate::{ - theme::Theme, tree::Tree, Document, DocumentId, LineEnding, RegisterSelection, View, ViewId, -}; +use crate::{theme::Theme, tree::Tree, Document, DocumentId, RegisterSelection, View, ViewId}; use tui::layout::Rect; use std::path::PathBuf; @@ -9,7 +7,7 @@ use slotmap::SlotMap; use anyhow::Error; -pub use helix_core::diagnostic::Severity; +pub use helix_core::{diagnostic::Severity, LineEnding}; pub struct Editor { pub tree: Tree, @@ -133,7 +131,7 @@ impl Editor { pub fn new_file(&mut self, action: Action) -> DocumentId { use helix_core::Rope; - let doc = Document::new(Rope::from("\n"), LineEnding::LF); + let doc = Document::new(Rope::from("\n")); let id = self.documents.insert(doc); self.documents[id].id = id; self.switch(id, action); diff --git a/helix-view/src/lib.rs b/helix-view/src/lib.rs index e8c12af7d13a..7e25332000c7 100644 --- a/helix-view/src/lib.rs +++ b/helix-view/src/lib.rs @@ -10,7 +10,6 @@ new_key_type! { pub struct DocumentId; } new_key_type! { pub struct ViewId; } pub use document::Document; -pub use document::LineEnding; pub use editor::Editor; pub use register_selection::RegisterSelection; pub use theme::Theme;