on master help sign in
Track document modified state.
commit1b5316ea74845a23bebf492dd091fd1fc3a8dcaa authorBlaž Hrastnik <blaz@mxxn.io> dateTue 30 Mar 2021, 18:19 (6 years ago) parent88bb7a1
3 files changed+19−5
helix-term/src/commands.rs+1−1 helix-term/src/ui/editor.rs+3−2 helix-view/src/document.rs+15−2
helix-term/src/commands.rs+1−1
@@ -830,7 +830,7 @@ pub fn command_mode(cx: &mut Context) {
830830 ["w"] | ["write"] => {
831831 // TODO: non-blocking via save() command
832832 let id = editor.view().doc;
833− let doc = &mut editor.document(id).unwrap();
833+ let doc = &mut editor.documents[id];
834834 smol::block_on(doc.save());
835835 }
836836
helix-term/src/ui/editor.rs+3−2
@@ -415,14 +415,15 @@ impl EditorView {
415415
416416 if let Some(path) = doc.relative_path() {
417417 let path = path.to_string_lossy();
418+
419+ let title = format!("{}{}", path, if doc.modified() { "[+]" } else { "" });
418420 surface.set_stringn(
419421 viewport.x + 6,
420422 viewport.y,
421− path,
423+ title,
422424 viewport.width.saturating_sub(6) as usize,
423425 text_color,
424426 );
425− // TODO: append [+] if modified
426427 }
427428
428429 surface.set_string(
helix-view/src/document.rs+15−2
@@ -31,6 +31,7 @@ pub struct Document {
3131 // /// Corresponding language scope name. Usually `source.<lang>`.
3232 pub(crate) language: Option<Arc<LanguageConfiguration>>,
3333
34+ modified: bool,
3435 /// Pending changes since last history commit.
3536 changes: ChangeSet,
3637 /// State at last commit. Used for calculating reverts.
@@ -75,6 +76,7 @@ impl Document {
7576 restore_cursor: false,
7677 syntax: None,
7778 language: None,
79+ modified: false,
7880 changes,
7981 old_state,
8082 diagnostics: Vec::new(),
@@ -111,7 +113,7 @@ impl Document {
111113
112114 // TODO: do we need some way of ensuring two save operations on the same doc can't run at once?
113115 // or is that handled by the OS/async layer
114− pub fn save(&self) -> impl Future<Output = Result<(), anyhow::Error>> {
116+ pub fn save(&mut self) -> impl Future<Output = Result<(), anyhow::Error>> {
115117 // we clone and move text + path into the future so that we asynchronously save the current
116118 // state without blocking any further edits.
117119
@@ -120,10 +122,12 @@ impl Document {
120122 let identifier = self.identifier();
121123
122124 // TODO: mark changes up to now as saved
123− // TODO: mark dirty false
124125
125126 let language_server = self.language_server.clone();
126127
128+ // reset the modified flag
129+ self.modified = false;
130+
127131 async move {
128132 use smol::{fs::File, prelude::*};
129133 let mut file = File::create(path).await?;
@@ -224,6 +228,10 @@ impl Document {
224228
225229 let success = self._apply(&transaction);
226230
231+ self.modified = true;
232+ // TODO: be smarter about modified by keeping track of saved version instead. That way if
233+ // current version == version then it's not modified.
234+
227235 if !transaction.changes().is_empty() {
228236 // Compose this transaction with the previous one
229237 take_with(&mut self.changes, |changes| {
@@ -279,6 +287,11 @@ impl Document {
279287 self.id
280288 }
281289
290+ #[inline]
291+ pub fn modified(&self) -> bool {
292+ self.modified
293+ }
294+
282295 #[inline]
283296 pub fn mode(&self) -> Mode {
284297 self.mode