Skip to content

Commit c415abe

Browse files
author
Yannick Scherer
committed
Moved prefix, suffix into rewrite-clj.zip.edit; added removal helpers to rewrite-clj.zip.utils
1 parent f296d08 commit c415abe

File tree

4 files changed

+114
-61
lines changed

4 files changed

+114
-61
lines changed

src/rewrite_clj/zip.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050

5151
insert-right insert-left
5252
insert-child append-child
53-
replace edit remove splice]
53+
replace edit remove splice
54+
prefix suffix]
5455

5556
[rewrite-clj.zip.seqs
5657

src/rewrite_clj/zip/edit.clj

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,57 @@
9595
z/remove
9696
z/next
9797
zc/skip-whitespace))))
98+
99+
;; ## Prefix
100+
101+
(defmulti prefix
102+
"Prefix the value of a zipper node with the given string. This supports multi-lined strings."
103+
(fn [zloc p]
104+
(when zloc
105+
(first (z/node zloc))))
106+
:default nil)
107+
108+
(defmethod prefix nil
109+
[zloc prefix]
110+
(throw (Exception. (str "Cannot prefix value of type: " (first (z/node zloc))))))
111+
112+
(defmethod prefix :token
113+
[zloc prefix]
114+
(let [v (second (z/node zloc))
115+
v1 (cond (string? v) (str prefix v)
116+
(symbol? v) (symbol (namespace v) (str prefix (name v)))
117+
(keyword? v) (keyword (namespace v) (str prefix (name v)))
118+
:else (throw (Exception. (str "Cannot prefix token: " v))))]
119+
(z/replace zloc [:token v1])))
120+
121+
(defmethod prefix :multi-line
122+
[zloc prefix]
123+
(let [[v & rst] (rest (z/node zloc))]
124+
(z/replace zloc (vec (list* :multi-line (str prefix v) rst)))))
125+
126+
;; ## Suffix
127+
128+
(defmulti suffix
129+
"Suffix the value of a zipper node with the given string. This supports multi-lined strings."
130+
(fn [zloc p]
131+
(when zloc
132+
(first (z/node zloc))))
133+
:default nil)
134+
135+
(defmethod suffix nil
136+
[zloc suffix]
137+
(throw (Exception. (str "Cannot suffix value of type: " (first (z/node zloc))))))
138+
139+
(defmethod suffix :token
140+
[zloc suffix]
141+
(let [v (second (z/node zloc))
142+
v1 (cond (string? v) (str v suffix)
143+
(symbol? v) (symbol (namespace v) (str (name v) suffix))
144+
(keyword? v) (keyword (namespace v) (str (name v) suffix))
145+
:else (throw (Exception. (str "Cannot suffix token: " v))))]
146+
(z/replace zloc [:token v1])))
147+
148+
(defmethod suffix :multi-line
149+
[zloc suffix]
150+
(let [[v & rst] (rest (z/node zloc))]
151+
(z/replace zloc (vec (list* :multi-line (str v suffix) rst)))))

src/rewrite_clj/zip/utils.clj

