Skip to content

Commit 177ac05

Browse files
committed
Indent within docstrings - not working 100%
This indentation is pretty aggressive. When trying to indent MORE than what clojure-ts-mode would like (defn foo "doc string default to indent here but indenting out here is difficult because treesit indent will pull it back to 2 spaces from (defn" ...)
1 parent a878216 commit 177ac05

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

clojure-ts-mode.el

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,10 @@ Only intended for use at development time.")
405405
"Return non-nil if NODE is a Clojure symbol."
406406
(string-equal "sym_lit" (treesit-node-type node)))
407407

408+
(defun clojure-ts--string-node-p (node)
409+
"Return non-nil if NODE is a Clojure string literal."
410+
(string-equal "str_lit" (treesit-node-type node)))
411+
408412
(defun clojure-ts--keyword-node-p (node)
409413
"Return non-nil if NODE is a Clojure keyword."
410414
(string-equal "kwd_lit" (treesit-node-type node)))
@@ -681,9 +685,50 @@ forms like deftype, defrecord, reify, proxy, etc."
681685
1 ;; NODE is the first arg, offset 1 from start of *->> symbol
682686
0)) ;; arg 2...n, match indentation of the previous argument
683687

684-
(defvar clojure-ts--semantic-indent-rules
688+
(defun clojure-ts--match-fn-docstring (node)
689+
"Match NODE when it is a docstring for PARENT function definition node."
690+
;; A string that is the third node in a function defn block
691+
(let ((parent (treesit-node-parent node)))
692+
(and (treesit-node-eq node (treesit-node-child parent 2 t))
693+
(let ((first-auncle (treesit-node-child parent 0 t)))
694+
(clojure-ts--symbol-matches-p
695+
clojure-ts--definition-symbol-regexp
696+
first-auncle)))))
697+
698+
(defun clojure-ts--match-def-docstring (node)
699+
"Match NODE when it is a docstring for PARENT variable definition node."
700+
;; A string that is the fourth node in a variable definition block.
701+
(let ((parent (treesit-node-parent node)))
702+
(and (treesit-node-eq node (treesit-node-child parent 2 t))
703+
;; There needs to be a value after the string.
704+
;; If there is no 4th child, then this string is the value.
705+
(treesit-node-child parent 3 t)
706+
(let ((first-auncle (treesit-node-child parent 0 t)))
707+
(clojure-ts--symbol-matches-p
708+
clojure-ts--variable-definition-symbol-regexp
709+
first-auncle)))))
710+
711+
(defun clojure-ts--match-method-docstring (node)
712+
"Match NODE when it is a docstring in a method definition."
713+
(let* ((grandparent (treesit-node-parent ;; the protocol/interface
714+
(treesit-node-parent node))) ;; the method definition
715+
(first-grandauncle (treesit-node-child grandparent 0 t)))
716+
(clojure-ts--symbol-matches-p
717+
clojure-ts--interface-def-symbol-regexp
718+
first-grandauncle)))
719+
720+
(defun clojure-ts--match-docstring (_node parent _bol)
721+
"Match PARENT when it is a docstring node."
722+
(and (clojure-ts--string-node-p parent) ;; We are IN a string
723+
(or (clojure-ts--match-def-docstring parent)
724+
(clojure-ts--match-fn-docstring parent)
725+
(clojure-ts--match-method-docstring parent))))
726+
727+
(defun clojure-ts--semantic-indent-rules ()
728+
"Return a list of indentation rules for `treesit-simple-indent-rules'."
685729
`((clojure
686730
((parent-is "source") parent-bol 0)
731+
(clojure-ts--match-docstring parent 0)
687732
;; https://guide.clojure.style/#body-indentation
688733
(clojure-ts--match-method-body parent 2)
689734
(clojure-ts--match-expression-in-body parent 2)

0 commit comments

Comments
 (0)