Add 'treesit-sexp-thing' to use instead of 'treesit-sexp-type-regexp'.

* lisp/treesit.el (treesit-sexp-thing): New variable to use
instead of 'treesit-sexp-type-regexp'.
(treesit-sexp-thing-down-list): Rename from 'treesit-sexp-type-down-list'.
(treesit-sexp-thing-up-list): Rename from 'treesit-sexp-type-up-list'.
(treesit-forward-sexp, treesit--forward-list-with-default)
(treesit-down-list, treesit-up-list): Update references to the
above variables.
(treesit-cycle-sexp-thing): Rename from 'treesit-cycle-sexp-type'.

* lisp/progmodes/elixir-ts-mode.el (elixir-ts-mode): Update same variables.

* lisp/progmodes/heex-ts-mode.el (heex-ts-mode): Set these variables
instead of calling the function that sets them.

* lisp/progmodes/python.el (python-ts-mode): Check if variables
'treesit-sentence-type-regexp' and 'treesit-sexp-type-regexp' are bound.

https://lists.gnu.org/archive/html/emacs-devel/2025-06/msg00182.html
This commit is contained in:
Juri Linkov 2025-06-12 20:22:42 +03:00
parent 799cef5d94
commit 76f422da26
5 changed files with 44 additions and 36 deletions

View file

@ -603,7 +603,7 @@ Visiting a file in such mode asks for confirmation before installing
its tree-sitter grammar. Then it highlights the visited file
according to the syntax defined by the grammar.
*** New command 'treesit-cycle-sexp-type'.
*** New command 'treesit-cycle-sexp-thing'.
It cycles the type of navigation for commands that move across sexp's
and lists, such as 'treesit-forward-sexp', 'treesit-forward-list',
'treesit-down-list', and 'treesit-up-list'. The type can be either

View file

@ -797,10 +797,10 @@ Return nil if NODE is not a defun node or doesn't have a name."
;; Enable the 'sexp' navigation by default
(setq-local forward-sexp-function #'treesit-forward-sexp
treesit-sexp-type-regexp 'sexp
treesit-sexp-thing 'sexp
;; But still use 'list' for `down-list' and `up-list'
treesit-sexp-type-down-list 'list
treesit-sexp-type-up-list 'list)))
treesit-sexp-thing-down-list 'list
treesit-sexp-thing-up-list 'list)))
(derived-mode-add-parents 'elixir-ts-mode '(elixir-mode))

View file

@ -255,8 +255,10 @@ Return nil if NODE is not a defun node or doesn't have a name."
`((elixir ,@elixir-ts--thing-settings)))))
(treesit-major-mode-setup)
;; Enable the 'sexp' navigation by default
(treesit-cycle-sexp-type)))
(setq-local forward-sexp-function #'treesit-forward-sexp
treesit-sexp-thing 'sexp)))
(derived-mode-add-parents 'heex-ts-mode '(heex-mode))

View file

@ -7332,18 +7332,20 @@ implementations: `python-mode' and `python-ts-mode'."
(setq-local treesit-defun-name-function
#'python--treesit-defun-name)
(setq-local treesit-sentence-type-regexp
(regexp-opt '("statement"
"clause")))
(when (boundp 'treesit-sentence-type-regexp)
(setq-local treesit-sentence-type-regexp
(regexp-opt '("statement"
"clause"))))
(setq-local treesit-sexp-type-regexp
(regexp-opt '("expression"
"string"
"call"
"operator"
"identifier"
"integer"
"float")))
(when (boundp 'treesit-sexp-type-regexp)
(setq-local treesit-sexp-type-regexp
(regexp-opt '("expression"
"string"
"call"
"operator"
"identifier"
"integer"
"float"))))
(treesit-major-mode-setup)

View file

