-
Notifications
You must be signed in to change notification settings - Fork 40
/
indent.clj
295 lines (221 loc) · 7.78 KB
/
indent.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
(ns cljfmt.indent
(:require
[cljfmt.zloc :as zl]
[clojure.string :as str]
[clojure.zip :as zip]
[rewrite-clj.node :as n]
[rewrite-clj.zip :as z]))
(def indent-size 2)
(def ^:private start-element
{:meta "^", :meta* "#^", :vector "[", :map "{"
:list "(", :eval "#=", :uneval "#_", :fn "#("
:set "#{", :deref "@", :reader-macro "#", :unquote "~"
:var "#'", :quote "'", :syntax-quote "`", :unquote-splicing "~@"})
;; ## Location Predicates
(defn- line-break?
"True if the node at this location is a linebreak or a comment."
[zloc]
(or (zl/zlinebreak? zloc) (zl/comment? zloc)))
(defn- indentation?
"True if the node at this location consists of whitespace and is the first
node on a line."
[zloc]
(and (line-break? (zip/prev zloc)) (zl/whitespace? zloc)))
(defn- comment-next?
"True if the next non-whitespace node after this location is a comment."
[zloc]
(-> zloc zip/next zl/skip-whitespace zl/comment?))
(defn- line-break-next?
"True if the next non-whitespace node after this location is a linebreak."
[zloc]
(-> zloc zip/next zl/skip-whitespace line-break?))
(defn should-indent?
"True if indentation should exist after the current location."
[zloc]
(and (line-break? zloc) (not (line-break-next? zloc))))
(defn should-unindent?
"True if the current location is indentation whitespace that should be
reformatted."
[zloc]
(and (indentation? zloc) (not (comment-next? zloc))))
;; ## Margin Calculation
(defn- prior-line-string
"Work backward from the current location to build out the string containing
the previous line of text."
[zloc]
(loop [zloc zloc
worklist '()]
(if-let [p (zip/left zloc)]
(let [s (str (n/string (z/node p)))
new-worklist (cons s worklist)]
(if-not (str/includes? s "\n")
(recur p new-worklist)
(apply str new-worklist)))
(if-let [p (zip/up zloc)]
;; newline cannot be introduced by start-element
(recur p (cons (start-element (n/tag (z/node p))) worklist))
(apply str worklist)))))
(defn- last-line-in-string
"Return a string containing the last line of text in `s`, which may be `s`
itself if it contains no newlines."
[s]
(if-let [i (str/last-index-of s "\n")]
(subs s (inc i))
s))
(defn- margin
"Return the column of the last character in the previous line."
[zloc]
(-> zloc prior-line-string last-line-in-string count))
;; ## Indentation Rules
(defn- index-of
"Determine the index of the node in the children of its parent."
[zloc]
(->> (iterate z/left zloc)
(take-while identity)
(count)
(dec)))
(defn- coll-indent
"Determine how indented a new collection element should be."
[zloc]
(-> zloc zip/leftmost margin))
(defn- list-indent
"Determine how indented a list at the current location should be."
[zloc]
(if (and (some-> zloc zip/leftmost zip/right zl/skip-whitespace zl/zlinebreak?)
(-> zloc z/leftmost z/tag (= :token)))
(+ (-> zloc zip/up margin)
indent-size
(if (= :fn (-> zloc z/up z/tag))
1
0))
(if (> (index-of zloc) 1)
(-> zloc zip/leftmost z/right margin)
(coll-indent zloc))))
(defmulti ^:private indenter-fn
"Multimethod for applying indentation rules to forms."
(fn [rule-key [rule-type & args]] rule-type))
(defn- make-indenter
"Construct an indentation function by mapping the multimethod over the
configured rule bodies."
[[rule-key opts]]
(apply some-fn (map (partial indenter-fn rule-key) opts)))
(defn- pattern?
"True if the value if a regular expression pattern."
[v]
(instance? java.util.regex.Pattern v))
(defn- indent-order
"Return a string for establishing the ranking of a rule key."
[[rule-key _]]
(cond
(symbol? rule-key)
(if (namespace rule-key)
(str 0 rule-key)
(str 1 rule-key))
(pattern? rule-key)
(str 2 rule-key)))
(defn- indent-matches?
"True if the rule key indicates that it should apply to this form symbol."
[rule-key sym]
(cond
(and (symbol? rule-key) (symbol? sym))
(or (= rule-key sym)
(= rule-key (symbol (name sym))))
(pattern? rule-key)
(re-find rule-key (str sym))))
(defn- custom-indent
"Look up custom indentation rules for the node at this location. Returns the
number of spaces to indent the node."
[zloc indents]
(if (empty? indents)
(list-indent zloc)
(let [indenter (->> (sort-by indent-order indents)
(map make-indenter)
(apply some-fn))]
(or (indenter zloc)
(list-indent zloc)))))
(defn indent-amount
"Calculates the number of spaces the node at this location should be
indented, based on the available custom indent rules."
[zloc indents]
(let [tag (-> zloc z/up z/tag)
gp (-> zloc z/up z/up)]
(cond
(zl/reader-conditional? gp)
(coll-indent zloc)
(#{:list :fn} tag)
(custom-indent zloc indents)
(#{:meta :meta* :reader-macro} tag)
(indent-amount (z/up zloc) indents)
:else
(coll-indent zloc))))
;; ## Inner Style Rule
(defn- indent-width
"Determine how many characters the form at the location should be indented
for an inner style."
[zloc]
(case (z/tag zloc)
:list indent-size
:fn (inc indent-size)))
(defn- index-matches-top-argument?
"True if the node at this location is a descendant of the `idx`-th child of
the node `depth` higher in the tree."
[zloc depth idx]
(and (pos? depth) (= idx (index-of (nth (iterate z/up zloc) (dec depth))))))
(defn- inner-indent
"Calculate how many spaces the node at this location should be indented,
based on the rule and previous margins. Returns nil if the rule does not
apply."
[zloc rule-key depth idx]
(let [top (nth (iterate z/up zloc) depth)]
(when (and (indent-matches? rule-key (zl/form-symbol-full top))
(or (nil? idx) (index-matches-top-argument? zloc depth idx)))
(let [zup (z/up zloc)]
(+ (margin zup) (indent-width zup))))))
(defmethod indenter-fn :inner
[rule-key [_ depth idx]]
(fn [zloc] (inner-indent zloc rule-key depth idx)))
;; ## Block Style Rule
(defn- nth-form
"Return the location of the n-th node from the left in this level."
[zloc n]
(reduce (fn [z f] (when z (f z)))
(z/leftmost zloc)
(repeat n z/right)))
(defn- first-form-in-line?
"True if the node at this location is the first non-whitespace node on the
line."
[zloc]
(if-let [zloc (zip/left zloc)]
(if (zl/whitespace? zloc)
(recur zloc)
(or (zl/zlinebreak? zloc) (zl/comment? zloc)))
true))
(defn- block-indent
"Calculate how many spaces the node at this location should be indented as a
block. Returns nil if the rule does not apply."
[zloc rule-key idx]
(when (indent-matches? rule-key (zl/form-symbol-full zloc))
(if (and (some-> zloc (nth-form (inc idx)) first-form-in-line?)
(> (index-of zloc) idx))
(inner-indent zloc rule-key 0 nil)
(list-indent zloc))))
(defmethod indenter-fn :block
[rule-key [_ idx]]
(fn [zloc] (block-indent zloc rule-key idx)))
;; ## Stair Style Rule
(defn- stair-indent
"Calculate how many spaces the node at this location should be indented as a
conditional block. Returns nil if the rule does not apply."
[zloc rule-key idx]
(when (indent-matches? rule-key (zl/form-symbol-full zloc))
(let [zloc-idx (index-of zloc)
leading-forms (if (some-> zloc (nth-form idx) first-form-in-line?)
0
idx)
indent (inner-indent zloc rule-key 0 nil)]
(if (even? (- zloc-idx leading-forms))
(+ indent indent-size)
indent))))
(defmethod indenter-fn :stair
[rule-key [_ idx]]
(fn [zloc] (stair-indent zloc rule-key idx)))