diff options
Diffstat (limited to 'lisp/progmodes/python.el')
| -rw-r--r-- | lisp/progmodes/python.el | 17 |
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. | ||
| 5453 | Return 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 |