Skip to content

Tree-sitter callback-parsing prevents correct query processing. #5746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
paul-ollis opened this issue Apr 14, 2025 · 5 comments
Open

Tree-sitter callback-parsing prevents correct query processing. #5746

paul-ollis opened this issue Apr 14, 2025 · 5 comments

Comments

@paul-ollis
Copy link
Contributor

paul-ollis commented Apr 14, 2025

The TextArea code invokes tree_sitter.Parser.parse with a callback function
as its first argument. Queries on the resulting tree_sitter.Tree do not support
predicates (#egq, #not-eq, etc.), which is an important feature of queries (it
is used by several of Textual's query definitions (SCM files)). In practice,
"not supported" means that too many query expressions produce matches:

  • The intention of the query definition wirter is obviously not met.

  • Syntax highlighting is not as rich or nuanced as it should be.

  • Unwanted captures are generated, which will have some impact on performance.

  • Users of Textual are limited when it comes to creating custom SCM files.

It is fairly easy to change the code so that tree_sitter.Parser.parse is invoked
with the full text of the TextArea as the first argument, in which case query
definitions are then fully supported. I have tried this on my local
textarea-speedup-2 branch - as used for #5645. There is no obvious detrimental
impact on performance and the code is simpler, but...

Py-tree-sitter 0.23.2 has a bug in its processing of the #any-of predicate. For
Textual's Python SCM file and the Monokai theme, this produces rather unpleasant
results. Some re-working of the SCM file could work around this. Other SCM
files might also need changes.

Py-tree-sitter 0.24.0 has a fix for the bug, which appears to work, based on a
quick trial. (Py-tree-sitter does not have tests coverage of #any-of.) But
0.24.0 drops support for Python 3.9!

The best way forward does not seem obvious to me, but I am willing to do the
work based on what you think is the correct approach.

Copy link

We found the following entry in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This is an automated reply, generated by FAQtory

@willmcgugan
Copy link
Collaborator

My gut says we should drop syntax support for 3.9. Users that really need syntax support for 3.9 will be stuck at the current version of Textual.

@darrenburns thoughts?

@darrenburns
Copy link
Member

I think the syntax functionality is so dependent on tree-sitter that we should follow their lead and drop 3.9 if that's what they've decided.

@willmcgugan
Copy link
Collaborator

@paul-ollis I think that is conclusive. We are happy for syntax support to be for 3.9 onwards.

@paul-ollis
Copy link
Contributor Author

@willmcgugan Thanks for the update.

I am happy to make a separate PR or roll this into #5645, assuming that you wish to pursue #5645 (some discussion and work still required).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants