@@ -1,4 +1,5 @@
1use anyhow::{Context, Error};
1use anyhow::{Context, Error};
2use std::future::Future;
3use std::future::Future;
3use std::path::{Component, Path, PathBuf};
4use std::path::{Component, Path, PathBuf};
4use std::sync::Arc;
5use std::sync::Arc;
@@ -40,7 +41,10 @@ pub struct Document {
40 /// State at last commit. Used for calculating reverts.
41 /// State at last commit. Used for calculating reverts.
41 old_state: Option<State>,
42 old_state: Option<State>,
42 /// Undo tree.
43 /// Undo tree.
44+ // It can be used as a cell where we will take it out to get some parts of the history and put
45+ // it back as it separated from the edits. We could split out the parts manually but that will
46+ // be more troublesome.
43− history: History,
47+ history: Cell<History>,
44 last_saved_revision: usize,
48 last_saved_revision: usize,
45 version: i32, // should be usize?
49 version: i32, // should be usize?
@@ -121,7 +125,7 @@ impl Document {
121 old_state,
125 old_state,
122 diagnostics: Vec::new(),
126 diagnostics: Vec::new(),
123 version: 0,
127 version: 0,
124− history: History::default(),
128+ history: Cell::new(History::default()),
125 last_saved_revision: 0,
129 last_saved_revision: 0,
126 language_server: None,
130 language_server: None,
@@ -190,7 +194,9 @@ impl Document {
190 let language_server = self.language_server.clone();
194 let language_server = self.language_server.clone();
192 // reset the modified flag
196 // reset the modified flag
197+ let history = self.history.take();
193− self.last_saved_revision = self.history.current_revision();
198+ self.last_saved_revision = history.current_revision();
199+ self.history.set(history);
195 async move {
201 async move {
196 use tokio::{fs::File, io::AsyncWriteExt};
202 use tokio::{fs::File, io::AsyncWriteExt};
@@ -335,7 +341,8 @@ impl Document {
337 pub fn undo(&mut self, view_id: ViewId) -> bool {
343 pub fn undo(&mut self, view_id: ViewId) -> bool {
344+ let mut history = self.history.take();
338− if let Some(transaction) = self.history.undo() {
345+ if let Some(transaction) = history.undo() {
339 let success = self._apply(&transaction, view_id);
346 let success = self._apply(&transaction, view_id);
341 // reset changeset to fix len
348 // reset changeset to fix len
@@ -343,11 +350,13 @@ impl Document {
344 return success;
351 return success;
353+ self.history.set(history);
349 pub fn redo(&mut self, view_id: ViewId) -> bool {
357 pub fn redo(&mut self, view_id: ViewId) -> bool {
358+ let mut history = self.history.take();
350− if let Some(transaction) = self.history.redo() {
359+ if let Some(transaction) = history.redo() {
351 let success = self._apply(&transaction, view_id);
360 let success = self._apply(&transaction, view_id);
353 // reset changeset to fix len
362 // reset changeset to fix len
@@ -355,6 +364,7 @@ impl Document {
356 return success;
365 return success;
367+ self.history.set(history);
@@ -373,7 +383,9 @@ impl Document {
373 // HAXX: we need to reconstruct the state as it was before the changes..
383 // HAXX: we need to reconstruct the state as it was before the changes..
374 let old_state = self.old_state.take().expect("no old_state available");
384 let old_state = self.old_state.take().expect("no old_state available");
386+ let mut history = self.history.take();
376− self.history.commit_revision(&transaction, &old_state);
387+ history.commit_revision(&transaction, &old_state);
388+ self.history.set(history);
379 #[inline]
391 #[inline]
@@ -383,9 +395,11 @@ impl Document {
384 #[inline]
396 #[inline]
385 pub fn is_modified(&self) -> bool {
397 pub fn is_modified(&self) -> bool {
398+ let history = self.history.take();
399+ let current_revision = history.current_revision();
400+ self.history.set(history);
386 self.path.is_some()
401 self.path.is_some()
387− && (self.history.current_revision() != self.last_saved_revision
402+ && (current_revision != self.last_saved_revision || !self.changes.is_empty())
388− || !self.changes.is_empty())
391 #[inline]
405 #[inline]