Add initial support for undo/redo stack

We're still not capturing selections/anchors, that's right up next.
This commit is contained in:
Antonio Scandurra
2021-04-12 16:01:41 +02:00
parent 07b8a105a6
commit e534fe9112
2 changed files with 141 additions and 0 deletions
+14
View File
@@ -28,6 +28,8 @@ pub fn init(app: &mut App) {
app.add_bindings(vec![
Binding::new("backspace", "buffer:backspace", Some("BufferView")),
Binding::new("enter", "buffer:newline", Some("BufferView")),
Binding::new("cmd-z", "buffer:undo", Some("BufferView")),
Binding::new("cmd-shift-Z", "buffer:redo", Some("BufferView")),
Binding::new("up", "buffer:move_up", Some("BufferView")),
Binding::new("down", "buffer:move_down", Some("BufferView")),
Binding::new("left", "buffer:move_left", Some("BufferView")),
@@ -52,6 +54,8 @@ pub fn init(app: &mut App) {
app.add_action("buffer:insert", BufferView::insert);
app.add_action("buffer:newline", BufferView::newline);
app.add_action("buffer:backspace", BufferView::backspace);
app.add_action("buffer:undo", BufferView::undo);
app.add_action("buffer:redo", BufferView::redo);
app.add_action("buffer:move_up", BufferView::move_up);
app.add_action("buffer:move_down", BufferView::move_down);
app.add_action("buffer:move_left", BufferView::move_left);
@@ -435,6 +439,16 @@ impl BufferView {
self.insert(&String::new(), ctx);
}
pub fn undo(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
self.buffer
.update(ctx, |buffer, ctx| buffer.undo(Some(ctx)));
}
pub fn redo(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
self.buffer
.update(ctx, |buffer, ctx| buffer.redo(Some(ctx)));
}
pub fn move_left(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
{
let app = ctx.app();