aboutsummaryrefslogtreecommitdiff
path: root/helix-view/src/document.rs
diff options
context:
space:
mode:
Diffstat (limited to 'helix-view/src/document.rs')
-rw-r--r--helix-view/src/document.rs84
1 files changed, 67 insertions, 17 deletions
diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs
index 86f3dfb8..b917b902 100644
--- a/helix-view/src/document.rs
+++ b/helix-view/src/document.rs
@@ -70,7 +70,6 @@ pub enum IndentStyle {
}
pub struct Document {
- // rope + selection
pub(crate) id: DocumentId,
text: Rope,
pub(crate) selections: HashMap<ViewId, Selection>,
@@ -307,6 +306,19 @@ pub async fn to_writer<'a, W: tokio::io::AsyncWriteExt + Unpin + ?Sized>(
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
/// original value.
fn take_with<T, F>(mut_ref: &mut T, closure: F)
@@ -395,12 +407,13 @@ pub fn normalize_path(path: &Path) -> PathBuf {
/// This function is used instead of `std::fs::canonicalize` because we don't want to verify
/// here if the path exists, just normalize it's components.
pub fn canonicalize_path(path: &Path) -> std::io::Result<PathBuf> {
- let normalized = normalize_path(path);
- if normalized.is_absolute() {
- Ok(normalized)
+ let path = if path.is_relative() {
+ std::env::current_dir().map(|current_dir| current_dir.join(path))?
} else {
- std::env::current_dir().map(|current_dir| current_dir.join(normalized))
- }
+ path.to_path_buf()
+ };
+
+ Ok(normalize_path(&path))
}
use helix_lsp::lsp;
@@ -448,7 +461,8 @@ impl Document {
}
let mut file = std::fs::File::open(&path).context(format!("unable to open {:?}", path))?;
- let (rope, encoding) = from_reader(&mut file, encoding)?;
+ let (mut rope, encoding) = from_reader(&mut file, encoding)?;
+ let line_ending = with_line_ending(&mut rope);
let mut doc = Self::from(rope, Some(encoding));
@@ -458,9 +472,9 @@ impl Document {
doc.detect_language(theme, loader);
}
- // Detect indentation style and line ending.
+ // Detect indentation style and set line ending.
doc.detect_indent_style();
- doc.line_ending = auto_detect_line_ending(&doc.text).unwrap_or(DEFAULT_LINE_ENDING);
+ doc.line_ending = line_ending;
Ok(doc)
}
@@ -578,6 +592,45 @@ impl Document {
}
}
+ /// Reload the document from its path.
+ pub fn reload(&mut self, view_id: ViewId) -> Result<(), Error> {
+ let encoding = &self.encoding;
+ let path = self.path().filter(|path| path.exists());
+
+ // If there is no path or the path no longer exists.
+ if path.is_none() {
+ return Err(anyhow!("can't find file to reload from"));
+ }
+
+ let mut file = std::fs::File::open(path.unwrap())?;
+ let (mut 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);
+ self.apply(&transaction, view_id);
+ self.append_changes_to_history(view_id);
+
+ // Detect indentation style and set line ending.
+ self.detect_indent_style();
+ self.line_ending = line_ending;
+
+ Ok(())
+ }
+
+ /// Sets the [`Document`]'s encoding with the encoding correspondent to `label`.
+ pub fn set_encoding(&mut self, label: &str) -> Result<(), Error> {
+ match encoding_rs::Encoding::for_label(label.as_bytes()) {
+ Some(encoding) => self.encoding = encoding,
+ None => return Err(anyhow::anyhow!("unknown encoding")),
+ }
+ Ok(())
+ }
+
+ /// Returns the [`Document`]'s current encoding.
+ pub fn encoding(&self) -> &'static encoding_rs::Encoding {
+ self.encoding
+ }
+
fn detect_indent_style(&mut self) {
// Build a histogram of the indentation *increases* between
// subsequent lines, ignoring lines that are all whitespace.
@@ -996,14 +1049,11 @@ impl Document {
let cwdir = std::env::current_dir().expect("couldn't determine current directory");
self.path.as_ref().map(|path| {
- let path = fold_home_dir(path);
- if path.is_relative() {
- path
- } else {
- path.strip_prefix(cwdir)
- .map(|p| p.to_path_buf())
- .unwrap_or(path)
- }
+ let mut path = path.as_path();
+ if path.is_absolute() {
+ path = path.strip_prefix(cwdir).unwrap_or(path)
+ };
+ fold_home_dir(path)
})
}