code.query
$
macro
added in 3.0
($ context path & args)select and manipulation of clojure source code
($ {:string “(defn hello1) (defn hello2)”} (defn _ ^:%+ (keyword “oeuoeuoe”))) => ’(defn hello1 :oeuoeuoe) (defn hello2 :oeuoeuoe)
($ {:string “(defn hello1) (defn hello2)”} (defn _ | ^:%+ (keyword “oeuoeuoe”))) => ’:oeuoeuoe :oeuoeuoe
(->> ($ {:string “(defn hello1) (defn hello2)”} (defn _ | ^:%+ (keyword “oeuoeuoe”)) {:return :string})) => “:oeuoeuoe” “:oeuoeuoe”
match
added in 3.0
(match zloc selector)matches the source code (match (nav/parse-string “(+ 1 1)”) ’(symbol? _ _)) => false
(match (nav/parse-string “(+ 1 1)”) ’(^:% symbol? _ _)) => true
(match (nav/parse-string “(+ 1 1)”) ’(^:%- symbol? _ | _)) => true
(match (nav/parse-string “(+ 1 1)”) ’(^:%+ symbol? _ _)) => false
modify
added in 3.0
(modify zloc selectors func)(modify zloc selectors func opts)modifies location given a function (nav/string (modify (nav/parse-root “^:a (defn hello3) (defn hello)”) [’(defn | _)] (fn zloc (nav/insert-token-to-left zloc :hello)))) => “^:a (defn :hello hello3) (defn :hello hello)”
select
added in 3.0
(select zloc selectors)(select zloc selectors opts)selects all patterns from a starting point (map nav/value (select (nav/parse-root “(defn hello ) (defn hello2 )”) ’defn if try)) => ’((defn hello ) (defn hello2 ))
traverse
added in 3.0
(traverse zloc pattern)(traverse zloc pattern func)uses a pattern to traverse as well as to edit the form
(nav/value (traverse (nav/parse-string “^:a (+ () 2 3)”) ’(+ () 2 3))) => ’(+ () 2 3)
(nav/value (traverse (nav/parse-string “()”) ’(^:&+ hello))) => ’(hello)
(nav/value (traverse (nav/parse-string “()”) ’(+ 1 2 3))) => (throws)
(nav/value (traverse (nav/parse-string “(defn hello "world" {:a 1} [])”) ’(defn ^:% symbol? ^:?%- string? ^:?%- map? ^:% vector? & _))) => ’(defn hello [])
wrap-return
added in 3.0
(wrap-return f)decides whether to return a string, zipper or sexp representation`