|
275 | 275 | (symbol? key) (= key sym) |
276 | 276 | (pattern? key) (re-find key (str sym))))) |
277 | 277 |
|
278 | | -(defn form-matches-key? [zloc key context] |
| 278 | +(defn- form-symbol-matches? [zloc pred context] |
279 | 279 | (let [possible-sym (form-symbol zloc)] |
280 | | - (or (symbol-matches-key? (fully-qualified-symbol possible-sym context) key) |
281 | | - (symbol-matches-key? (remove-namespace possible-sym) key)))) |
| 280 | + (or (pred (fully-qualified-symbol possible-sym context)) |
| 281 | + (pred (remove-namespace possible-sym))))) |
| 282 | + |
| 283 | +(defn- form-matches-key? [zloc key context] |
| 284 | + (form-symbol-matches? zloc #(symbol-matches-key? % key) context)) |
282 | 285 |
|
283 | 286 | (defn- form-matches-thread-macro? [zloc context] |
284 | | - (let [possible-sym (form-symbol zloc) |
285 | | - fully-qualified-sym (fully-qualified-symbol possible-sym context) |
286 | | - sym-without-namespace (remove-namespace possible-sym)] |
287 | | - (some #(or (symbol-matches-key? fully-qualified-sym %) |
288 | | - (symbol-matches-key? sym-without-namespace %)) |
289 | | - #{'-> 'as-> 'some-> 'cond->}))) |
| 287 | + (form-symbol-matches? zloc (:threads context) context)) |
290 | 288 |
|
291 | | -(defn- first-argument? [zloc] |
292 | | - (= (z/right (z/leftmost zloc)) zloc)) |
| 289 | +(defn- get-zipper-position [zloc] |
| 290 | + (loop [idx 0 |
| 291 | + loop-zloc (z/leftmost zloc)] |
| 292 | + (cond |
| 293 | + (= zloc loop-zloc) idx |
| 294 | + (= loop-zloc (z/rightmost zloc)) nil |
| 295 | + :else (recur (inc idx) (z/right loop-zloc))))) |
293 | 296 |
|
294 | 297 | (defn- in-thread-macro? [zloc context] |
295 | | - (and (form-matches-thread-macro? zloc context) (not (first-argument? zloc)))) |
| 298 | + (when (= zloc (z/root zloc)) |
| 299 | + (when-some [idx (form-matches-thread-macro? zloc context)] |
| 300 | + (let [position (cond-> (get-zipper-position (z/up zloc)) |
| 301 | + (in-thread-macro? (z/up (z/up zloc)) context) inc)] |
| 302 | + (cond |
| 303 | + (< position 2) false |
| 304 | + (= idx :odd) (odd? position) |
| 305 | + (= idx :even) (even? position) |
| 306 | + :else (<= idx position)))))) |
296 | 307 |
|
297 | 308 | (defn- inner-indent [zloc key depth idx context] |
298 | 309 | (let [top (nth (iterate z/up zloc) depth) |
299 | | - adjusted-idx (cond-> idx (in-thread-macro? (z/up top) context) (some-> dec))] |
| 310 | + adjusted-idx (cond-> idx (in-thread-macro? zloc context) (some-> dec))] |
300 | 311 | (when (and (form-matches-key? top key context) |
301 | 312 | (or (nil? idx) (index-matches-top-argument? zloc depth adjusted-idx))) |
302 | 313 | (let [zup (z/up zloc)] |
|
317 | 328 |
|
318 | 329 | (defn- block-indent [zloc key idx context] |
319 | 330 | (when (form-matches-key? zloc key context) |
320 | | - (let [adjusted-idx (cond-> idx (in-thread-macro? (z/up zloc) context) (some-> dec (max 0))) |
| 331 | + (let [adjusted-idx (cond-> idx (in-thread-macro? zloc context) (some-> dec (max 0))) |
321 | 332 | zloc-after-idx (some-> zloc (nth-form (inc adjusted-idx)))] |
322 | 333 | (if (and (or (nil? zloc-after-idx) (first-form-in-line? zloc-after-idx)) |
323 | 334 | (> (index-of zloc) adjusted-idx)) |
|
352 | 363 | (defmethod indenter-fn :block [sym context [_ idx]] |
353 | 364 | (fn [zloc] (block-indent zloc sym idx context))) |
354 | 365 |
|
| 366 | +(defmethod indenter-fn :thread [_ _ _] |
| 367 | + (constantly nil)) |
| 368 | + |
355 | 369 | (defn- make-indenter [[key opts] context] |
356 | 370 | (apply some-fn (map (partial indenter-fn key context) opts))) |
357 | 371 |
|
|
388 | 402 | (defn- find-namespace [zloc] |
389 | 403 | (some-> zloc root z/down (z/find z/right ns-form?) z/down z/next z/sexpr)) |
390 | 404 |
|
| 405 | +(defn- get-thread-indents [indents] |
| 406 | + (->> indents |
| 407 | + (keep (fn [[k v]] |
| 408 | + (when-first [[_ idx] (filter (fn [[rule]] (= rule :thread)) v)] |
| 409 | + [k (or idx 2)]))) |
| 410 | + (into {}))) |
| 411 | + |
391 | 412 | (defn indent |
392 | 413 | ([form] |
393 | 414 | (indent form default-indents {})) |
|
400 | 421 | sorted-indents (sort-by indent-order indents) |
401 | 422 | context (merge (select-keys opts [:function-arguments-indentation]) |
402 | 423 | {:alias-map alias-map |
403 | | - :ns-name ns-name})] |
| 424 | + :ns-name ns-name |
| 425 | + :threads (get-thread-indents indents)})] |
404 | 426 | (transform form edit-all should-indent? |
405 | 427 | #(indent-line % sorted-indents context))))) |
406 | 428 |
|
|
0 commit comments