tree-sitter icon indicating copy to clipboard operation
tree-sitter copied to clipboard

Incorrect quantified query behavior with anchor

Open ribru17 opened this issue 1 year ago • 1 comments

Problem

Currently quantified captures are not properly calculated when anchored to another element (as a sibling). The behavior is dependent on previous context as well (explained below)

Steps to reproduce

In the playground, enter the following ruby code:

  # Try deleting me
  require 'asdf'

  # Hello
  # Hello
  # Hello
  class RiemannPuppetPing

  end

With the query:

  (program
    (comment)+ @comment.documentation
    .
    (class))

Actual behavior: Nothing is captured

Expected behavior

I would expect all three "Hello" comments to be captured. Strangely enough, deleting the "delete me" comment allows them to be captured :thinking: Deleting the anchor makes it so that only the "delete me" comment is captured, which is also incorrect I believe

Tree-sitter version (tree-sitter --version)

0.22.6

Operating system/version

NixOS, linux kernel 6.10.6

ribru17 avatar Aug 30 '24 04:08 ribru17

There's also a related problem where if I make the comment optional in this example and the function doesn't have a preceding comment, the . anchor changes the meaning to "(class) must be the first child within (program)".

  (program
    (comment)* @comment.documentation
    .
    (class))

And, yes, I've tried:

  (program
    ((comment)+ @comment.documentation .)?
    (class))

That seems to just ignore the . anchor and match any comment that precedes (though not necessarily immediately precedes) the class.

jmehnle avatar Aug 31 '24 05:08 jmehnle