Skip to content

Commit 38b0fa6

Browse files
committed
docs: zip API docstrings are now clearer about coercion
For regular whitespace-smart operations: - consistently using parameter name `item` (replace fn was using `value`) - now expressly stating we do coercion on `item` For raw * operations: - parameter `item` is now described as "node `item`" to more clearly distinguish it from `item` above. I would have preferred naming the parameter `node`, but that would be in conflict/confusion with the `node` fn. - now expressly stating we do no coercion Closes #168
1 parent 7f4d4e4 commit 38b0fa6

File tree

7 files changed

+59
-43
lines changed

7 files changed

+59
-43
lines changed

CHANGELOG.adoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ For a list of breaking changes see link:#v1-breaking[breaking changes].
1616

1717
=== Unreleased
1818

19-
* docs: user guide correction, thanks @rgkirch!
19+
* docs:
20+
** user guide correction, thanks @rgkirch!
21+
** zip API docstrings now clearer about coercion https://github.com/clj-commons/rewrite-clj/issues/168[#168]
2022

2123
=== v1.0.699-alpha
2224

src/rewrite_clj/custom_zipper/core.cljc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
zloc)))
187187

188188
(defn-switchable insert-left
189-
"Returns zipper with `item` inserted as the left sibling of current node in `zloc`,
189+
"Returns zipper with node `item` inserted as the left sibling of current node in `zloc`,
190190
without moving location."
191191
[zloc item]
192192
(let [{:keys [parent position left]} zloc]
@@ -198,7 +198,7 @@
198198
:position (node/+extent position (node/extent item))))))
199199

200200
(defn-switchable insert-right
201-
"Returns zipper with `item` inserted as the right sibling of the current node in `zloc`,
201+
"Returns zipper with node `item` inserted as the right sibling of the current node in `zloc`,
202202
without moving location."
203203
[zloc item]
204204
(let [{:keys [parent right]} zloc]
@@ -209,9 +209,9 @@
209209
:right (cons item right)))))
210210

211211
(defn-switchable replace
212-
"Returns zipper with `node` replacing current node in `zloc`, without moving location."
213-
[zloc node]
214-
(assoc zloc :changed? true :node node))
212+
"Returns zipper with node `item` replacing current node in `zloc`, without moving location."
213+
[zloc item]
214+
(assoc zloc :changed? true :node item))
215215

216216
(defn edit
217217
"Returns zipper with value of `(apply f current-node args)` replacing current node in `zloc`.
@@ -223,13 +223,13 @@
223223
(apply clj-zip/edit zloc f args)))
224224

225225
(defn-switchable insert-child
226-
"Returns zipper with `item` inserted as the leftmost child of the current node in `zloc`,
226+
"Returns zipper with node `item` inserted as the leftmost child of the current node in `zloc`,
227227
without moving location."
228228
[zloc item]
229229
(replace zloc (make-node zloc (node zloc) (cons item (children zloc)))))
230230

231231
(defn-switchable append-child
232-
"Returns zipper with `item` inserted as the rightmost child of the current node in `zloc`,
232+
"Returns zipper with node `item` inserted as the rightmost child of the current node in `zloc`,
233233
without moving."
234234
[zloc item]
235235
(replace zloc (make-node zloc (node zloc) (concat (children zloc) [item]))))

src/rewrite_clj/zip.cljc

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,11 @@
304304

305305
;; DO NOT EDIT FILE, automatically imported from: rewrite-clj.zip.editz
306306
(defn replace
307-
"Return `zloc` with the current node replaced by `value`.
308-
If `value` is not already a node, an attempt will be made to coerce it to one.
307+
"Return `zloc` with the current node replaced by `item`.
308+
If `item` is not already a node, an attempt will be made to coerce it to one.
309309
310310
Use [[replace*]] for non-coercing version of replace."
311-
[zloc value] (rewrite-clj.zip.editz/replace zloc value))
311+
[zloc item] (rewrite-clj.zip.editz/replace zloc item))
312312

