GNU bug report logs - #60355
29.0.60; Tree sitter incorrectly handles of PRED for treesit-defun-type-regexp

Previous Next

Package: emacs;

Reported by: Wilhelm H Kirschbaum <wkirschbaum <at> gmail.com>

Date: Tue, 27 Dec 2022 12:26:01 UTC

Severity: normal

Found in version 29.0.60

Fixed in version 29.1

Done: Yuan Fu <casouri <at> gmail.com>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 60355 in the body.
You can then email your comments to 60355 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#60355; Package emacs. (Tue, 27 Dec 2022 12:26:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to Wilhelm H Kirschbaum <wkirschbaum <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Tue, 27 Dec 2022 12:26:01 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Wilhelm H Kirschbaum <wkirschbaum <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: 29.0.60; Tree sitter incorrectly handles of PRED for
 treesit-defun-type-regexp
Date: Tue, 27 Dec 2022 14:09:29 +0200
When `treesit-defun-type-regexp` is set with a cons cell (regexp . 
some-pred)
the following case does not get handled as expected when calling 
(end-of-defun):

```elixir
defmodule Example do
 def foo() do ; regexp match, pred match
 end

 ;; <point here>
 @impl true    ; regexp match, pred does not match
 def bar() do  ; regexp match, pred match
 end
 ;; <should jump to here>

 def baz() do
 end
end
;; <jumps to point here>
```

The function `treesit--things-around` only looks at the PRED after 
it searches forward and
would then not be aware of the non matching node.  The following 
change works for my case
where search-forward takes in the PRED, not just the regexp:

diff --git a/lisp/treesit.el b/lisp/treesit.el
index 2130cd0061..7997509f50 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -1776,7 +1776,13 @@ treesit--things-around
                            node "" nil nil t))
                       node))
         (result (list nil nil nil))
-         (pred (or pred (lambda (_) t))))
+         (pred (if (null pred)
+                (lambda (node)
+                  (string-match-p regexp (treesit-node-type 
node)))
+                (lambda (node)
+                  (and (string-match-p regexp (treesit-node-type 
node))
+                       (funcall pred node))))))
+
    ;; 1. Find previous and next sibling defuns.
    (cl-loop
     for idx from 0 to 1
@@ -1798,12 +1804,10 @@ treesit--things-around
     do (cl-loop for cursor = (when node
                                (save-excursion
                                  (treesit-search-forward
-                                   node regexp backward 
                                   backward)))
+                                   node pred backward backward)))
                 then (treesit-node-parent cursor)
                 while cursor
-                 if (and (string-match-p
-                          regexp (treesit-node-type cursor))
-                         (funcall pred cursor)
+                 if (and (funcall pred cursor)
                         (funcall pos-pred cursor))
                 do (setf (nth idx result) cursor)))
    ;; 2. Find the parent defun.


Hope this makes sense,
Wilhelm

ps. Sorry about the email swapping, think it should be fixed now. 




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#60355; Package emacs. (Wed, 28 Dec 2022 01:48:01 GMT) Full text and rfc822 format available.

Message #8 received at 60355 <at> debbugs.gnu.org (full text, mbox):

From: Yuan Fu <casouri <at> gmail.com>
To: Wilhelm <wkirschbaum <at> gmail.com>
Cc: 60355 <at> debbugs.gnu.org
Subject: Re: bug#60355: 29.0.60; Tree sitter incorrectly handles of PRED for 
 treesit-defun-type-regexp
Date: Tue, 27 Dec 2022 17:47:35 -0800
Wilhelm H Kirschbaum <wkirschbaum <at> gmail.com> writes:

> When `treesit-defun-type-regexp` is set with a cons cell (regexp .
> some-pred)
> the following case does not get handled as expected when calling
> (end-of-defun):
>
> ```elixir
> defmodule Example do
>  def foo() do ; regexp match, pred match
>  end
>
>  ;; <point here>
>  @impl true    ; regexp match, pred does not match
>  def bar() do  ; regexp match, pred match
>  end
>  ;; <should jump to here>
>
>  def baz() do
>  end
> end
> ;; <jumps to point here>
> ```

Thank you very much! The root cause is actually another problem, but
anyway it is fixed now.

May I also suggest you to use this function for the predicate:

(defun elixir-ts-mode--capture-defun (node)
  ;; NODE should be a ‘call’ node.
  (member (treesit-node-text
           (treesit-node-child-by-field-name node "target"))
          elixir-ts-mode--definition-keywords))

