Skip to content

Commit 6999a8f

Browse files
authored
paredit: whitespace formatting only (#355)
Before I embark on larger changes to the paredit API, get rid of some of the odd formatting in paredit. What was with all those newlines anyway?
1 parent 7104f50 commit 6999a8f

File tree

1 file changed

+50
-82
lines changed

1 file changed

+50
-82
lines changed

src/rewrite_clj/paredit.cljc

Lines changed: 50 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,11 @@
5151
(rest nodes)
5252
nodes)))
5353

54-
5554
(defn- remove-ws-or-comment [zloc]
5655
(if-not (ws/whitespace-or-comment? zloc)
5756
zloc
5857
(recur (z/remove* zloc))))
5958

60-
6159
(defn- create-seq-node
6260
"Creates a sequence node of given type `t` with node values of `v`"
6361
[t v]
@@ -75,24 +73,19 @@
7573
;; Paredit functions
7674
;;*****************************
7775

78-
79-
80-
8176
(defn kill
8277
"Kill all sibling nodes to the right of the current node in `zloc`.
8378
8479
- `[1 2| 3 4] => [1 2|]`"
8580
[zloc]
8681
(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
9186
(global-find-by-node % (z/node left))
9287
%)))))
9388

94-
95-
9689
(defn- kill-in-string-node [zloc pos]
9790
(if (= (z/string zloc) "\"\"")
9891
(z/remove zloc)
@@ -121,8 +114,6 @@
121114
(z/insert-right* % (nd/newlines 1))
122115
%))))))
123116

124-
125-
126117
(defn kill-at-pos
127118
"In string and comment aware kill
128119
@@ -147,8 +138,6 @@
147138
:else (kill candidate)))
148139
zloc))
149140

150-
151-
152141
(defn- find-word-bounds
153142
[v col]
154143
(when (<= col (count v))
@@ -162,39 +151,34 @@
162151
count
163152
(+ col))]))
164153

165-
166154
(defn- remove-word-at
167155
[v col]
168156
(when-let [[start end] (find-word-bounds v col)]
169157
(str (subs v 0 start)
170158
(subs v end))))
171159

172-
173-
174160
(defn- kill-word-in-comment-node [zloc pos]
175161
(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)))))
182168

183169
(defn- kill-word-in-string-node [zloc pos]
184170
(let [bounds (-> zloc z/node meta)
185171
row-idx (- (:row pos) (:row bounds))
186172
col (if (= 0 row-idx)
187173
(- (:col pos) (:col bounds))
188174
(: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)))))
198182

199183
(defn kill-one-at-pos
200184
"In string and comment aware kill for one node/word at `pos` in `zloc`.
@@ -217,31 +201,27 @@
217201
kill-in-node? (not (and (= (:row pos) bounds-row)
218202
(<= (:col pos) bounds-col)))]
219203
(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)))
225209
zloc))
226210

227-
228211
(defn- find-slurpee-up [zloc f]
229212
(loop [l (z/up zloc)
230213
n 1]
231214
(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)))))
236219

237220
(defn- find-slurpee [zloc f]
238221
(if (empty-seq? zloc)
239222
[(f zloc) 0]
240223
(some-> zloc (find-slurpee-up f) reverse)))
241224

242-
243-
244-
245225
(defn slurp-forward
246226
"Pull in next right outer node (if none at first level, tries next etc) into
247227
current S-expression
@@ -275,10 +255,9 @@
275255
num-slurps (some-> curr-slurpee (nodes-by-dir z/right) count inc)]
276256

277257
(->> zloc
278-
(iterate slurp-forward)
279-
(take num-slurps)
280-
last)))
281-
258+
(iterate slurp-forward)
259+
(take num-slurps)
260+
last)))
282261

283262
(defn slurp-backward
284263
"Pull in prev left outer node (if none at first level, tries next etc) into
@@ -317,10 +296,9 @@
317296
num-slurps (some-> curr-slurpee (nodes-by-dir z/left) count inc)]
318297

319298
(->> zloc
320-
(iterate slurp-backward)
321-
(take num-slurps)
322-
last)))
323-
299+
(iterate slurp-backward)
300+
(take num-slurps)
301+
last)))
324302

325303
(defn barf-forward
326304
"Push out the rightmost node of the current S-expression into outer right form.
@@ -339,12 +317,11 @@
339317
(-> barfee-loc
340318
(u/remove-left-while ws/whitespace-or-comment?)
341319
(u/remove-right-while ws/whitespace?)
342-
u/remove-and-move-up
320+
u/remove-and-move-up
343321
(z/insert-right (z/node barfee-loc))
344322
((partial reduce z/insert-right) preserves)
345323
(#(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)))))))))
348325

349326
(defn barf-backward
350327
"Push out the leftmost node of the current S-expression into outer left form.
@@ -367,7 +344,6 @@
367344
(#(or (global-find-by-node % (z/node zloc))
368345
(global-find-by-node % (z/node barfee-loc)))))))))
369346

370-
371347
(defn wrap-around
372348
"Wrap current node with a given type `t` where `t` can be one of `:vector`, `:list`, `:set`, `:map` `:fn`.
373349
@@ -396,7 +372,6 @@
396372
"See [[rewrite-clj.zip/splice]]"
397373
z/splice)
398374

399-
400375
(defn- splice-killing
401376
[zloc f]
402377
(if-not (z/up zloc)
@@ -425,7 +400,6 @@
425400
(-> zloc z/up z/remove)
426401
zloc)))
427402

428-
429403
(defn split
430404
"Split current s-sexpression in two at given node `zloc`
431405
@@ -447,7 +421,6 @@
447421
(#(or (global-find-by-node % (z/node zloc))
448422
(global-find-by-node % (last lefts))))))))))
449423

450-
451424
(defn- split-string [zloc pos]
452425
(let [bounds (-> zloc z/node meta)
453426
row-idx (- (:row pos) (:row bounds))
@@ -462,10 +435,9 @@
462435
(update-in [row-idx] #(subs % 0 split-col)))))
463436
(z/insert-right (nd/string-node
464437
(-> (drop row-idx lines)
465-
vec
438+
vec
466439
(update-in [0] #(subs % split-col))))))))
467440

468-
469441
(defn split-at-pos
470442
"In string aware split
471443
@@ -486,20 +458,19 @@
486458

487459
(defn- join-seqs [left right]
488460
(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)]
502463

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)))))
503474

504475
(defn- join-strings [left right]
505476
(-> right
@@ -517,14 +488,12 @@
517488
(let [left (some-> zloc z/left)
518489
right (if (some-> zloc z/node nd/whitespace?) (z/right zloc) zloc)]
519490

520-
521491
(if-not (and left right)
522492
zloc
523493
(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))))
528497

529498
(defn raise
530499
"Delete siblings and raise node at zloc one level up
@@ -536,7 +505,6 @@
536505
(z/replace (z/node zloc)))
537506
zloc))
538507

539-
540508
(defn move-to-prev
541509
"Move node at current location to the position of previous location given a depth first traversal
542510

0 commit comments

Comments
 (0)