Skip to content

Commit ff86d52

Browse files
author
Yannick Scherer
committed
Extended zipper capabilities.
1 parent 6e5e126 commit ff86d52

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/rewrite_clj/zip.clj

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
[node]
3939
(when (clojure.core/vector? node)
4040
(let [[k & _] node]
41-
(contains? #{:list :vector :set :map} k))))
41+
(contains? #{:list :vector :set :map :meta :meta* :reader-macro} k))))
4242

4343
(defn- z-make-node
4444
[node ch]
@@ -57,6 +57,7 @@
5757
(->> zloc
5858
(iterate f)
5959
(take-while identity)
60+
(take-while (complement z/end?))
6061
(drop-while p?)
6162
(first)))
6263

@@ -147,6 +148,16 @@
147148
(-> zloc (z/append-child item))
148149
(-> zloc (z/append-child SPACE) (z/append-child item)))))
149150

151+
(defn prepend-space
152+
"Prepend a whitespace node of the given width."
153+
([zloc] (prepend-space zloc 1))
154+
([zloc n] (z/insert-left zloc [:whitespace (apply str (repeat n \space))])))
155+
156+
(defn append-space
157+
"Append a whitespace node of the given width."
158+
([zloc] (append-space zloc 1))
159+
([zloc n] (z/insert-right zloc [:whitespace (apply str (repeat n \space))])))
160+
150161
;; ## Modify
151162

152163
(defn replace
@@ -166,6 +177,12 @@
166177
;; TODO
167178
(z/remove zloc))
168179

180+
(defn splice
181+
"Add the current node's children to the parent branch (in place of the current node)."
182+
[zloc]
183+
(let [ch (z/children zloc)]
184+
(-> (reduce z/insert-right zloc (reverse ch)) z/remove z/right)))
185+
169186
;; ## Others
170187

171188
(def node z/node)

0 commit comments

Comments
 (0)