Subject: Re: bug#61529: 30.0.50; tree-sitter: weird off-by-one error but only in css-ts-mode(?) with `treesit-node-at'

From: Theodor Thornhill <[email-masked]>
Date: Wed, 15 Feb 2023 19:35:26 +0100

Mickey Petersen <[email-masked]> writes:

> Eli Zaretskii <[email-masked]> writes:
>
>>> From: Mickey Petersen <[email-masked]>
>>> Date: Wed, 15 Feb 2023 08:25:53 +0000
>>>
>>>
>>> With point at '2', then I'd expect `treesit-node-at' to yield that node. But it does not:
>>>
>>> (cons (point) (treesit-node-at (point)))
>>>
>>> => (34 . #<treesit-node "(" in 34-35>)
>>
>> The value of point is the number of the character which _follows_
>> point, yes?  So when the cursor is on '2', point is actually between
>> '(' and '2'.  Right?  What does this mean in terms of the node that
>> should be returned by tree-sitter?
>
> Correct, point is between '(' and '2'. So 34-35 means it occupies
> position 34-35 or [34,35). So point is outside the scope of the '('
> single-char anonymous node.
>
> Or at least it should be: the problem is that it *is* inside it in
> this one weird instance and, near as I can find, only in this mode,
> and then only in this place, it isn't. I suspect `treesit-node-at' has
> a bug.
>

Hi, Mickey!

> Consider:
>
>     a {
>       background: linear-gradient(210deg, rgba(|255,82,41,1) 0%, rgba(251,165,85,1) 54%, rgba(163,73,73,1) 100%);
>     }
>
> Note the new position of point in rgba. `treesit-node-at` with `(point)` now correctly returns
>
>     #<treesit-node integer_value in 48-51>
>
> Move point back one position:
>
>     a {
>       background: linear-gradient(210deg, rgba|(255,82,41,1) 0%, rgba(251,165,85,1) 54%, rgba(163,73,73,1) 100%);
>     }
>
> And now:
>
>   (treesit-node-at (point)) => #<treesit-node "(" in 47-48>
>
> In start contrast to the original example.

So the docstring of treesit-node-at states:

  "Return the leaf node at position POS.

A leaf node is a node that doesn't have any child nodes.

The returned node's span covers POS: the node's beginning is before
or at POS, and the node's end is at or after POS.

If no leaf node's span covers POS (e.g., POS is on whitespace
between two leaf nodes), return the first leaf node after POS.

If there is no leaf node after POS, return the first leaf node
before POS.

Return nil if no leaf node can be returned.  If NAMED is non-nil,
only look for named nodes."

Doesn't this describe this behavior?

Theo