From c353bb2ef5410a89c3816f60f6990a6c433e001c Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Thu, 10 Apr 2025 19:20:35 +0300 Subject: [PATCH] Fix treesit-forward-sexp/list navigation in the middle of a node. * lisp/treesit.el (treesit--forward-list-with-default): Check the thing 'sexp-default' (bug#76791). * lisp/progmodes/elixir-ts-mode.el (elixir-ts-mode): * lisp/progmodes/go-ts-mode.el (go-ts-mode): * lisp/progmodes/ruby-ts-mode.el (ruby-ts-mode): * lisp/progmodes/sh-script.el (bash-ts-mode): Add the thing 'sexp-default' to 'treesit-thing-settings'. (cherry picked from commit ec62674cb959278240a56246cb6a958217dd17c0) --- lisp/progmodes/elixir-ts-mode.el | 5 +++++ lisp/progmodes/go-ts-mode.el | 5 +++++ lisp/progmodes/ruby-ts-mode.el | 21 ++++++++------------- lisp/progmodes/sh-script.el | 6 ++++++ lisp/treesit.el | 8 ++++++++ 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/lisp/progmodes/elixir-ts-mode.el b/lisp/progmodes/elixir-ts-mode.el index 9d938e10fde..43cb6d12cc3 100644 --- a/lisp/progmodes/elixir-ts-mode.el +++ b/lisp/progmodes/elixir-ts-mode.el @@ -720,6 +720,11 @@ Return nil if NODE is not a defun node or doesn't have a name." "do_block" "anonymous_function") eos))) + (sexp-default + ;; For `C-M-f' in "&|(a)" + ("(" . ,(lambda (node) + (equal (treesit-node-type (treesit-node-parent node)) + "unary_operator")))) (sentence ,(rx bos (or "call") eos)) (text diff --git a/lisp/progmodes/go-ts-mode.el b/lisp/progmodes/go-ts-mode.el index 130e3829ae4..82bcf5ef7c1 100644 --- a/lisp/progmodes/go-ts-mode.el +++ b/lisp/progmodes/go-ts-mode.el @@ -306,6 +306,11 @@ "argument_list" "literal_value") eos)) + (sexp-default + ;; For `C-M-f' in "switch a |{ }" + (lambda (node) + (equal (treesit-node-type (treesit-node-parent node)) + "expression_switch_statement"))) (sentence (or "declaration" "statement"))))) diff --git a/lisp/progmodes/ruby-ts-mode.el b/lisp/progmodes/ruby-ts-mode.el index f646e5e9385..6b53dc8ef60 100644 --- a/lisp/progmodes/ruby-ts-mode.el +++ b/lisp/progmodes/ruby-ts-mode.el @@ -1219,6 +1219,12 @@ leading double colon is not added." "hash") eos) #'ruby-ts--list-p)) + (sexp-default + ;; For `C-M-f' in "#|{a}" + ("#{" . ,(lambda (node) + (and (eq (char-after (point)) ?{) + (equal (treesit-node-type (treesit-node-parent node)) + "interpolation"))))) (sentence ,(rx bos (or "return" "body_statement" "call" @@ -1226,19 +1232,8 @@ leading double colon is not added." eos)) (text ,(lambda (node) (or (member (treesit-node-type node) - '("comment" "string_content" "heredoc_content")) - ;; for C-M-f in hash[:key] and hash['key'] - (and (member (treesit-node-text node) - '("[" "]")) - (equal (treesit-node-type - (treesit-node-parent node)) - "element_reference")) - ;; for C-M-f in "abc #{ghi} def" - (and (member (treesit-node-text node) - '("#{" "}")) - (equal (treesit-node-type - (treesit-node-parent node)) - "interpolation")))))))) + '("comment" "string_content" + "heredoc_content")))))))) ;; Imenu. (setq-local imenu-create-index-function #'ruby-ts--imenu) diff --git a/lisp/progmodes/sh-script.el b/lisp/progmodes/sh-script.el index 38306ed26e6..795e79ef2f7 100644 --- a/lisp/progmodes/sh-script.el +++ b/lisp/progmodes/sh-script.el @@ -1662,6 +1662,12 @@ not written in Bash or sh." "command_substitution" "process_substitution") eos)) + (sexp-default + ;; For `C-M-f' in "$|(a)" + ("$(" . + ,(lambda (node) + (equal (treesit-node-type (treesit-node-parent node)) + "command_substitution")))) (sentence ,(rx bos (or "redirected_statement" "declaration_command" diff --git a/lisp/treesit.el b/lisp/treesit.el index 1270c197111..8c0fdd64253 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -3041,6 +3041,14 @@ ARG is described in the docstring of `forward-list'." ;; Use the default function only if it doesn't go ;; over the sibling and doesn't go out of the current group. (or (when (and default-pos + ;; Fallback to the default sexp function when + ;; matching the thing 'sexp-default' at point. + (treesit-node-match-p + (treesit-node-at (if (> arg 0) (point) + (max (1- (point)) (point-min)))) + 'sexp-default t)) + (goto-char default-pos)) + (when (and default-pos (or (null sibling) (if (> arg 0) (<= default-pos (treesit-node-start sibling)) -- 2.39.5