@@ -18,39 +18,60 @@ __Leiningen ([via Clojars](http://clojars.org/rewrite-clj))__
1818
1919[ ![ Clojars Project] ( http://clojars.org/rewrite-clj/latest-version.svg )] ( http://clojars.org/rewrite-clj )
2020
21+ Auto-generated documentation can be found [ here] ( http://xsc.github.io/rewrite-clj/ ) .
22+
2123### Parsing Data
2224
23- The parser relies on [ clojure.tools.reader] ( https://github.com/clojure/tools.reader ) when handling simple
24- tokens. It generates a structure of nested vectors whose first elements represent the kind of data
25- contained (` :token ` , ` :whitespace ` , ` :comment ` , ` :list ` , ...).
25+ The parser relies on [ clojure.tools.reader] ( https://github.com/clojure/tools.reader ) when
26+ handling simple tokens and generates a custom node type representing EDN forms:
2627
2728``` clojure
2829(require '[rewrite-clj.parser :as p])
29- (p/parse-string " (defn my-function [a]\n (* a 3))" )
30- ; ; =>
31- ; ; [:list
32- ; ; [:token defn] [:whitespace " "] [:token my-function] [:whitespace " "]
33- ; ; [:vector [:token a]] [:newline "\n"] [:whitespace " "]
34- ; ; [:list
35- ; ; [:token *] [:whitespace " "] [:token a] [:whitespace " "] [:token 3]]]
30+
31+ (p/parse-string " (defn my-function [a]\n (* a 3))"
32+ ; ; => <list:
33+ ; ; (defn my-function [a]
34+ ; ; (* a 3))
35+ ; ; >
3636```
3737
38- ### Printing Data
38+ These nodes can be analysed using functions in `rewrite-clj.node`:
39+
40+ ```clojure
41+ (require '[rewrite-clj.node :as n])
42+
43+ (n/tag form) ; ; => :list
44+ (n/children form) ; ; => (<token: defn> <whitespace: " "> <token: my-function> ...)
45+ (n/sexpr form) ; ; => (defn my-function [a] (* a 3))
46+ (n/child-sexprs form) ; ; => (defn my-function [a] (* a 3))
47+ ```
3948
40- The printer incorporates whitespaces and comments in its output.
49+ To convert the structure back to a printable string, use:
4150
4251```clojure
43- (require '[rewrite-clj.printer :as prn])
44- (prn/print-edn (p/parse-string " (defn my-function [a]\n (* a 3))" ))
45- ; ; (defn my-function [a]
46- ; ; (* a 3))
47- ; ; => nil
52+ (n/string form) ; ; => "(defn my-function [a]\n (* a 3))"
53+ ```
54+
55+ You can create a node from nearly any value using `coerce`:
56+
57+ ```
58+ (n/coerce '[a b c]) ; ; => <vector: [a b c]>
59+ ```
60+
61+ Alternatively, by hand:
62+
63+ ```
64+ (n/meta-node
65+ (n/token-node :private )
66+ (n/token-node 'sym))
67+ ; ; => <meta: ^:private sym>
4868```
4969
5070### Clojure Zipper
5171
52- To traverse/modify the generated structure you can use rewrite-clj's whitespace-/comment-/value-aware zipper
53- operations, based on [ fast-zip] ( https://github.com/akhudek/fast-zip ) .
72+ To traverse/modify the generated structure you can use rewrite-clj's
73+ whitespace-/comment-/value-aware zipper operations, based on
74+ [fast-zip](https://github.com/akhudek/fast-zip ).
5475
5576```clojure
5677(require '[rewrite-clj.zip :as z])
@@ -61,7 +82,7 @@ operations, based on [fast-zip](https://github.com/akhudek/fast-zip).
6182(def data (z/of-string data-string))
6283
6384(z/sexpr data) ; ; => (defn my-function [a] (* a 3))
64- (-> data z/down z/right z/node) ; ; => [: token my-function]
85+ (-> data z/down z/right z/node) ; ; => < token: my-function>
6586(-> data z/down z/right z/sexpr) ; ; => my-function
6687
6788(-> data z/down z/right (z/edit (comp symbol str) " 2" ) z/up z/sexpr)
@@ -74,26 +95,28 @@ operations, based on [fast-zip](https://github.com/akhudek/fast-zip).
7495; ; => nil
7596```
7697
77- ` rewrite-clj.zip/edit ` and ` rewrite-clj.zip/replace ` try to facilitate their use by transparently converting
78- between the node's internal representation (` [:token my-function] ` ) and its corresponding s-expression (` my-function ` ).
98+ `rewrite-clj.zip/edit` and `rewrite-clj.zip/replace` try to facilitate their use
99+ by transparently coercing between the node's internal representation (`<token: my-function>`)
100+ and its corresponding s-expression (`my-function` ).
79101
80102## Sweet Code Traversal
81103
82104### Example
83105
84- ` rewrite-clj.zip ` offers a series of ` find ` operations that can be used to determine specific positions in the code.
85- For example, you might want to modify a ` project.clj ` of the following form by replacing the ` :description ` placeholder
86- text with something meaningful:
106+ `rewrite-clj.zip` offers a series of `find` operations that can be used to determine specific
107+ positions in the code. For example, you might want to modify a `project.clj` of the following
108+ form by replacing the `:description` placeholder text with something meaningful:
87109
88110```clojure
89111(defproject my-project " 0.1.0-SNAPSHOT"
90112 :description " Enter description"
91113 ... )
92114```
93115
94- Most find operations take an optional movement function as parameter. If you wanted to perform a depth-first search you'd
95- use ` rewrite-clj.zip/next ` , if you wanted to look for something on the same level as the current location, you'd employ
96- ` rewrite-clj.zip/right ` (the default) or ` rewrite-clj.zip/left ` .
116+ Most find operations take an optional movement function as parameter. If you wanted to perform
117+ a depth-first search you'd use `rewrite-clj.zip/next`, if you wanted to look for something on
118+ the same level as the current location, you'd employ `rewrite-clj.zip/right` (the default ) or
119+ `rewrite-clj.zip/left`.
97120
98121Now, to enter the project map, you'd look for the symbol `defproject` in a depth-first way:
99122
@@ -119,25 +142,13 @@ Replace it, zip up and print the result:
119142; ; => nil
120143```
121144
122- ### Searching the Tree
123-
124- Search functions include:
125-
126- - ` (find zloc [f] p?) ` : find the first match for the given predicate by repeatedly applying ` f ` to the current zipper
127- location (default movement: ` rewrite-clj.zip/right ` ). This might return ` zloc ` itself.
128- - ` (find-next zloc [f] p?) ` : find the next match for the given predicate by repeatedly applying ` f ` to the current zipper
129- location (default movement: ` rewrite-clj.zip/right ` ). This will not return ` zloc ` itself.
130- - ` (find-tag zloc [f] t) ` : uses ` find ` to get the first node with the given tag.
131- - ` (find-next-tag zloc [f] t) ` : uses ` find-next ` to get the first node with the given tag.
132- - ` (find-token zloc [f] p?) ` : like ` find ` but will only check ` :token ` nodes. The predicate is applied to the node's value.
133- - ` (find-next-token zloc [f] p?) ` : like ` find-next ` but will only check ` :token ` nodes.
134- - ` (find-value zloc [f] v) ` : uses ` find ` to get the first ` :token ` node with the given value.
135- - ` (find-next-value zloc [f] v) ` : uses ` find-next ` to get the first ` :token ` node with the given value.
145+ See the [auto-generated documentation](http://xsc.github.io/rewrite-clj/ ) for more information.
136146
137147### Handling Clojure Data Structures
138148
139- rewrite-clj aims at providing easy ways to work with Clojure data structures. It offers functions corresponding
140- to the standard seq functions designed to work with zipper nodes containing said structures, e.g.:
149+ rewrite-clj aims at providing easy ways to work with Clojure data structures. It offers
150+ functions corresponding to the standard seq functions designed to work with zipper nodes
151+ containing said structures, e.g.:
141152
142153```clojure
143154(def data (z/of-string " [1\n 2\n 3]" ))
@@ -151,16 +162,7 @@ to the standard seq functions designed to work with zipper nodes containing said
151162; ; => "[5\n6\n7]"
152163```
153164
154- The following functions exist:
155-
156- - ` map ` : takes a function to be applied to the zipper nodes of the seq's values, has to return the
157- modified zipper node. If a ` :map ` node is supplied, the value nodes will be iterated over. Returns
158- the supplied node incorporating all changes.
159- - ` map-keys ` : Iterate over the key nodes of a ` :map ` node.
160- - ` get ` : can be applied to ` :map ` nodes (with a key) or ` :vector ` /` :list ` /` :set ` nodes (with a numerical index)
161- and will return the desired zipper location.
162- - ` assoc ` : will replace the value at the location obtained via ` get ` .
163- - ` seq? ` , ` map? ` , ` vector? ` , ` list? ` , ` set? ` : check the type of the given zipper node.
165+ See the [auto-generated documentation](http://xsc.github.io/rewrite-clj/ ) for more information.
164166
165167## License
166168
0 commit comments