Skip to content

Commit 2e7765c

Browse files
kommenbbatsov
authored andcommitted
Fix indentation of definitions with metadata
1 parent 49844a0 commit 2e7765c

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- [#53]: Let `clojure-ts-mode` derive from `clojure-mode` for Emacs 30+.
99
- [#42]: Fix imenu support for definitions with metadata.
1010
- [#42]: Fix font locking of definitions with metadata
11+
- [#42]: Fix indentation of definitions with metadata
1112

1213
## 0.2.2 (2024-02-16)
1314

clojure-ts-mode.el

+10-2
Original file line numberDiff line numberDiff line change
@@ -737,19 +737,20 @@ https://github.com/weavejester/cljfmt/blob/fb26b22f569724b05c93eb2502592dfc2de89
737737
(or (clojure-ts--symbol-node-p first-child)
738738
(clojure-ts--keyword-node-p first-child)))))
739739

740-
(defun clojure-ts--match-expression-in-body (_node parent _bol)
740+
(defun clojure-ts--match-expression-in-body (node parent _bol)
741741
"Match NODE if it is an expression used in a body argument.
742742
PARENT is expected to be a list literal.
743743
See `treesit-simple-indent-rules'."
744744
(and
745745
(clojure-ts--list-node-p parent)
746-
(let ((first-child (treesit-node-child parent 0 t)))
746+
(let ((first-child (clojure-ts--node-child-skip-metadata parent 0)))
747747
(and
748748
(not
749749
(clojure-ts--symbol-matches-p
750750
;; Symbols starting with this are false positives
751751
(rx line-start (or "default" "deflate" "defer"))
752752
first-child))
753+
(not (clojure-ts--match-with-metadata node))
753754
(clojure-ts--symbol-matches-p
754755
clojure-ts--symbols-with-body-expressions-regexp
755756
first-child)))))
@@ -821,6 +822,12 @@ forms like deftype, defrecord, reify, proxy, etc."
821822
(clojure-ts--match-fn-docstring parent)
822823
(clojure-ts--match-method-docstring parent))))
823824

825+
(defun clojure-ts--match-with-metadata (node &optional _parent _bol)
826+
"Match NODE when it has metadata."
827+
(let ((prev-sibling (treesit-node-prev-sibling node)))
828+
(and prev-sibling
829+
(clojure-ts--metadata-node-p prev-sibling))))
830+
824831
(defun clojure-ts--semantic-indent-rules ()
825832
"Return a list of indentation rules for `treesit-simple-indent-rules'."
826833
`((clojure
@@ -833,6 +840,7 @@ forms like deftype, defrecord, reify, proxy, etc."
833840
(clojure-ts--match-threading-macro-arg prev-sibling 0)
834841
;; https://guide.clojure.style/#vertically-align-fn-args
835842
(clojure-ts--match-function-call-arg (nth-sibling 2 nil) 0)
843+
(clojure-ts--match-with-metadata parent 0)
836844
;; Literal Sequences
837845
((parent-is "list_lit") parent 1) ;; https://guide.clojure.style/#one-space-indent
838846
((parent-is "vec_lit") parent 1) ;; https://guide.clojure.style/#bindings-alignment

test/samples/indentation.clj

+25
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,28 @@
118118
([a] a)
119119
([a b]
120120
b))})
121+
122+
123+
^:foo
124+
(def a 1)
125+
126+
^{:foo true}
127+
(def b 2)
128+
129+
^{:foo true}
130+
[1 2]
131+
132+
(comment
133+
^{:a 1}
134+
(def a 2))
135+
136+
(defn hinted
137+
(^String [])
138+
(^java.util.List
139+
[a & args]))
140+
141+
^{:foo true}
142+
(defn c
143+
"hello"
144+
[_foo]
145+
(+ 1 1))

0 commit comments

Comments
 (0)