on master help sign in
fix change -> change -> undo -> change -> undo -> undo.
commitea502c8665332932b2311df3852b5ac8df6509af authorBlaž Hrastnik <blaz@mxxn.io> dateMon 21 Dec 2020, 13:58 (6 years ago) parentd181027
2 files changed+49−11
helix-term/src/commands.rs+5−11 helix-view/src/document.rs+44
helix-term/src/commands.rs+5−11
@@ -660,21 +660,15 @@ pub fn insert_char_prompt(prompt: &mut Prompt, c: char) {
660660
661661// Undo / Redo
662662
663−pub fn undo(cx: &mut Context) {
664− if let Some(revert) = cx.view.doc.history.undo() {
665− cx.view.doc.version += 1;
666− cx.view.doc.apply(&revert);
667− // TODO: undo/redo needs to avoid storing in self.changes/self.old_state
668− }
663+// TODO: each command could simply return a Option<transaction>, then the higher level handles
664+// storing it?
669665
670− // TODO: each command could simply return a Option<transaction>, then the higher level handles storing it?
666+pub fn undo(cx: &mut Context) {
667+ cx.view.doc.undo();
671668}
672669
673670pub fn redo(cx: &mut Context) {
674− if let Some(transaction) = cx.view.doc.history.redo() {
675− cx.view.doc.version += 1;
676− cx.view.doc.apply(&transaction);
677− }
671+ cx.view.doc.redo();
678672}
679673
680674// Yank / Paste
helix-view/src/document.rs+44
@@ -185,6 +185,50 @@ impl Document {
185185 success
186186 }
187187
188+ pub fn undo(&mut self) -> bool {
189+ if let Some(transaction) = self.history.undo() {
190+ let old_doc = self.text().clone();
191+ self.version += 1;
192+ let success = transaction.apply(&mut self.state);
193+
194+ // update tree-sitter syntax tree
195+ if let Some(syntax) = &mut self.syntax {
196+ // TODO: no unwrap
197+ syntax
198+ .update(&old_doc, &self.state.doc, transaction.changes())
199+ .unwrap();
200+ }
201+
202+ // reset changeset to fix len
203+ self.changes = ChangeSet::new(self.text());
204+
205+ return success;
206+ }
207+ false
208+ }
209+
210+ pub fn redo(&mut self) -> bool {
211+ if let Some(transaction) = self.history.redo() {
212+ let old_doc = self.text().clone();
213+ self.version += 1;
214+ let success = transaction.apply(&mut self.state);
215+
216+ // update tree-sitter syntax tree
217+ if let Some(syntax) = &mut self.syntax {
218+ // TODO: no unwrap
219+ syntax
220+ .update(&old_doc, &self.state.doc, transaction.changes())
221+ .unwrap();
222+ }
223+
224+ // reset changeset to fix len
225+ self.changes = ChangeSet::new(self.text());
226+
227+ return success;
228+ }
229+ false
230+ }
231+
188232 #[inline]
189233 pub fn mode(&self) -> Mode {
190234 self.mode