@ -2984,12 +2984,16 @@ delimits medium sized statements in the source code. It is,
however, smaller in scope than sentences. This is used by
`treesit-forward-sexp' and friends.")
(defvar-local treesit-sexp-type-down-list nil
"A regexp that matches the sexp nodes for `down-list'.
(defvar-local treesit-sexp-thing nil
"A thing that matches the sexp nodes for `forward-sexp'.
This is used by `treesit-forward-sexp' and `treesit-forward-list'.")
(defvar-local treesit-sexp-thing-down-list nil
"A thing that matches the sexp nodes for `down-list'.
This is used by `treesit-down-list'.")
(defvar-local treesit-sexp-type-up-list nil
"A regexp that matches the sexp nodes for `up-list'.
(defvar-local treesit-sexp-thing-up-list nil
"A thing that matches the sexp nodes for `up-list'.
This is used by `treesit-up-list'.")
;; Avoid interpreting the symbol `list' as a function.
@ -3023,7 +3027,7 @@ across lists, whereas uses `forward-sexp-default-function' to move
across atoms (such as symbols or words) inside the list."
(interactive "^p")
(let ((arg (or arg 1))
(pred (or treesit-sexp-type-regexp 'sexp))
(pred (or treesit-sexp-thing 'sexp))
(node-at-point
(treesit-node-at (point) (treesit-language-at (point)))))
(or (when (and node-at-point
@ -3050,7 +3054,7 @@ Fall back to DEFAULT-FUNCTION as long as it doesn't cross
the boundaries of the list.
ARG is described in the docstring of `forward-list'."
(let* ((pred (or treesit-sexp-type-regexp 'list))
(let* ((pred (or treesit-sexp-thing 'list))
(arg (or arg 1))
(treesit--parser-overlay-offset (if (> arg 0) 0 -1))
(cnt arg)
@ -3137,8 +3141,8 @@ redefined by the variable `down-list-function'.
ARG is described in the docstring of `down-list'."
(interactive "^p")
(let* ((pred (or treesit-sexp-type-down-list
treesit-sexp-type-regexp
(let* ((pred (or treesit-sexp-thing-down-list
treesit-sexp-thing
'list))
(arg (or arg 1))
(cnt arg)
@ -3155,8 +3159,8 @@ ARG is described in the docstring of `down-list'."
(treesit-thing-prev (point) pred)))
(child (when sibling
(treesit-node-child sibling (if (> arg 0) 0 -1)))))
(or (when (and (null (or treesit-sexp-type-down-list
treesit-sexp-type-regexp))
(or (when (and (null (or treesit-sexp-thing-down-list
treesit-sexp-thing))
default-pos
(or (null child)
(if (> arg 0)
@ -3181,8 +3185,8 @@ redefined by the variable `up-list-function'.
ARG is described in the docstring of `up-list'."
(interactive "^p")
(let* ((pred (or treesit-sexp-type-up-list
treesit-sexp-type-regexp
(let* ((pred (or treesit-sexp-thing-up-list
treesit-sexp-thing
'list))
(arg (or arg 1))
(treesit--parser-overlay-offset -1)
@ -3211,8 +3215,8 @@ ARG is described in the docstring of `up-list'."
(treesit-node-at (point) (car parsers)) pred)
parsers (cdr parsers)))))
(or (when (and (null (or treesit-sexp-type-up-list
treesit-sexp-type-regexp))
(or (when (and (null (or treesit-sexp-thing-up-list
treesit-sexp-thing))
default-pos
(or (null parent)
(if (> arg 0)
@ -3231,7 +3235,7 @@ ARG is described in the docstring of `up-list'."
(point) (point))))))
(setq cnt (- cnt inc)))))
(defun treesit-cycle-sexp-type (&optional interactive)
(defun treesit-cycle-sexp-thing (&optional interactive)
"Cycle the type of navigation for sexp and list commands.
This type affects navigation commands such as `treesit-forward-sexp',
`treesit-forward-list', `treesit-down-list', `treesit-up-list'.
@ -3251,19 +3255,19 @@ treesit-based modes."
(interactive "p")
(if (not (treesit-thing-defined-p 'list (treesit-language-at (point))))
(user-error "No `list' thing is defined in `treesit-thing-settings'")
(setq-local treesit-sexp-type-regexp
(unless treesit-sexp-type-regexp
(setq-local treesit-sexp-thing
(unless treesit-sexp-thing
(if (treesit-thing-defined-p
'sexp (treesit-language-at (point)))
'sexp
#'treesit-node-named))
forward-sexp-function
(if treesit-sexp-type-regexp
(if treesit-sexp-thing
#'treesit-forward-sexp
#'treesit-forward-sexp-list))
(when interactive
(message "Cycle sexp type to navigate %s"
(or (and treesit-sexp-type-regexp
(message "Cycle sexp thing to navigate %s"
(or (and treesit-sexp-thing
"treesit nodes")
"syntax symbols and treesit lists")))))