aboutsummaryrefslogtreecommitdiff
path: root/helix-view
diff options
context:
space:
mode:
authorBlaž Hrastnik2021-06-22 02:09:19 +0000
committerGitHub2021-06-22 02:09:19 +0000
commita70de6e980ec58cabf58c33e8b91bfafbea312eb (patch)
tree476c07b84ee3f399eb55c8b549641a59eedc4e1c /helix-view
parentc704970fd71a1a29ef8397ff2ab9e12c5b780a81 (diff)
parentf2954fa153ccb6b147d8d38020341a2f1b0b6df2 (diff)
Merge pull request #224 from helix-editor/line_ending_detection
Line ending detection
Diffstat (limited to 'helix-view')
-rw-r--r--helix-view/src/document.rs34
-rw-r--r--helix-view/src/editor.rs5
2 files changed, 24 insertions, 15 deletions
diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs
index fd127e1b..bd45db5a 100644
--- a/helix-view/src/document.rs
+++ b/helix-view/src/document.rs
@@ -7,10 +7,12 @@ use std::str::FromStr;
use std::sync::Arc;
use helix_core::{
- chars::{char_is_linebreak, char_is_whitespace},
+ chars::{char_is_line_ending, char_is_whitespace},
history::History,
+ line_ending::auto_detect_line_ending,
syntax::{self, LanguageConfiguration},
- ChangeSet, Diagnostic, Rope, Selection, State, Syntax, Transaction,
+ ChangeSet, Diagnostic, LineEnding, Rope, Selection, State, Syntax, Transaction,
+ DEFAULT_LINE_ENDING,
};
use crate::{DocumentId, Theme, ViewId};
@@ -45,6 +47,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>>,
@@ -232,6 +237,7 @@ impl Document {
history: Cell::new(History::default()),
last_saved_revision: 0,
language_server: None,
+ line_ending: DEFAULT_LINE_ENDING,
}
}
@@ -243,22 +249,26 @@ impl Document {
) -> Result<Self, Error> {
use std::{fs::File, io::BufReader};
- let doc = if !path.exists() {
- Rope::from("\n")
+ 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)?;
doc.detect_indent_style();
+ doc.line_ending = line_ending;
if let Some(loader) = config_loader {
doc.detect_language(theme, loader);
@@ -366,7 +376,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;
@@ -390,7 +400,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 5d18030a..839bcdcd 100644
--- a/helix-view/src/editor.rs
+++ b/helix-view/src/editor.rs
@@ -15,10 +15,9 @@ use slotmap::SlotMap;
use anyhow::Error;
-use helix_core::Position;
-
pub use helix_core::diagnostic::Severity;
pub use helix_core::register::Registers;
+use helix_core::{Position, DEFAULT_LINE_ENDING};
#[derive(Debug)]
pub struct Editor {
@@ -173,7 +172,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);