|
51 | 51 | (rest nodes) |
52 | 52 | nodes))) |
53 | 53 |
|
54 | | - |
55 | 54 | (defn- remove-ws-or-comment [zloc] |
56 | 55 | (if-not (ws/whitespace-or-comment? zloc) |
57 | 56 | zloc |
58 | 57 | (recur (z/remove* zloc)))) |
59 | 58 |
|
60 | | - |
61 | 59 | (defn- create-seq-node |
62 | 60 | "Creates a sequence node of given type `t` with node values of `v`" |
63 | 61 | [t v] |
|
75 | 73 | ;; Paredit functions |
76 | 74 | ;;***************************** |
77 | 75 |
|
78 | | - |
79 | | - |
80 | | - |
81 | 76 | (defn kill |
82 | 77 | "Kill all sibling nodes to the right of the current node in `zloc`. |
83 | 78 |
|
84 | 79 | - `[1 2| 3 4] => [1 2|]`" |
85 | 80 | [zloc] |
86 | 81 | (let [left (z/left* zloc)] |
87 | | - (-> zloc |
88 | | - (u/remove-right-while (constantly true)) |
89 | | - z/remove* |
90 | | - (#(if left |
| 82 | + (-> zloc |
| 83 | + (u/remove-right-while (constantly true)) |
| 84 | + z/remove* |
| 85 | + (#(if left |
91 | 86 | (global-find-by-node % (z/node left)) |
92 | 87 | %))))) |
93 | 88 |
|
94 | | - |
95 | | - |
96 | 89 | (defn- kill-in-string-node [zloc pos] |
97 | 90 | (if (= (z/string zloc) "\"\"") |
98 | 91 | (z/remove zloc) |
|
121 | 114 | (z/insert-right* % (nd/newlines 1)) |
122 | 115 | %)))))) |
123 | 116 |
|
124 | | - |
125 | | - |
126 | 117 | (defn kill-at-pos |
127 | 118 | "In string and comment aware kill |
128 | 119 |
|
|
147 | 138 | :else (kill candidate))) |
148 | 139 | zloc)) |
149 | 140 |
|
150 | | - |
151 | | - |
152 | 141 | (defn- find-word-bounds |
153 | 142 | [v col] |
154 | 143 | (when (<= col (count v)) |
|
162 | 151 | count |
163 | 152 | (+ col))])) |
164 | 153 |
|
165 | | - |
166 | 154 | (defn- remove-word-at |
167 | 155 | [v col] |
168 | 156 | (when-let [[start end] (find-word-bounds v col)] |
169 | 157 | (str (subs v 0 start) |
170 | 158 | (subs v end)))) |
171 | 159 |
|
172 | | - |
173 | | - |
174 | 160 | (defn- kill-word-in-comment-node [zloc pos] |
175 | 161 | (let [col-bounds (-> zloc z/node meta :col)] |
176 | | - (-> zloc |
177 | | - (z/replace (-> zloc |
178 | | - z/node |
179 | | - :s |
180 | | - (remove-word-at (- (:col pos) col-bounds)) |
181 | | - nd/comment-node))))) |
| 162 | + (-> zloc |
| 163 | + (z/replace (-> zloc |
| 164 | + z/node |
| 165 | + :s |
| 166 | + (remove-word-at (- (:col pos) col-bounds)) |
| 167 | + nd/comment-node))))) |
182 | 168 |
|
183 | 169 | (defn- kill-word-in-string-node [zloc pos] |
184 | 170 | (let [bounds (-> zloc z/node meta) |
185 | 171 | row-idx (- (:row pos) (:row bounds)) |
186 | 172 | col (if (= 0 row-idx) |
187 | 173 | (- (:col pos) (:col bounds)) |
188 | 174 | (:col pos))] |
189 | | - (-> zloc |
190 | | - (z/replace (-> zloc |
191 | | - z/node |
192 | | - :lines |
193 | | - (update-in [row-idx] |
194 | | - #(remove-word-at % col)) |
195 | | - nd/string-node))))) |
196 | | - |
197 | | - |
| 175 | + (-> zloc |
| 176 | + (z/replace (-> zloc |
| 177 | + z/node |
| 178 | + :lines |
| 179 | + (update-in [row-idx] |
| 180 | + #(remove-word-at % col)) |
| 181 | + nd/string-node))))) |
198 | 182 |
|
199 | 183 | (defn kill-one-at-pos |
200 | 184 | "In string and comment aware kill for one node/word at `pos` in `zloc`. |
|
217 | 201 | kill-in-node? (not (and (= (:row pos) bounds-row) |
218 | 202 | (<= (:col pos) bounds-col)))] |
219 | 203 | (cond |
220 | | - (and kill-in-node? (string-node? candidate)) (kill-word-in-string-node candidate pos) |
221 | | - (and kill-in-node? (ws/comment? candidate)) (kill-word-in-comment-node candidate pos) |
222 | | - (not (z/leftmost? candidate)) (-> (z/remove candidate) |
223 | | - (global-find-by-node (-> candidate z/left z/node))) |
224 | | - :else (z/remove candidate))) |
| 204 | + (and kill-in-node? (string-node? candidate)) (kill-word-in-string-node candidate pos) |
| 205 | + (and kill-in-node? (ws/comment? candidate)) (kill-word-in-comment-node candidate pos) |
| 206 | + (not (z/leftmost? candidate)) (-> (z/remove candidate) |
| 207 | + (global-find-by-node (-> candidate z/left z/node))) |
| 208 | + :else (z/remove candidate))) |
225 | 209 | zloc)) |
226 | 210 |
|
227 | | - |
228 | 211 | (defn- find-slurpee-up [zloc f] |
229 | 212 | (loop [l (z/up zloc) |
230 | 213 | n 1] |
231 | 214 | (cond |
232 | | - (nil? l) nil |
233 | | - (not (nil? (f l))) [n (f l)] |
234 | | - (nil? (z/up l)) nil |
235 | | - :else (recur (z/up l) (inc n))))) |
| 215 | + (nil? l) nil |
| 216 | + (not (nil? (f l))) [n (f l)] |
| 217 | + (nil? (z/up l)) nil |
| 218 | + :else (recur (z/up l) (inc n))))) |
236 | 219 |
|
237 | 220 | (defn- find-slurpee [zloc f] |
238 | 221 | (if (empty-seq? zloc) |
239 | 222 | [(f zloc) 0] |
240 | 223 | (some-> zloc (find-slurpee-up f) reverse))) |
241 | 224 |
|
242 | | - |
243 | | - |
244 | | - |
245 | 225 | (defn slurp-forward |
246 | 226 | "Pull in next right outer node (if none at first level, tries next etc) into |
247 | 227 | current S-expression |
|
275 | 255 | num-slurps (some-> curr-slurpee (nodes-by-dir z/right) count inc)] |
276 | 256 |
|
277 | 257 | (->> zloc |
278 | | - (iterate slurp-forward) |
279 | | - (take num-slurps) |
280 | | - last))) |
281 | | - |
| 258 | + (iterate slurp-forward) |
| 259 | + (take num-slurps) |
| 260 | + last))) |
282 | 261 |
|
283 | 262 | (defn slurp-backward |
284 | 263 | "Pull in prev left outer node (if none at first level, tries next etc) into |
|
317 | 296 | num-slurps (some-> curr-slurpee (nodes-by-dir z/left) count inc)] |
318 | 297 |
|
319 | 298 | (->> zloc |
320 | | - (iterate slurp-backward) |
321 | | - (take num-slurps) |
322 | | - last))) |
323 | | - |
| 299 | + (iterate slurp-backward) |
| 300 | + (take num-slurps) |
| 301 | + last))) |
324 | 302 |
|
325 | 303 | (defn barf-forward |
326 | 304 | "Push out the rightmost node of the current S-expression into outer right form. |
|
339 | 317 | (-> barfee-loc |
340 | 318 | (u/remove-left-while ws/whitespace-or-comment?) |
341 | 319 | (u/remove-right-while ws/whitespace?) |
342 | | - u/remove-and-move-up |
| 320 | + u/remove-and-move-up |
343 | 321 | (z/insert-right (z/node barfee-loc)) |
344 | 322 | ((partial reduce z/insert-right) preserves) |
345 | 323 | (#(or (global-find-by-node % (z/node zloc)) |
346 | | - (global-find-by-node % (z/node barfee-loc))))))))) |
347 | | - |
| 324 | + (global-find-by-node % (z/node barfee-loc))))))))) |
348 | 325 |
|
349 | 326 | (defn barf-backward |
350 | 327 | "Push out the leftmost node of the current S-expression into outer left form. |
|
367 | 344 | (#(or (global-find-by-node % (z/node zloc)) |
368 | 345 | (global-find-by-node % (z/node barfee-loc))))))))) |
369 | 346 |
|
370 | | - |
371 | 347 | (defn wrap-around |
372 | 348 | "Wrap current node with a given type `t` where `t` can be one of `:vector`, `:list`, `:set`, `:map` `:fn`. |
373 | 349 |
|
|
396 | 372 | "See [[rewrite-clj.zip/splice]]" |
397 | 373 | z/splice) |
398 | 374 |
|
399 | | - |
400 | 375 | (defn- splice-killing |
401 | 376 | [zloc f] |
402 | 377 | (if-not (z/up zloc) |
|
425 | 400 | (-> zloc z/up z/remove) |
426 | 401 | zloc))) |
427 | 402 |
|
428 | | - |
429 | 403 | (defn split |
430 | 404 | "Split current s-sexpression in two at given node `zloc` |
431 | 405 |
|
|
447 | 421 | (#(or (global-find-by-node % (z/node zloc)) |
448 | 422 | (global-find-by-node % (last lefts)))))))))) |
449 | 423 |
|
450 | | - |
451 | 424 | (defn- split-string [zloc pos] |
452 | 425 | (let [bounds (-> zloc z/node meta) |
453 | 426 | row-idx (- (:row pos) (:row bounds)) |
|
462 | 435 | (update-in [row-idx] #(subs % 0 split-col))))) |
463 | 436 | (z/insert-right (nd/string-node |
464 | 437 | (-> (drop row-idx lines) |
465 | | - vec |
| 438 | + vec |
466 | 439 | (update-in [0] #(subs % split-col)))))))) |
467 | 440 |
|
468 | | - |
469 | 441 | (defn split-at-pos |
470 | 442 | "In string aware split |
471 | 443 |
|
|
486 | 458 |
|
487 | 459 | (defn- join-seqs [left right] |
488 | 460 | (let [lefts (-> left z/node nd/children) |
489 | | - ws-nodes (-> (z/right* left) (nodes-by-dir z/right* ws/whitespace-or-comment?)) |
490 | | - rights (-> right z/node nd/children)] |
491 | | - |
492 | | - (-> right |
493 | | - z/remove* |
494 | | - remove-ws-or-comment |
495 | | - z/up |
496 | | - (z/insert-left (create-seq-node :vector |
497 | | - (concat lefts |
498 | | - ws-nodes |
499 | | - rights))) |
500 | | - z/remove |
501 | | - (global-find-by-node (first rights))))) |
| 461 | + ws-nodes (-> (z/right* left) (nodes-by-dir z/right* ws/whitespace-or-comment?)) |
| 462 | + rights (-> right z/node nd/children)] |
502 | 463 |
|
| 464 | + (-> right |
| 465 | + z/remove* |
| 466 | + remove-ws-or-comment |
| 467 | + z/up |
| 468 | + (z/insert-left (create-seq-node :vector |
| 469 | + (concat lefts |
| 470 | + ws-nodes |
| 471 | + rights))) |
| 472 | + z/remove |
| 473 | + (global-find-by-node (first rights))))) |
503 | 474 |
|
504 | 475 | (defn- join-strings [left right] |
505 | 476 | (-> right |
|
517 | 488 | (let [left (some-> zloc z/left) |
518 | 489 | right (if (some-> zloc z/node nd/whitespace?) (z/right zloc) zloc)] |
519 | 490 |
|
520 | | - |
521 | 491 | (if-not (and left right) |
522 | 492 | zloc |
523 | 493 | (cond |
524 | | - (and (z/seq? left) (z/seq? right)) (join-seqs left right) |
525 | | - (and (string-node? left) (string-node? right)) (join-strings left right) |
526 | | - :else zloc)))) |
527 | | - |
| 494 | + (and (z/seq? left) (z/seq? right)) (join-seqs left right) |
| 495 | + (and (string-node? left) (string-node? right)) (join-strings left right) |
| 496 | + :else zloc)))) |
528 | 497 |
|
529 | 498 | (defn raise |
530 | 499 | "Delete siblings and raise node at zloc one level up |
|
536 | 505 | (z/replace (z/node zloc))) |
537 | 506 | zloc)) |
538 | 507 |
|
539 | | - |
540 | 508 | (defn move-to-prev |
541 | 509 | "Move node at current location to the position of previous location given a depth first traversal |
542 | 510 |
|
|
0 commit comments