unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#70789: treesit navigate for outlines at bobp
@ 2024-05-05 16:52 Juri Linkov
  2024-05-06  3:11 ` Yuan Fu
  0 siblings, 1 reply; 5+ messages in thread
From: Juri Linkov @ 2024-05-05 16:52 UTC (permalink / raw)
  To: 70789; +Cc: Yuan Fu

[-- Attachment #1: Type: text/plain, Size: 414 bytes --]

'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':


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: treesit-outline-search-bobp.patch --]
[-- Type: text/x-diff, Size: 1493 bytes --]

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

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* bug#70789: treesit navigate for outlines at bobp
  2024-05-05 16:52 bug#70789: treesit navigate for outlines at bobp Juri Linkov
@ 2024-05-06  3:11 ` Yuan Fu
  2024-05-06  6:43   ` Juri Linkov
  0 siblings, 1 reply; 5+ messages in thread
From: Yuan Fu @ 2024-05-06  3:11 UTC (permalink / raw)
  To: Juri Linkov; +Cc: 70789



> On May 5, 2024, at 9:52 AM, Juri Linkov <juri@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







^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#70789: treesit navigate for outlines at bobp
  2024-05-06  3:11 ` Yuan Fu
@ 2024-05-06  6:43   ` Juri Linkov
  2024-05-09  0:16     ` Yuan Fu
  0 siblings, 1 reply; 5+ messages in thread
From: Juri Linkov @ 2024-05-06  6:43 UTC (permalink / raw)
  To: Yuan Fu; +Cc: 70789

>> '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.





^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#70789: treesit navigate for outlines at bobp
  2024-05-06  6:43   ` Juri Linkov
@ 2024-05-09  0:16     ` Yuan Fu
  2024-05-09  6:28       ` Juri Linkov
  0 siblings, 1 reply; 5+ messages in thread
From: Yuan Fu @ 2024-05-09  0:16 UTC (permalink / raw)
  To: Juri Linkov; +Cc: 70789



> On May 5, 2024, at 11:43 PM, Juri Linkov <juri@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







^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#70789: treesit navigate for outlines at bobp
  2024-05-09  0:16     ` Yuan Fu
@ 2024-05-09  6:28       ` Juri Linkov
  0 siblings, 0 replies; 5+ messages in thread
From: Juri Linkov @ 2024-05-09  6:28 UTC (permalink / raw)
  To: Yuan Fu; +Cc: 70789

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.





^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-05-09  6:28 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-05 16:52 bug#70789: treesit navigate for outlines at bobp Juri Linkov
2024-05-06  3:11 ` Yuan Fu
2024-05-06  6:43   ` Juri Linkov
2024-05-09  0:16     ` Yuan Fu
2024-05-09  6:28       ` Juri Linkov

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).