Apply transactions to the jumplist for undo/redo (#4227)

Undo/redo/earlier/later call `Document::apply_impl` which applies
transactions to the document. These transactions also need to be
applied to the view as in 0aedef0.
pull/4281/head
Michael Davis 2 years ago committed by GitHub
parent 7f75458e6f
commit e16c632760
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3258,7 +3258,7 @@ fn undo(cx: &mut Context) {
let count = cx.count(); let count = cx.count();
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
for _ in 0..count { for _ in 0..count {
if !doc.undo(view.id) { if !doc.undo(view) {
cx.editor.set_status("Already at oldest change"); cx.editor.set_status("Already at oldest change");
break; break;
} }
@ -3269,7 +3269,7 @@ fn redo(cx: &mut Context) {
let count = cx.count(); let count = cx.count();
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
for _ in 0..count { for _ in 0..count {
if !doc.redo(view.id) { if !doc.redo(view) {
cx.editor.set_status("Already at newest change"); cx.editor.set_status("Already at newest change");
break; break;
} }
@ -3281,7 +3281,7 @@ fn earlier(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
for _ in 0..count { for _ in 0..count {
// rather than doing in batch we do this so get error halfway // rather than doing in batch we do this so get error halfway
if !doc.earlier(view.id, UndoKind::Steps(1)) { if !doc.earlier(view, UndoKind::Steps(1)) {
cx.editor.set_status("Already at oldest change"); cx.editor.set_status("Already at oldest change");
break; break;
} }
@ -3293,7 +3293,7 @@ fn later(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
for _ in 0..count { for _ in 0..count {
// rather than doing in batch we do this so get error halfway // rather than doing in batch we do this so get error halfway
if !doc.later(view.id, UndoKind::Steps(1)) { if !doc.later(view, UndoKind::Steps(1)) {
cx.editor.set_status("Already at newest change"); cx.editor.set_status("Already at newest change");
break; break;
} }

@ -483,7 +483,7 @@ fn earlier(
let uk = args.join(" ").parse::<UndoKind>().map_err(|s| anyhow!(s))?; let uk = args.join(" ").parse::<UndoKind>().map_err(|s| anyhow!(s))?;
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let success = doc.earlier(view.id, uk); let success = doc.earlier(view, uk);
if !success { if !success {
cx.editor.set_status("Already at oldest change"); cx.editor.set_status("Already at oldest change");
} }
@ -502,7 +502,7 @@ fn later(
let uk = args.join(" ").parse::<UndoKind>().map_err(|s| anyhow!(s))?; let uk = args.join(" ").parse::<UndoKind>().map_err(|s| anyhow!(s))?;
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let success = doc.later(view.id, uk); let success = doc.later(view, uk);
if !success { if !success {
cx.editor.set_status("Already at newest change"); cx.editor.set_status("Already at newest change");
} }

@ -834,11 +834,11 @@ impl Document {
success success
} }
fn undo_redo_impl(&mut self, view_id: ViewId, undo: bool) -> bool { fn undo_redo_impl(&mut self, view: &mut View, undo: bool) -> bool {
let mut history = self.history.take(); let mut history = self.history.take();
let txn = if undo { history.undo() } else { history.redo() }; let txn = if undo { history.undo() } else { history.redo() };
let success = if let Some(txn) = txn { let success = if let Some(txn) = txn {
self.apply_impl(txn, view_id) self.apply_impl(txn, view.id) && view.apply(txn, self)
} else { } else {
false false
}; };
@ -852,13 +852,13 @@ impl Document {
} }
/// Undo the last modification to the [`Document`]. Returns whether the undo was successful. /// Undo the last modification to the [`Document`]. Returns whether the undo was successful.
pub fn undo(&mut self, view_id: ViewId) -> bool { pub fn undo(&mut self, view: &mut View) -> bool {
self.undo_redo_impl(view_id, true) self.undo_redo_impl(view, true)
} }
/// Redo the last modification to the [`Document`]. Returns whether the redo was successful. /// Redo the last modification to the [`Document`]. Returns whether the redo was successful.
pub fn redo(&mut self, view_id: ViewId) -> bool { pub fn redo(&mut self, view: &mut View) -> bool {
self.undo_redo_impl(view_id, false) self.undo_redo_impl(view, false)
} }
pub fn savepoint(&mut self) { pub fn savepoint(&mut self) {
@ -871,7 +871,7 @@ impl Document {
} }
} }
fn earlier_later_impl(&mut self, view_id: ViewId, uk: UndoKind, earlier: bool) -> bool { fn earlier_later_impl(&mut self, view: &mut View, uk: UndoKind, earlier: bool) -> bool {
let txns = if earlier { let txns = if earlier {
self.history.get_mut().earlier(uk) self.history.get_mut().earlier(uk)
} else { } else {
@ -879,7 +879,7 @@ impl Document {
}; };
let mut success = false; let mut success = false;
for txn in txns { for txn in txns {
if self.apply_impl(&txn, view_id) { if self.apply_impl(&txn, view.id) && view.apply(&txn, self) {
success = true; success = true;
} }
} }
@ -891,13 +891,13 @@ impl Document {
} }
/// Undo modifications to the [`Document`] according to `uk`. /// Undo modifications to the [`Document`] according to `uk`.
pub fn earlier(&mut self, view_id: ViewId, uk: UndoKind) -> bool { pub fn earlier(&mut self, view: &mut View, uk: UndoKind) -> bool {
self.earlier_later_impl(view_id, uk, true) self.earlier_later_impl(view, uk, true)
} }
/// Redo modifications to the [`Document`] according to `uk`. /// Redo modifications to the [`Document`] according to `uk`.
pub fn later(&mut self, view_id: ViewId, uk: UndoKind) -> bool { pub fn later(&mut self, view: &mut View, uk: UndoKind) -> bool {
self.earlier_later_impl(view_id, uk, false) self.earlier_later_impl(view, uk, false)
} }
/// Commit pending changes to history /// Commit pending changes to history

Loading…
Cancel
Save