From: Denis Zubarev <dvzubarev@yandex.ru>
To: 67117@debbugs.gnu.org
Subject: bug#67117: [PATCH] Tree-sitter: fix an issue when searching subtree backward
Date: Sun, 12 Nov 2023 02:38:33 +0300 [thread overview]
Message-ID: <87h6lruah2.fsf@yandex.ru> (raw)
[-- Attachment #1: Type: text/plain, Size: 1265 bytes --]
Tags: patch
Steps to reproduce the issue:
1. emacs -Q
2. M-x find-file /tmp/t.py
3. paste to the buffer
Temp(1, 2)
4. M-x python-ts-mode
5. Call search-subtree with backward flag
M-x eval-expression (treesit-search-subtree
(treesit--thing-at (point) "call")
(lambda (n) (equal (treesit-node-type n ) "integer"))
t)
It should return the second int node (#<treesit-node integer in 9-10>), but it returns nil.
I fixed it in 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 the last node will be skipped.
I've added test for this bug.
In GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
3.24.33, cairo version 1.16.0) of 2023-11-12 built on NUC-here
Repository revision: 400a71b8f2c5a49dce4f542adfd2fdb59eb34243
Repository branch: search-subtree-bacward-fix
Windowing system distributor 'The X.Org Foundation', version 11.0.12101004
System Description: Ubuntu 22.04.3 LTS
Configured using:
'configure --with-modules --with-native-compilation=aot
--with-imagemagick --with-json --with-tree-sitter --with-xft'
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Fix-an-issue-when-searching-subtree-backward.patch --]
[-- Type: text/patch, Size: 2721 bytes --]
From 88e913940cdd3c82afbdf2ad6520e1a1b9c2797b Mon Sep 17 00:00:00 2001
From: Denis Zubarev <dvzubarev@yandex.ru>
Date: Sun, 12 Nov 2023 01:42:42 +0300
Subject: [PATCH] Fix an issue when searching subtree backward
* 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
---
src/treesit.c | 4 ++--
test/src/treesit-tests.el | 30 ++++++++++++++++++++++++++++++
2 files changed, 32 insertions(+), 2 deletions(-)
diff --git a/src/treesit.c b/src/treesit.c
index 69b59fca11..4dcad751f4 100644
--- a/src/treesit.c
+++ b/src/treesit.c
@@ -3247,9 +3247,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
diff --git a/test/src/treesit-tests.el b/test/src/treesit-tests.el
index 791e902bd0..c9b15c618c 100644
--- a/test/src/treesit-tests.el
+++ b/test/src/treesit-tests.el
@@ -1167,6 +1167,36 @@ treesit-defun-navigation-top-level
treesit--ert-defun-navigation-top-level-master
'top-level))
+(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
--
2.34.1
next reply other threads:[~2023-11-11 23:38 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-11 23:38 Denis Zubarev [this message]
2023-11-15 13:21 ` bug#67117: [PATCH] Tree-sitter: fix an issue when searching subtree backward Eli Zaretskii
2023-11-15 17:01 ` Eli Zaretskii
2023-11-18 18:47 ` Yuan Fu
2023-11-19 5:47 ` Eli Zaretskii
2023-11-19 6:13 ` Yuan Fu
2023-11-19 6:40 ` Eli Zaretskii
2023-11-21 4:23 ` Yuan Fu
2023-11-21 11:43 ` Eli Zaretskii
2023-11-25 3:44 ` Yuan Fu
2023-11-19 9:15 ` Eli Zaretskii
2023-11-19 11:25 ` Denis Zubarev
2023-11-19 11:45 ` Eli Zaretskii
2023-12-19 0:24 ` Denis Zubarev
2023-12-19 2:26 ` Yuan Fu
2023-12-19 16:52 ` Eli Zaretskii
2023-12-19 17:08 ` Denis Zubarev
2023-12-23 7:09 ` Yuan Fu
2023-12-26 0:39 ` Yuan Fu
2023-12-19 3:24 ` Eli Zaretskii
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87h6lruah2.fsf@yandex.ru \
--to=dvzubarev@yandex.ru \
--cc=67117@debbugs.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).