-
Notifications
You must be signed in to change notification settings - Fork 40
/
zloc.clj
115 lines (81 loc) · 2.5 KB
/
zloc.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
(ns cljfmt.zloc
"Common utility functions for using rewrite-clj zippers."
(:refer-clojure :exclude [reader-conditional?])
(:require
[clojure.string :as str]
[clojure.zip :as zip]
[rewrite-clj.node :as n]
[rewrite-clj.zip :as z
:refer [skip whitespace-or-comment?]]))
(defn zprn
"Print a zipper location for debugging purposes. Returns the
location unchanged."
[zloc tag]
(prn tag (:l (second zloc)) (first zloc) (:r (second zloc)))
zloc)
(def zwhitespace?
"True if the node is a whitespace node."
z/whitespace?)
(def zlinebreak?
"True if the node contains a line break."
z/linebreak?)
(defn comment?
"True if the node at this location is a comment."
[zloc]
(some-> zloc z/node n/comment?))
(defn root?
"True if this location is the root node."
[zloc]
(nil? (zip/up zloc)))
(defn top?
"True if the node at this location has a parent node."
[zloc]
(and zloc (not= (z/node zloc) (z/root zloc))))
(defn element?
"True if the node at this location represents a syntactically important
token."
[zloc]
(and zloc (not (whitespace-or-comment? zloc))))
(defn token?
"True if the node at this location is a token."
[zloc]
(= (z/tag zloc) :token))
(defn token-value
"Return the s-expression form of the token at this location."
[zloc]
(and (token? zloc) (z/sexpr zloc)))
(defn reader-macro?
"True if the node at this location is a reader macro expression."
[zloc]
(and zloc (= (n/tag (z/node zloc)) :reader-macro)))
(defn reader-conditional?
"True if the node at this location is a reader conditional form."
[zloc]
(and (reader-macro? zloc)
(contains? #{"?" "?@"} (-> zloc z/down token-value str))))
(defn whitespace?
"True if the node at this location is whitespace and _not_ a line break
character."
[zloc]
(= (z/tag zloc) :whitespace))
(defn skip-whitespace
"Skip to the location of the next non-whitespace node."
[zloc]
(skip zip/next whitespace? zloc))
(defn multiline?
"True if the form at this location spans more than one line."
[zloc]
(str/includes? (z/string zloc) "\n"))
(defn- remove-namespace
"Remove the namespace from a symbol. Non-symbol arguments are returned
unchanged."
[x]
(if (symbol? x) (symbol (name x)) x))
(defn form-symbol-full
"Return the symbol in the leftmost node from this location."
[zloc]
(-> zloc z/leftmost token-value))
(defn form-symbol
"Return a name-only symbol for the leftmost node from this location."
[zloc]
(-> zloc form-symbol-full remove-namespace))