From 28c167d71d910d113d5217349830a42fa52e9a9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Mon, 3 May 2021 17:23:11 +0900 Subject: [PATCH] doc: Be smarter about calculating modified status. This way edit -> undo will properly show up as unmodified. --- helix-core/src/history.rs | 5 +++++ helix-term/src/ui/editor.rs | 2 +- helix-view/src/document.rs | 16 +++++++--------- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/helix-core/src/history.rs b/helix-core/src/history.rs index df4b9fc4d..5a9ec8de2 100644 --- a/helix-core/src/history.rs +++ b/helix-core/src/history.rs @@ -55,6 +55,11 @@ impl History { self.cursor = new_cursor; } + #[inline] + pub fn current_revision(&self) -> usize { + self.cursor + } + #[inline] pub const fn at_root(&self) -> bool { self.cursor == 0 diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 227ccdaac..13ac88fd0 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -440,7 +440,7 @@ impl EditorView { if let Some(path) = doc.relative_path() { let path = path.to_string_lossy(); - let title = format!("{}{}", path, if doc.modified() { "[+]" } else { "" }); + let title = format!("{}{}", path, if doc.is_modified() { "[+]" } else { "" }); surface.set_stringn( viewport.x + 6, viewport.y, diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index a04600b67..16a7a88af 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -35,13 +35,13 @@ pub struct Document { // /// Corresponding language scope name. Usually `source.`. pub(crate) language: Option>, - modified: bool, /// Pending changes since last history commit. changes: ChangeSet, /// State at last commit. Used for calculating reverts. old_state: Option, /// Undo tree. history: History, + last_saved_revision: usize, version: i32, // should be usize? pub diagnostics: Vec, @@ -81,12 +81,12 @@ impl Document { restore_cursor: false, syntax: None, language: None, - modified: false, changes, old_state, diagnostics: Vec::new(), version: 0, history: History::default(), + last_saved_revision: 0, language_server: None, } } @@ -131,7 +131,7 @@ impl Document { let language_server = self.language_server.clone(); // reset the modified flag - self.modified = false; + self.last_saved_revision = self.history.current_revision(); async move { use smol::{fs::File, prelude::*}; @@ -246,10 +246,6 @@ impl Document { let success = self._apply(transaction, view_id); - self.modified = true; - // TODO: be smarter about modified by keeping track of saved version instead. That way if - // current version == version then it's not modified. - if !transaction.changes().is_empty() { // Compose this transaction with the previous one take_with(&mut self.changes, |changes| { @@ -307,8 +303,10 @@ impl Document { } #[inline] - pub fn modified(&self) -> bool { - self.modified + pub fn is_modified(&self) -> bool { + self.path.is_some() + && (self.history.current_revision() != self.last_saved_revision + || !self.changes.is_empty()) } #[inline]