From: Juri Linkov Date: Sat, 11 Jan 2025 18:28:43 +0000 (+0200) Subject: Improve treesit list navigation support for #if/#endif (bug#73404) X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=9ce2ea2cac3d17f5847b125a3f053f0a0d085f6e;p=emacs.git Improve treesit list navigation support for #if/#endif (bug#73404) * lisp/treesit.el (treesit--forward-list-with-default): Improve to better support the case when point is inside an opening/closing node with a name longer than 1 character such as "#if" and "#endif". (treesit-navigate-thing): No need to check for thing 'list' since list commands now use other functions. * lisp/progmodes/c-ts-mode.el (c-ts-mode--thing-settings): Add more preproc nodes such as #if/#endif and #ifdef/#endif and their variants to the 'list' thing. (cherry picked from commit db4bf410f2bae6c77f2a0e660dcc680d73a18cd1) --- diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el index 80e635fe76d..dd08731edb4 100644 --- a/lisp/progmodes/c-ts-mode.el +++ b/lisp/progmodes/c-ts-mode.el @@ -1153,6 +1153,14 @@ if `c-ts-mode-emacs-sources-support' is non-nil." (sexp (not ,(rx (or "{" "}" "[" "]" "(" ")" ",")))) (list ,(regexp-opt '("preproc_params" + "preproc_if" + "preproc_ifdef" + "preproc_if_in_field_declaration_list" + "preproc_ifdef_in_field_declaration_list" + "preproc_if_in_enumerator_list" + "preproc_ifdef_in_enumerator_list" + "preproc_if_in_enumerator_list_no_comma" + "preproc_ifdef_in_enumerator_list_no_comma" "preproc_parenthesized_expression" "preproc_argument_list" "attribute_declaration" diff --git a/lisp/treesit.el b/lisp/treesit.el index 9e967c4a8f8..efae59c0973 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -2481,11 +2481,13 @@ ARG is described in the docstring of `forward-list'." (funcall default-function inc) (point)) (scan-error nil))) + (parent (treesit-thing-at (point) pred t)) (sibling (if (> arg 0) (treesit-thing-next (point) pred) - (treesit-thing-prev (point) pred))) - (current (when default-pos - (treesit-thing-at (point) pred t)))) + (treesit-thing-prev (point) pred)))) + (when (and parent sibling + (not (treesit-node-enclosed-p sibling parent))) + (setq sibling nil)) ;; 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 @@ -2493,10 +2495,10 @@ ARG is described in the docstring of `forward-list'." (if (> arg 0) (<= default-pos (treesit-node-start sibling)) (>= default-pos (treesit-node-end sibling)))) - (or (null current) + (or (null parent) (if (> arg 0) - (<= default-pos (treesit-node-end current)) - (>= default-pos (treesit-node-start current))))) + (< default-pos (treesit-node-end parent)) + (> default-pos (treesit-node-start parent))))) (goto-char default-pos)) (when sibling (goto-char (if (> arg 0) @@ -3070,9 +3072,7 @@ function is called recursively." (if (eq tactic 'restricted) (setq pos (funcall advance - (cond ((and (null next) (null prev) - (not (eq thing 'list))) - parent) + (cond ((and (null next) (null prev)) parent) ((> arg 0) next) (t prev)))) ;; For `nested', it's a bit more work: