From 9112b4da06ab09e2901370ec9a1d407ffb19924a Mon Sep 17 00:00:00 2001 From: Dieter Komendera Date: Sun, 31 Mar 2024 23:07:23 +0200 Subject: [PATCH 01/22] Allow font locking of `def`s at the toplevel with metadata --- clojure-ts-mode.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clojure-ts-mode.el b/clojure-ts-mode.el index 99f28cd..a6a2d11 100644 --- a/clojure-ts-mode.el +++ b/clojure-ts-mode.el @@ -349,7 +349,7 @@ with the markdown_inline grammar." :feature 'builtin :language 'clojure - `(((list_lit :anchor (sym_lit (sym_name) @font-lock-keyword-face)) + `(((list_lit meta: _ :? :anchor (sym_lit (sym_name) @font-lock-keyword-face)) (:match ,clojure-ts--builtin-symbol-regexp @font-lock-keyword-face)) ((sym_name) @font-lock-builtin-face (:match ,clojure-ts--builtin-dynamic-var-regexp @font-lock-builtin-face))) From ce90a9b6650421252c7e2c7730ff7a7b8d7fe558 Mon Sep 17 00:00:00 2001 From: Dieter Komendera Date: Sun, 31 Mar 2024 23:11:22 +0200 Subject: [PATCH 02/22] Don't indent toplevel `def`s with metadata --- clojure-ts-mode.el | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/clojure-ts-mode.el b/clojure-ts-mode.el index a6a2d11..e57f133 100644 --- a/clojure-ts-mode.el +++ b/clojure-ts-mode.el @@ -810,6 +810,13 @@ forms like deftype, defrecord, reify, proxy, etc." (clojure-ts--match-fn-docstring parent) (clojure-ts--match-method-docstring parent)))) +(defun clojure-ts--match-toplevel-with-meta (node parent _bol) + "Match NODE when it is toplevel form and it has metadata" + (let* ((grandparent (treesit-node-parent parent))) + (and (string-equal "source" (treesit-node-type grandparent)) + (clojure-ts--list-node-p parent) + (treesit-node-child-by-field-name parent "meta")))) + (defun clojure-ts--semantic-indent-rules () "Return a list of indentation rules for `treesit-simple-indent-rules'." `((clojure @@ -822,6 +829,7 @@ forms like deftype, defrecord, reify, proxy, etc." (clojure-ts--match-threading-macro-arg prev-sibling 0) ;; https://guide.clojure.style/#vertically-align-fn-args (clojure-ts--match-function-call-arg (nth-sibling 2 nil) 0) + (clojure-ts--match-toplevel-with-meta parent 0) ;; Literal Sequences ((parent-is "list_lit") parent 1) ;; https://guide.clojure.style/#one-space-indent ((parent-is "vec_lit") parent 1) ;; https://guide.clojure.style/#bindings-alignment From 7867ed5e0c77d0ce4cf066e119177def6525e213 Mon Sep 17 00:00:00 2001 From: Dieter Komendera Date: Sun, 31 Mar 2024 23:31:28 +0200 Subject: [PATCH 03/22] Fix bytecompile --- clojure-ts-mode.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clojure-ts-mode.el b/clojure-ts-mode.el index e57f133..0760f7c 100644 --- a/clojure-ts-mode.el +++ b/clojure-ts-mode.el @@ -810,7 +810,7 @@ forms like deftype, defrecord, reify, proxy, etc." (clojure-ts--match-fn-docstring parent) (clojure-ts--match-method-docstring parent)))) -(defun clojure-ts--match-toplevel-with-meta (node parent _bol) +(defun clojure-ts--match-toplevel-with-meta (_node parent _bol) "Match NODE when it is toplevel form and it has metadata" (let* ((grandparent (treesit-node-parent parent))) (and (string-equal "source" (treesit-node-type grandparent)) From c956302b4ff21fd0dbc4b06cfddb1aa39df4758c Mon Sep 17 00:00:00 2001 From: Dieter Komendera Date: Tue, 15 Oct 2024 21:14:29 +0200 Subject: [PATCH 04/22] Better solution for indentation of forms with meta, add tests --- clojure-ts-mode.el | 13 ++++++------- test/indentation.clj | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/clojure-ts-mode.el b/clojure-ts-mode.el index 0760f7c..2e37916 100644 --- a/clojure-ts-mode.el +++ b/clojure-ts-mode.el @@ -810,12 +810,11 @@ forms like deftype, defrecord, reify, proxy, etc." (clojure-ts--match-fn-docstring parent) (clojure-ts--match-method-docstring parent)))) -(defun clojure-ts--match-toplevel-with-meta (_node parent _bol) - "Match NODE when it is toplevel form and it has metadata" - (let* ((grandparent (treesit-node-parent parent))) - (and (string-equal "source" (treesit-node-type grandparent)) - (clojure-ts--list-node-p parent) - (treesit-node-child-by-field-name parent "meta")))) +(defun clojure-ts--match-with-meta (node _parent _bol) + "Match NODE when it has metadata" + (let ((prev-sibling (treesit-node-prev-sibling node))) + (and prev-sibling + (string-equal (treesit-node-type prev-sibling) "meta_lit")))) (defun clojure-ts--semantic-indent-rules () "Return a list of indentation rules for `treesit-simple-indent-rules'." @@ -829,7 +828,7 @@ forms like deftype, defrecord, reify, proxy, etc." (clojure-ts--match-threading-macro-arg prev-sibling 0) ;; https://guide.clojure.style/#vertically-align-fn-args (clojure-ts--match-function-call-arg (nth-sibling 2 nil) 0) - (clojure-ts--match-toplevel-with-meta parent 0) + (clojure-ts--match-with-meta parent 0) ;; Literal Sequences ((parent-is "list_lit") parent 1) ;; https://guide.clojure.style/#one-space-indent ((parent-is "vec_lit") parent 1) ;; https://guide.clojure.style/#bindings-alignment diff --git a/test/indentation.clj b/test/indentation.clj index a5fe041..2d96431 100644 --- a/test/indentation.clj +++ b/test/indentation.clj @@ -118,3 +118,21 @@ ([a] a) ([a b] b))}) + +^:foo +(def a 1) + +^{:foo true} +(def b 2) + +^{:foo true} +[1 2] + +(comment + ^{:a 1} + (def a 2)) + +(defn hinted + (^String []) + (^java.util.List + [a & args])) From db539365581e571f27a383601c881ba996b3c3b4 Mon Sep 17 00:00:00 2001 From: Dieter Komendera Date: Tue, 15 Oct 2024 22:00:43 +0200 Subject: [PATCH 05/22] Lint --- clojure-ts-mode.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clojure-ts-mode.el b/clojure-ts-mode.el index 2e37916..9fe4a50 100644 --- a/clojure-ts-mode.el +++ b/clojure-ts-mode.el @@ -811,7 +811,7 @@ forms like deftype, defrecord, reify, proxy, etc." (clojure-ts--match-method-docstring parent)))) (defun clojure-ts--match-with-meta (node _parent _bol) - "Match NODE when it has metadata" + "Match NODE when it has metadata." (let ((prev-sibling (treesit-node-prev-sibling node))) (and prev-sibling (string-equal (treesit-node-type prev-sibling) "meta_lit")))) From 84a56ce8e9b962da691ce11c5881c3d6571a3e24 Mon Sep 17 00:00:00 2001 From: Dieter Komendera Date: Fri, 18 Oct 2024 22:42:40 +0200 Subject: [PATCH 06/22] Correctly font-lock docstring in def/defn with meta data --- clojure-ts-mode.el | 6 ++++-- test/samples/test.clj | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/clojure-ts-mode.el b/clojure-ts-mode.el index 5582647..7765468 100644 --- a/clojure-ts-mode.el +++ b/clojure-ts-mode.el @@ -260,7 +260,8 @@ if a third argument (the value) is provided. (defun clojure-ts--docstring-query (capture-symbol) "Return a query that captures docstrings with CAPTURE-SYMBOL." `(;; Captures docstrings in def - ((list_lit :anchor (sym_lit) @_def_symbol + ((list_lit :anchor (meta_lit) :? + :anchor (sym_lit) @_def_symbol :anchor (comment) :? :anchor (sym_lit) ; variable name :anchor (comment) :? @@ -288,7 +289,8 @@ if a third argument (the value) is provided. @_def_symbol) (:equal @_doc-keyword ":doc")) ;; Captures docstrings defn, defmacro, ns, and things like that - ((list_lit :anchor (sym_lit) @_def_symbol + ((list_lit :anchor (meta_lit) :? + :anchor (sym_lit) @_def_symbol :anchor (comment) :? :anchor (sym_lit) ; function_name :anchor (comment) :? diff --git a/test/samples/test.clj b/test/samples/test.clj index ce4c029..1ab5efa 100644 --- a/test/samples/test.clj +++ b/test/samples/test.clj @@ -289,6 +289,9 @@ clojure.core/map (def ^Integer x 1) +^{:foo true} +(defn b "hello" [] "world") + (comment (defrecord TestRecord [field] AutoCloseable From 32dc24a5bceae982e054b83b26e8bde39f8a7f7b Mon Sep 17 00:00:00 2001 From: Dieter Komendera Date: Sat, 19 Oct 2024 00:22:32 +0200 Subject: [PATCH 07/22] Fix body indentation with meta data --- clojure-ts-mode.el | 16 ++++++++++++---- test/samples/indentation.clj | 6 ++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/clojure-ts-mode.el b/clojure-ts-mode.el index 7765468..707e6c7 100644 --- a/clojure-ts-mode.el +++ b/clojure-ts-mode.el @@ -522,6 +522,10 @@ with the markdown_inline grammar." "Return non-nil if NODE is a Clojure keyword." (string-equal "kwd_lit" (treesit-node-type node))) +(defun clojure-ts--meta-node-p (node) + "Return non-nil if NODE is a Clojure metadata node." + (string-equal "meta_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." @@ -726,22 +730,26 @@ https://github.com/weavejester/cljfmt/blob/fb26b22f569724b05c93eb2502592dfc2de89 (or (clojure-ts--symbol-node-p first-child) (clojure-ts--keyword-node-p first-child))))) -(defun clojure-ts--match-expression-in-body (_node parent _bol) +(defun clojure-ts--match-expression-in-body (node parent _bol) "Match NODE if it is an expression used in a body argument. PARENT is expected to be a list literal. See `treesit-simple-indent-rules'." (and (clojure-ts--list-node-p parent) - (let ((first-child (treesit-node-child parent 0 t))) + (let* ((first-child (treesit-node-child parent 0 t)) + (non-meta-first-child (if (clojure-ts--meta-node-p first-child) + (treesit-node-child parent 1 t) + first-child))) (and (not (clojure-ts--symbol-matches-p ;; Symbols starting with this are false positives (rx line-start (or "default" "deflate" "defer")) - first-child)) + non-meta-first-child)) + (not (clojure-ts--match-with-meta node parent _bol)) (clojure-ts--symbol-matches-p clojure-ts--symbols-with-body-expressions-regexp - first-child))))) + non-meta-first-child))))) (defun clojure-ts--match-method-body (_node parent _bol) "Matches a `NODE' in the body of a `PARENT' method implementation. diff --git a/test/samples/indentation.clj b/test/samples/indentation.clj index 2d96431..c605b8b 100644 --- a/test/samples/indentation.clj +++ b/test/samples/indentation.clj @@ -136,3 +136,9 @@ (^String []) (^java.util.List [a & args])) + +^{:foo true} +(defn c + "hello" + [_foo] + (+ 1 1)) From 3b2e674d40fda00872906fffcd1b77aa4bd52b51 Mon Sep 17 00:00:00 2001 From: Dieter Komendera Date: Sat, 19 Oct 2024 00:25:35 +0200 Subject: [PATCH 08/22] Use clojure-ts--meta-node-p --- clojure-ts-mode.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clojure-ts-mode.el b/clojure-ts-mode.el index 707e6c7..e54045e 100644 --- a/clojure-ts-mode.el +++ b/clojure-ts-mode.el @@ -822,7 +822,7 @@ forms like deftype, defrecord, reify, proxy, etc." "Match NODE when it has metadata." (let ((prev-sibling (treesit-node-prev-sibling node))) (and prev-sibling - (string-equal (treesit-node-type prev-sibling) "meta_lit")))) + (clojure-ts--meta-node-p prev-sibling)))) (defun clojure-ts--semantic-indent-rules () "Return a list of indentation rules for `treesit-simple-indent-rules'." From 702af2edba72f8acecbc2e73a6d1fd669cd89fee Mon Sep 17 00:00:00 2001 From: Dieter Komendera Date: Sat, 19 Oct 2024 01:08:05 +0200 Subject: [PATCH 09/22] Fix imenu entries for def/defn having meta data --- clojure-ts-mode.el | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/clojure-ts-mode.el b/clojure-ts-mode.el index e54045e..ae9563a 100644 --- a/clojure-ts-mode.el +++ b/clojure-ts-mode.el @@ -536,6 +536,12 @@ This does not include the NODE's namespace." (and (clojure-ts--symbol-node-p node) (string-equal expected-symbol-name (clojure-ts--named-node-text node)))) +(defun clojure-ts--node-child-skip-meta (node n) + "Returns the Nth child of node like treesit-node-child, but skips the optional meta node at pos 0." + (let* ((first-child (treesit-node-child node 0 t)) + (n1 (if (clojure-ts--meta-node-p first-child) (1+ n) n))) + (treesit-node-child node n1 t))) + (defun clojure-ts--symbol-matches-p (symbol-regexp node) "Return non-nil if NODE is a symbol that matches SYMBOL-REGEXP." (and (clojure-ts--symbol-node-p node) @@ -556,7 +562,7 @@ like \"defn\". See `clojure-ts--definition-node-p' when an exact match is possible." (and (clojure-ts--list-node-p node) - (let* ((child (treesit-node-child node 0 t)) + (let* ((child (clojure-ts--node-child-skip-meta node 0)) (child-txt (clojure-ts--named-node-text child))) (and (clojure-ts--symbol-node-p child) (string-match-p definition-type-regexp child-txt))))) @@ -571,8 +577,8 @@ that a node is a definition is intended to be done elsewhere. Can be called directly, but intended for use as `treesit-defun-name-function'." (when (and (clojure-ts--list-node-p node) - (clojure-ts--symbol-node-p (treesit-node-child node 0 t))) - (let ((sym (treesit-node-child node 1 t))) + (clojure-ts--symbol-node-p (clojure-ts--node-child-skip-meta node 0))) + (let ((sym (clojure-ts--node-child-skip-meta node 1))) (when (clojure-ts--symbol-node-p sym) ;; Extracts ns and name, and recreates the full var name. ;; We can't just get the node-text of the full symbol because From 026b65d105118e2e9c0a488a5769659a1e7d1ad8 Mon Sep 17 00:00:00 2001 From: Dieter Komendera Date: Sat, 19 Oct 2024 01:12:57 +0200 Subject: [PATCH 10/22] Refactor using clojure-ts--node-child-skip-meta --- clojure-ts-mode.el | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/clojure-ts-mode.el b/clojure-ts-mode.el index ae9563a..224e2a4 100644 --- a/clojure-ts-mode.el +++ b/clojure-ts-mode.el @@ -742,20 +742,17 @@ PARENT is expected to be a list literal. See `treesit-simple-indent-rules'." (and (clojure-ts--list-node-p parent) - (let* ((first-child (treesit-node-child parent 0 t)) - (non-meta-first-child (if (clojure-ts--meta-node-p first-child) - (treesit-node-child parent 1 t) - first-child))) + (let* ((first-child (clojure-ts--node-child-skip-meta parent 0))) (and (not (clojure-ts--symbol-matches-p ;; Symbols starting with this are false positives (rx line-start (or "default" "deflate" "defer")) - non-meta-first-child)) + first-child)) (not (clojure-ts--match-with-meta node parent _bol)) (clojure-ts--symbol-matches-p clojure-ts--symbols-with-body-expressions-regexp - non-meta-first-child))))) + first-child))))) (defun clojure-ts--match-method-body (_node parent _bol) "Matches a `NODE' in the body of a `PARENT' method implementation. From 75aa34db109e778fba9315db1fe87c64dff722bc Mon Sep 17 00:00:00 2001 From: Dieter Komendera Date: Sat, 19 Oct 2024 01:20:10 +0200 Subject: [PATCH 11/22] Correctly font lock the name symbols of def and defn with meta data --- clojure-ts-mode.el | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/clojure-ts-mode.el b/clojure-ts-mode.el index 224e2a4..90332b7 100644 --- a/clojure-ts-mode.el +++ b/clojure-ts-mode.el @@ -371,7 +371,8 @@ with the markdown_inline grammar." ;; No wonder the tree-sitter-clojure grammar only touches syntax, and not semantics :feature 'definition ;; defn and defn like macros :language 'clojure - `(((list_lit :anchor (sym_lit (sym_name) @def) + `(((list_lit :anchor meta: _ :? + :anchor (sym_lit (sym_name) @def) :anchor (sym_lit (sym_name) @font-lock-function-name-face)) (:match ,(rx-to-string `(seq bol @@ -412,7 +413,8 @@ with the markdown_inline grammar." :feature 'variable ;; def, defonce :language 'clojure - `(((list_lit :anchor (sym_lit (sym_name) @def) + `(((list_lit :anchor meta: _ :? + :anchor (sym_lit (sym_name) @def) :anchor (sym_lit (sym_name) @font-lock-variable-name-face)) (:match ,clojure-ts--variable-definition-symbol-regexp @def))) From 75edb9577d46141041001d03960e34050a4d1614 Mon Sep 17 00:00:00 2001 From: Dieter Komendera Date: Sat, 19 Oct 2024 01:25:34 +0200 Subject: [PATCH 12/22] Lint --- clojure-ts-mode.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clojure-ts-mode.el b/clojure-ts-mode.el index 90332b7..ce55620 100644 --- a/clojure-ts-mode.el +++ b/clojure-ts-mode.el @@ -738,7 +738,7 @@ https://github.com/weavejester/cljfmt/blob/fb26b22f569724b05c93eb2502592dfc2de89 (or (clojure-ts--symbol-node-p first-child) (clojure-ts--keyword-node-p first-child))))) -(defun clojure-ts--match-expression-in-body (node parent _bol) +(defun clojure-ts--match-expression-in-body (node parent bol) "Match NODE if it is an expression used in a body argument. PARENT is expected to be a list literal. See `treesit-simple-indent-rules'." @@ -751,7 +751,7 @@ See `treesit-simple-indent-rules'." ;; Symbols starting with this are false positives (rx line-start (or "default" "deflate" "defer")) first-child)) - (not (clojure-ts--match-with-meta node parent _bol)) + (not (clojure-ts--match-with-meta node parent bol)) (clojure-ts--symbol-matches-p clojure-ts--symbols-with-body-expressions-regexp first-child))))) From 774330d4bc163b3d3911edd17f89f28159dcfe57 Mon Sep 17 00:00:00 2001 From: Dieter Komendera Date: Sat, 19 Oct 2024 10:12:44 +0200 Subject: [PATCH 13/22] Use 'metadata' instead of just 'meta' in function names --- clojure-ts-mode.el | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/clojure-ts-mode.el b/clojure-ts-mode.el index ce55620..834d9d6 100644 --- a/clojure-ts-mode.el +++ b/clojure-ts-mode.el @@ -524,7 +524,7 @@ with the markdown_inline grammar." "Return non-nil if NODE is a Clojure keyword." (string-equal "kwd_lit" (treesit-node-type node))) -(defun clojure-ts--meta-node-p (node) +(defun clojure-ts--metadata-node-p (node) "Return non-nil if NODE is a Clojure metadata node." (string-equal "meta_lit" (treesit-node-type node) )) @@ -538,10 +538,10 @@ This does not include the NODE's namespace." (and (clojure-ts--symbol-node-p node) (string-equal expected-symbol-name (clojure-ts--named-node-text node)))) -(defun clojure-ts--node-child-skip-meta (node n) - "Returns the Nth child of node like treesit-node-child, but skips the optional meta node at pos 0." +(defun clojure-ts--node-child-skip-metadata (node n) + "Return the Nth child of node like treesit-node-child, but skips the optional meta node at pos 0." (let* ((first-child (treesit-node-child node 0 t)) - (n1 (if (clojure-ts--meta-node-p first-child) (1+ n) n))) + (n1 (if (clojure-ts--metadata-node-p first-child) (1+ n) n))) (treesit-node-child node n1 t))) (defun clojure-ts--symbol-matches-p (symbol-regexp node) @@ -564,7 +564,7 @@ like \"defn\". See `clojure-ts--definition-node-p' when an exact match is possible." (and (clojure-ts--list-node-p node) - (let* ((child (clojure-ts--node-child-skip-meta node 0)) + (let* ((child (clojure-ts--node-child-skip-metadata node 0)) (child-txt (clojure-ts--named-node-text child))) (and (clojure-ts--symbol-node-p child) (string-match-p definition-type-regexp child-txt))))) @@ -579,8 +579,8 @@ that a node is a definition is intended to be done elsewhere. Can be called directly, but intended for use as `treesit-defun-name-function'." (when (and (clojure-ts--list-node-p node) - (clojure-ts--symbol-node-p (clojure-ts--node-child-skip-meta node 0))) - (let ((sym (clojure-ts--node-child-skip-meta node 1))) + (clojure-ts--symbol-node-p (clojure-ts--node-child-skip-metadata node 0))) + (let ((sym (clojure-ts--node-child-skip-metadata node 1))) (when (clojure-ts--symbol-node-p sym) ;; Extracts ns and name, and recreates the full var name. ;; We can't just get the node-text of the full symbol because @@ -744,14 +744,14 @@ PARENT is expected to be a list literal. See `treesit-simple-indent-rules'." (and (clojure-ts--list-node-p parent) - (let* ((first-child (clojure-ts--node-child-skip-meta parent 0))) + (let* ((first-child (clojure-ts--node-child-skip-metadata parent 0))) (and (not (clojure-ts--symbol-matches-p ;; Symbols starting with this are false positives (rx line-start (or "default" "deflate" "defer")) first-child)) - (not (clojure-ts--match-with-meta node parent bol)) + (not (clojure-ts--match-with-metadata node parent bol)) (clojure-ts--symbol-matches-p clojure-ts--symbols-with-body-expressions-regexp first-child))))) @@ -823,11 +823,11 @@ forms like deftype, defrecord, reify, proxy, etc." (clojure-ts--match-fn-docstring parent) (clojure-ts--match-method-docstring parent)))) -(defun clojure-ts--match-with-meta (node _parent _bol) +(defun clojure-ts--match-with-metadata (node _parent _bol) "Match NODE when it has metadata." (let ((prev-sibling (treesit-node-prev-sibling node))) (and prev-sibling - (clojure-ts--meta-node-p prev-sibling)))) + (clojure-ts--metadata-node-p prev-sibling)))) (defun clojure-ts--semantic-indent-rules () "Return a list of indentation rules for `treesit-simple-indent-rules'." @@ -841,7 +841,7 @@ forms like deftype, defrecord, reify, proxy, etc." (clojure-ts--match-threading-macro-arg prev-sibling 0) ;; https://guide.clojure.style/#vertically-align-fn-args (clojure-ts--match-function-call-arg (nth-sibling 2 nil) 0) - (clojure-ts--match-with-meta parent 0) + (clojure-ts--match-with-metadata parent 0) ;; Literal Sequences ((parent-is "list_lit") parent 1) ;; https://guide.clojure.style/#one-space-indent ((parent-is "vec_lit") parent 1) ;; https://guide.clojure.style/#bindings-alignment From cf90573d2342a3297d460ff371c5b1484ce749b4 Mon Sep 17 00:00:00 2001 From: Dieter Komendera Date: Sat, 19 Oct 2024 10:13:41 +0200 Subject: [PATCH 14/22] Small code formatting fix --- test/samples/indentation.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/samples/indentation.clj b/test/samples/indentation.clj index c605b8b..1854787 100644 --- a/test/samples/indentation.clj +++ b/test/samples/indentation.clj @@ -93,7 +93,7 @@ IProto (foo [this x] x)) -(defn foo2 [x]b) +(defn foo2 [x] b) (reify IProto From 6d71712a227812e9b6964ba07e17e060001fe47d Mon Sep 17 00:00:00 2001 From: Dieter Komendera Date: Sat, 19 Oct 2024 10:25:15 +0200 Subject: [PATCH 15/22] Touch up clojure-ts--node-child-skip-metadata --- clojure-ts-mode.el | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/clojure-ts-mode.el b/clojure-ts-mode.el index 834d9d6..09697bc 100644 --- a/clojure-ts-mode.el +++ b/clojure-ts-mode.el @@ -539,10 +539,9 @@ This does not include the NODE's namespace." (string-equal expected-symbol-name (clojure-ts--named-node-text node)))) (defun clojure-ts--node-child-skip-metadata (node n) - "Return the Nth child of node like treesit-node-child, but skips the optional meta node at pos 0." - (let* ((first-child (treesit-node-child node 0 t)) - (n1 (if (clojure-ts--metadata-node-p first-child) (1+ n) n))) - (treesit-node-child node n1 t))) + "Return the Nth child of NODE like `treesit-node-child`, skipping the optional metadata node at pos 0 if present." + (let ((first-child (treesit-node-child node 0 t))) + (treesit-node-child node (if (clojure-ts--metadata-node-p first-child) (1+ n) n) t))) (defun clojure-ts--symbol-matches-p (symbol-regexp node) "Return non-nil if NODE is a symbol that matches SYMBOL-REGEXP." From 919884bd7f2942b14dd17be0d63099d8888546f2 Mon Sep 17 00:00:00 2001 From: Dieter Komendera Date: Sat, 19 Oct 2024 10:32:55 +0200 Subject: [PATCH 16/22] Remove unused function args --- clojure-ts-mode.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clojure-ts-mode.el b/clojure-ts-mode.el index 09697bc..893da4d 100644 --- a/clojure-ts-mode.el +++ b/clojure-ts-mode.el @@ -750,7 +750,7 @@ See `treesit-simple-indent-rules'." ;; Symbols starting with this are false positives (rx line-start (or "default" "deflate" "defer")) first-child)) - (not (clojure-ts--match-with-metadata node parent bol)) + (not (clojure-ts--match-with-metadata node)) (clojure-ts--symbol-matches-p clojure-ts--symbols-with-body-expressions-regexp first-child))))) @@ -822,7 +822,7 @@ forms like deftype, defrecord, reify, proxy, etc." (clojure-ts--match-fn-docstring parent) (clojure-ts--match-method-docstring parent)))) -(defun clojure-ts--match-with-metadata (node _parent _bol) +(defun clojure-ts--match-with-metadata (node) "Match NODE when it has metadata." (let ((prev-sibling (treesit-node-prev-sibling node))) (and prev-sibling From b1e4ab0c89a195c53431badc281e55a734c2637b Mon Sep 17 00:00:00 2001 From: Dieter Komendera Date: Sat, 19 Oct 2024 10:35:06 +0200 Subject: [PATCH 17/22] Lint --- clojure-ts-mode.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clojure-ts-mode.el b/clojure-ts-mode.el index 893da4d..4662b23 100644 --- a/clojure-ts-mode.el +++ b/clojure-ts-mode.el @@ -737,7 +737,7 @@ https://github.com/weavejester/cljfmt/blob/fb26b22f569724b05c93eb2502592dfc2de89 (or (clojure-ts--symbol-node-p first-child) (clojure-ts--keyword-node-p first-child))))) -(defun clojure-ts--match-expression-in-body (node parent bol) +(defun clojure-ts--match-expression-in-body (node parent _bol) "Match NODE if it is an expression used in a body argument. PARENT is expected to be a list literal. See `treesit-simple-indent-rules'." From 997c5912b35d34e5def0c91b4dca89e0adb898bf Mon Sep 17 00:00:00 2001 From: Dieter Komendera Date: Sun, 20 Oct 2024 19:40:47 +0200 Subject: [PATCH 18/22] Add tests for the imenu fixes --- test/clojure-ts-mode-imenu-test.el | 38 ++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 test/clojure-ts-mode-imenu-test.el diff --git a/test/clojure-ts-mode-imenu-test.el b/test/clojure-ts-mode-imenu-test.el new file mode 100644 index 0000000..1dbe71c --- /dev/null +++ b/test/clojure-ts-mode-imenu-test.el @@ -0,0 +1,38 @@ +;;; clojure-ts-mode-util-test.el --- Clojure TS Mode: util test suite -*- lexical-binding: t; -*- + +;; Copyright © 2022-2024 Danny Freeman + +;; This file is not part of GNU Emacs. + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Commentary: + +;; The unit test suite of Clojure TS Mode + +(require 'clojure-ts-mode) +(require 'buttercup) +(require 'imenu) + + +(describe "clojure-ts-mode imenu integration" + (it "should index def with meta data" + (with-clojure-ts-buffer "^{:foo 1}(def a 1)" + (expect (imenu--in-alist "a" (imenu--make-index-alist)) + :not :to-be nil))) + + (it "should index defn with meta data" + (with-clojure-ts-buffer "^{:foo 1}(defn a [])" + (expect (imenu--in-alist "a" (imenu--make-index-alist)) + :not :to-be nil)))) From 7e44026b85dcada8bd0bcf4d28ce5ddfa9156ab9 Mon Sep 17 00:00:00 2001 From: Dieter Komendera Date: Sun, 20 Oct 2024 19:47:39 +0200 Subject: [PATCH 19/22] Replace unneded let* --- clojure-ts-mode.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clojure-ts-mode.el b/clojure-ts-mode.el index 4662b23..c7c5e11 100644 --- a/clojure-ts-mode.el +++ b/clojure-ts-mode.el @@ -743,7 +743,7 @@ PARENT is expected to be a list literal. See `treesit-simple-indent-rules'." (and (clojure-ts--list-node-p parent) - (let* ((first-child (clojure-ts--node-child-skip-metadata parent 0))) + (let ((first-child (clojure-ts--node-child-skip-metadata parent 0))) (and (not (clojure-ts--symbol-matches-p From 9152ab6d20c0e2d04aa8fcbf985e12944260b99b Mon Sep 17 00:00:00 2001 From: Dieter Komendera Date: Mon, 21 Oct 2024 20:23:15 +0200 Subject: [PATCH 20/22] Fix call to clojure-ts--match-with-metadata --- clojure-ts-mode.el | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/clojure-ts-mode.el b/clojure-ts-mode.el index c7c5e11..a437796 100644 --- a/clojure-ts-mode.el +++ b/clojure-ts-mode.el @@ -737,7 +737,7 @@ https://github.com/weavejester/cljfmt/blob/fb26b22f569724b05c93eb2502592dfc2de89 (or (clojure-ts--symbol-node-p first-child) (clojure-ts--keyword-node-p first-child))))) -(defun clojure-ts--match-expression-in-body (node parent _bol) +(defun clojure-ts--match-expression-in-body (node parent bol) "Match NODE if it is an expression used in a body argument. PARENT is expected to be a list literal. See `treesit-simple-indent-rules'." @@ -750,7 +750,7 @@ See `treesit-simple-indent-rules'." ;; Symbols starting with this are false positives (rx line-start (or "default" "deflate" "defer")) first-child)) - (not (clojure-ts--match-with-metadata node)) + (not (clojure-ts--match-with-metadata node parent bol)) (clojure-ts--symbol-matches-p clojure-ts--symbols-with-body-expressions-regexp first-child))))) @@ -822,7 +822,7 @@ forms like deftype, defrecord, reify, proxy, etc." (clojure-ts--match-fn-docstring parent) (clojure-ts--match-method-docstring parent)))) -(defun clojure-ts--match-with-metadata (node) +(defun clojure-ts--match-with-metadata (node _parent _bol) "Match NODE when it has metadata." (let ((prev-sibling (treesit-node-prev-sibling node))) (and prev-sibling From d495e11d9db7d72eef21fe885800ee0a60bec523 Mon Sep 17 00:00:00 2001 From: Dieter Komendera Date: Mon, 21 Oct 2024 20:32:35 +0200 Subject: [PATCH 21/22] Try lint --- clojure-ts-mode.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clojure-ts-mode.el b/clojure-ts-mode.el index a437796..f306030 100644 --- a/clojure-ts-mode.el +++ b/clojure-ts-mode.el @@ -737,7 +737,7 @@ https://github.com/weavejester/cljfmt/blob/fb26b22f569724b05c93eb2502592dfc2de89 (or (clojure-ts--symbol-node-p first-child) (clojure-ts--keyword-node-p first-child))))) -(defun clojure-ts--match-expression-in-body (node parent bol) +(defun clojure-ts--match-expression-in-body (node parent _bol) "Match NODE if it is an expression used in a body argument. PARENT is expected to be a list literal. See `treesit-simple-indent-rules'." @@ -750,7 +750,7 @@ See `treesit-simple-indent-rules'." ;; Symbols starting with this are false positives (rx line-start (or "default" "deflate" "defer")) first-child)) - (not (clojure-ts--match-with-metadata node parent bol)) + (not (clojure-ts--match-with-metadata node parent _bol)) (clojure-ts--symbol-matches-p clojure-ts--symbols-with-body-expressions-regexp first-child))))) From eafed20bd92d9976eeeb855cf9e3b69c1e89d94e Mon Sep 17 00:00:00 2001 From: Dieter Komendera Date: Mon, 21 Oct 2024 20:42:07 +0200 Subject: [PATCH 22/22] Try lint --- clojure-ts-mode.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clojure-ts-mode.el b/clojure-ts-mode.el index f306030..9c2891d 100644 --- a/clojure-ts-mode.el +++ b/clojure-ts-mode.el @@ -750,7 +750,7 @@ See `treesit-simple-indent-rules'." ;; Symbols starting with this are false positives (rx line-start (or "default" "deflate" "defer")) first-child)) - (not (clojure-ts--match-with-metadata node parent _bol)) + (not (clojure-ts--match-with-metadata node)) (clojure-ts--symbol-matches-p clojure-ts--symbols-with-body-expressions-regexp first-child))))) @@ -822,7 +822,7 @@ forms like deftype, defrecord, reify, proxy, etc." (clojure-ts--match-fn-docstring parent) (clojure-ts--match-method-docstring parent)))) -(defun clojure-ts--match-with-metadata (node _parent _bol) +(defun clojure-ts--match-with-metadata (node &optional _parent _bol) "Match NODE when it has metadata." (let ((prev-sibling (treesit-node-prev-sibling node))) (and prev-sibling