From 3ca382c3ccf2ad560d0974229ec88963e82d2fe7 Mon Sep 17 00:00:00 2001 From: Ryan Schmukler Date: Thu, 25 Jul 2024 17:35:06 -0600 Subject: [PATCH] Fix semantic indentation of quoted functions Fixes an error where quoted functions would not align correctly with semantic indentation. Adds an example test and updates the changelog --- CHANGELOG.md | 1 + clojure-ts-mode.el | 15 ++++++++++----- test/clojure-ts-mode-indentation-test.el | 7 ++++++- test/samples/indentation.clj | 2 ++ 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 666bc9a..c167910 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/clojure-ts-mode.el b/clojure-ts-mode.el index 09247cc..2c56c19 100644 --- a/clojure-ts-mode.el +++ b/clojure-ts-mode.el @@ -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." @@ -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) @@ -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'. @@ -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. diff --git a/test/clojure-ts-mode-indentation-test.el b/test/clojure-ts-mode-indentation-test.el index e4d73a6..8fbc50c 100644 --- a/test/clojure-ts-mode-indentation-test.el +++ b/test/clojure-ts-mode-indentation-test.el @@ -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)")) diff --git a/test/samples/indentation.clj b/test/samples/indentation.clj index 2996229..f87870d 100644 --- a/test/samples/indentation.clj +++ b/test/samples/indentation.clj @@ -60,6 +60,8 @@ (clojure.core/filter even? (range 1 10)) +(#'filter even? + (range 10)) (filter even?