@@ -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(¤t_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();