on master help sign in
rebase on branch line_ending_detection
3756c21Jan Hrastnik
helix-core/src/lib.rs+1
@@ -104,6 +104,7 @@ pub use position::{coords_at_pos, pos_at_coords, Position};
104pub use selection::{Range, Selection};
104pub use selection::{Range, Selection};
105pub use smallvec::SmallVec;
105pub use smallvec::SmallVec;
106pub use syntax::Syntax;
106pub use syntax::Syntax;
107+pub use graphemes::RopeGraphemes;
107
108
108pub use diagnostic::Diagnostic;
109pub use diagnostic::Diagnostic;
109pub use state::State;
110pub use state::State;
helix-term/src/ui/editor.rs+1−1
@@ -179,7 +179,7 @@ impl EditorView {
179
179
180 // iterate over range char by char
180 // iterate over range char by char
181 for grapheme in RopeGraphemes::new(text) {
181 for grapheme in RopeGraphemes::new(text) {
182− if grapheme == "\n" {
182+ if grapheme == "\n" || grapheme == "\r\n" {
183 visual_x = 0;
183 visual_x = 0;
184 line += 1;
184 line += 1;
185
185
helix-view/src/document.rs+74−3
@@ -8,7 +8,7 @@ use helix_core::{
8 chars::{char_is_linebreak, char_is_whitespace},
8 chars::{char_is_linebreak, char_is_whitespace},
9 history::History,
9 history::History,
10 syntax::{LanguageConfiguration, LOADER},
10 syntax::{LanguageConfiguration, LOADER},
11− ChangeSet, Diagnostic, Rope, Selection, State, Syntax, Transaction,
11+ ChangeSet, Diagnostic, History, Rope, RopeSlice, RopeGraphemes, Selection, State, Syntax, Transaction,
12};
12};
13
13
14use crate::{DocumentId, ViewId};
14use crate::{DocumentId, ViewId};
@@ -22,12 +22,28 @@ pub enum Mode {
22 Insert,
22 Insert,
23}
23}
24
24
25+
25#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
26#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
26pub enum IndentStyle {
27pub enum IndentStyle {
27 Tabs,
28 Tabs,
28 Spaces(u8),
29 Spaces(u8),
29}
30}
30
31
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+
31pub struct Document {
47pub struct Document {
32 // rope + selection
48 // rope + selection
33 pub(crate) id: DocumentId,
49 pub(crate) id: DocumentId,
@@ -61,6 +77,7 @@ pub struct Document {
61
77
62 diagnostics: Vec<Diagnostic>,
78 diagnostics: Vec<Diagnostic>,
63 language_server: Option<Arc<helix_lsp::Client>>,
79 language_server: Option<Arc<helix_lsp::Client>>,
80+ line_ending: LineEnding
64}
81}
65
82
66use std::fmt;
83use std::fmt;
@@ -146,11 +163,61 @@ pub fn canonicalize_path(path: &Path) -> std::io::Result<PathBuf> {
146 std::env::current_dir().map(|current_dir| normalize_path(&current_dir.join(path)))
163 std::env::current_dir().map(|current_dir| normalize_path(&current_dir.join(path)))
147}
164}
148
165
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+
149use helix_lsp::lsp;
216use helix_lsp::lsp;
150use url::Url;
217use url::Url;
151
218
152impl Document {
219impl Document {
153− pub fn new(text: Rope) -> Self {
220+ pub fn new(text: Rope, line_ending: LineEnding) -> Self {
154 let changes = ChangeSet::new(&text);
221 let changes = ChangeSet::new(&text);
155 let old_state = None;
222 let old_state = None;
156
223
@@ -171,6 +238,7 @@ impl Document {
171 history: Cell::new(History::default()),
238 history: Cell::new(History::default()),
172 last_saved_revision: 0,
239 last_saved_revision: 0,
173 language_server: None,
240 language_server: None,
241+ line_ending: line_ending
174 }
242 }
175 }
243 }
176
244
@@ -190,7 +258,10 @@ impl Document {
190 doc
258 doc
191 };
259 };
192
260
261+ // search for line endings
262+ let line_ending = auto_detect_line_ending(&doc);
263+
193− let mut doc = Self::new(doc);
264+ let mut doc = Self::new(doc, line_ending);
194 // set the path and try detecting the language
265 // set the path and try detecting the language
195 doc.set_path(&path)?;
266 doc.set_path(&path)?;
196 doc.detect_indent_style();
267 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};
2use tui::layout::Rect;
2use tui::layout::Rect;
3use tui::terminal::CursorKind;
3use tui::terminal::CursorKind;
4
4
@@ -148,7 +148,7 @@ impl Editor {
148
148
149 pub fn new_file(&mut self, action: Action) -> DocumentId {
149 pub fn new_file(&mut self, action: Action) -> DocumentId {
150 use helix_core::Rope;
150 use helix_core::Rope;
151− let doc = Document::new(Rope::from("\n"));
151+ let doc = Document::new(Rope::from("\n"), LineEnding::LF);
152 let id = self.documents.insert(doc);
152 let id = self.documents.insert(doc);
153 self.documents[id].id = id;
153 self.documents[id].id = id;
154 self.switch(id, action);
154 self.switch(id, action);
helix-view/src/lib.rs+1
@@ -10,6 +10,7 @@ new_key_type! { pub struct DocumentId; }
10new_key_type! { pub struct ViewId; }
10new_key_type! { pub struct ViewId; }
11
11
12pub use document::Document;
12pub use document::Document;
13+pub use document::LineEnding;
13pub use editor::Editor;
14pub use editor::Editor;
14pub use register_selection::RegisterSelection;
15pub use register_selection::RegisterSelection;
15pub use theme::Theme;
16pub use theme::Theme;