on master help sign in
Fix undo/redo not updating the syntax tree.
commitc0e17dd324f016401d56d66b7c113dada0644155 authorBlaž Hrastnik <blaz@mxxn.io> dateFri 23 Oct 2020, 11:32 (6 years ago) parentb39849d
3 files changed+31−24
helix-core/src/history.rs+25−21 helix-view/src/commands.rs+6−2 helix-view/src/document.rs−1
helix-core/src/history.rs+25−21
@@ -57,37 +57,31 @@ impl History {
5757 self.cursor == 0
5858 }
5959
60− pub fn undo(&mut self, state: &mut State) {
60+ // TODO: I'd like to pass Transaction by reference but it fights with the borrowck
61+
62+ pub fn undo(&mut self) -> Option<Transaction> {
6163 if self.at_root() {
6264 // We're at the root of undo, nothing to do.
63− return;
65+ return None;
6466 }
6567
6668 let current_revision = &self.revisions[self.cursor];
6769
68− // TODO: pass the return value through? It should always succeed
69− let success = current_revision.revert.apply(state);
70−
71− if !success {
72− panic!("Failed to apply undo!");
73− }
74−
7570 self.cursor = current_revision.parent;
71+
72+ Some(current_revision.revert.clone())
7673 }
7774
78− pub fn redo(&mut self, state: &mut State) {
75+ pub fn redo(&mut self) -> Option<Transaction> {
7976 let current_revision = &self.revisions[self.cursor];
8077
8178 // for now, simply pick the latest child (linear undo / redo)
8279 if let Some((index, transaction)) = current_revision.children.last() {
83− let success = transaction.apply(state);
84−
85− if !success {
86− panic!("Failed to apply redo!");
87− }
88−
8980 self.cursor = *index;
81+
82+ return Some(transaction.clone());
9083 }
84+ None
9185 }
9286}
9387
@@ -120,17 +114,27 @@ mod test {
120114 assert_eq!("hello 世界!", state.doc());
121115
122116 // ---
117+ fn undo(history: &mut History, state: &mut State) {
118+ if let Some(transaction) = history.undo() {
119+ transaction.apply(state);
120+ }
121+ }
122+ fn redo(history: &mut History, state: &mut State) {
123+ if let Some(transaction) = history.redo() {
124+ transaction.apply(state);
125+ }
126+ }
123127
124− history.undo(&mut state);
128+ undo(&mut history, &mut state);
125129 assert_eq!("hello world!", state.doc());
126− history.redo(&mut state);
130+ redo(&mut history, &mut state);
127131 assert_eq!("hello 世界!", state.doc());
128− history.undo(&mut state);
129− history.undo(&mut state);
132+ undo(&mut history, &mut state);
133+ undo(&mut history, &mut state);
130134 assert_eq!("hello", state.doc());
131135
132136 // undo at root is a no-op
133− history.undo(&mut state);
137+ undo(&mut history, &mut state);
134138 assert_eq!("hello", state.doc());
135139 }
136140}
helix-view/src/commands.rs+6−2
@@ -509,13 +509,17 @@ pub fn insert_char_prompt(prompt: &mut Prompt, c: char) {
509509// Undo / Redo
510510
511511pub fn undo(view: &mut View, _count: usize) {
512− view.doc.history.undo(&mut view.doc.state);
512+ if let Some(revert) = view.doc.history.undo() {
513+ view.doc.apply(&revert);
514+ }
513515
514516 // TODO: each command could simply return a Option<transaction>, then the higher level handles storing it?
515517}
516518
517519pub fn redo(view: &mut View, _count: usize) {
518− view.doc.history.redo(&mut view.doc.state);
520+ if let Some(transaction) = view.doc.history.redo() {
521+ view.doc.apply(&transaction);
522+ }
519523}
520524
521525// Yank / Paste
helix-view/src/document.rs−1
@@ -108,7 +108,6 @@ impl Document {
108108 };
109109 }
110110
111− // TODO: needs to run on undo/redo
112111 pub fn apply(&mut self, transaction: &Transaction) -> bool {
113112 let old_doc = self.text().clone();
114113