Skip to content

Commit ff5d7e1

Browse files
committed
Semantic indentation of method implementations
method implemenations like (deftype MyType Object (toString [this] "hello")) Look like regular function calls, but should be indented like defns. Found myself wanting to calling a node aunt or uncle, but nodes aren't gendered. Internet suggested auncle or pibling. Auncle sounds better to me.
1 parent 64d8fde commit ff5d7e1

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

clojure-ts-mode.el

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,26 @@ See `treesit-simple-indent-rules'."
615615
clojure-ts--symbols-with-body-expressions-regexp
616616
first-child)))))
617617

618+
(defconst clojure-ts--reifying-symbol-regexp
619+
(eval-and-compile
620+
(rx line-start
621+
(or "deftype" "defrecord"
622+
"reify" "proxy" "extend-type" "extend-protocol")))
623+
"A regular expression matching a symbol used to define a concrete type.")
624+
625+
(defun clojure-ts--match-method-body (node parent _bol)
626+
"Matches a `NODE' in the body of a `PARENT' method implementation.
627+
A method implementation referes to concrete implemntations being defined in
628+
forms like deftype, defrecord, reify, proxy, etc."
629+
(and
630+
(clojure-ts--list-node-p parent)
631+
(let* ((grandparent (treesit-node-parent parent))
632+
;; auncle: gender neutral sibling of parent, aka child of grandparent
633+
(first-auncle (treesit-node-child grandparent 0 t)))
634+
(and (clojure-ts--list-node-p grandparent)
635+
(clojure-ts--symbol-matches-p clojure-ts--reifying-symbol-regexp
636+
first-auncle)))))
637+
618638
(defvar clojure-ts--threading-macro
619639
(eval-and-compile
620640
(rx (and "->" (? ">") line-end)))
@@ -623,7 +643,7 @@ See `treesit-simple-indent-rules'."
623643
(defun clojure-ts--match-threading-macro-arg (_node parent _)
624644
"Match NODE if it is an argument to a PARENT threading macro."
625645
;; We want threading macros to indent 2 only if the ->> is on it's own line.
626-
;; If not, then align functoin arg.
646+
;; If not, then align function arg.
627647
(and (clojure-ts--list-node-p parent)
628648
(let ((first-child (treesit-node-child parent 0 t)))
629649
(clojure-ts--symbol-matches-p
@@ -640,6 +660,7 @@ See `treesit-simple-indent-rules'."
640660
`((clojure
641661
((parent-is "source") parent-bol 0)
642662
;; https://guide.clojure.style/#body-indentation
663+
(clojure-ts--match-method-body parent 2)
643664
(clojure-ts--match-expression-in-body parent 2)
644665
;; https://guide.clojure.style/#threading-macros-alignment
645666
(clojure-ts--match-threading-macro-arg prev-sibling 0)
@@ -654,7 +675,7 @@ See `treesit-simple-indent-rules'."
654675
(defun clojure-ts--configured-indent-rules ()
655676
"Gets the configured choice of indent rules."
656677
(cond
657-
((eq clojure-ts-indent-style 'semantic) clojure-ts--semantic-indent-rules)
678+
((eq clojure-ts-indent-style 'semantic) (clojure-ts--semantic-indent-rules))
658679
((eq clojure-ts-indent-style 'fixed) clojure-ts--fixed-indent-rules)
659680
(t (error
660681
(format

0 commit comments

Comments
 (0)