on master help sign in
Don't insert a final line ending on file load/reload.
commit198fe409513d43bc2c38fbb148131bf8cab58d19 authorNathan Vegdahl <cessen@cessen.com> dateTue 20 Jul 2021, 18:40 (5 years ago) parentc848ed7
1 file changed+6−21
helix-view/src/document.rs+6−21
helix-view/src/document.rs+6−21
@@ -306,19 +306,6 @@ pub async fn to_writer<'a, W: tokio::io::AsyncWriteExt + Unpin + ?Sized>(
306306 Ok(())
307307}
308308
309−/// 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)
310−pub fn with_line_ending(rope: &mut Rope) -> LineEnding {
311− // search for line endings
312− let line_ending = auto_detect_line_ending(rope).unwrap_or(DEFAULT_LINE_ENDING);
313−
314− // add missing newline at the end of file
315− if rope.len_bytes() == 0 || !char_is_line_ending(rope.char(rope.len_chars() - 1)) {
316− rope.insert(rope.len_chars(), line_ending.as_str());
317− }
318−
319− line_ending
320−}
321−
322309/// Like std::mem::replace() except it allows the replacement value to be mapped from the
323310/// original value.
324311fn take_with<T, F>(mut_ref: &mut T, closure: F)
@@ -456,7 +443,7 @@ impl Document {
456443 theme: Option<&Theme>,
457444 config_loader: Option<&syntax::Loader>,
458445 ) -> Result<Self, Error> {
459− let (mut rope, encoding) = if path.exists() {
446+ let (rope, encoding) = if path.exists() {
460447 let mut file =
461448 std::fs::File::open(&path).context(format!("unable to open {:?}", path))?;
462449 from_reader(&mut file, encoding)?
@@ -465,7 +452,6 @@ impl Document {
465452 (Rope::from(DEFAULT_LINE_ENDING.as_str()), encoding)
466453 };
467454
468− let line_ending = with_line_ending(&mut rope);
469455 let mut doc = Self::from(rope, Some(encoding));
470456
471457 // set the path and try detecting the language
@@ -474,9 +460,9 @@ impl Document {
474460 doc.detect_language(theme, loader);
475461 }
476462
477− // Detect indentation style and set line ending.
463+ // Detect indentation style and line ending.
478464 doc.detect_indent_style();
479− doc.line_ending = line_ending;
465+ doc.line_ending = auto_detect_line_ending(&doc.text).unwrap_or(DEFAULT_LINE_ENDING);
480466
481467 Ok(doc)
482468 }
@@ -605,16 +591,15 @@ impl Document {
605591 }
606592
607593 let mut file = std::fs::File::open(path.unwrap())?;
608− let (mut rope, ..) = from_reader(&mut file, Some(encoding))?;
609− let line_ending = with_line_ending(&mut rope);
594+ let (rope, ..) = from_reader(&mut file, Some(encoding))?;
610595
611596 let transaction = helix_core::diff::compare_ropes(self.text(), &rope);
612597 self.apply(&transaction, view_id);
613598 self.append_changes_to_history(view_id);
614599
615− // Detect indentation style and set line ending.
600+ // Detect indentation style and line ending.
616601 self.detect_indent_style();
617− self.line_ending = line_ending;
602+ self.line_ending = auto_detect_line_ending(&self.text).unwrap_or(DEFAULT_LINE_ENDING);
618603
619604 Ok(())
620605 }