[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#68824: treesitter support for outline-minor-mode
From: |
Yuan Fu |
Subject: |
bug#68824: treesitter support for outline-minor-mode |
Date: |
Thu, 1 Feb 2024 16:34:47 -0800 |
> On Feb 1, 2024, at 9:12 AM, Juri Linkov <juri@linkov.net> wrote:
>
> Yuan, do you think it makes sense to add a new optional arg to
> 'treesit-search-forward' that will define the traversal order?
>
> The problem is that to search the next outline heading in
> outline-minor-mode, there is a need to visit headings in
> the same order as they appear in the buffer
> where a parent is located before its children:
>
> * Parent
> ** Child 1
> ** Child 2
>
> Currently 'treesit-search-forward' returns "Child 1" before "Parent".
>
> This means that 'treesit-search-forward' should return the
> topmost matched parent instead of starting to traverse children
> from the deepest leaf.
>
> IOW, it's necessary to traverse nodes in this order:
>
> o
> |
> о---------------------11
> | |
> o--------S----------3 12
> | | |
> o--o-+--o 1--+--2 4--+-----7
> | | | |
> o o +-+-+ +--+--+
> | | | | |
> 5 6 8 9 10
>
> Please note there is no 11 or 12 at the parent of S node because
> it should traverse only the remaining siblings of the parent.
> This is because remaining parent siblings have treesit-node-start
> positions greater than treesit-node-start of S node.
>
>> +(defun treesit-outline-search (&optional bound move backward looking-at)
>> + (let* ((current (treesit-node-at (pos-bol)))
>> + (current (or (treesit-parent-until current
>> treesit-outline-predicate)
>> + current))
>> + (node (treesit-search-forward
>> + current treesit-outline-predicate backward))
Instead of using treesit-search-forward, can you use treesit-beginning-of-thing
or treesit--navigate-thing to do what you want? They handle the “child before
parent” problem for you, and handles some other edge cases.
Yuan