Lines changed: 55 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,57 @@
1-
(ns ^{ :doc "Utility operations for nodes."
1+
(ns ^{ :doc "Utility Operations for Zippers"
22
:author "Yannick Scherer" }
33
rewrite-clj.zip.utils
4-
(:require [fast-zip.core :as z]))
5-
6-
;; ## Prefix
7-
8-
(defmulti prefix
9-
"Prefix the value of a zipper node with the given string. This supports multi-lined strings."
10-
(fn [zloc p]
11-
(when zloc
12-
(first (z/node zloc))))
13-
:default nil)
14-
15-
(defmethod prefix nil
16-
[zloc prefix]
17-
(throw (Exception. (str "Cannot prefix value of type: " (first (z/node zloc))))))
18-
19-
(defmethod prefix :token
20-
[zloc prefix]
21-
(let [v (second (z/node zloc))
22-
v1 (cond (string? v) (str prefix v)
23-
(symbol? v) (symbol (namespace v) (str prefix (name v)))
24-
(keyword? v) (keyword (namespace v) (str prefix (name v)))
25-
:else (throw (Exception. (str "Cannot prefix token: " v))))]
26-
(z/replace zloc [:token v1])))
27-
28-
(defmethod prefix :multi-line
29-
[zloc prefix]
30-
(let [[v & rst] (rest (z/node zloc))]
31-
(z/replace zloc (vec (list* :multi-line (str prefix v) rst)))))
32-
33-
;; ## Suffix
34-
35-
(defmulti suffix
36-
"Suffix the value of a zipper node with the given string. This supports multi-lined strings."
37-
(fn [zloc p]
38-
(when zloc
39-
(first (z/node zloc))))
40-
:default nil)
41-
42-
(defmethod suffix nil
43-
[zloc suffix]
44-
(throw (Exception. (str "Cannot suffix value of type: " (first (z/node zloc))))))
45-
46-
(defmethod suffix :token
47-
[zloc suffix]
48-
(let [v (second (z/node zloc))
49-
v1 (cond (string? v) (str v suffix)
50-
(symbol? v) (symbol (namespace v) (str (name v) suffix))
51-
(keyword? v) (keyword (namespace v) (str (name v) suffix))
52-
:else (throw (Exception. (str "Cannot suffix token: " v))))]
53-
(z/replace zloc [:token v1])))
54-
55-
(defmethod suffix :multi-line
56-
[zloc suffix]
57-
(let [[v & rst] (rest (z/node zloc))]
58-
(z/replace zloc (vec (list* :multi-line (str v suffix) rst)))))
4+
(:require [fast-zip.core :as z])
5+
(:import [fast_zip.core ZipperPath ZipperLocation]))
6+
7+
(defn remove-right
8+
"Remove right sibling of the current node (if there is one)."
9+
[^ZipperLocation zloc]
10+
(let [path ^ZipperPath (.path zloc)]
11+
(if (zero? (count (.r path)))
12+
zloc
13+
(ZipperLocation.
14+
(.branch? zloc)
15+
(.children zloc)
16+
(.make-node zloc)
17+
(.node zloc)
18+
(assoc path :r (clojure.core/next (.r path)) :changed? true)))))
19+
20+
(defn remove-left
21+
"Remove left sibling of the current node (if there is one)."
22+
[^ZipperLocation zloc]
23+
(let [path ^ZipperPath (.path zloc)]
24+
(if (zero? (count (.l path)))
25+
zloc
26+
(ZipperLocation.
27+
(.branch? zloc)
28+
(.children zloc)
29+
(.make-node zloc)
30+
(.node zloc)
31+
(assoc path :l (pop (.l path)) :changed? true)))))
32+
33+
(defn remove-and-move-left
34+
"Remove current node and move left. If current node is at the leftmost
35+
location, returns `nil`."
36+
[^ZipperLocation zloc]
37+
(let [path ^ZipperPath (.path zloc)]
38+
(when (pos? (count (.l path)))
39+
(ZipperLocation.
40+
(.branch? zloc)
41+
(.children zloc)
42+
(.make-node zloc)
43+
(peek (.l path))
44+
(assoc path :l (pop (.l path)) :changed? true)))))
45+
46+
(defn remove-and-move-right
47+
"Remove current node and move right. If current node is at the rightmost
48+
location, returns `nil`."
49+
[^ZipperLocation zloc]
50+
(let [path ^ZipperPath (.path zloc)]
51+
(when (pos? (count (.r path)))
52+
(ZipperLocation.
53+
(.branch? zloc)
54+
(.children zloc)
55+
(.make-node zloc)
56+
(first (.r path))
57+
(assoc path :r (clojure.core/next (.r path)) :changed? true)))))

test/rewrite_clj/transform_test.clj

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
:author "Yannick Scherer" }
33
rewrite-clj.transform-test
44
(:require [midje.sweet :refer :all]
5-
[rewrite-clj.zip :as z]
6-
[rewrite-clj.zip.utils :as zu]))
5+
[rewrite-clj.zip :as z]))
76

87
(def data-string
98
";; This is a Project File.
@@ -39,7 +38,7 @@
3938
(z/map
4039
(fn [loc]
4140
(-> loc z/down
42-
(zu/prefix "prefix-")
41+
(z/prefix "prefix-")
4342
z/up))))
4443
z/->root-string)
4544
=>
@@ -59,7 +58,7 @@
5958
(z/map
6059
(fn [loc]
6160
(-> loc z/down
62-
(zu/suffix "-suffix")
61+
(z/suffix "-suffix")
6362
z/up))))
6463
z/->root-string)
6564
=>

0 commit comments

Comments
 (0)