Skip to content

Commit 5af8de7

Browse files
committed
Beef up node creation docs
- User guide now gives guidance - Node API now lists fns by category in ns docstring - Node API creation fns now include code examples in docstrings - brought in current version of test-doc-blocks to test these - fixed escaping issue for docstrings in apply-import-vars CommonMark code blocks in docstrings are awkward when they require escaping, but they sure look good on cljdoc. Closes #97
1 parent db337b3 commit 5af8de7

21 files changed

+1082
-108
lines changed

CHANGELOG.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ For a list of breaking changes see link:#v1-breaking[breaking changes].
1616

1717
=== Unreleased
1818

19+
* Beef up docs on node creation https://github.com/clj-commons/rewrite-clj/issues/97[#97]
20+
1921
=== v1.0.579-alpha
2022

2123
* Release workflow now creates a GitHub release

deps.edn

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@
2626
:extra-paths ["test"]}
2727

2828
;; document block testing
29-
:test-doc-blocks {:replace-deps {lread/test-doc-blocks {:mvn/version "1.0.101-alpha"}}
29+
:test-doc-blocks {:replace-deps {lread/test-doc-blocks {:mvn/version "1.0.107-alpha"}}
3030
:replace-paths []
3131
:ns-default lread.test-doc-blocks
3232
:exec-args {:docs ["doc/01-user-guide.adoc"
3333
"doc/design/01-merging-rewrite-clj-and-rewrite-cljs.adoc"
34-
"doc/design/namespaced-elements.adoc"]}}
34+
"doc/design/namespaced-elements.adoc"
35+
"src/rewrite_clj/node.cljc"]}}
3536

3637
:test-docs {:extra-paths ["target/test-doc-blocks/test"]}
3738

doc/01-user-guide.adoc

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,81 @@ Inspect, analyze, create and render rewrite-clj nodes.
498498

499499
See link:{cljdoc-api-url}/rewrite-clj.node[node API docs].
500500

501+
==== Creating Nodes
502+
503+
Rewrite-clj nodes can be created in a number of ways:
504+
505+
1. Indirectly via the parser API:
506+
+
507+
[source,Clojure]
508+
----
509+
(-> (p/parse-string "[1 2 3]")
510+
n/string)
511+
;; => "[1 2 3]"
512+
----
513+
2. Indirectly via the zip API (which uses the parser API):
514+
+
515+
[source,Clojure]
516+
----
517+
(-> (z/of-string "[1 2 3]")
518+
z/node
519+
n/string)
520+
;; => "[1 2 3]"
521+
----
522+
3. Via coercion from Clojure forms:
523+
+
524+
[source,Clojure]
525+
----
526+
(-> (n/coerce '[1 2 3])
527+
n/string)
528+
;; => "[1 2 3]"
529+
----
530+
4. By explicitly calling node creation functions.
531+
+
532+
[source,Clojure]
533+
----
534+
(-> (n/vector-node [(n/token-node 1)
535+
(n/whitespace-node " ")
536+
(n/token-node 2)
537+
(n/whitespace-node " ")
538+
(n/token-node 3)])
539+
n/string)
540+
;; => "[1 2 3]"
541+
----
542+
+
543+
The node creation function are what the parser API uses to create nodes.
544+
545+
Which technique you use depends on our needs.
546+
547+
Coercion is convenient, but doesn't offer control over whitespace. In some cases coercion might not give you the result you expect:
548+
549+
//:test-doc-blocks/skip
550+
[source,Clojure]
551+
----
552+
(-> (n/coerce '#(+ %1 %2))
553+
n/string)
554+
;; => "(fn* [p1__10532# p2__10533#] (+ p1__10532# p2__10533#))"
555+
----
556+
557+
Be aware that node creation functions do not force you to use rewrite-clj nodes (notice the raw `1` `2` and `3`):
558+
559+
[source,Clojure]
560+
----
561+
(-> (n/vector-node [1 (n/spaces 1) 2 (n/spaces 1) 3])
562+
n/string)
563+
;; => "[1 2 3]"
564+
----
565+
566+
...but no automatic coercion will be done on non rewrite-clj elements and their `tag` will return unknown.
567+
568+
[source,Clojure]
569+
----
570+
(n/tag 1)
571+
;; :unknown
572+
----
573+
574+
Finally, there are a handful of node whitespace creation convenience functions such as `spaces`, `newlines`, `line-separated` and `comma-separated`, see link:{cljdoc-api-url}/rewrite-clj.node[the node API docs for details].
575+
501576
=== Paredit API
502577
Structured editing was introduce by rewrite-cljs and carried over to rewrite-clj v1.
503578

script/lread/apply_import_vars.clj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@
163163
;; mimic what parser does
164164
(nstring/string-node (some->> (imported-docstring vm opts)
165165
string/split-lines
166-
(map #(string/replace % "\"" "\\\""))
166+
;; TODO: this escaping seems too awkward
167+
(map #(-> %
168+
(string/replace "\\" "\\\\")
169+
(string/replace "\"" "\\\"")))
167170
(into []))))
168171
(if (= 1 (count delegators))
169172
(concat [(nws/newlines 1) (nws/spaces 2)]

0 commit comments

Comments
 (0)