@@ -5,11 +5,14 @@ use std::path::{Component, Path, PathBuf};
5use std::sync::Arc;
5use std::sync::Arc;
7use helix_core::{
7use helix_core::{
8 chars::{char_is_linebreak, char_is_whitespace},
9 chars::{char_is_linebreak, char_is_whitespace},
9 history::History,
10 history::History,
12+ auto_detect_line_ending, default_line_ending,
13+>>>>>>> 491a8b3 (resolved conflict in rebase)
10 syntax::{LanguageConfiguration, LOADER},
14 syntax::{LanguageConfiguration, LOADER},
11− ChangeSet, Diagnostic, History, Rope, RopeGraphemes, RopeSlice, Selection, State, Syntax,
15+ ChangeSet, Diagnostic, History, LineEnding, Rope, Selection, State, Syntax, Transaction,
15use crate::{DocumentId, ViewId};
18use crate::{DocumentId, ViewId};
@@ -23,6 +26,7 @@ pub enum Mode {
27#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
31#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
28pub enum IndentStyle {
32pub enum IndentStyle {
@@ -45,6 +49,8 @@ pub enum LineEnding {
45 PS = 8, // U+2029 -- ParagraphSeparator
49 PS = 8, // U+2029 -- ParagraphSeparator
53+>>>>>>> 491a8b3 (resolved conflict in rebase)
48pub struct Document {
54pub struct Document {
49 // rope + selection
55 // rope + selection
50 pub(crate) id: DocumentId,
56 pub(crate) id: DocumentId,
@@ -78,7 +84,7 @@ pub struct Document {
79 diagnostics: Vec<Diagnostic>,
85 diagnostics: Vec<Diagnostic>,
80 language_server: Option<Arc<helix_lsp::Client>>,
86 language_server: Option<Arc<helix_lsp::Client>>,
81− _line_ending: LineEnding,
87+ line_ending: Option<LineEnding>,
84use std::fmt;
90use std::fmt;
@@ -164,61 +170,14 @@ pub fn canonicalize_path(path: &Path) -> std::io::Result<PathBuf> {
164 std::env::current_dir().map(|current_dir| normalize_path(¤t_dir.join(path)))
170 std::env::current_dir().map(|current_dir| normalize_path(¤t_dir.join(path)))
167−pub fn auto_detect_line_ending(doc: &Rope) -> LineEnding {
168− // based on https://github.com/cessen/led/blob/27572c8838a1c664ee378a19358604063881cc1d/src/editor/mod.rs#L88-L162
170− let mut ending = LineEnding::None;
171− for line in doc.lines().take(1) { // check first line only - unsure how sound this is
172− ending = match line.len_chars() {
173− 1 => { let g = RopeGraphemes::new(line.slice((line.len_chars() - 1)..))
176− rope_slice_to_line_ending(&g)}
177− n if n > 1 => { let g = RopeGraphemes::new(line.slice((line.len_chars() - 2)..))
180− rope_slice_to_line_ending(&g) }
181− _ => LineEnding::None
188−pub fn rope_slice_to_line_ending(g: &RopeSlice) -> LineEnding {
189− if let Some(text) = g.as_str() {
190− str_to_line_ending(text)
191− } else if g == "\u{000D}\u{000A}" {
194− // Not a line ending
199−pub fn str_to_line_ending(g: &str) -> LineEnding {
201− "\u{000D}\u{000A}" => LineEnding::Crlf,
202− "\u{000A}" => LineEnding::LF,
203− "\u{000B}" => LineEnding::VT,
204− "\u{000C}" => LineEnding::FF,
205− "\u{000D}" => LineEnding::CR,
206− "\u{0085}" => LineEnding::Nel,
207− "\u{2028}" => LineEnding::LS,
208− "\u{2029}" => LineEnding::PS,
210− // Not a line ending
211− _ => LineEnding::None,
215use helix_lsp::lsp;
173use helix_lsp::lsp;
216use url::Url;
174use url::Url;
218impl Document {
176impl Document {
219− pub fn new(text: Rope, _line_ending: LineEnding) -> Self {
177+ pub fn new(text: Rope) -> Self {
220 let changes = ChangeSet::new(&text);
178 let changes = ChangeSet::new(&text);
221 let old_state = None;
179 let old_state = None;
180+ let line_ending = default_line_ending();
224 id: DocumentId::default(),
183 id: DocumentId::default(),
@@ -237,7 +196,7 @@ impl Document {
237 history: Cell::new(History::default()),
196 history: Cell::new(History::default()),
238 last_saved_revision: 0,
197 last_saved_revision: 0,
239 language_server: None,
198 language_server: None,
240− _line_ending
199+ line_ending,
@@ -260,10 +219,14 @@ impl Document {
260 // search for line endings
219 // search for line endings
261 let line_ending = auto_detect_line_ending(&doc);
220 let line_ending = auto_detect_line_ending(&doc);
263− let mut doc = Self::new(doc, line_ending);
222+ let mut doc = Self::new(doc);
264 // set the path and try detecting the language
223 // set the path and try detecting the language
265 doc.set_path(&path)?;
224 doc.set_path(&path)?;
266 doc.detect_indent_style();
226 doc.detect_indent_style();
228+ doc.set_line_ending(line_ending);
229+>>>>>>> 491a8b3 (resolved conflict in rebase)
@@ -522,6 +485,10 @@ impl Document {
522 self.selections.insert(view_id, selection);
485 self.selections.insert(view_id, selection);
488+ pub fn set_line_ending(&mut self, line_ending: Option<LineEnding>) {
489+ self.line_ending = line_ending;
525 fn _apply(&mut self, transaction: &Transaction, view_id: ViewId) -> bool {
492 fn _apply(&mut self, transaction: &Transaction, view_id: ViewId) -> bool {
526 let old_doc = self.text().clone();
493 let old_doc = self.text().clone();