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

Fix semantic indentation of quoted functions #49

Merged
merged 1 commit into from
Nov 4, 2024
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 @@ -9,6 +9,7 @@
- [#42]: Fix imenu support for definitions with metadata.
- [#42]: Fix font locking of definitions with metadata
- [#42]: Fix indentation of definitions with metadata
- Fix semantic indentation of quoted functions

## 0.2.2 (2024-02-16)

Expand Down
15 changes: 10 additions & 5 deletions clojure-ts-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,10 @@ with the markdown_inline grammar."
"Return non-nil if NODE is a Clojure metadata node."
(string-equal "meta_lit" (treesit-node-type node)))

(defun clojure-ts--var-node-p (node)
"Return non-nil if NODE is a var (eg. #\\'foo)."
(string-equal "var_quoting_lit" (treesit-node-type node)))

(defun clojure-ts--named-node-text (node)
"Gets the name of a symbol or keyword NODE.
This does not include the NODE's namespace."
Expand Down Expand Up @@ -616,13 +620,13 @@ Includes a dispatch value when applicable (defmethods)."
"Return non-nil if NODE is a ns form."
(clojure-ts--definition-node-p "ns" node))

(defvar clojure-ts--variable-type-regexp
(defvar clojure-ts--variable-definition-type-regexp
(rx string-start (or "def" "defonce") string-end)
"Regular expression for matching definition nodes that resemble variables.")

(defun clojure-ts--variable-node-p (node)
(defun clojure-ts--variable-definition-node-p (node)
"Return non-nil if NODE is a def or defonce form."
(clojure-ts--definition-node-match-p clojure-ts--variable-type-regexp node))
(clojure-ts--definition-node-match-p clojure-ts--variable-definition-type-regexp node))

(defvar clojure-ts--class-type-regexp
(rx string-start (or "deftype" "defrecord" "defstruct") string-end)
Expand All @@ -647,7 +651,7 @@ Includes a dispatch value when applicable (defmethods)."
;; Used instead of treesit-defun-name-function.
clojure-ts--function-node-name)
("Macro" "list_lit" clojure-ts--defmacro-node-p)
("Variable" "list_lit" clojure-ts--variable-node-p)
("Variable" "list_lit" clojure-ts--variable-definition-node-p)
("Interface" "list_lit" clojure-ts--interface-node-p)
("Class" "list_lit" clojure-ts--class-node-p))
"The value for `treesit-simple-imenu-settings'.
Expand Down Expand Up @@ -735,7 +739,8 @@ https://github.com/weavejester/cljfmt/blob/fb26b22f569724b05c93eb2502592dfc2de89
(not (treesit-node-eq (treesit-node-child parent 1 t) node))
(let ((first-child (treesit-node-child parent 0 t)))
(or (clojure-ts--symbol-node-p first-child)
(clojure-ts--keyword-node-p first-child)))))
(clojure-ts--keyword-node-p first-child)
(clojure-ts--var-node-p first-child)))))

(defun clojure-ts--match-expression-in-body (node parent _bol)
"Match NODE if it is an expression used in a body argument.
Expand Down
7 changes: 6 additions & 1 deletion test/clojure-ts-mode-indentation-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,9 @@ DESCRIPTION is a string with the description of the spec."
(defn c
\"hello\"
[_foo]
(+ 1 1))"))
(+ 1 1))")

(when-indenting-it "should support function calls via vars"
"
(#'foo 5
6)"))
2 changes: 2 additions & 0 deletions test/samples/indentation.clj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
(clojure.core/filter even?
(range 1 10))

(#'filter even?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I the indentation OK if it was just 'filter? Also it might be good to change the example to make more sense in the context of quoting being used.

Copy link
Contributor Author

@rschmukler rschmukler Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure how 'filter should indent. Specifically, the reason I think a quoted var should indent like this, is because it implements 'IFn (ie. it can be called) while a symbol doesn't. Let me know if you agree with this. On the others, happy to change the name.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm OK with this.

(range 10))

(filter
even?
Expand Down