Skip to content

Commit

Permalink
Semantic indentation of method implementations
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dannyfreeman committed Sep 8, 2023
1 parent 64d8fde commit ff5d7e1
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions clojure-ts-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,26 @@ See `treesit-simple-indent-rules'."
clojure-ts--symbols-with-body-expressions-regexp
first-child)))))

(defconst clojure-ts--reifying-symbol-regexp
(eval-and-compile
(rx line-start
(or "deftype" "defrecord"
"reify" "proxy" "extend-type" "extend-protocol")))
"A regular expression matching a symbol used to define a concrete type.")

(defun clojure-ts--match-method-body (node parent _bol)
"Matches a `NODE' in the body of a `PARENT' method implementation.
A method implementation referes to concrete implemntations being defined in
forms like deftype, defrecord, reify, proxy, etc."
(and
(clojure-ts--list-node-p parent)
(let* ((grandparent (treesit-node-parent parent))
;; auncle: gender neutral sibling of parent, aka child of grandparent
(first-auncle (treesit-node-child grandparent 0 t)))
(and (clojure-ts--list-node-p grandparent)
(clojure-ts--symbol-matches-p clojure-ts--reifying-symbol-regexp
first-auncle)))))

(defvar clojure-ts--threading-macro
(eval-and-compile
(rx (and "->" (? ">") line-end)))
Expand All @@ -623,7 +643,7 @@ See `treesit-simple-indent-rules'."
(defun clojure-ts--match-threading-macro-arg (_node parent _)
"Match NODE if it is an argument to a PARENT threading macro."
;; We want threading macros to indent 2 only if the ->> is on it's own line.
;; If not, then align functoin arg.
;; If not, then align function arg.
(and (clojure-ts--list-node-p parent)
(let ((first-child (treesit-node-child parent 0 t)))
(clojure-ts--symbol-matches-p
Expand All @@ -640,6 +660,7 @@ See `treesit-simple-indent-rules'."
`((clojure
((parent-is "source") parent-bol 0)
;; https://guide.clojure.style/#body-indentation
(clojure-ts--match-method-body parent 2)
(clojure-ts--match-expression-in-body parent 2)
;; https://guide.clojure.style/#threading-macros-alignment
(clojure-ts--match-threading-macro-arg prev-sibling 0)
Expand All @@ -654,7 +675,7 @@ See `treesit-simple-indent-rules'."
(defun clojure-ts--configured-indent-rules ()
"Gets the configured choice of indent rules."
(cond
((eq clojure-ts-indent-style 'semantic) clojure-ts--semantic-indent-rules)
((eq clojure-ts-indent-style 'semantic) (clojure-ts--semantic-indent-rules))
((eq clojure-ts-indent-style 'fixed) clojure-ts--fixed-indent-rules)
(t (error
(format
Expand Down

0 comments on commit ff5d7e1

Please sign in to comment.