@@ -307,6 +307,19 @@ pub async fn to_writer<'a, W: tokio::io::AsyncWriteExt + Unpin + ?Sized>(
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);
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());
310/// Like std::mem::replace() except it allows the replacement value to be mapped from the
323/// Like std::mem::replace() except it allows the replacement value to be mapped from the
311/// original value.
324/// original value.
312fn take_with<T, F>(mut_ref: &mut T, closure: F)
325fn take_with<T, F>(mut_ref: &mut T, closure: F)
@@ -449,14 +462,7 @@ impl Document {
450 let mut file = std::fs::File::open(&path).context(format!("unable to open {:?}", path))?;
463 let mut file = std::fs::File::open(&path).context(format!("unable to open {:?}", path))?;
451 let (mut rope, encoding) = from_reader(&mut file, encoding)?;
464 let (mut rope, encoding) = from_reader(&mut file, encoding)?;
452−
465+ let line_ending = with_line_ending(&mut rope);
453− // search for line endings
454− let line_ending = auto_detect_line_ending(&rope).unwrap_or(DEFAULT_LINE_ENDING);
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());
461 let mut doc = Self::from(rope, Some(encoding));
467 let mut doc = Self::from(rope, Some(encoding));
@@ -586,6 +592,45 @@ impl Document {
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());
600+ // If there is no path or the path no longer exists.
602+ return Err(anyhow!("can't find file to reload from"));
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);
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);
613+ // Detect indentation style and set line ending.
614+ self.detect_indent_style();
615+ self.line_ending = line_ending;
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")),
629+ /// Returns the [`Document`]'s current encoding.
630+ pub fn encoding(&self) -> &'static encoding_rs::Encoding {
589 fn detect_indent_style(&mut self) {
634 fn detect_indent_style(&mut self) {
590 // Build a histogram of the indentation *increases* between
635 // Build a histogram of the indentation *increases* between
591 // subsequent lines, ignoring lines that are all whitespace.
636 // subsequent lines, ignoring lines that are all whitespace.