diff options
author | Nathan Vegdahl | 2021-06-20 22:09:10 +0000 |
---|---|---|
committer | Nathan Vegdahl | 2021-06-20 22:33:02 +0000 |
commit | 4efd6713c5b30b33c497a1f85b77a7b0a7fd17e0 (patch) | |
tree | 7661f09f2279a3f9ae6a8f76770a69fd08f95981 /helix-view/src | |
parent | 5d22e3c4e574eb24260966de7f20f582e6184e24 (diff) |
Work on moving code over to LineEnding instead of assuming '\n'.
Also some general cleanup and some minor fixes along the way.
Diffstat (limited to 'helix-view/src')
-rw-r--r-- | helix-view/src/document.rs | 24 | ||||
-rw-r--r-- | helix-view/src/editor.rs | 4 |
2 files changed, 15 insertions, 13 deletions
diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 80be1ed2..3e38c24d 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -10,7 +10,7 @@ use std::sync::Arc; use helix_core::{ auto_detect_line_ending, - chars::{char_is_linebreak, char_is_whitespace}, + chars::{char_is_line_ending, char_is_whitespace}, history::History, syntax::{LanguageConfiguration, LOADER}, ChangeSet, Diagnostic, LineEnding, Rope, Selection, State, Syntax, Transaction, @@ -81,6 +81,9 @@ pub struct Document { /// Current indent style. pub indent_style: IndentStyle, + /// The document's default line ending. + pub line_ending: LineEnding, + syntax: Option<Syntax>, // /// Corresponding language scope name. Usually `source.<lang>`. pub(crate) language: Option<Arc<LanguageConfiguration>>, @@ -99,7 +102,6 @@ pub struct Document { diagnostics: Vec<Diagnostic>, language_server: Option<Arc<helix_lsp::Client>>, - line_ending: LineEnding, } use std::fmt; @@ -254,21 +256,21 @@ impl Document { pub fn load(path: PathBuf) -> Result<Self, Error> { use std::{fs::File, io::BufReader}; - let doc = if !path.exists() { + let mut doc = if !path.exists() { Rope::from(DEFAULT_LINE_ENDING.as_str()) } else { let file = File::open(&path).context(format!("unable to open {:?}", path))?; - let mut doc = Rope::from_reader(BufReader::new(file))?; - // add missing newline at the end of file - if doc.len_bytes() == 0 || doc.byte(doc.len_bytes() - 1) != b'\n' { - doc.insert_char(doc.len_chars(), '\n'); - } - doc + Rope::from_reader(BufReader::new(file))? }; // search for line endings let line_ending = auto_detect_line_ending(&doc).unwrap_or(DEFAULT_LINE_ENDING); + // add missing newline at the end of file + if doc.len_bytes() == 0 || char_is_line_ending(doc.char(doc.len_chars() - 1)) { + doc.insert(doc.len_chars(), line_ending.as_str()); + } + let mut doc = Self::new(doc); // set the path and try detecting the language doc.set_path(&path)?; @@ -379,7 +381,7 @@ impl Document { Some(' ') => false, // Ignore blank lines. - Some(c) if char_is_linebreak(c) => continue, + Some(c) if char_is_line_ending(c) => continue, _ => { prev_line_is_tabs = false; @@ -403,7 +405,7 @@ impl Document { c if char_is_whitespace(c) => count_is_done = true, // Ignore blank lines. - c if char_is_linebreak(c) => continue 'outer, + c if char_is_line_ending(c) => continue 'outer, _ => break, } diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index db8ae87a..fb2eb36d 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -12,7 +12,7 @@ use anyhow::Error; pub use helix_core::diagnostic::Severity; pub use helix_core::register::Registers; -use helix_core::Position; +use helix_core::{Position, DEFAULT_LINE_ENDING}; #[derive(Debug)] pub struct Editor { @@ -150,7 +150,7 @@ impl Editor { pub fn new_file(&mut self, action: Action) -> DocumentId { use helix_core::Rope; - let doc = Document::new(Rope::from("\n")); + let doc = Document::new(Rope::from(DEFAULT_LINE_ENDING.as_str())); let id = self.documents.insert(doc); self.documents[id].id = id; self.switch(id, action); |