]> git.eshelyaron.com Git - emacs.git/commitdiff
Improve treesit list navigation support for #if/#endif (bug#73404)
authorJuri Linkov <juri@linkov.net>
Sat, 11 Jan 2025 18:28:43 +0000 (20:28 +0200)
committerEshel Yaron <me@eshelyaron.com>
Sun, 12 Jan 2025 14:58:37 +0000 (15:58 +0100)
* 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)

lisp/progmodes/c-ts-mode.el
lisp/treesit.el

index 80e635fe76d4c159ad18b04ad03ed29c22b27794..dd08731edb4583d6e5be31e9750b7608aa13f34a 100644 (file)
@@ -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"
index 9e967c4a8f85ddc768d54bee88de5a0301828905..efae59c09733b481d90ff735c4384e448ff1db2b 100644 (file)
@@ -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: