Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass fully qualified symbol to clojure-ts-get-indent-function #72

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- [#69](https://github.com/clojure-emacs/clojure-ts-mode/pull/69): Add basic support for dynamic indentation via `clojure-ts-get-indent-function`.
- [#70](https://github.com/clojure-emacs/clojure-ts-mode/pull/70): Add support for nested indentation rules.
- [#71](https://github.com/clojure-emacs/clojure-ts-mode/pull/71): Properly highlight function name in `letfn` form.
- [#72](https://github.com/clojure-emacs/clojure-ts-mode/pull/72): Pass fully qualified symbol to `clojure-ts-get-indent-function`.

## 0.2.3 (2025-03-04)

Expand Down
23 changes: 18 additions & 5 deletions clojure-ts-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,12 @@ with the markdown_inline grammar."
This does not include the NODE's namespace."
(treesit-node-text (treesit-node-child-by-field-name node "name")))

(defun clojure-ts--node-namespace-text (node)
"Gets the namespace of a symbol or keyword NODE.

If there is no namespace, returns nil."
(treesit-node-text (treesit-node-child-by-field-name node "namespace")))

(defun clojure-ts--symbol-named-p (expected-symbol-name node)
"Return non-nil if NODE is a symbol with text matching EXPECTED-SYMBOL-NAME."
(and (clojure-ts--symbol-node-p node)
Expand Down Expand Up @@ -909,8 +915,8 @@ and (:defn) is converted to (:inner 1)."
((equal spec :defn) (list :inner current-depth))
(t nil))))

(defun clojure-ts--dynamic-indent-for-symbol (symbol-name)
"Returns the dynamic indentation specification for SYMBOL-NAME, if found.
(defun clojure-ts--dynamic-indent-for-symbol (sym &optional ns)
"Returns the dynamic indentation specification for SYM, if found.

If the function `clojure-ts-get-indent-function' is defined, call it and
produce a valid indentation specification from its return value.
Expand All @@ -919,9 +925,15 @@ The `clojure-ts-get-indent-function' should return an indentation
specification compatible with `clojure-mode', which will then be
converted to a suitable `clojure-ts-mode' specification.

For example, (1 ((:defn)) nil) is converted to ((:block 1) (:inner 2))."
For example, (1 ((:defn)) nil) is converted to ((:block 1) (:inner 2)).

If NS is defined, then the fully qualified symbol is passed to
`clojure-ts-get-indent-function'."
(when (functionp clojure-ts-get-indent-function)
(let ((spec (funcall clojure-ts-get-indent-function symbol-name)))
(let* ((full-symbol (if ns
(concat ns "/" sym)
sym))
(spec (funcall clojure-ts-get-indent-function full-symbol)))
(if (integerp spec)
(list (list :block spec))
(when (sequencep spec)
Expand All @@ -948,8 +960,9 @@ root of the syntax tree, it returns nil. A rule is considered a match
only if the CURRENT-DEPTH matches the rule's required depth."
(let* ((first-child (clojure-ts--node-child-skip-metadata parent 0))
(symbol-name (clojure-ts--named-node-text first-child))
(symbol-namespace (clojure-ts--node-namespace-text first-child))
(idx (- (treesit-node-index node) 2)))
(if-let* ((rule-set (or (clojure-ts--dynamic-indent-for-symbol symbol-name)
(if-let* ((rule-set (or (clojure-ts--dynamic-indent-for-symbol symbol-name symbol-namespace)
(alist-get symbol-name
(seq-union clojure-ts-semantic-indent-rules
clojure-ts--semantic-indent-rules-defaults
Expand Down
18 changes: 9 additions & 9 deletions test/clojure-ts-mode-indentation-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ DESCRIPTION is a string with the description of the spec."
(when (stringp symbol-name)
(cond
((string-equal symbol-name "my-with-in-str") 1)
((string-equal symbol-name "my-letfn") '(1 ((:defn)) :form)))))
((string-equal symbol-name "my.alias/my-letfn") '(1 ((:defn)) :form)))))


(describe "indentation"
Expand Down Expand Up @@ -305,10 +305,10 @@ DESCRIPTION is a string with the description of the spec."
[fnspecs & body]
~@body)

(my-letfn [(twice [x]
(* x 2))
(six-times [y]
(* (twice y) 3))]
(my.alias/my-letfn [(twice [x]
(* x 2))
(six-times [y]
(* (twice y) 3))]
(println \"Twice 15 =\" (twice 15))
(println \"Six times 15 =\" (six-times 15)))"
(setq-local clojure-ts-get-indent-function #'cider--get-symbol-indent-mock)
Expand All @@ -320,9 +320,9 @@ DESCRIPTION is a string with the description of the spec."
[fnspecs & body]
~@body)

(my-letfn [(twice [x]
(* x 2))
(six-times [y]
(* (twice y) 3))]
(my.alias/my-letfn [(twice [x]
(* x 2))
(six-times [y]
(* (twice y) 3))]
(println \"Twice 15 =\" (twice 15))
(println \"Six times 15 =\" (six-times 15)))"))))
Loading