Subject: bug#70789: treesit navigate for outlines at bobp

From: Yuan Fu <[email-masked]>
Date: Wed, 8 May 2024 17:16:22 -0700

> On May 5, 2024, at 11:43 PM, Juri Linkov <[email-masked]> 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