Skip to content

Commit 666ec15

Browse files
committed
Move :changed? up a level
1 parent e3fe1cb commit 666ec15

File tree

2 files changed

+45
-56
lines changed

2 files changed

+45
-56
lines changed

src/rewrite_clj/zip/utils.clj

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
[{:keys [node path parent position] :as loc} k f]
88
(let [v (get path k)]
99
(if (seq v)
10-
{:node node
11-
:path (assoc path k (f v) :changed? true)
12-
:parent parent
13-
:position position}
10+
(assoc loc
11+
:changed? true
12+
:node node
13+
:path (assoc path k (f v)))
1414
loc)))
1515

1616
(defn remove-right
@@ -52,21 +52,17 @@
5252
location, returns `nil`."
5353
[{:keys [position parent] {:keys [l] :as path} :path :as loc}]
5454
(if (seq l)
55-
{:node (peek l)
56-
:path (-> path
57-
(update-in [:l] pop)
58-
(assoc :changed? true))
59-
:parent parent
60-
:position position}))
55+
(assoc loc
56+
:changed? true
57+
:node (peek l)
58+
:path (update-in path [:l] pop))))
6159

6260
(defn remove-and-move-right
6361
"Remove current node and move right. If current node is at the rightmost
6462
location, returns `nil`."
6563
[{:keys [position parent] {:keys [r] :as path} :path :as loc}]
6664
(if (seq r)
67-
{:node (first r)
68-
:path (-> path
69-
(update-in [:r] next)
70-
(assoc :changed? true))
71-
:parent parent
72-
:position position}))
65+
(assoc loc
66+
:changed? true
67+
:node (first r)
68+
:path (update-in path [:r] next))))

src/rewrite_clj/zip/zip.clj

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,17 @@
7676
"Returns the loc of the parent of the node at this loc, or nil if at
7777
the top"
7878
[loc]
79-
(let [{:keys [node parent position] {:keys [l ppath r changed?]} :path} loc]
79+
(let [{:keys [node parent position changed?] {:keys [l ppath r]} :path} loc]
8080
(when parent
8181
(if changed?
82-
{:node (make-node loc (:node parent) (concat l (cons node r)))
83-
:path (and ppath (assoc ppath :changed? true))
84-
:parent (:parent parent)
85-
:position position}
82+
(assoc parent
83+
:changed? true
84+
:node (make-node loc (:node parent) (concat l (cons node r)))
85+
:path ppath)
8686
parent))))
8787

8888
(defn root
89-
"zips all the way up and returns the root node, reflecting any
90-
changes."
89+
"zips all the way up and returns the root node, reflecting any changes."
9190
[{:keys [end?] :as loc}]
9291
(if end?
9392
(node loc)
@@ -101,10 +100,10 @@
101100
[loc]
102101
(let [{:keys [node parent position] {l :l [r & rnext :as rs] :r :as path} :path} loc]
103102
(when (and path rs)
104-
{:node r
105-
:path (assoc path :l (conj l node) :r rnext)
106-
:parent parent
107-
:position (node/+extent position (node/extent node))})))
103+
(assoc loc
104+
:node r
105+
:path (assoc path :l (conj l node) :r rnext)
106+
:position (node/+extent position (node/extent node))))))
108107

109108
(defn rightmost
110109
"Returns the loc of the rightmost sibling of the node at this loc, or self"
@@ -118,33 +117,30 @@
118117
[loc]
119118
(let [{:keys [node parent position] {:keys [l r] :as path} :path} loc]
120119
(when (and path (seq l))
121-
{:node (peek l)
122-
:path (assoc path :l (pop l) :r (cons node r))
123-
:parent parent
124-
:position position})))
120+
(assoc loc
121+
:node (peek l)
122+
:path (assoc path :l (pop l) :r (cons node r))))))
125123

126124
(defn leftmost
127125
"Returns the loc of the leftmost sibling of the node at this loc, or self"
128126
[loc]
129127
(let [{:keys [node parent position] {:keys [l r] :as path} :path} loc]
130128
(if (and path (seq l))
131-
{:node (first l)
132-
:path (assoc path :l [] :r (concat (rest l) [node] r))
133-
:parent parent
134-
:position position}
129+
(assoc loc
130+
:node (first l)
131+
:path (assoc path :l [] :r (concat (rest l) [node] r)))
135132
loc)))
136133

137134
(defn insert-left
138135
"Inserts the item as the left sibling of the node at this loc,
139136
without moving"
140137
[loc item]
141-
(let [{:keys [node parent position] {:keys [l] :as path} :path} loc]
138+
(let [{:keys [parent position] {:keys [l] :as path} :path} loc]
142139
(if (nil? path)
143140
(throw (new Exception "Insert at top"))
144-
{:node node
145-
:path (assoc path :l (conj l item) :changed? true)
146-
:parent parent
147-
:position position})))
141+
(assoc loc
142+
:changed? true
143+
:path (assoc path :l (conj l item))))))
148144

149145
(defn insert-right
150146
"Inserts the item as the right sibling of the node at this loc,
@@ -153,18 +149,14 @@
153149
(let [{:keys [node parent position] {:keys [r] :as path} :path} loc]
154150
(if (nil? path)
155151
(throw (new Exception "Insert at top"))
156-
{:node node
157-
:path (assoc path :r (cons item r) :changed? true)
158-
:parent parent
159-
:position position})))
152+
(assoc loc
153+
:changed? true
154+
:path (assoc path :r (cons item r))))))
160155

161156
(defn replace
162157
"Replaces the node at this loc, without moving"
163158
[loc node]
164-
(let [{:keys [path]} loc]
165-
(assoc loc
166-
:node node
167-
:path (assoc path :changed? true))))
159+
(assoc loc :changed? true :node node))
168160

169161
(defn edit
170162
"Replaces the node at this loc with the value of (f node args)"
@@ -222,14 +214,15 @@
222214
(if (nil? path)
223215
(throw (new Exception "Remove at top"))
224216
(if (pos? (count l))
225-
(loop [loc {:node (peek l)
226-
:path (assoc path :l (pop l) :changed? true)
227-
:parent parent
228-
:position position}]
217+
(loop [loc (assoc loc
218+
:changed? true
219+
:node (peek l)
220+
:path (assoc path :l (pop l)))]
229221
(if-let [child (and (branch? loc) (down loc))]
230222
(recur (rightmost child))
231223
loc))
232-
{:node (make-node loc (:node parent) r)
233-
:path (and ppath (assoc ppath :changed? true))
234-
:parent (:parent parent)
235-
:position position}))))
224+
(assoc parent
225+
:changed? true
226+
:node (make-node loc (:node parent) r)
227+
:path ppath
228+
:parent (:parent parent))))))

0 commit comments

Comments
 (0)