on master help sign in
Track document modified state.
1b5316eBlaž Hrastnik
helix-term/src/commands.rs+1−1
@@ -830,7 +830,7 @@ pub fn command_mode(cx: &mut Context) {
830 ["w"] | ["write"] => {
830 ["w"] | ["write"] => {
831 // TODO: non-blocking via save() command
831 // TODO: non-blocking via save() command
832 let id = editor.view().doc;
832 let id = editor.view().doc;
833− let doc = &mut editor.document(id).unwrap();
833+ let doc = &mut editor.documents[id];
834 smol::block_on(doc.save());
834 smol::block_on(doc.save());
835 }
835 }
836
836
helix-term/src/ui/editor.rs+3−2
@@ -415,14 +415,15 @@ impl EditorView {
415
415
416 if let Some(path) = doc.relative_path() {
416 if let Some(path) = doc.relative_path() {
417 let path = path.to_string_lossy();
417 let path = path.to_string_lossy();
418+
419+ let title = format!("{}{}", path, if doc.modified() { "[+]" } else { "" });
418 surface.set_stringn(
420 surface.set_stringn(
419 viewport.x + 6,
421 viewport.x + 6,
420 viewport.y,
422 viewport.y,
421− path,
423+ title,
422 viewport.width.saturating_sub(6) as usize,
424 viewport.width.saturating_sub(6) as usize,
423 text_color,
425 text_color,
424 );
426 );
425− // TODO: append [+] if modified
426 }
427 }
427
428
428 surface.set_string(
429 surface.set_string(
helix-view/src/document.rs+15−2
@@ -31,6 +31,7 @@ pub struct Document {
31 // /// Corresponding language scope name. Usually `source.<lang>`.
31 // /// Corresponding language scope name. Usually `source.<lang>`.
32 pub(crate) language: Option<Arc<LanguageConfiguration>>,
32 pub(crate) language: Option<Arc<LanguageConfiguration>>,
33
33
34+ modified: bool,
34 /// Pending changes since last history commit.
35 /// Pending changes since last history commit.
35 changes: ChangeSet,
36 changes: ChangeSet,
36 /// State at last commit. Used for calculating reverts.
37 /// State at last commit. Used for calculating reverts.
@@ -75,6 +76,7 @@ impl Document {
75 restore_cursor: false,
76 restore_cursor: false,
76 syntax: None,
77 syntax: None,
77 language: None,
78 language: None,
79+ modified: false,
78 changes,
80 changes,
79 old_state,
81 old_state,
80 diagnostics: Vec::new(),
82 diagnostics: Vec::new(),
@@ -111,7 +113,7 @@ impl Document {
111
113
112 // TODO: do we need some way of ensuring two save operations on the same doc can't run at once?
114 // TODO: do we need some way of ensuring two save operations on the same doc can't run at once?
113 // or is that handled by the OS/async layer
115 // 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>> {
115 // we clone and move text + path into the future so that we asynchronously save the current
117 // we clone and move text + path into the future so that we asynchronously save the current
116 // state without blocking any further edits.
118 // state without blocking any further edits.
117
119
@@ -120,10 +122,12 @@ impl Document {
120 let identifier = self.identifier();
122 let identifier = self.identifier();
121
123
122 // TODO: mark changes up to now as saved
124 // TODO: mark changes up to now as saved
123− // TODO: mark dirty false
124
125
125 let language_server = self.language_server.clone();
126 let language_server = self.language_server.clone();
126
127
128+ // reset the modified flag
129+ self.modified = false;
130+
127 async move {
131 async move {
128 use smol::{fs::File, prelude::*};
132 use smol::{fs::File, prelude::*};
129 let mut file = File::create(path).await?;
133 let mut file = File::create(path).await?;
@@ -224,6 +228,10 @@ impl Document {
224
228
225 let success = self._apply(&transaction);
229 let success = self._apply(&transaction);
226
230
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+
227 if !transaction.changes().is_empty() {
235 if !transaction.changes().is_empty() {
228 // Compose this transaction with the previous one
236 // Compose this transaction with the previous one
229 take_with(&mut self.changes, |changes| {
237 take_with(&mut self.changes, |changes| {
@@ -279,6 +287,11 @@ impl Document {
279 self.id
287 self.id
280 }
288 }
281
289
290+ #[inline]
291+ pub fn modified(&self) -> bool {
292+ self.modified
293+ }
294+
282 #[inline]
295 #[inline]
283 pub fn mode(&self) -> Mode {
296 pub fn mode(&self) -> Mode {
284 self.mode
297 self.mode