git.fiddlerwoaroof.com
Browse code

chore: fix transforms to be immutable

Edward authored on 25/02/2021 12:07:12
Showing 1 changed files
... ...
@@ -42,12 +42,13 @@
42 42
         (end (subseq base point)))
43 43
     (concatenate 'string begin new end)))
44 44
 (defmethod edit ((base editable-string) (operation insert))
45
-  (let ((current-transform (transform base))
46
-        (op-point (point operation))
47
-        (string-to-edit (string-to-edit base)))
48
-    (setf (point operation) (funcall current-transform op-point)
49
-          (string-to-edit base) (do-insert string-to-edit (value operation) (point operation))
50
-          (transform base) (alexandria:compose (transform-for-op operation) current-transform))
45
+  (let* ((current-transform (transform base))
46
+         (op-point (point operation))
47
+         (string-to-edit (string-to-edit base))
48
+         (transformed-op (insert (funcall current-transform op-point)
49
+                                 (value operation))))
50
+    (setf (string-to-edit base) (do-insert string-to-edit (value transformed-op) (point transformed-op))
51
+          (transform base) (alexandria:compose (transform-for-op transformed-op) current-transform))
51 52
     base))
52 53
 
53 54
 (fw.lu:defclass+ replace-char (op)
... ...
@@ -68,12 +69,13 @@
68 69
         (end (subseq base (1+ point))))
69 70
     (concatenate 'string begin new end)))
70 71
 (defmethod edit ((base editable-string) (operation replace-char))
71
-  (let ((current-transform (transform base))
72
-        (op-point (point operation))
73
-        (string-to-edit (string-to-edit base)))
74
-    (setf (point operation) (funcall current-transform op-point)
75
-          (string-to-edit base) (do-replace-char string-to-edit (value operation) (point operation))
76
-          (transform base) (alexandria:compose (transform-for-op operation) current-transform))
72
+  (let* ((current-transform (transform base))
73
+         (op-point (point operation))
74
+         (string-to-edit (string-to-edit base))
75
+         (transformed-op (replace-char (funcall current-transform op-point)
76
+                                       (value operation))))
77
+    (setf (string-to-edit base) (do-replace-char string-to-edit (value transformed-op) (point transformed-op))
78
+          (transform base) (alexandria:compose (transform-for-op transformed-op) current-transform))
77 79
     base))
78 80
 
79 81
 
... ...
@@ -81,4 +83,23 @@
81 83
   (assert (equal (apply-edits "ac" (list (insert 0 "z")
82 84
                                          (insert 1 "b")
83 85
                                          (insert 2 "d")))
84
-                 "zabcd")))
86
+                 "zabcd"))
87
+  (assert
88
+   (let ((ops (list (insert 1 "fff")
89
+                    (insert 1 "ggg")
90
+                    (insert 1 "hhh"))))
91
+     (list (equal (apply-edits #1="abcd" ops)
92
+                  (apply-edits #1# (reverse ops)))
93
+           (equal (apply-edits #1# (reverse ops))
94
+                  (apply-edits #1# (serapeum:reshuffle ops)))
95
+           (equal (apply-edits #1# (reverse ops))
96
+                  "afffggghhhbcd"))))
97
+  (let ((ops (lambda ()
98
+               (list (insert 2 "fff")
99
+                     (insert 1 "bbb")
100
+                     (insert 1 "ddd")))))
101
+    (:printv (funcall ops))
102
+    (assert (equal (:printv (apply-edits "ac" (:printv (serapeum:reshuffle (funcall ops)))))
103
+                   (:printv (apply-edits "ac" (:printv (serapeum:reshuffle (funcall ops)))))))
104
+    (assert (equal (:printv (apply-edits "ace" (serapeum:reshuffle (funcall ops))))
105
+                   (:printv (apply-edits "ace" (serapeum:reshuffle (funcall ops))))))))