@@ -307,6 +307,19 @@ pub async fn to_writer<'a, W: tokio::io::AsyncWriteExt + Unpin + ?Sized>(
307307 Ok(())
308308}
309309
310+/// 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)
311+pub fn with_line_ending(rope: &mut Rope) -> LineEnding {
312+ // search for line endings
313+ let line_ending = auto_detect_line_ending(rope).unwrap_or(DEFAULT_LINE_ENDING);
314+
315+ // add missing newline at the end of file
316+ if rope.len_bytes() == 0 || !char_is_line_ending(rope.char(rope.len_chars() - 1)) {
317+ rope.insert(rope.len_chars(), line_ending.as_str());
318+ }
319+
320+ line_ending
321+}
322+
310323/// Like std::mem::replace() except it allows the replacement value to be mapped from the
311324/// original value.
312325fn take_with<T, F>(mut_ref: &mut T, closure: F)
@@ -449,14 +462,7 @@ impl Document {
449462
450463 let mut file = std::fs::File::open(&path).context(format!("unable to open {:?}", path))?;
451464 let (mut rope, encoding) = from_reader(&mut file, encoding)?;
452−
453− // search for line endings
454− let line_ending = auto_detect_line_ending(&rope).unwrap_or(DEFAULT_LINE_ENDING);
455−
456− // add missing newline at the end of file
457− if rope.len_bytes() == 0 || !char_is_line_ending(rope.char(rope.len_chars() - 1)) {
458− rope.insert(rope.len_chars(), line_ending.as_str());
459− }
465+ let line_ending = with_line_ending(&mut rope);
460466
461467 let mut doc = Self::from(rope, Some(encoding));
462468
@@ -586,6 +592,45 @@ impl Document {
586592 }
587593 }
588594
595+ /// Reload the document from its path.
596+ pub fn reload(&mut self, view_id: ViewId) -> Result<(), Error> {
597+ let encoding = &self.encoding;
598+ let path = self.path().filter(|path| path.exists());
599+
600+ // If there is no path or the path no longer exists.
601+ if path.is_none() {
602+ return Err(anyhow!("can't find file to reload from"));
603+ }
604+
605+ let mut file = std::fs::File::open(path.unwrap())?;
606+ let (mut rope, ..) = from_reader(&mut file, Some(encoding))?;
607+ let line_ending = with_line_ending(&mut rope);
608+
609+ let transaction = helix_core::diff::compare_ropes(self.text(), &rope);
610+ self.apply(&transaction, view_id);
611+ self.append_changes_to_history(view_id);
612+
613+ // Detect indentation style and set line ending.
614+ self.detect_indent_style();
615+ self.line_ending = line_ending;
616+
617+ Ok(())
618+ }
619+
620+ /// Sets the [`Document`]'s encoding with the encoding correspondent to `label`.
621+ pub fn set_encoding(&mut self, label: &str) -> Result<(), Error> {
622+ match encoding_rs::Encoding::for_label(label.as_bytes()) {
623+ Some(encoding) => self.encoding = encoding,
624+ None => return Err(anyhow::anyhow!("unknown encoding")),
625+ }
626+ Ok(())
627+ }
628+
629+ /// Returns the [`Document`]'s current encoding.
630+ pub fn encoding(&self) -> &'static encoding_rs::Encoding {
631+ self.encoding
632+ }
633+
589634 fn detect_indent_style(&mut self) {
590635 // Build a histogram of the indentation *increases* between
591636 // subsequent lines, ignoring lines that are all whitespace.