313313
;; DO NOT EDIT FILE, automatically imported from: rewrite-clj.zip.editz
314314
(defn edit
@@ -473,6 +473,8 @@
473473
;; DO NOT EDIT FILE, automatically imported from: rewrite-clj.zip.insert
474474
(defn insert-right
475475
"Return zipper with `item` inserted to the right of the current node in `zloc`, without moving location.
476+
If `item` is not already a node, an attempt will be made to coerce it to one.
477+
476478
Will insert a space if necessary.
477479
478480
Use [[rewrite-clj.zip/insert-right*]] to insert without adding any whitespace."
@@ -482,6 +484,7 @@
482484
(defn insert-left
483485
"Return zipper with `item` inserted to the left of the current node in `zloc`, without moving location.
484486
Will insert a space if necessary.
487+
If `item` is not already a node, an attempt will be made to coerce it to one.
485488
486489
Use [[insert-left*]] to insert without adding any whitespace."
487490
[zloc item] (rewrite-clj.zip.insert/insert-left zloc item))
@@ -490,6 +493,7 @@
490493
(defn insert-child
491494
"Return zipper with `item` inserted as the first child of the current node in `zloc`, without moving location.
492495
Will insert a space if necessary.
496+
If `item` is not already a node, an attempt will be made to coerce it to one.
493497
494498
Use [[insert-child*]] to insert without adding any whitespace."
495499
[zloc item] (rewrite-clj.zip.insert/insert-child zloc item))
@@ -498,6 +502,7 @@
498502
(defn append-child
499503
"Return zipper with `item` inserted as the last child of the current node in `zloc`, without moving.
500504
Will insert a space if necessary.
505+
If `item` is not already a node, an attempt will be made to coerce it to one.
501506
502507
Use [[append-child*]] to append without adding any whitespace."
503508
[zloc item] (rewrite-clj.zip.insert/append-child zloc item))
@@ -922,7 +927,6 @@
922927
"DEPRECATED: renamed to [[insert-newline-right]]."
923928
([zloc n] (rewrite-clj.zip.whitespace/append-newline zloc n))
924929
([zloc] (rewrite-clj.zip.whitespace/append-newline zloc)))
925-
;; TODO: clj-kondo barfs on an empty reader cond
926930
#?(:clj
927931

928932
;; DO NOT EDIT FILE, automatically imported from: rewrite-clj.zip.base
@@ -1013,14 +1017,24 @@ Returns zipper with location at the leftmost sibling of the current node in `zlo
10131017
NOTE: This function does not skip, nor provide any special handling for whitespace/comment nodes."
10141018
[zloc] (rewrite-clj.custom-zipper.core/leftmost zloc))
10151019

1020+
;; DO NOT EDIT FILE, automatically imported from: rewrite-clj.custom-zipper.core
1021+
(defn remove*
1022+
"Raw version of [[remove]].
1023+
1024+
Returns zipper with current node in `zloc` removed, with location at node that would have preceded
1025+
it in a depth-first walk.
1026+
1027+
NOTE: This function does not skip, nor provide any special handling for whitespace/comment nodes."
1028+
[zloc] (rewrite-clj.custom-zipper.core/remove zloc))
1029+
10161030
;; DO NOT EDIT FILE, automatically imported from: rewrite-clj.custom-zipper.core
10171031
(defn replace*
10181032
"Raw version of [[replace]].
10191033
1020-
Returns zipper with `node` replacing current node in `zloc`, without moving location.
1034+
Returns zipper with node `item` replacing current node in `zloc`, without moving location.
10211035
1022-
NOTE: This function does not skip, nor provide any special handling for whitespace/comment nodes."
1023-
[zloc node] (rewrite-clj.custom-zipper.core/replace zloc node))
1036+
NOTE: This function does no coercion, does not skip, nor provide any special handling for whitespace/comment nodes."
1037+
[zloc item] (rewrite-clj.custom-zipper.core/replace zloc item))
10241038

10251039
;; DO NOT EDIT FILE, automatically imported from: rewrite-clj.custom-zipper.core
10261040
(defn edit*
@@ -1030,55 +1044,45 @@ Returns zipper with value of `(apply f current-node args)` replacing current nod
10301044
10311045
The result of `f` should be a rewrite-clj node.
10321046
1033-
NOTE: This function does not skip, nor provide any special handling for whitespace/comment nodes."
1047+
NOTE: This function does no coercion, does not skip, nor provide any special handling for whitespace/comment nodes."
10341048
[zloc f & args] (apply rewrite-clj.custom-zipper.core/edit zloc f args))
10351049

1036-
;; DO NOT EDIT FILE, automatically imported from: rewrite-clj.custom-zipper.core
1037-
(defn remove*
1038-
"Raw version of [[remove]].
1039-
1040-
Returns zipper with current node in `zloc` removed, with location at node that would have preceded
1041-
it in a depth-first walk.
1042-
1043-
NOTE: This function does not skip, nor provide any special handling for whitespace/comment nodes."
1044-
[zloc] (rewrite-clj.custom-zipper.core/remove zloc))
1045-
10461050
;; DO NOT EDIT FILE, automatically imported from: rewrite-clj.custom-zipper.core
10471051
(defn insert-left*
10481052
"Raw version of [[insert-left]].
10491053
1050-
Returns zipper with `item` inserted as the left sibling of current node in `zloc`,
1054+
Returns zipper with node `item` inserted as the left sibling of current node in `zloc`,
10511055
without moving location.
10521056
1053-
NOTE: This function does not skip, nor provide any special handling for whitespace/comment nodes."
1057+
NOTE: This function does no coercion, does not skip, nor provide any special handling for whitespace/comment nodes."
10541058
[zloc item] (rewrite-clj.custom-zipper.core/insert-left zloc item))
10551059

10561060
;; DO NOT EDIT FILE, automatically imported from: rewrite-clj.custom-zipper.core
10571061
(defn insert-right*
10581062
"Raw version of [[insert-right]].
10591063
1060-
Returns zipper with `item` inserted as the right sibling of the current node in `zloc`,
1064+
Returns zipper with node `item` inserted as the right sibling of the current node in `zloc`,
10611065
without moving location.
10621066
1063-
NOTE: This function does not skip, nor provide any special handling for whitespace/comment nodes."
1067+
NOTE: This function does no coercion, does not skip, nor provide any special handling for whitespace/comment nodes."
10641068
[zloc item] (rewrite-clj.custom-zipper.core/insert-right zloc item))
10651069

10661070
;; DO NOT EDIT FILE, automatically imported from: rewrite-clj.custom-zipper.core
10671071
(defn insert-child*
10681072
"Raw version of [[insert-child]].
10691073
1070-
Returns zipper with `item` inserted as the leftmost child of the current node in `zloc`,
1074+
Returns zipper with node `item` inserted as the leftmost child of the current node in `zloc`,
10711075
without moving location.
10721076
1073-
NOTE: This function does not skip, nor provide any special handling for whitespace/comment nodes."
1077+
NOTE: This function does no coercion, does not skip, nor provide any special handling for whitespace/comment nodes."
10741078
[zloc item] (rewrite-clj.custom-zipper.core/insert-child zloc item))
10751079

10761080
;; DO NOT EDIT FILE, automatically imported from: rewrite-clj.custom-zipper.core
10771081
(defn append-child*
10781082
"Raw version of [[append-child]].
10791083
1080-
Returns zipper with `item` inserted as the rightmost child of the current node in `zloc`,
1084+
Returns zipper with node `item` inserted as the rightmost child of the current node in `zloc`,
10811085
without moving.
10821086
1083-
NOTE: This function does not skip, nor provide any special handling for whitespace/comment nodes."
1087+
NOTE: This function does no coercion, does not skip, nor provide any special handling for whitespace/comment nodes."
10841088
[zloc item] (rewrite-clj.custom-zipper.core/append-child zloc item))

src/rewrite_clj/zip/edit.clj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
;; DO NOT EDIT FILE, automatically imported from: rewrite-clj.zip.editz
1212
(defn replace
13-
"Return `zloc` with the current node replaced by `value`.
14-
If `value` is not already a node, an attempt will be made to coerce it to one.
13+
"Return `zloc` with the current node replaced by `item`.
14+
If `item` is not already a node, an attempt will be made to coerce it to one.
1515
1616
Use [[replace*]] for non-coercing version of replace."
17-
[zloc value] (rewrite-clj.zip.editz/replace zloc value))
17+
[zloc item] (rewrite-clj.zip.editz/replace zloc item))
1818

1919
;; DO NOT EDIT FILE, automatically imported from: rewrite-clj.zip.editz
2020
(defn edit

src/rewrite_clj/zip/editz.cljc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
;; ## In-Place Modification
1616

1717
(defn replace
18-
"Return `zloc` with the current node replaced by `value`.
19-
If `value` is not already a node, an attempt will be made to coerce it to one.
18+
"Return `zloc` with the current node replaced by `item`.
19+
If `item` is not already a node, an attempt will be made to coerce it to one.
2020
2121
Use [[replace*]] for non-coercing version of replace."
22-
[zloc value]
23-
(zraw/replace zloc (node/coerce value)))
22+
[zloc item]
23+
(zraw/replace zloc (node/coerce item)))
2424

2525
(defn- node-editor
2626
"Create s-expression from node, apply the function and create

src/rewrite_clj/zip/insert.cljc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
(defn insert-right
2727
"Return zipper with `item` inserted to the right of the current node in `zloc`, without moving location.
28+
If `item` is not already a node, an attempt will be made to coerce it to one.
29+
2830
Will insert a space if necessary.
2931
3032
Use [[rewrite-clj.zip/insert-right*]] to insert without adding any whitespace."
@@ -38,6 +40,7 @@
3840
(defn insert-left
3941
"Return zipper with `item` inserted to the left of the current node in `zloc`, without moving location.
4042
Will insert a space if necessary.
43+
If `item` is not already a node, an attempt will be made to coerce it to one.
4144
4245
Use [[insert-left*]] to insert without adding any whitespace."
4346
[zloc item]
@@ -50,6 +53,7 @@
5053
(defn insert-child
5154
"Return zipper with `item` inserted as the first child of the current node in `zloc`, without moving location.
5255
Will insert a space if necessary.
56+
If `item` is not already a node, an attempt will be made to coerce it to one.
5357
5458
Use [[insert-child*]] to insert without adding any whitespace."
5559
[zloc item]
@@ -62,6 +66,7 @@
6266
(defn append-child
6367
"Return zipper with `item` inserted as the last child of the current node in `zloc`, without moving.
6468
Will insert a space if necessary.
69+
If `item` is not already a node, an attempt will be made to coerce it to one.
6570
6671
Use [[append-child*]] to append without adding any whitespace."
6772
[zloc item]

template/rewrite_clj/zip.cljc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@
247247
^{:deprecated "0.5.0"} prepend-newline
248248
^{:deprecated "0.5.0"} append-newline]]}}
249249

250-
;; TODO: clj-kondo barfs on an empty reader cond
251250
#?(:clj
252251
#_{:import-vars/import
253252
{:from [[rewrite-clj.zip.base
@@ -260,7 +259,13 @@
260259
right left up down
261260
next prev
262261
rightmost leftmost
263-
replace edit remove
262+
remove]]}}
263+
264+
#_{:import-vars/import
265+
{:opts {:sym-to-pattern "@@orig-name@@*"
266+
:doc-to-pattern "Raw version of [[@@orig-name@@]].\n\n@@orig-doc@@\n\nNOTE: This function does no coercion, does not skip, nor provide any special handling for whitespace/comment nodes."}
267+
:from [[rewrite-clj.custom-zipper.core
268+
replace edit
264269
insert-left insert-right
265270
insert-child
266271
append-child]]}}

0 commit comments

Comments
 (0)