on master help sign in
Don't insert a final line ending on file load/reload.
198fe40Nathan Vegdahl
helix-view/src/document.rs+6−21
@@ -306,19 +306,6 @@ pub async fn to_writer<'a, W: tokio::io::AsyncWriteExt + Unpin + ?Sized>(
306 Ok(())
306 Ok(())
307}
307}
308
308
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−
322/// Like std::mem::replace() except it allows the replacement value to be mapped from the
309/// Like std::mem::replace() except it allows the replacement value to be mapped from the
323/// original value.
310/// original value.
324fn take_with<T, F>(mut_ref: &mut T, closure: F)
311fn take_with<T, F>(mut_ref: &mut T, closure: F)
@@ -456,7 +443,7 @@ impl Document {
456 theme: Option<&Theme>,
443 theme: Option<&Theme>,
457 config_loader: Option<&syntax::Loader>,
444 config_loader: Option<&syntax::Loader>,
458 ) -> Result<Self, Error> {
445 ) -> Result<Self, Error> {
459− let (mut rope, encoding) = if path.exists() {
446+ let (rope, encoding) = if path.exists() {
460 let mut file =
447 let mut file =
461 std::fs::File::open(&path).context(format!("unable to open {:?}", path))?;
448 std::fs::File::open(&path).context(format!("unable to open {:?}", path))?;
462 from_reader(&mut file, encoding)?
449 from_reader(&mut file, encoding)?
@@ -465,7 +452,6 @@ impl Document {
465 (Rope::from(DEFAULT_LINE_ENDING.as_str()), encoding)
452 (Rope::from(DEFAULT_LINE_ENDING.as_str()), encoding)
466 };
453 };
467
454
468− let line_ending = with_line_ending(&mut rope);
469 let mut doc = Self::from(rope, Some(encoding));
455 let mut doc = Self::from(rope, Some(encoding));
470
456
471 // set the path and try detecting the language
457 // set the path and try detecting the language
@@ -474,9 +460,9 @@ impl Document {
474 doc.detect_language(theme, loader);
460 doc.detect_language(theme, loader);
475 }
461 }
476
462
477− // Detect indentation style and set line ending.
463+ // Detect indentation style and line ending.
478 doc.detect_indent_style();
464 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);
480
466
481 Ok(doc)
467 Ok(doc)
482 }
468 }
@@ -605,16 +591,15 @@ impl Document {
605 }
591 }
606
592
607 let mut file = std::fs::File::open(path.unwrap())?;
593 let mut file = std::fs::File::open(path.unwrap())?;
608− let (mut rope, ..) = from_reader(&mut file, Some(encoding))?;
594+ let (rope, ..) = from_reader(&mut file, Some(encoding))?;
609− let line_ending = with_line_ending(&mut rope);
610
595
611 let transaction = helix_core::diff::compare_ropes(self.text(), &rope);
596 let transaction = helix_core::diff::compare_ropes(self.text(), &rope);
612 self.apply(&transaction, view_id);
597 self.apply(&transaction, view_id);
613 self.append_changes_to_history(view_id);
598 self.append_changes_to_history(view_id);
614
599
615− // Detect indentation style and set line ending.
600+ // Detect indentation style and line ending.
616 self.detect_indent_style();
601 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);
618
603
619 Ok(())
604 Ok(())
620 }
605 }