GNU bug report logs - #70789
treesit navigate for outlines at bobp

Previous Next

Package: emacs;

Reported by: Juri Linkov <juri <at> linkov.net>

Date: Sun, 5 May 2024 16:57:01 UTC

Severity: normal

Fixed in version 30.0.50

Done: Juri Linkov <juri <at> linkov.net>

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 70789 in the body.
You can then email your comments to 70789 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 casouri <at> gmail.com, bug-gnu-emacs <at> gnu.org:
bug#70789; Package emacs. (Sun, 05 May 2024 16:57:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to Juri Linkov <juri <at> linkov.net>:
New bug report received and forwarded. Copy sent to casouri <at> gmail.com, bug-gnu-emacs <at> gnu.org. (Sun, 05 May 2024 16:57:01 GMT) Full text and rfc822 format available.

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

From: Juri Linkov <juri <at> linkov.net>
To: bug-gnu-emacs <at> gnu.org
Subject: treesit navigate for outlines at bobp
Date: Sun, 05 May 2024 19:52:43 +0300
[Message part 1 (text/plain, inline)]
'treesit-outline-search' didn't match outlines at the beginning of
the buffer because unlike 're-search-forward' (used by outline-mode)
that matches the text that immediately follows point,
'treesit-navigate-thing' misses text at point and stars the search
after point.

So there is a need to handle this difference specially.  Therefore this
patch adds such special-handling of bobp to 'treesit-outline-search':

[treesit-outline-search-bobp.patch (text/x-diff, inline)]
diff --git a/lisp/treesit.el b/lisp/treesit.el
index e55e04e53b3..86ed1bbae33 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -2884,15 +2884,21 @@ treesit-outline-search
                   (start (treesit-node-start node)))
         (eq (pos-bol) (save-excursion (goto-char start) (pos-bol))))
 
