From 30e66532da4d31a98c5056fc08876c017fb65203 Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Thu, 12 Jun 2025 20:22:42 +0300 Subject: [PATCH] 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 (cherry picked from commit 76f422da26b09a5d2646c0597871423f019a9573) --- lisp/progmodes/elixir-ts-mode.el | 6 ++--- lisp/progmodes/heex-ts-mode.el | 4 ++- lisp/progmodes/python.el | 26 ++++++++++--------- lisp/treesit.el | 44 +++++++++++++++++--------------- 4 files changed, 44 insertions(+), 36 deletions(-) diff --git a/lisp/progmodes/elixir-ts-mode.el b/lisp/progmodes/elixir-ts-mode.el index b98a1fb96e8..d1a78ca1018 100644 --- a/lisp/progmodes/elixir-ts-mode.el +++ b/lisp/progmodes/elixir-ts-mode.el @@ -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)) diff --git a/lisp/progmodes/heex-ts-mode.el b/lisp/progmodes/heex-ts-mode.el index cf3fad4af10..08c9019e6bc 100644 --- a/lisp/progmodes/heex-ts-mode.el +++ b/lisp/progmodes/heex-ts-mode.el @@ -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)) diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 66f911ac697..3941a73e044 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -7262,18 +7262,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"))) - - (setq-local treesit-sexp-type-regexp - (regexp-opt '("expression" - "string" - "call" - "operator" - "identifier" - "integer" - "float"))) + (when (boundp 'treesit-sentence-type-regexp) + (setq-local treesit-sentence-type-regexp + (regexp-opt '("statement" + "clause")))) + + (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) diff --git a/lisp/treesit.el b/lisp/treesit.el index 1d06efb2e6d..dae9a3bc575 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -2976,12 +2976,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. @@ -3015,7 +3019,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 @@ -3042,7 +3046,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) @@ -3129,8 +3133,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) @@ -3147,8 +3151,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) @@ -3173,8 +3177,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) @@ -3203,8 +3207,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) @@ -3223,7 +3227,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'. @@ -3243,19 +3247,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"))))) -- 2.39.5