on master help sign in
doc: Be smarter about calculating modified status.

This way edit -> undo will properly show up as unmodified.

commit28c167d71d910d113d5217349830a42fa52e9a9d authorBlaž Hrastnik <blaz@mxxn.io> dateMon 3 May 2021, 17:23 (5 years ago) parent594575b
3 files changed+13−10
helix-core/src/history.rs+5 helix-term/src/ui/editor.rs+1−1 helix-view/src/document.rs+7−9
helix-core/src/history.rs+5
@@ -55,6 +55,11 @@ impl History {
5555 self.cursor = new_cursor;
5656 }
5757
58+ #[inline]
59+ pub fn current_revision(&self) -> usize {
60+ self.cursor
61+ }
62+
5863 #[inline]
5964 pub const fn at_root(&self) -> bool {
6065 self.cursor == 0
helix-term/src/ui/editor.rs+1−1
@@ -440,7 +440,7 @@ impl EditorView {
440440 if let Some(path) = doc.relative_path() {
441441 let path = path.to_string_lossy();
442442
443− let title = format!("{}{}", path, if doc.modified() { "[+]" } else { "" });
443+ let title = format!("{}{}", path, if doc.is_modified() { "[+]" } else { "" });
444444 surface.set_stringn(
445445 viewport.x + 6,
446446 viewport.y,
helix-view/src/document.rs+7−9
@@ -35,13 +35,13 @@ pub struct Document {
3535 // /// Corresponding language scope name. Usually `source.<lang>`.
3636 pub(crate) language: Option<Arc<LanguageConfiguration>>,
3737
38− modified: bool,
3938 /// Pending changes since last history commit.
4039 changes: ChangeSet,
4140 /// State at last commit. Used for calculating reverts.
4241 old_state: Option<State>,
4342 /// Undo tree.
4443 history: History,
44+ last_saved_revision: usize,
4545 version: i32, // should be usize?
4646
4747 pub diagnostics: Vec<Diagnostic>,
@@ -81,12 +81,12 @@ impl Document {
8181 restore_cursor: false,
8282 syntax: None,
8383 language: None,
84− modified: false,
8584 changes,
8685 old_state,
8786 diagnostics: Vec::new(),
8887 version: 0,
8988 history: History::default(),
89+ last_saved_revision: 0,
9090 language_server: None,
9191 }
9292 }
@@ -131,7 +131,7 @@ impl Document {
131131 let language_server = self.language_server.clone();
132132
133133 // reset the modified flag
134− self.modified = false;
134+ self.last_saved_revision = self.history.current_revision();
135135
136136 async move {
137137 use smol::{fs::File, prelude::*};
@@ -246,10 +246,6 @@ impl Document {
246246
247247 let success = self._apply(transaction, view_id);
248248
249− self.modified = true;
250− // TODO: be smarter about modified by keeping track of saved version instead. That way if
251− // current version == version then it's not modified.
252−
253249 if !transaction.changes().is_empty() {
254250 // Compose this transaction with the previous one
255251 take_with(&mut self.changes, |changes| {
@@ -307,8 +303,10 @@ impl Document {
307303 }
308304
309305 #[inline]
310− pub fn modified(&self) -> bool {
311− self.modified
306+ pub fn is_modified(&self) -> bool {
307+ self.path.is_some()
308+ && (self.history.current_revision() != self.last_saved_revision
309+ || !self.changes.is_empty())
312310 }
313311
314312 #[inline]