|
76 | 76 | "Returns the loc of the parent of the node at this loc, or nil if at
|
77 | 77 | the top"
|
78 | 78 | [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] |
80 | 80 | (when parent
|
81 | 81 | (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) |
86 | 86 | parent))))
|
87 | 87 |
|
88 | 88 | (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." |
91 | 90 | [{:keys [end?] :as loc}]
|
92 | 91 | (if end?
|
93 | 92 | (node loc)
|
|
101 | 100 | [loc]
|
102 | 101 | (let [{:keys [node parent position] {l :l [r & rnext :as rs] :r :as path} :path} loc]
|
103 | 102 | (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)))))) |
108 | 107 |
|
109 | 108 | (defn rightmost
|
110 | 109 | "Returns the loc of the rightmost sibling of the node at this loc, or self"
|
|
118 | 117 | [loc]
|
119 | 118 | (let [{:keys [node parent position] {:keys [l r] :as path} :path} loc]
|
120 | 119 | (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)))))) |
125 | 123 |
|
126 | 124 | (defn leftmost
|
127 | 125 | "Returns the loc of the leftmost sibling of the node at this loc, or self"
|
128 | 126 | [loc]
|
129 | 127 | (let [{:keys [node parent position] {:keys [l r] :as path} :path} loc]
|
130 | 128 | (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))) |
135 | 132 | loc)))
|
136 | 133 |
|
137 | 134 | (defn insert-left
|
138 | 135 | "Inserts the item as the left sibling of the node at this loc,
|
139 | 136 | without moving"
|
140 | 137 | [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] |
142 | 139 | (if (nil? path)
|
143 | 140 | (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)))))) |
148 | 144 |
|
149 | 145 | (defn insert-right
|
150 | 146 | "Inserts the item as the right sibling of the node at this loc,
|
|
153 | 149 | (let [{:keys [node parent position] {:keys [r] :as path} :path} loc]
|
154 | 150 | (if (nil? path)
|
155 | 151 | (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)))))) |
160 | 155 |
|
161 | 156 | (defn replace
|
162 | 157 | "Replaces the node at this loc, without moving"
|
163 | 158 | [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)) |
168 | 160 |
|
169 | 161 | (defn edit
|
170 | 162 | "Replaces the node at this loc with the value of (f node args)"
|
|
222 | 214 | (if (nil? path)
|
223 | 215 | (throw (new Exception "Remove at top"))
|
224 | 216 | (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)))] |
229 | 221 | (if-let [child (and (branch? loc) (down loc))]
|
230 | 222 | (recur (rightmost child))
|
231 | 223 | 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