mirror of
git://git.sv.gnu.org/emacs.git
synced 2025-12-23 14:10:28 -08:00
; Comment and stylistic change in treesit.el
* lisp/treesit.el (treesit-defun-skipper): Docstring change. (treesit--navigate-defun): Comment and stylistic change.
This commit is contained in:
parent
489b02d03c
commit
1b0e282a7f
1 changed files with 48 additions and 22 deletions
|
|
@ -1584,9 +1584,12 @@ defuns, if the value is `nested', Emacs recognizes nested defuns.")
|
||||||
|
|
||||||
(defvar-local treesit-defun-skipper #'treesit-default-defun-skipper
|
(defvar-local treesit-defun-skipper #'treesit-default-defun-skipper
|
||||||
"A function called after tree-sitter navigation moved a step.
|
"A function called after tree-sitter navigation moved a step.
|
||||||
|
|
||||||
It is called with no arguments. By default, this function tries
|
It is called with no arguments. By default, this function tries
|
||||||
to move to the beginning of a line, either by moving to the empty
|
to move to the beginning of a line, either by moving to the empty
|
||||||
newline after a defun, or the beginning of a defun.")
|
newline after a defun, or the beginning of a defun.
|
||||||
|
|
||||||
|
If the value is nil, no skipping is performed.")
|
||||||
|
|
||||||
(defvar-local treesit-defun-prefer-top-level nil
|
(defvar-local treesit-defun-prefer-top-level nil
|
||||||
"When non-nil, Emacs prefers top-level defun.
|
"When non-nil, Emacs prefers top-level defun.
|
||||||
|
|
@ -1760,6 +1763,33 @@ REGEXP and PRED are the same as in `treesit-defun-type-regexp'."
|
||||||
do (setq node cursor))
|
do (setq node cursor))
|
||||||
node))
|
node))
|
||||||
|
|
||||||
|
;; The basic idea for nested defun navigation is that we first try to
|
||||||
|
;; move across sibling defuns in the same level, if no more siblings
|
||||||
|
;; exist, we move to parents's beg/end, rinse and repeat. We never
|
||||||
|
;; move into a defun, only outwards.
|
||||||
|
;;
|
||||||
|
;; Let me describe roughly what does this function do: there are four
|
||||||
|
;; possible operations: prev-beg, next-end, prev-end, next-beg, and
|
||||||
|
;; each of (prev-sibling next-sibling and parent) could exist or not
|
||||||
|
;; exist. So there are 4 times 8 = 32 situations.
|
||||||
|
;;
|
||||||
|
;; I'll only describe the situation when we go backward (prev-beg &
|
||||||
|
;; prev-end), and consider only prev-sibling & parent. Deriving the
|
||||||
|
;; reverse situations is left as an exercise for the reader.
|
||||||
|
;;
|
||||||
|
;; prev-beg (easy case):
|
||||||
|
;; 1. prev-sibling or parent exists
|
||||||
|
;; -> go the prev-sibling/parent's beg
|
||||||
|
;;
|
||||||
|
;; prev-end (tricky):
|
||||||
|
;; 1. prev-sibling exists
|
||||||
|
;; -> If you think about it, we are already at prev-sibling's end!
|
||||||
|
;; So we need to go one step further, either to
|
||||||
|
;; prev-prev-sibling's end, or parent's prev-sibling's end, etc.
|
||||||
|
;; 2. prev-sibling is nil but parent exists
|
||||||
|
;; -> Obviously we don't want to go to parent's end, instead, we
|
||||||
|
;; want to go to parent's prev-sibling's end. Again, we recurse
|
||||||
|
;; in the function to do that.
|
||||||
(defun treesit--navigate-defun (pos arg side &optional recursing)
|
(defun treesit--navigate-defun (pos arg side &optional recursing)
|
||||||
"Navigate defun ARG steps from POS.
|
"Navigate defun ARG steps from POS.
|
||||||
|
|
||||||
|
|
@ -1793,9 +1823,9 @@ function is called recursively."
|
||||||
(pcase-let
|
(pcase-let
|
||||||
((`(,prev ,next ,parent)
|
((`(,prev ,next ,parent)
|
||||||
(treesit--defuns-around pos regexp pred)))
|
(treesit--defuns-around pos regexp pred)))
|
||||||
;; When PARENT is nil, nested and top-level are the same,
|
;; When PARENT is nil, nested and top-level are the same, if
|
||||||
;; there there is a PARENT, make PARENT to be the top-level
|
;; there is a PARENT, make PARENT to be the top-level parent
|
||||||
;; parent and pretend there is no nested PREV and NEXT.
|
;; and pretend there is no nested PREV and NEXT.
|
||||||
(when (and (eq treesit-defun-tactic 'top-level)
|
(when (and (eq treesit-defun-tactic 'top-level)
|
||||||
parent)
|
parent)
|
||||||
(setq parent (treesit--top-level-defun
|
(setq parent (treesit--top-level-defun
|
||||||
|
|
@ -1811,16 +1841,15 @@ function is called recursively."
|
||||||
(parent t) ; [2]
|
(parent t) ; [2]
|
||||||
(t nil)))
|
(t nil)))
|
||||||
;; Special case: go to next beg-of-defun. Set POS
|
;; Special case: go to next beg-of-defun. Set POS
|
||||||
;; to the end of next/parent defun, and run one more
|
;; to the end of next-sib/parent defun, and run one
|
||||||
;; step. If there is a next defun, step over it, so
|
;; more step. If there is a next-sib defun, we only
|
||||||
;; we only need to recurse once, so we don't need to
|
;; need to recurse once, so we don't need to recurse
|
||||||
;; recurse if we are already recursing [1]. If there
|
;; if we are already recursing [1]. If there is no
|
||||||
;; is no next but a parent, keep stepping out
|
;; next-sib but a parent, keep stepping out
|
||||||
;; (recursing) until we got out of the parents until
|
;; (recursing) until we got out of the parents until
|
||||||
;; (1) there is a next sibling defun, or (2) no more
|
;; (1) there is a next sibling defun, or (2) no more
|
||||||
;; parents [2].
|
;; parents [2].
|
||||||
(setq pos
|
(setq pos (or (treesit--navigate-defun
|
||||||
(or (treesit--navigate-defun
|
|
||||||
(treesit-node-end (or next parent))
|
(treesit-node-end (or next parent))
|
||||||
1 'beg t)
|
1 'beg t)
|
||||||
(throw 'term nil)))
|
(throw 'term nil)))
|
||||||
|
|
@ -1832,8 +1861,7 @@ function is called recursively."
|
||||||
(parent t)
|
(parent t)
|
||||||
(t nil)))
|
(t nil)))
|
||||||
;; Special case: go to prev end-of-defun.
|
;; Special case: go to prev end-of-defun.
|
||||||
(setq pos
|
(setq pos (or (treesit--navigate-defun
|
||||||
(or (treesit--navigate-defun
|
|
||||||
(treesit-node-start (or prev parent))
|
(treesit-node-start (or prev parent))
|
||||||
-1 'end t)
|
-1 'end t)
|
||||||
(throw 'term nil)))
|
(throw 'term nil)))
|
||||||
|
|
@ -1842,9 +1870,7 @@ function is called recursively."
|
||||||
;; A successful step! Decrement counter.
|
;; A successful step! Decrement counter.
|
||||||
(cl-decf counter))))
|
(cl-decf counter))))
|
||||||
;; Counter equal to 0 means we successfully stepped ARG steps.
|
;; Counter equal to 0 means we successfully stepped ARG steps.
|
||||||
(if (eq counter 0)
|
(if (eq counter 0) pos nil)))
|
||||||
pos
|
|
||||||
nil)))
|
|
||||||
|
|
||||||
;;; Activating tree-sitter
|
;;; Activating tree-sitter
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue