Skip to content

Commit

Permalink
Better solution for indentaion of forms with meta, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kommen committed Oct 15, 2024
1 parent 7867ed5 commit bad7fd9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
13 changes: 6 additions & 7 deletions clojure-ts-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -810,12 +810,11 @@ forms like deftype, defrecord, reify, proxy, etc."
(clojure-ts--match-fn-docstring parent)
(clojure-ts--match-method-docstring parent))))

(defun clojure-ts--match-toplevel-with-meta (_node parent _bol)
"Match NODE when it is toplevel form and it has metadata"
(let* ((grandparent (treesit-node-parent parent)))
(and (string-equal "source" (treesit-node-type grandparent))
(clojure-ts--list-node-p parent)
(treesit-node-child-by-field-name parent "meta"))))
(defun clojure-ts--match-with-meta (node _parent _bol)
"Match NODE when it has metadata"
(let ((prev-sibling (treesit-node-prev-sibling node)))
(and prev-sibling
(string-equal (treesit-node-type prev-sibling) "meta_lit"))))

(defun clojure-ts--semantic-indent-rules ()
"Return a list of indentation rules for `treesit-simple-indent-rules'."
Expand All @@ -829,7 +828,7 @@ forms like deftype, defrecord, reify, proxy, etc."
(clojure-ts--match-threading-macro-arg prev-sibling 0)
;; https://guide.clojure.style/#vertically-align-fn-args
(clojure-ts--match-function-call-arg (nth-sibling 2 nil) 0)
(clojure-ts--match-toplevel-with-meta parent 0)
(clojure-ts--match-with-meta parent 0)
;; Literal Sequences
((parent-is "list_lit") parent 1) ;; https://guide.clojure.style/#one-space-indent
((parent-is "vec_lit") parent 1) ;; https://guide.clojure.style/#bindings-alignment
Expand Down
18 changes: 18 additions & 0 deletions test/indentation.clj
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,21 @@
([a] a)
([a b]
b))})

^:foo
(def a 1)

^{:foo true}
(def b 2)

^{:foo true}
[1 2]

(comment
^{:a 1}
(def a 2))

(defn hinted
(^String [])
(^java.util.List
[a & args]))

0 comments on commit bad7fd9

Please # to comment.