Don't insert a final line ending on file load/reload.

pull/376/head
Nathan Vegdahl 3 years ago
parent c848ed7abc
commit 198fe40951

@ -306,19 +306,6 @@ pub async fn to_writer<'a, W: tokio::io::AsyncWriteExt + Unpin + ?Sized>(
Ok(()) Ok(())
} }
/// Inserts the final line ending into `rope` if it's missing. [Why?](https://stackoverflow.com/questions/729692/why-should-text-files-end-with-a-newline)
pub fn with_line_ending(rope: &mut Rope) -> LineEnding {
// search for line endings
let line_ending = auto_detect_line_ending(rope).unwrap_or(DEFAULT_LINE_ENDING);
// add missing newline at the end of file
if rope.len_bytes() == 0 || !char_is_line_ending(rope.char(rope.len_chars() - 1)) {
rope.insert(rope.len_chars(), line_ending.as_str());
}
line_ending
}
/// Like std::mem::replace() except it allows the replacement value to be mapped from the /// Like std::mem::replace() except it allows the replacement value to be mapped from the
/// original value. /// original value.
fn take_with<T, F>(mut_ref: &mut T, closure: F) fn take_with<T, F>(mut_ref: &mut T, closure: F)
@ -456,7 +443,7 @@ impl Document {
theme: Option<&Theme>, theme: Option<&Theme>,
config_loader: Option<&syntax::Loader>, config_loader: Option<&syntax::Loader>,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
let (mut rope, encoding) = if path.exists() { let (rope, encoding) = if path.exists() {
let mut file = let mut file =
std::fs::File::open(&path).context(format!("unable to open {:?}", path))?; std::fs::File::open(&path).context(format!("unable to open {:?}", path))?;
from_reader(&mut file, encoding)? from_reader(&mut file, encoding)?
@ -465,7 +452,6 @@ impl Document {
(Rope::from(DEFAULT_LINE_ENDING.as_str()), encoding) (Rope::from(DEFAULT_LINE_ENDING.as_str()), encoding)
}; };
let line_ending = with_line_ending(&mut rope);
let mut doc = Self::from(rope, Some(encoding)); let mut doc = Self::from(rope, Some(encoding));
// set the path and try detecting the language // set the path and try detecting the language
@ -474,9 +460,9 @@ impl Document {
doc.detect_language(theme, loader); doc.detect_language(theme, loader);
} }
// Detect indentation style and set line ending. // Detect indentation style and line ending.
doc.detect_indent_style(); doc.detect_indent_style();
doc.line_ending = line_ending; doc.line_ending = auto_detect_line_ending(&doc.text).unwrap_or(DEFAULT_LINE_ENDING);
Ok(doc) Ok(doc)
} }
@ -605,16 +591,15 @@ impl Document {
} }
let mut file = std::fs::File::open(path.unwrap())?; let mut file = std::fs::File::open(path.unwrap())?;
let (mut rope, ..) = from_reader(&mut file, Some(encoding))?; let (rope, ..) = from_reader(&mut file, Some(encoding))?;
let line_ending = with_line_ending(&mut rope);
let transaction = helix_core::diff::compare_ropes(self.text(), &rope); let transaction = helix_core::diff::compare_ropes(self.text(), &rope);
self.apply(&transaction, view_id); self.apply(&transaction, view_id);
self.append_changes_to_history(view_id); self.append_changes_to_history(view_id);
// Detect indentation style and set line ending. // Detect indentation style and line ending.
self.detect_indent_style(); self.detect_indent_style();
self.line_ending = line_ending; self.line_ending = auto_detect_line_ending(&self.text).unwrap_or(DEFAULT_LINE_ENDING);
Ok(()) Ok(())
} }

Loading…
Cancel
Save