|
348 | 348 | :function-arguments-indentation :community |
349 | 349 | :indents default-indents |
350 | 350 | :extra-indents {} |
| 351 | + :align-maps? false |
351 | 352 | :alias-map {}}) |
352 | 353 |
|
353 | 354 | (defmulti ^:private indenter-fn |
|
555 | 556 | (defn sort-ns-references [form] |
556 | 557 | (transform form edit-all ns-reference? sort-arguments)) |
557 | 558 |
|
| 559 | +(defn- node-width [zloc] |
| 560 | + (-> zloc z/node n/string count)) |
| 561 | + |
| 562 | +(defn- node-column [zloc] |
| 563 | + (loop [zloc (z/left* zloc), n 0] |
| 564 | + (if (or (nil? zloc) (line-break? zloc)) |
| 565 | + n |
| 566 | + (recur (z/left* zloc) |
| 567 | + (if (clojure-whitespace? zloc) n (inc n)))))) |
| 568 | + |
| 569 | +(defn- group-separator? [zloc] |
| 570 | + (= (z/string zloc) "\n\n")) |
| 571 | + |
| 572 | +(defn- node-group [zloc] |
| 573 | + (loop [zloc (z/left* zloc), n 0] |
| 574 | + (if (nil? zloc) |
| 575 | + n |
| 576 | + (recur (z/left* zloc) |
| 577 | + (if (group-separator? zloc) (inc n) n))))) |
| 578 | + |
| 579 | +(defn- comma-after? [zloc] |
| 580 | + (let [right (z/right* zloc)] |
| 581 | + (or (comma? right) |
| 582 | + (and (z/whitespace? right) (comma? (z/right* right)))))) |
| 583 | + |
| 584 | +(defn- max-group-column-widths [zloc] |
| 585 | + (loop [zloc (z/down zloc), max-widths {}] |
| 586 | + (if (nil? zloc) |
| 587 | + max-widths |
| 588 | + (let [width (if (comma-after? zloc) |
| 589 | + (inc (node-width zloc)) |
| 590 | + (node-width zloc)) |
| 591 | + column (node-column zloc) |
| 592 | + group (node-group zloc)] |
| 593 | + (recur (z/right zloc) |
| 594 | + (update-in max-widths [group column] (fnil max 0) width)))))) |
| 595 | + |
| 596 | +(defn- quote? [zloc] |
| 597 | + (-> zloc |
| 598 | + z/node |
| 599 | + n/tag |
| 600 | + (= :quote))) |
| 601 | + |
| 602 | +(defn- remove-space-right [zloc] |
| 603 | + (let [right (z/right* zloc)] |
| 604 | + (if (space? right) |
| 605 | + (if (quote? zloc) |
| 606 | + (z/up (z/remove* right)) |
| 607 | + (z/remove* right)) |
| 608 | + zloc))) |
| 609 | + |
| 610 | +(defn- insert-space-right [zloc n] |
| 611 | + (let [right (z/right* zloc)] |
| 612 | + (if (comma? right) |
| 613 | + (insert-space-right (remove-space-right right) (dec n)) |
| 614 | + (z/insert-space-right zloc n)))) |
| 615 | + |
| 616 | +(defn- set-spacing-right [zloc n] |
| 617 | + (-> zloc (remove-space-right) (insert-space-right n))) |
| 618 | + |
| 619 | +(defn- map-children [zloc f] |
| 620 | + (if-let [zloc (z/down zloc)] |
| 621 | + (loop [zloc zloc] |
| 622 | + (let [zloc (f zloc)] |
| 623 | + (if-let [zloc (z/right zloc)] |
| 624 | + (recur zloc) |
| 625 | + (z/up zloc)))) |
| 626 | + zloc)) |
| 627 | + |
| 628 | +(defn- pad-node [zloc width] |
| 629 | + (set-spacing-right zloc (- width (node-width zloc)))) |
| 630 | + |
| 631 | +(defn- end-of-line? [zloc] |
| 632 | + (line-break? (skip-whitespace-and-commas (z/right* zloc)))) |
| 633 | + |
| 634 | +(defn- align-form-columns [zloc] |
| 635 | + (let [max-widths (max-group-column-widths zloc)] |
| 636 | + (map-children zloc #(cond-> % |
| 637 | + (and (z/right %) (not (end-of-line? %))) |
| 638 | + (pad-node (inc (get-in max-widths [(node-group %) (node-column %)]))))))) |
| 639 | + |
| 640 | +(defn align-maps [form] |
| 641 | + (transform form edit-all z/map? align-form-columns)) |
| 642 | + |
558 | 643 | (defn reformat-form |
559 | 644 | ([form] |
560 | 645 | (reformat-form form {})) |
|
573 | 658 | insert-missing-whitespace) |
574 | 659 | (cond-> (:remove-multiple-non-indenting-spaces? opts) |
575 | 660 | remove-multiple-non-indenting-spaces) |
| 661 | + (cond-> (:align-maps? opts) |
| 662 | + align-maps) |
576 | 663 | (cond-> (:indentation? opts) |
577 | 664 | (reindent (merge (:indents opts) (:extra-indents opts)) |
578 | 665 | (:alias-map opts) |
|
0 commit comments