Skip to content

Commit 505fccc

Browse files
committed
Fix definitions with metadata not being index by imenu
Partly addresses #42
1 parent dc74c45 commit 505fccc

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- [#46]: Fix missing `comment-add` variable in `clojure-ts-mode-variables` mentioned in [#26]
77
- Add imenu support for `deftest` definitions.
88
- [#53]: Let `clojure-ts-mode` derive from `clojure-mode` for Emacs 30+.
9+
- [#42]: Fix imenu support for definitions with metadata.
910

1011
## 0.2.2 (2024-02-16)
1112

clojure-ts-mode.el

+12-3
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,10 @@ with the markdown_inline grammar."
520520
"Return non-nil if NODE is a Clojure keyword."
521521
(string-equal "kwd_lit" (treesit-node-type node)))
522522

523+
(defun clojure-ts--metadata-node-p (node)
524+
"Return non-nil if NODE is a Clojure metadata node."
525+
(string-equal "meta_lit" (treesit-node-type node)))
526+
523527
(defun clojure-ts--named-node-text (node)
524528
"Gets the name of a symbol or keyword NODE.
525529
This does not include the NODE's namespace."
@@ -530,6 +534,11 @@ This does not include the NODE's namespace."
530534
(and (clojure-ts--symbol-node-p node)
531535
(string-equal expected-symbol-name (clojure-ts--named-node-text node))))
532536

537+
(defun clojure-ts--node-child-skip-metadata (node n)
538+
"Return the Nth child of NODE like `treesit-node-child`, skipping the optional metadata node at pos 0 if present."
539+
(let ((first-child (treesit-node-child node 0 t)))
540+
(treesit-node-child node (if (clojure-ts--metadata-node-p first-child) (1+ n) n) t)))
541+
533542
(defun clojure-ts--symbol-matches-p (symbol-regexp node)
534543
"Return non-nil if NODE is a symbol that matches SYMBOL-REGEXP."
535544
(and (clojure-ts--symbol-node-p node)
@@ -550,7 +559,7 @@ like \"defn\".
550559
See `clojure-ts--definition-node-p' when an exact match is possible."
551560
(and
552561
(clojure-ts--list-node-p node)
553-
(let* ((child (treesit-node-child node 0 t))
562+
(let* ((child (clojure-ts--node-child-skip-metadata node 0))
554563
(child-txt (clojure-ts--named-node-text child)))
555564
(and (clojure-ts--symbol-node-p child)
556565
(string-match-p definition-type-regexp child-txt)))))
@@ -565,8 +574,8 @@ that a node is a definition is intended to be done elsewhere.
565574
566575
Can be called directly, but intended for use as `treesit-defun-name-function'."
567576
(when (and (clojure-ts--list-node-p node)
568-
(clojure-ts--symbol-node-p (treesit-node-child node 0 t)))
569-
(let ((sym (treesit-node-child node 1 t)))
577+
(clojure-ts--symbol-node-p (clojure-ts--node-child-skip-metadata node 0)))
578+
(let ((sym (clojure-ts--node-child-skip-metadata node 1)))
570579
(when (clojure-ts--symbol-node-p sym)
571580
;; Extracts ns and name, and recreates the full var name.
572581
;; We can't just get the node-text of the full symbol because

test/clojure-ts-mode-imenu-test.el

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
;;; clojure-ts-mode-util-test.el --- Clojure TS Mode: util test suite -*- lexical-binding: t; -*-
2+
3+
;; Copyright © 2022-2024 Danny Freeman
4+
5+
;; This file is not part of GNU Emacs.
6+
7+
;; This program is free software; you can redistribute it and/or modify
8+
;; it under the terms of the GNU General Public License as published by
9+
;; the Free Software Foundation, either version 3 of the License, or
10+
;; (at your option) any later version.
11+
12+
;; This program is distributed in the hope that it will be useful,
13+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
;; GNU General Public License for more details.
16+
17+
;; You should have received a copy of the GNU General Public License
18+
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
20+
;;; Commentary:
21+
22+
;; The unit test suite of Clojure TS Mode
23+
24+
(require 'clojure-ts-mode)
25+
(require 'buttercup)
26+
(require 'imenu)
27+
28+
29+
(describe "clojure-ts-mode imenu integration"
30+
(it "should index def with meta data"
31+
(with-clojure-ts-buffer "^{:foo 1}(def a 1)"
32+
(expect (imenu--in-alist "a" (imenu--make-index-alist))
33+
:not :to-be nil)))
34+
35+
(it "should index defn with meta data"
36+
(with-clojure-ts-buffer "^{:foo 1}(defn a [])"
37+
(expect (imenu--in-alist "a" (imenu--make-index-alist))
38+
:not :to-be nil))))

0 commit comments

Comments
 (0)