Skip to content

Commit e9f5306

Browse files
committed
remove-left tracks current position correctly
1 parent 8e5e7ec commit e9f5306

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/rewrite_clj/zip/utils.clj

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
;; ## Remove
55

66
(defn- update-in-path
7-
[{:keys [node path] :as loc} k f]
7+
[loc k f]
88
(if-let [v (get loc k)]
99
(assoc loc :changed? true k (f v))
1010
loc))
@@ -16,8 +16,13 @@
1616

1717
(defn remove-left
1818
"Remove left sibling of the current node (if there is one)."
19-
[loc]
20-
(update-in-path loc :left pop))
19+
[{:keys [left] :as loc}]
20+
(if-let [[_ lpos] (peek left)]
21+
(assoc loc
22+
:left (pop left)
23+
:position lpos
24+
:changed? true)
25+
loc))
2126

2227
(defn remove-right-while
2328
"Remove elements to the right of the current zipper location as long as

test/rewrite_clj/zip/utils_test.clj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,12 @@
4646
3 [1 4]
4747
1 [1 2]
4848
2 [1 3])
49+
50+
(tabular
51+
(fact "`remove-left` tracks current position correctly"
52+
(let [root (base/of-string "[a bb ccc]")
53+
zloc (nth (iterate z/next root) ?n)]
54+
(z/position (remove-left zloc)) => ?pos))
55+
?n ?pos
56+
3 [1 3]
57+
5 [1 6])

0 commit comments

Comments
 (0)