-    (let* ((pos
+    (let* ((bob-pos
+            ;; `treesit-navigate-thing' can't find a thing at bobp,
+            ;; so use `looking-at' to match at bobp.
+            (and (bobp) (treesit-outline-search bound move backward t) (point)))
+           (pos
             ;; When function wants to find the current outline, point
             ;; is at the beginning of the current line.  When it wants
             ;; to find the next outline, point is at the second column.
-            (if (eq (point) (pos-bol))
-                (if (bobp) (point) (1- (point)))
-              (pos-eol)))
-           (found (treesit-navigate-thing pos (if backward -1 1) 'beg
-                                          treesit-outline-predicate)))
+            (unless bob-pos
+              (if (eq (point) (pos-bol))
+                  (if (bobp) (point) (1- (point)))
+                (pos-eol))))
+           (found (or bob-pos
+                      (treesit-navigate-thing pos (if backward -1 1) 'beg
+                                              treesit-outline-predicate))))
       (if found
           (if (or (not bound) (if backward (>= found bound) (<= found bound)))
               (progn

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#70789; Package emacs. (Mon, 06 May 2024 03:13:01 GMT) Full text and rfc822 format available.

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

From: Yuan Fu <casouri <at> gmail.com>
To: Juri Linkov <juri <at> linkov.net>
Cc: 70789 <at> debbugs.gnu.org
Subject: Re: bug#70789: treesit navigate for outlines at bobp
Date: Sun, 5 May 2024 20:11:56 -0700

> On May 5, 2024, at 9:52 AM, Juri Linkov <juri <at> linkov.net> wrote:
> 
> 'treesit-outline-search' didn't match outlines at the beginning of
> the buffer because unlike 're-search-forward' (used by outline-mode)
> that matches the text that immediately follows point,
> 'treesit-navigate-thing' misses text at point and stars the search
> after point.
> 
> So there is a need to handle this difference specially.  Therefore this
> patch adds such special-handling of bobp to 'treesit-outline-search’:

Thanks. Just for me to understand it better, could you show an example where treesit-navigate-thing misses text at point? If it does, that should be a bug that treesit-navigate-thing should fix, right?

Yuan






Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#70789; Package emacs. (Mon, 06 May 2024 07:09:02 GMT) Full text and rfc822 format available.

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

From: Juri Linkov <juri <at> linkov.net>
To: Yuan Fu <casouri <at> gmail.com>
Cc: 70789 <at> debbugs.gnu.org
Subject: Re: bug#70789: treesit navigate for outlines at bobp
Date: Mon, 06 May 2024 09:43:54 +0300
>> 'treesit-outline-search' didn't match outlines at the beginning of
>> the buffer because unlike 're-search-forward' (used by outline-mode)
>> that matches the text that immediately follows point,
>> 'treesit-navigate-thing' misses text at point and stars the search
>> after point.
>>
>> So there is a need to handle this difference specially.  Therefore this
>> patch adds such special-handling of bobp to 'treesit-outline-search’:
>
> Thanks.  Just for me to understand it better, could you show an
> example where treesit-navigate-thing misses text at point?

Here is an example:

0. emacs -Q
1. C-x C-f test/lisp/progmodes/ruby-mode-resources/ruby-method-params-indent.rb
2. M-x ruby-ts-mode
3. M-: (setq outline-minor-mode-use-buttons t)
4. M-x outline-minor-mode

There are no outline buttons because the top thing "class"
is at the beginning of the buffer.  Then you can insert
an empty line before "class", and disable/enable
outline-minor-mode again, and outline buttons appear
on "class" and "def" lines.

Basically this is because

  (treesit-navigate-thing (point-min) 1 'beg "\\`\\(?:class\\|method\\)\\'")

can't find "class" at the beginning of the buffer,
but after inserting an empty line before "class" it can find it.

> If it does, that should be a bug that treesit-navigate-thing should
> fix, right?

I'm not sure if this is a bug.  Maybe it can be described just as
a difference between re-search-forward and treesit-navigate-thing.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#70789; Package emacs. (Thu, 09 May 2024 00:18:02 GMT) Full text and rfc822 format available.

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

From: Yuan Fu <casouri <at> gmail.com>
To: Juri Linkov <juri <at> linkov.net>
Cc: 70789 <at> debbugs.gnu.org
Subject: Re: bug#70789: treesit navigate for outlines at bobp
Date: Wed, 8 May 2024 17:16:22 -0700

> On May 5, 2024, at 11:43 PM, Juri Linkov <juri <at> linkov.net> wrote:
> 
>>> 'treesit-outline-search' didn't match outlines at the beginning of
>>> the buffer because unlike 're-search-forward' (used by outline-mode)
>>> that matches the text that immediately follows point,
>>> 'treesit-navigate-thing' misses text at point and stars the search
>>> after point.
>>> 
>>> So there is a need to handle this difference specially.  Therefore this
>>> patch adds such special-handling of bobp to 'treesit-outline-search’:
>> 
>> Thanks.  Just for me to understand it better, could you show an
>> example where treesit-navigate-thing misses text at point?
> 
> Here is an example:
> 
> 0. emacs -Q
> 1. C-x C-f test/lisp/progmodes/ruby-mode-resources/ruby-method-params-indent.rb
> 2. M-x ruby-ts-mode
> 3. M-: (setq outline-minor-mode-use-buttons t)
> 4. M-x outline-minor-mode
> 
> There are no outline buttons because the top thing "class"
> is at the beginning of the buffer.  Then you can insert
> an empty line before "class", and disable/enable
> outline-minor-mode again, and outline buttons appear
> on "class" and "def" lines.
> 
> Basically this is because
> 
>  (treesit-navigate-thing (point-min) 1 'beg "\\`\\(?:class\\|method\\)\\'")
> 
> can't find "class" at the beginning of the buffer,
> but after inserting an empty line before "class" it can find it.

I see, from treesit-navigate-thing’s point of view, point is already at the beginning of a class, therefore the correct behavior is to go to the beg of the next class. Using ‘end is probably more appropriate here, but I know you must have good reasons to use ‘beg. How about using treesit-search-forward? Wait, am I the one that suggested treesit-navigate-thing to you, because it returns higher-leveled nodes first?

In that case, maybe you can use treesit-thing-next to test if point is at the beginning of an outline header? That feels a bit easier to write and understand to me. If you prefer the current patch I don’t have problem with it either.

Yuan






Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#70789; Package emacs. (Thu, 09 May 2024 06:32:03 GMT) Full text and rfc822 format available.

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

From: Juri Linkov <juri <at> linkov.net>
To: Yuan Fu <casouri <at> gmail.com>
Cc: 70789 <at> debbugs.gnu.org
Subject: Re: bug#70789: treesit navigate for outlines at bobp
Date: Thu, 09 May 2024 09:28:59 +0300
close 70789 30.0.50
thanks

>>  (treesit-navigate-thing (point-min) 1 'beg "\\`\\(?:class\\|method\\)\\'")
>>
>> can't find "class" at the beginning of the buffer,
>> but after inserting an empty line before "class" it can find it.
>
> I see, from treesit-navigate-thing’s point of view, point is already at the
> beginning of a class, therefore the correct behavior is to go to the beg of
> the next class. Using ‘end is probably more appropriate here, but I know
> you must have good reasons to use ‘beg. How about using
> treesit-search-forward? Wait, am I the one that suggested
> treesit-navigate-thing to you, because it returns higher-leveled nodes
> first?

Indeed.

> In that case, maybe you can use treesit-thing-next to test if point is
> at the beginning of an outline header? That feels a bit easier to
> write and understand to me.

I don't see how treesit-thing-next could be used here since
quoting its doc:

  The returned node, if non-nil, must be after POS, i.e., its
  start >= POS.

> If you prefer the current patch I don’t have problem with it either.

Thanks, so now pushed.




bug marked as fixed in version 30.0.50, send any further explanations to 70789 <at> debbugs.gnu.org and Juri Linkov <juri <at> linkov.net> Request was from Juri Linkov <juri <at> linkov.net> to control <at> debbugs.gnu.org. (Thu, 09 May 2024 06:32:03 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. (Thu, 06 Jun 2024 11:24:12 GMT) Full text and rfc822 format available.

This bug report was last modified 1 year and 11 days ago.

Previous Next


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