This should be more accurate than the current one, and is IMO simpler
too.

Yuan




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#60355; Package emacs. (Wed, 28 Dec 2022 06:34:02 GMT) Full text and rfc822 format available.

Message #11 received at 60355 <at> debbugs.gnu.org (full text, mbox):

From: Wilhelm H Kirschbaum <wkirschbaum <at> gmail.com>
To: Yuan Fu <casouri <at> gmail.com>
Cc: 60355 <at> debbugs.gnu.org
Subject: Re: bug#60355: 29.0.60; Tree sitter incorrectly handles of PRED for
 treesit-defun-type-regexp
Date: Wed, 28 Dec 2022 08:31:32 +0200
Thanks Yuan, just checked and both the fix and your suggestion 
works.

I see there is a typo in your last change:

@@ -310,7 +310,7 @@ java-ts-mode
  ;; Imenu.
  (setq-local treesit-simple-imenu-settings
              '(("Class" "\\`class_declaration\\'" nil nil)
-                ("Interface "\\`interface_declaration\\'" nil 
                nil)
+                ("Interface" "\\`interface_declaration\\'" nil 
nil)
                ("Enum" "\\`record_declaration\\'" nil nil)
                ("Method" "\\`method_declaration\\'" nil nil)))
  (treesit-major-mode-setup))


Yuan Fu <casouri <at> gmail.com> writes:

> Wilhelm H Kirschbaum <wkirschbaum <at> gmail.com> writes:
>
>> When `treesit-defun-type-regexp` is set with a cons cell 
>> (regexp .
>> some-pred)
>> the following case does not get handled as expected when 
>> calling
>> (end-of-defun):
>>
>> ```elixir
>> defmodule Example do
>>  def foo() do ; regexp match, pred match
>>  end
>>
>>  ;; <point here>
>>  @impl true    ; regexp match, pred does not match
>>  def bar() do  ; regexp match, pred match
>>  end
>>  ;; <should jump to here>
>>
>>  def baz() do
>>  end
>> end
>> ;; <jumps to point here>
>> ```
>
> Thank you very much! The root cause is actually another problem, 
> but
> anyway it is fixed now.
>
> May I also suggest you to use this function for the predicate:
>
> (defun elixir-ts-mode--capture-defun (node)
>   ;; NODE should be a ‘call’ node.
>   (member (treesit-node-text
>            (treesit-node-child-by-field-name node "target"))
>           elixir-ts-mode--definition-keywords))
>
> This should be more accurate than the current one, and is IMO 
> simpler
> too.
>
> Yuan





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#60355; Package emacs. (Wed, 28 Dec 2022 08:35:02 GMT) Full text and rfc822 format available.

Message #14 received at 60355 <at> debbugs.gnu.org (full text, mbox):

From: Yuan Fu <casouri <at> gmail.com>
To: Wilhelm H Kirschbaum <wkirschbaum <at> gmail.com>
Cc: 60355 <at> debbugs.gnu.org
Subject: Re: bug#60355: 29.0.60; Tree sitter incorrectly handles of PRED for
 treesit-defun-type-regexp
Date: Wed, 28 Dec 2022 00:34:37 -0800

> On Dec 27, 2022, at 10:31 PM, Wilhelm H Kirschbaum <wkirschbaum <at> gmail.com> wrote:
> 
> 
> Thanks Yuan, just checked and both the fix and your suggestion works.
> 
> I see there is a typo in your last change:
> 
> @@ -310,7 +310,7 @@ java-ts-mode
>  ;; Imenu.
>  (setq-local treesit-simple-imenu-settings
>              '(("Class" "\\`class_declaration\\'" nil nil)
> -                ("Interface "\\`interface_declaration\\'" nil                 nil)
> +                ("Interface" "\\`interface_declaration\\'" nil nil)
>                ("Enum" "\\`record_declaration\\'" nil nil)
>                ("Method" "\\`method_declaration\\'" nil nil)))
>  (treesit-major-mode-setup))

Oops, was in a bit of hurry and forgot to byte-compile. Thanks for the catch!

Yuan



bug marked as fixed in version 29.1, send any further explanations to 60355 <at> debbugs.gnu.org and Wilhelm H Kirschbaum <wkirschbaum <at> gmail.com> Request was from Yuan Fu <casouri <at> gmail.com> to control <at> debbugs.gnu.org. (Sat, 07 Jan 2023 23:31:01 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sun, 05 Feb 2023 12:24:05 GMT) Full text and rfc822 format available.

This bug report was last modified 2 years and 140 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.