Skip to content

Commit

Permalink
Indent within docstrings - not working 100%
Browse files Browse the repository at this point in the history
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"
  ...)
  • Loading branch information
dannyfreeman committed Sep 8, 2023
1 parent a878216 commit 177ac05
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion clojure-ts-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,10 @@ Only intended for use at development time.")
"Return non-nil if NODE is a Clojure symbol."
(string-equal "sym_lit" (treesit-node-type node)))

(defun clojure-ts--string-node-p (node)
"Return non-nil if NODE is a Clojure string literal."
(string-equal "str_lit" (treesit-node-type node)))

(defun clojure-ts--keyword-node-p (node)
"Return non-nil if NODE is a Clojure keyword."
(string-equal "kwd_lit" (treesit-node-type node)))
Expand Down Expand Up @@ -681,9 +685,50 @@ forms like deftype, defrecord, reify, proxy, etc."
1 ;; NODE is the first arg, offset 1 from start of *->> symbol
0)) ;; arg 2...n, match indentation of the previous argument

(defvar clojure-ts--semantic-indent-rules
(defun clojure-ts--match-fn-docstring (node)
"Match NODE when it is a docstring for PARENT function definition node."
;; A string that is the third node in a function defn block
(let ((parent (treesit-node-parent node)))
(and (treesit-node-eq node (treesit-node-child parent 2 t))
(let ((first-auncle (treesit-node-child parent 0 t)))
(clojure-ts--symbol-matches-p
clojure-ts--definition-symbol-regexp
first-auncle)))))

(defun clojure-ts--match-def-docstring (node)
"Match NODE when it is a docstring for PARENT variable definition node."
;; A string that is the fourth node in a variable definition block.
(let ((parent (treesit-node-parent node)))
(and (treesit-node-eq node (treesit-node-child parent 2 t))
;; There needs to be a value after the string.
;; If there is no 4th child, then this string is the value.
(treesit-node-child parent 3 t)
(let ((first-auncle (treesit-node-child parent 0 t)))
(clojure-ts--symbol-matches-p
clojure-ts--variable-definition-symbol-regexp
first-auncle)))))

(defun clojure-ts--match-method-docstring (node)
"Match NODE when it is a docstring in a method definition."
(let* ((grandparent (treesit-node-parent ;; the protocol/interface
(treesit-node-parent node))) ;; the method definition
(first-grandauncle (treesit-node-child grandparent 0 t)))
(clojure-ts--symbol-matches-p
clojure-ts--interface-def-symbol-regexp
first-grandauncle)))

(defun clojure-ts--match-docstring (_node parent _bol)
"Match PARENT when it is a docstring node."
(and (clojure-ts--string-node-p parent) ;; We are IN a string
(or (clojure-ts--match-def-docstring parent)
(clojure-ts--match-fn-docstring parent)
(clojure-ts--match-method-docstring parent))))

(defun clojure-ts--semantic-indent-rules ()
"Return a list of indentation rules for `treesit-simple-indent-rules'."
`((clojure
((parent-is "source") parent-bol 0)
(clojure-ts--match-docstring parent 0)
;; https://guide.clojure.style/#body-indentation
(clojure-ts--match-method-body parent 2)
(clojure-ts--match-expression-in-body parent 2)
Expand Down

0 comments on commit 177ac05

Please sign in to comment.