diff options
author | Yuan Fu <casouri@gmail.com> | 2022-12-24 18:24:01 -0800 |
---|---|---|
committer | Yuan Fu <casouri@gmail.com> | 2022-12-24 18:43:03 -0800 |
commit | fbb4eb919b4c91dd8517a06934bf1f897eaa34bb (patch) | |
tree | 9b019410546f08047252fa8d02328ad3ee566f1b /lisp/textmodes/toml-ts-mode.el | |
parent | 6253184afc2e53c6782a41ec1b59779449152172 (diff) | |
download | emacs-fbb4eb919b4c91dd8517a06934bf1f897eaa34bb.tar.gz emacs-fbb4eb919b4c91dd8517a06934bf1f897eaa34bb.tar.bz2 emacs-fbb4eb919b4c91dd8517a06934bf1f897eaa34bb.zip |
Support treesit-defun-name in tree-sitter major modes
* lisp/progmodes/csharp-mode.el (csharp-ts-mode--defun-name): New
function.
(csharp-ts-mode--imenu-1): Extract into new function.
(csharp-ts-mode): Setup treesit-defun-name-function.
* lisp/progmodes/java-ts-mode.el (java-ts-mode--defun-name): New
function.
(java-ts-mode--imenu-1): Extract into new function.
(java-ts-mode): Setup treesit-defun-name-function.
* lisp/progmodes/js.el (js-treesit-current-defun): Remove function.
This function is not used (for a while already).
(js--treesit-defun-name): New function.
(js--treesit-imenu-1): Extract into new function.
(js-ts-mode): Setup treesit-defun-name-function.
* lisp/progmodes/json-ts-mode.el (json-ts-mode--defun-name): New
function.
(json-ts-mode--imenu-1): Extract into new function.
(json-ts-mode): Setup treesit-defun-name-function.
* lisp/progmodes/python.el (python--treesit-defun-name): New function.
(python--imenu-treesit-create-index-1): Extract into new function.
(python-ts-mode): Setup treesit-defun-name-function.
* lisp/progmodes/rust-ts-mode.el (rust-ts-mode--defun-name): New
function.
(rust-ts-mode--imenu-1): Extract into new function.
(rust-ts-mode): Setup treesit-defun-name-function.
* lisp/textmodes/css-mode.el (css--treesit-defun-name): New function.
(css--treesit-imenu-1): Extract into new function.
(css-ts-mode): Setup treesit-defun-name-function.
* lisp/textmodes/toml-ts-mode.el (toml-ts-mode--get-table-name):
Remove function.
(toml-ts-mode--defun-name): New function.
(toml-ts-mode--imenu-1): Extract into new function.
(toml-ts-mode): Setup treesit-defun-name-function.
Diffstat (limited to 'lisp/textmodes/toml-ts-mode.el')
-rw-r--r-- | lisp/textmodes/toml-ts-mode.el | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/lisp/textmodes/toml-ts-mode.el b/lisp/textmodes/toml-ts-mode.el index bca6a5e81ad..790de2133e8 100644 --- a/lisp/textmodes/toml-ts-mode.el +++ b/lisp/textmodes/toml-ts-mode.el @@ -107,12 +107,12 @@ '((ERROR) @font-lock-warning-face)) "Font-lock settings for TOML.") -(defun toml-ts-mode--get-table-name (node) - "Obtains the header-name for the associated tree-sitter `NODE'." - (if node - (treesit-node-text - (car (cdr (treesit-node-children node)))) - "Root table")) +(defun toml-ts-mode--defun-name (node) + "Return the defun name of NODE. +Return nil if there is no name or if NODE is not a defun node." + (pcase (treesit-node-type node) + ((or "table" "table_array_element") + (car (cdr (treesit-node-children node)))))) (defun toml-ts-mode--imenu-1 (node) "Helper for `toml-ts-mode--imenu'. @@ -120,7 +120,8 @@ Find string representation for NODE and set marker, then recurse the subtrees." (let* ((ts-node (car node)) (subtrees (mapcan #'toml-ts-mode--imenu-1 (cdr node))) - (name (toml-ts-mode--get-table-name ts-node)) + (name (or (treesit-defun-name ts-node) + "Root table")) (marker (when ts-node (set-marker (make-marker) (treesit-node-start ts-node))))) @@ -167,6 +168,7 @@ the subtrees." ;; Navigation. (setq-local treesit-defun-type-regexp (rx (or "table" "table_array_element"))) + (setq-local treesit-defun-name-function #'toml-ts-mode--defun-name) ;; Font-lock. (setq-local treesit-font-lock-settings toml-ts-mode--font-lock-settings) |