aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorYuan Fu2022-11-23 12:08:47 -0800
committerYuan Fu2022-11-23 13:27:56 -0800
commit936831579490c2e2a057298f5f915465fbb116d8 (patch)
treeef2acb2ec1da7c873c7a80b8076d8b44f62e41c3 /lisp/progmodes/python.el
parent6785273a8251a2d3dc0450264196f3f19f6403bc (diff)
downloademacs-936831579490c2e2a057298f5f915465fbb116d8.tar.gz
emacs-936831579490c2e2a057298f5f915465fbb116d8.zip
Don't skip nested defuns in python-ts-mode defun navigation
This fixes bug#59495. Before this change, python tries to skip nested function definition. Now we don't skip any nested defun. * lisp/progmodes/python.el (python-treesit-beginning-of-defun) (python-treesit-end-of-defun): Remove functions. * lisp/progmodes/python.el (python-ts-mode): Use tree-sitter's default navigation function.
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el54
1 files changed, 2 insertions, 52 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 18594a3e23d..f97ae81508a 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -2396,55 +2396,6 @@ position, else returns nil."
2396 (ignore (goto-char point))))) 2396 (ignore (goto-char point)))))
2397 2397
2398 2398
2399;;; Tree-sitter navigation
2400
2401(defun python-treesit-beginning-of-defun (&optional arg)
2402 "Tree-sitter `beginning-of-defun' function.
2403ARG is the same as in `beginning-of-defun'."
2404 (let ((arg (or arg 1))
2405 (node (treesit-node-at (point)))
2406 (function-or-class (rx (or "function" "class") "_definition")))
2407 (if (> arg 0)
2408 ;; Go backward.
2409 (while (and (> arg 0)
2410 (setq node (treesit-search-forward-goto
2411 node function-or-class t t)))
2412 ;; Here we deviate from `treesit-beginning-of-defun': if
2413 ;; NODE is function_definition, find the top-level
2414 ;; function_definition, if NODE is class_definition, find
2415 ;; the top-level class_definition, don't mix the two like
2416 ;; `treesit-beginning-of-defun' would.
2417 (setq node (or (treesit-node-top-level node)
2418 node))
2419 (setq arg (1- arg)))
2420 ;; Go forward.
2421 (while (and (< arg 0)
2422 (setq node (treesit-search-forward-goto
2423 node function-or-class)))
2424 (setq node (or (treesit-node-top-level node)
2425 node))
2426 (setq arg (1+ arg))))
2427 (when node
2428 (goto-char (treesit-node-start node))
2429 t)))
2430
2431(defun python-treesit-end-of-defun ()
2432 "Tree-sitter `end-of-defun' function."
2433 ;; Why not simply get the largest node at point: when point is at
2434 ;; (point-min), that gives us the root node.
2435 (let* ((node (treesit-node-at (point)))
2436 (top-func (treesit-node-top-level
2437 node
2438 "function_definition"))
2439 (top-class (treesit-node-top-level
2440 node
2441 "class_definition")))
2442 ;; Prefer function_definition over class_definition: when we are
2443 ;; in a function_definition inside a class_definition, jump to the
2444 ;; end of function_definition.
2445 (goto-char (or (treesit-node-end (or top-func top-class)) (point)))))
2446
2447
2448;;; Shell integration 2399;;; Shell integration
2449 2400
2450(defcustom python-shell-buffer-name "Python" 2401(defcustom python-shell-buffer-name "Python"
@@ -6655,9 +6606,8 @@ implementations: `python-mode' and `python-ts-mode'."
6655 (setq-local treesit-font-lock-settings python--treesit-settings) 6606 (setq-local treesit-font-lock-settings python--treesit-settings)
6656 (setq-local imenu-create-index-function 6607 (setq-local imenu-create-index-function
6657 #'python-imenu-treesit-create-index) 6608 #'python-imenu-treesit-create-index)
6658 (setq-local beginning-of-defun-function 6609 (setq-local treesit-defun-type-regexp (rx (or "function" "class")
6659 #'python-treesit-beginning-of-defun) 6610 "_definition"))
6660 (setq-local end-of-defun-function #'python-treesit-end-of-defun)
6661 (treesit-major-mode-setup))) 6611 (treesit-major-mode-setup)))
6662 6612
6663;;; Completion predicates for M-x 6613;;; Completion predicates for M-x