Skip to content

Commit 6db5d33

Browse files
committed
Better handling of namespaced maps
1 parent c25fe8b commit 6db5d33

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

CHANGELOG.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
- [#91](https://github.com/clojure-emacs/clojure-ts-mode/pull/91): Introduce `clojure-ts-cycle-keyword-string`.
1818
- [#92](https://github.com/clojure-emacs/clojure-ts-mode/pull/92): Add commands to convert between collections types.
1919
- [#93](https://github.com/clojure-emacs/clojure-ts-mode/pull/93): Introduce `clojure-ts-add-arity`.
20-
- Fix an issue where `clojure-ts-align` would hang when called within an
21-
expression containing ignored forms.
20+
- [#94](https://github.com/clojure-emacs/clojure-ts-mode/pull/94): Add indentation rules and `clojure-ts-align` support for namespaced maps.
2221

2322
## 0.3.0 (2025-04-15)
2423

clojure-ts-mode.el

+17-11
Original file line numberDiff line numberDiff line change
@@ -1412,36 +1412,37 @@ if NODE has metadata and its parent has type NODE-TYPE."
14121412
(defun clojure-ts--semantic-indent-rules ()
14131413
"Return a list of indentation rules for `treesit-simple-indent-rules'."
14141414
`((clojure
1415-
((parent-is "source") parent-bol 0)
1415+
((parent-is "^source$") parent-bol 0)
14161416
(clojure-ts--match-docstring parent 0)
14171417
;; Collections items with metadata.
14181418
;;
14191419
;; This should be before `clojure-ts--match-with-metadata', otherwise they
14201420
;; will never be matched.
1421-
(,(clojure-ts--match-collection-item-with-metadata "vec_lit") grand-parent 1)
1422-
(,(clojure-ts--match-collection-item-with-metadata "map_lit") grand-parent 1)
1423-
(,(clojure-ts--match-collection-item-with-metadata "set_lit") grand-parent 2)
1421+
(,(clojure-ts--match-collection-item-with-metadata "^vec_lit$") grand-parent 1)
1422+
(,(clojure-ts--match-collection-item-with-metadata "^map_lit$") grand-parent 1)
1423+
(,(clojure-ts--match-collection-item-with-metadata "^set_lit$") grand-parent 2)
14241424
;;
14251425
;; If we enable this rule for lists, it will break many things.
14261426
;; (,(clojure-ts--match-collection-item-with-metadata "list_lit") grand-parent 1)
14271427
;;
14281428
;; All other forms with metadata.
14291429
(clojure-ts--match-with-metadata parent 0)
14301430
;; Literal Sequences
1431-
((parent-is "vec_lit") parent 1) ;; https://guide.clojure.style/#bindings-alignment
1432-
((parent-is "map_lit") parent 1) ;; https://guide.clojure.style/#map-keys-alignment
1433-
((parent-is "set_lit") parent 2)
1434-
((parent-is "splicing_read_cond_lit") parent 4)
1435-
((parent-is "read_cond_lit") parent 3)
1436-
((parent-is "tagged_or_ctor_lit") parent 0)
1431+
((parent-is "^vec_lit$") parent 1) ;; https://guide.clojure.style/#bindings-alignment
1432+
((parent-is "^map_lit$") parent 1) ;; https://guide.clojure.style/#map-keys-alignment
1433+
((parent-is "^set_lit$") parent 2)
1434+
((parent-is "^splicing_read_cond_lit$") parent 4)
1435+
((parent-is "^read_cond_lit$") parent 3)
1436+
((parent-is "^tagged_or_ctor_lit$") parent 0)
1437+
((parent-is "^ns_map_lit$") (nth-sibling 2) 1)
14371438
;; https://guide.clojure.style/#body-indentation
14381439
(clojure-ts--match-form-body clojure-ts--anchor-parent-opening-paren 2)
14391440
;; https://guide.clojure.style/#threading-macros-alignment
14401441
(clojure-ts--match-threading-macro-arg prev-sibling 0)
14411442
;; https://guide.clojure.style/#vertically-align-fn-args
14421443
(clojure-ts--match-function-call-arg ,(clojure-ts--anchor-nth-sibling 1) 0)
14431444
;; https://guide.clojure.style/#one-space-indent
1444-
((parent-is "list_lit") parent 1))))
1445+
((parent-is "^list_lit$") parent 1))))
14451446

14461447
(defun clojure-ts--configured-indent-rules ()
14471448
"Gets the configured choice of indent rules."
@@ -1640,6 +1641,7 @@ have changed."
16401641
(query (treesit-query-compile 'clojure
16411642
(append
16421643
`(((map_lit) @map)
1644+
((ns_map_lit) @ns-map)
16431645
((list_lit
16441646
((sym_lit) @sym
16451647
(:match ,(clojure-ts-symbol-regexp clojure-ts-align-binding-forms) @sym))
@@ -1686,6 +1688,10 @@ subsequent special arguments based on block indentation rules."
16861688
(goto-char (treesit-node-start node))
16871689
(when-let* ((cur-sexp (treesit-node-first-child-for-pos node (point) t)))
16881690
(goto-char (treesit-node-start cur-sexp))
1691+
;; For namespaced maps we need to skip the namespace, which is the first
1692+
;; nested sexp.
1693+
(when (equal sexp-type 'ns-map)
1694+
(treesit-beginning-of-thing 'sexp -1 'nested))
16891695
;; For cond forms we need to skip first n + 1 nodes according to block
16901696
;; indentation rules. First node to skip is the symbol itself.
16911697
(when (equal sexp-type 'cond)

test/clojure-ts-mode-indentation-test.el

+6-1
Original file line numberDiff line numberDiff line change
@@ -604,4 +604,9 @@ b |20])"
604604

605605
"{:map \"with\"
606606
:multiple \"ignored\"
607-
#_#_:forms \"foo\"}"))
607+
#_#_:forms \"foo\"}")
608+
609+
(when-aligning-it "should support namespaced maps"
610+
"#:hello {:world true
611+
:foo \"bar\"
612+
:some-very-long \"value\"}"))

test/samples/refactoring.clj

+5-3
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,12 @@
8989

9090
[1 2 3]
9191

92-
;; TODO: Define indentation rule for `ns_map_lit`
93-
#:hello{:name "Roma"
94-
:world true}
92+
#:hello {:world true
93+
:foo "bar"
94+
:some-very-long "value"}
9595

96+
{:name "Roma"
97+
:foo true}
9698

9799
(reify
98100
java.io.FileFilter

0 commit comments

Comments
 (0)