on master help sign in
fix change -> change -> undo -> change -> undo -> undo.
ea502c8Blaž Hrastnik
helix-term/src/commands.rs+5−11
@@ -660,21 +660,15 @@ pub fn insert_char_prompt(prompt: &mut Prompt, c: char) {
660
660
661// Undo / Redo
661// Undo / Redo
662
662
663−pub fn undo(cx: &mut Context) {
663+// TODO: each command could simply return a Option<transaction>, then the higher level handles
664− if let Some(revert) = cx.view.doc.history.undo() {
664+// storing it?
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− }
669
665
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();
671}
668}
672
669
673pub fn redo(cx: &mut Context) {
670pub fn redo(cx: &mut Context) {
674− if let Some(transaction) = cx.view.doc.history.redo() {
671+ cx.view.doc.redo();
675− cx.view.doc.version += 1;
676− cx.view.doc.apply(&transaction);
677− }
678}
672}
679
673
680// Yank / Paste
674// Yank / Paste
helix-view/src/document.rs+44
@@ -185,6 +185,50 @@ impl Document {
185 success
185 success
186 }
186 }
187
187
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+
188 #[inline]
232 #[inline]
189 pub fn mode(&self) -> Mode {
233 pub fn mode(&self) -> Mode {
190 self.mode
234 self.mode