* bug#60355: 29.0.60; Tree sitter incorrectly handles of PRED for treesit-defun-type-regexp
@ 2022-12-27 12:09 Wilhelm H Kirschbaum
2022-12-28 1:47 ` Yuan Fu
0 siblings, 1 reply; 4+ messages in thread
From: Wilhelm H Kirschbaum @ 2022-12-27 12:09 UTC (permalink / raw)
To: 60355
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.
^ permalink raw reply related [flat|nested] 4+ messages in thread
* bug#60355: 29.0.60; Tree sitter incorrectly handles of PRED for treesit-defun-type-regexp
2022-12-27 12:09 bug#60355: 29.0.60; Tree sitter incorrectly handles of PRED for treesit-defun-type-regexp Wilhelm H Kirschbaum
@ 2022-12-28 1:47 ` Yuan Fu
2022-12-28 6:31 ` Wilhelm H Kirschbaum
0 siblings, 1 reply; 4+ messages in thread
From: Yuan Fu @ 2022-12-28 1:47 UTC (permalink / raw)
To: Wilhelm; +Cc: 60355
Wilhelm H Kirschbaum <wkirschbaum@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
^ permalink raw reply [flat|nested] 4+ messages in thread
* bug#60355: 29.0.60; Tree sitter incorrectly handles of PRED for treesit-defun-type-regexp
2022-12-28 1:47 ` Yuan Fu
@ 2022-12-28 6:31 ` Wilhelm H Kirschbaum
2022-12-28 8:34 ` Yuan Fu
0 siblings, 1 reply; 4+ messages in thread
From: Wilhelm H Kirschbaum @ 2022-12-28 6:31 UTC (permalink / raw)
To: Yuan Fu; +Cc: 60355
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@gmail.com> writes:
> Wilhelm H Kirschbaum <wkirschbaum@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
^ permalink raw reply [flat|nested] 4+ messages in thread
* bug#60355: 29.0.60; Tree sitter incorrectly handles of PRED for treesit-defun-type-regexp
2022-12-28 6:31 ` Wilhelm H Kirschbaum
@ 2022-12-28 8:34 ` Yuan Fu
0 siblings, 0 replies; 4+ messages in thread
From: Yuan Fu @ 2022-12-28 8:34 UTC (permalink / raw)
To: Wilhelm H Kirschbaum; +Cc: 60355
> On Dec 27, 2022, at 10:31 PM, Wilhelm H Kirschbaum <wkirschbaum@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
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-12-28 8:34 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-12-27 12:09 bug#60355: 29.0.60; Tree sitter incorrectly handles of PRED for treesit-defun-type-regexp Wilhelm H Kirschbaum
2022-12-28 1:47 ` Yuan Fu
2022-12-28 6:31 ` Wilhelm H Kirschbaum
2022-12-28 8:34 ` Yuan Fu
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.