Fix an issue when searching subtree backward (bug#67117)

* src/treesit.c (treesit_traverse_child_helper):
Do not call treesit_traverse_sibling_helper when the named node is
required and the last child is the named node.
Otherwise treesit_traverse_sibling_helper will move cursor to the
previous sibling and last node will be skipped.
* test/src/treesit-tests.el (treesit-search-subtree-forward-1):
(treesit-search-subtree-backward-1):
Add tests.
This commit is contained in:
Denis Zubarev 2023-11-12 01:42:42 +03:00 committed by Yuan Fu
parent 03625c2fef
commit 7b315e8a5c
No known key found for this signature in database
GPG key ID: 56E19BC57664A442
2 changed files with 32 additions and 2 deletions

View file

@ -3061,9 +3061,9 @@ treesit_traverse_child_helper (TSTreeCursor *cursor,
/* First go to the last child. */
while (ts_tree_cursor_goto_next_sibling (cursor));
if (!named)
if (!named || (named && ts_node_is_named (ts_tree_cursor_current_node(cursor))))
return true;
/* Else named... */
/* Else named is required and last child is not named node */
if (treesit_traverse_sibling_helper(cursor, false, true))
return true;
else

View file

@ -1083,6 +1083,36 @@ This tests bug#60355."
treesit--ert-defun-navigation-python-program
treesit--ert-defun-navigation-top-level-master)))
(ert-deftest treesit-search-subtree-forward-1 ()
"Test search subtree forward."
(skip-unless (treesit-language-available-p 'python))
(require 'python)
(python-ts-mode)
(insert "Temp(1, 2)")
(goto-char (point-min))
(let ((node (treesit-search-subtree
(treesit--thing-at (point) "call")
(lambda (n) (equal (treesit-node-type n ) "integer")))))
(should node)
(should (equal (treesit-node-text node) "1"))))
(ert-deftest treesit-search-subtree-backward-1 ()
"Test search subtree with backward=t."
(skip-unless (treesit-language-available-p 'python))
(require 'python)
(python-ts-mode)
(insert "Temp(1, 2)")
(goto-char (point-min))
(let ((node (treesit-search-subtree
(treesit--thing-at (point) "call")
(lambda (n) (equal (treesit-node-type n ) "integer"))
t)))
(should node)
(should (equal (treesit-node-text node) "2"))))
;; TODO
;; - Functions in treesit.el
;; - treesit-load-name-override-list