Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat modifications while in INSERT mode as a single undo stack #991

Merged
merged 2 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion extensions/vi-mode/lem-vi-mode.asd
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
:components
((:file "motion")
(:file "operator")
(:file "visual")))
(:file "visual")
(:file "commands")))
(:file "utils"
:pathname "tests/utils"))
:perform (test-op (op c) (symbol-call :rove '#:run c)))
27 changes: 27 additions & 0 deletions extensions/vi-mode/tests/commands.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(defpackage :lem-vi-mode/tests/commands
(:use :cl
:lem
:rove
:lem-vi-mode/tests/utils)
(:import-from :lem-fake-interface
:with-fake-interface)
(:import-from :named-readtables
:in-readtable))
(in-package :lem-vi-mode/tests/commands)

(in-readtable :interpol-syntax)

(deftest vi-undo
(with-fake-interface ()
(with-vi-buffer (#?"[1]\n2\n3\n")
(cmd "a: Hello!<Esc>")
(ok (buf= #?"1: Hello[!]\n2\n3\n"))
(ok (state= :normal))
(cmd "u")
(ok (buf= #?"[1]\n2\n3\n"))
(cmd "<C-r>")
(ok (buf= #?"1: Hello[!]\n2\n3\n"))
(cmd "o4: World!<Esc>")
(ok (buf= #?"1: Hello!\n4: World[!]\n2\n3\n"))
(cmd "2u")
(ok (buf= #?"[1]\n2\n3\n")))))
12 changes: 8 additions & 4 deletions extensions/vi-mode/tests/utils.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@
(appendf key-args '(:shift t)))))
(let ((sym-str (ppcre:scan-to-strings "[^<-]+(?=>$)" key-str)))
(apply #'make-key :sym (if-let (char (name-char sym-str))
(string char)
(if (char= char #\Esc)
"Escape"
(string char))
sym-str)
key-args))))
keys))
Expand All @@ -187,7 +189,9 @@
(multiple-value-bind (buffer-text position visual-regions)
(parse-buffer-string content)
(let ((point (buffer-point buffer)))
(insert-string point buffer-text)
(lem:insert-string point buffer-text)
(setf (lem-base::buffer-edit-history buffer)
(make-array 0 :adjustable t :fill-pointer 0))
Comment on lines +193 to +194
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be a good idea to prepare an API for buffer-edit-history and use that.

(when position
(move-to-position point position))
(dolist (region visual-regions)
Expand Down Expand Up @@ -329,10 +333,10 @@
:negative negative))

(defmethod form-description ((function (eql 'text=)) args values &key negative)
(declare (ignore args))
(let ((expected-text (first values))
(actual-text (buffer-text (current-buffer))))
(format nil "Expect ~W~:[~; not~] to be ~S (actual: ~S)"
(first args)
(format nil "Expect the buffer text~:[~; not~] to be ~S (actual: ~S)"
negative
(text-backslashed expected-text)
(text-backslashed actual-text))))
Expand Down
8 changes: 8 additions & 0 deletions extensions/vi-mode/vi-mode.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@
(when (and (typep command 'vi-command)
(eq (vi-command-repeat command) t))
(setf *last-repeat-keys* (vi-this-command-keys)))))

(defmethod post-command-hook ((state insert))
(when (eq :separator (lem-base::last-edit-history (current-buffer)))
(vector-pop (lem-base::buffer-edit-history (current-buffer)))))

(defmethod state-disabled-hook ((state insert))
(unless (eq :separator (lem-base::last-edit-history (current-buffer)))
(buffer-undo-boundary)))