aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorYuan Fu2022-12-24 18:24:01 -0800
committerYuan Fu2022-12-24 18:43:03 -0800
commitfbb4eb919b4c91dd8517a06934bf1f897eaa34bb (patch)
tree9b019410546f08047252fa8d02328ad3ee566f1b /lisp/progmodes/python.el
parent6253184afc2e53c6782a41ec1b59779449152172 (diff)
downloademacs-fbb4eb919b4c91dd8517a06934bf1f897eaa34bb.tar.gz
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/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el17
1 files changed, 14 insertions, 3 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index bdc9e6fa78c..d383fa57c04 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -5448,6 +5448,16 @@ To this:
5448 5448
5449;;; Tree-sitter imenu 5449;;; Tree-sitter imenu
5450 5450
5451(defun python--treesit-defun-name (node)
5452 "Return the defun name of NODE.
5453Return nil if there is no name or if NODE is not a defun node."
5454 (pcase (treesit-node-type node)
5455 ((or "function_definition" "class_definition")
5456 (treesit-node-text
5457 (treesit-node-child-by-field-name
5458 node "name")
5459 t))))
5460
5451(defun python--imenu-treesit-create-index-1 (node) 5461(defun python--imenu-treesit-create-index-1 (node)
5452 "Given a sparse tree, create an imenu alist. 5462 "Given a sparse tree, create an imenu alist.
5453 5463
@@ -5473,9 +5483,8 @@ definition*\"."
5473 ("class_definition" 'class))) 5483 ("class_definition" 'class)))
5474 ;; The root of the tree could have a nil ts-node. 5484 ;; The root of the tree could have a nil ts-node.
5475 (name (when ts-node 5485 (name (when ts-node
5476 (treesit-node-text 5486 (or (treesit-defun-name ts-node)
5477 (treesit-node-child-by-field-name 5487 "Anonymous")))
5478 ts-node "name") t)))
5479 (marker (when ts-node 5488 (marker (when ts-node
5480 (set-marker (make-marker) 5489 (set-marker (make-marker)
5481 (treesit-node-start ts-node))))) 5490 (treesit-node-start ts-node)))))
@@ -6643,6 +6652,8 @@ implementations: `python-mode' and `python-ts-mode'."
6643 #'python-imenu-treesit-create-index) 6652 #'python-imenu-treesit-create-index)
6644 (setq-local treesit-defun-type-regexp (rx (or "function" "class") 6653 (setq-local treesit-defun-type-regexp (rx (or "function" "class")
6645 "_definition")) 6654 "_definition"))
6655 (setq-local treesit-defun-name-function
6656 #'python--treesit-defun-name)
6646 (treesit-major-mode-setup) 6657 (treesit-major-mode-setup)
6647 6658
6648 (when python-indent-guess-indent-offset 6659 (when python-indent-guess-indent-offset