From 248c13dcfe1b9618811a6fe67e967b25b1a8f139 Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Tue, 27 Dec 2022 20:57:12 -0800 Subject: Update tree-sitter major modes to use the new Imenu facility See previous commit for more explanation. * lisp/progmodes/c-ts-mode.el (c-ts-mode--defun-name): Handle more types. (c-ts-mode--imenu-1) (c-ts-mode--imenu): Remove functions. (c-ts-base-mode): Setup Imenu. * lisp/progmodes/csharp-mode.el (csharp-ts-mode--imenu-1) (csharp-ts-mode--imenu): Remove functions. (csharp-ts-mode): Setup Imenu. * lisp/progmodes/java-ts-mode.el (java-ts-mode--imenu-1) (java-ts-mode--imenu): Remove functions. (java-ts-mode): Setup Imenu. * lisp/progmodes/js.el (js--treesit-imenu-1) (js--treesit-imenu): Remove functions. (js--treesit-valid-imenu-entry): New function. (js-ts-mode): Setup Imenu. * lisp/progmodes/json-ts-mode.el (json-ts-mode--defun-name): Trim the quotes. (json-ts-mode--imenu-1) (json-ts-mode--imenu): Remove functions. (json-ts-mode): Setup Imenu. * lisp/progmodes/rust-ts-mode.el (rust-ts-mode--imenu) (rust-ts-mode--imenu-1): Remove functions. (rust-ts-mode): Setup Imenu. * lisp/progmodes/typescript-ts-mode.el: (typescript-ts-base-mode): Remove treesit-defun-prefer-top-level, it's not used anymore. Setup Imenu. Setup treesit-defun-name-function. * lisp/textmodes/css-mode.el (css--treesit-imenu-1) (css--treesit-imenu): Remove functions. (css-ts-mode): Setup Imenu. * lisp/textmodes/toml-ts-mode.el (toml-ts-mode--defun-name): Fix it and add a fallback. (toml-ts-mode--imenu-1) (toml-ts-mode--imenu): Remove functions. (toml-ts-mode): Setup Imenu. --- lisp/progmodes/json-ts-mode.el | 39 +++++++-------------------------------- 1 file changed, 7 insertions(+), 32 deletions(-) (limited to 'lisp/progmodes/json-ts-mode.el') diff --git a/lisp/progmodes/json-ts-mode.el b/lisp/progmodes/json-ts-mode.el index 6725c5f2270..2a467dccecc 100644 --- a/lisp/progmodes/json-ts-mode.el +++ b/lisp/progmodes/json-ts-mode.el @@ -112,36 +112,11 @@ Return nil if there is no name or if NODE is not a defun node." (pcase (treesit-node-type node) ((or "pair" "object") - (treesit-node-text - (treesit-node-child-by-field-name - node "key") - t)))) - -(defun json-ts-mode--imenu-1 (node) - "Helper for `json-ts-mode--imenu'. -Find string representation for NODE and set marker, then recurse -the subtrees." - (let* ((ts-node (car node)) - (subtrees (mapcan #'json-ts-mode--imenu-1 (cdr node))) - (name (when ts-node - (or (treesit-defun-name ts-node) - "Anonymous"))) - (marker (when ts-node - (set-marker (make-marker) - (treesit-node-start ts-node))))) - (cond - ((null ts-node) subtrees) - (subtrees - `((,name ,(cons name marker) ,@subtrees))) - (t - `((,name . ,marker)))))) - -(defun json-ts-mode--imenu () - "Return Imenu alist for the current buffer." - (let* ((node (treesit-buffer-root-node)) - (tree (treesit-induce-sparse-tree - node "pair" nil 1000))) - (json-ts-mode--imenu-1 tree))) + (string-trim (treesit-node-text + (treesit-node-child-by-field-name + node "key") + t) + "\"" "\"")))) ;;;###autoload (define-derived-mode json-ts-mode prog-mode "JSON" @@ -179,8 +154,8 @@ the subtrees." (bracket delimiter error))) ;; Imenu. - (setq-local imenu-create-index-function #'json-ts-mode--imenu) - (setq-local which-func-functions nil) ;; Piggyback on imenu + (setq-local treesit-simple-imenu-settings + '((nil "\\`pair\\'" nil nil))) (treesit-major-mode-setup)) -- cgit v1.2.3 From 2d0a92148630858754319bd067f8ce409231f176 Mon Sep 17 00:00:00 2001 From: Eli Zaretskii Date: Wed, 28 Dec 2022 16:41:58 +0200 Subject: ; Avoid treesit-related byte-compiler warnings * lisp/progmodes/json-ts-mode.el (treesit-node-child-by-field-name): * lisp/textmodes/toml-ts-mode.el (treesit-node-child-by-field-name): * lisp/progmodes/java-ts-mode.el (treesit-node-child-by-field-name): * lisp/progmodes/csharp-mode.el (treesit-node-child-by-field-name): Avoid byte-compilation warnings about treesit-node-type. --- lisp/progmodes/csharp-mode.el | 1 + lisp/progmodes/java-ts-mode.el | 1 + lisp/progmodes/json-ts-mode.el | 1 + lisp/textmodes/toml-ts-mode.el | 1 + 4 files changed, 4 insertions(+) (limited to 'lisp/progmodes/json-ts-mode.el') diff --git a/lisp/progmodes/csharp-mode.el b/lisp/progmodes/csharp-mode.el index b967571db7d..66e4a65184c 100644 --- a/lisp/progmodes/csharp-mode.el +++ b/lisp/progmodes/csharp-mode.el @@ -43,6 +43,7 @@ (declare-function treesit-parser-create "treesit.c") (declare-function treesit-induce-sparse-tree "treesit.c") (declare-function treesit-node-start "treesit.c") +(declare-function treesit-node-type "treesit.c") (declare-function treesit-node-child-by-field-name "treesit.c") (defgroup csharp nil diff --git a/lisp/progmodes/java-ts-mode.el b/lisp/progmodes/java-ts-mode.el index 6dd69a44a4a..c13cf032c44 100644 --- a/lisp/progmodes/java-ts-mode.el +++ b/lisp/progmodes/java-ts-mode.el @@ -34,6 +34,7 @@ (declare-function treesit-parser-create "treesit.c") (declare-function treesit-induce-sparse-tree "treesit.c") (declare-function treesit-node-start "treesit.c") +(declare-function treesit-node-type "treesit.c") (declare-function treesit-node-child-by-field-name "treesit.c") (defcustom java-ts-mode-indent-offset 4 diff --git a/lisp/progmodes/json-ts-mode.el b/lisp/progmodes/json-ts-mode.el index 2a467dccecc..adba2f820fa 100644 --- a/lisp/progmodes/json-ts-mode.el +++ b/lisp/progmodes/json-ts-mode.el @@ -33,6 +33,7 @@ (declare-function treesit-parser-create "treesit.c") (declare-function treesit-induce-sparse-tree "treesit.c") (declare-function treesit-node-start "treesit.c") +(declare-function treesit-node-type "treesit.c") (declare-function treesit-node-child-by-field-name "treesit.c") diff --git a/lisp/textmodes/toml-ts-mode.el b/lisp/textmodes/toml-ts-mode.el index 7771cfa6e2a..983a1401008 100644 --- a/lisp/textmodes/toml-ts-mode.el +++ b/lisp/textmodes/toml-ts-mode.el @@ -32,6 +32,7 @@ (declare-function treesit-parser-create "treesit.c") (declare-function treesit-induce-sparse-tree "treesit.c") (declare-function treesit-node-start "treesit.c") +(declare-function treesit-node-type "treesit.c") (declare-function treesit-node-child-by-field-name "treesit.c") (defcustom toml-ts-mode-indent-offset 2 -- cgit v1.2.3