Skip to content

Commit

Permalink
Fixed an internal error related to undo when co-editing
Browse files Browse the repository at this point in the history
  • Loading branch information
cxxxr committed Jan 1, 2025
1 parent b15c5bb commit 538ca0d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/buffer/internal/edit.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
:type edit-kind
:read-only t)
(position (alexandria:required-argument :position)
:type (integer 0 *))
:type (integer 1 *))
(string (alexandria:required-argument :string)
:type string
:read-only t))
Expand Down Expand Up @@ -45,6 +45,7 @@
((:delete-string)
(when (< (edit-position src)
(edit-position dest))
(decf (edit-position dest) (length (edit-string src)))
(setf (edit-position dest)
(max 1 (- (edit-position dest) (length (edit-string src)))))
(when (< (edit-position dest) (edit-position src))
(setf (edit-position dest) (edit-position src)))))))
49 changes: 48 additions & 1 deletion tests/buffer/internal.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ qrstuvwxyz"
(point (lem:buffer-point buffer)))
(print-buffer point :cursor cursor))))

(deftest multiuser-undo
(deftest multiuser-undo-case-1
;; Arrange
(let* ((alice-buffer (lem:make-buffer "Alice's buffer" :temporary t))
(bob-buffer (lem:make-buffer "Bob's buffer" :temporary t)))
Expand Down Expand Up @@ -202,6 +202,53 @@ qrstuvwxyz"
(format t "Bob: ")
(print-buffer bob-point :cursor #'cl-ansi-text:green)

;; Assertion
(terpri)
(ok (equal "___" (lem:buffer-text alice-buffer)))
(ok (equal "___" (lem:buffer-text bob-buffer))))))

(deftest multiuser-undo-case-2
;; Arrange
(let* ((alice-buffer (lem:make-buffer "Alice's buffer" :temporary t))
(bob-buffer (lem:make-buffer "Bob's buffer" :temporary t)))

(lem:add-hook (lem:variable-value 'lem:before-change-functions :buffer alice-buffer)
(on-before-change "Alice"))
(lem:add-hook (lem:variable-value 'lem:after-change-functions :buffer alice-buffer)
(on-after-change #'cl-ansi-text:red))

(lem:add-hook (lem:variable-value 'lem:before-change-functions :buffer bob-buffer)
(on-before-change "Bob"))
(lem:add-hook (lem:variable-value 'lem:after-change-functions :buffer bob-buffer)
(on-after-change #'cl-ansi-text:green))

(format t "~%## Arrange~%")

(lem:with-point ((alice-point (lem:buffer-point alice-buffer) :right-inserting)
(alice-temporary-point (lem:buffer-point alice-buffer) :left-inserting)
(bob-point (lem:buffer-point bob-buffer) :right-inserting)
(bob-temporary-point (lem:buffer-point bob-buffer) :left-inserting))

;; Act
(format t "~%## Act~%")

(write-line "### Alice inserts \"abc\"")
(lem:insert-string alice-point "abc")
(lem:with-inhibit-undo ()
(lem:insert-string (lem:move-to-position bob-temporary-point
(lem:position-at-point alice-point))
"abc"))

(terpri)

(write-line "### Bob deletes \"abc\"")
(lem:delete-character (lem:buffer-start bob-point) 3)
(lem:with-inhibit-undo ()
(lem:delete-character (lem:move-to-position alice-temporary-point
(lem:position-at-point bob-point))
3))

(terpri)

;; Assertion
(pass "No internal errors within recompute-undo-position-offset"))))

0 comments on commit 538ca0d

Please sign in to comment.