Skip to content
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

TextArea improvements - lazy language import and allow installation of only required languages #5639

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

darrenburns
Copy link
Member

@darrenburns darrenburns commented Mar 12, 2025

Launching TextArea with the syntax extras has a large upfront cost the first time it's run in a new virtualenv.

A one-off compilation step is performed when the language is imported, and since all languages were being imported at the module level, it was adding a 1-2 second overhead at startup on the first run.

Subsequent launches within the same venv had little overhead (a few milliseconds).

image

Lazy importing of tree-sitter languages

This PR updates the TextArea to lazily import the languages, only when they're used, trimming 1-2 seconds off of these "cold starts".

Allowing users to install only what they need

With this PR, users don't need to install the syntax extras which includes a bunch of languages. They can instead just install tree-sitter plus the languages they require.

For example, with Posting I uninstalled the syntax extras then done this:

uv add tree-sitter tree-sitter-json tree-sitter-html tree-sitter-xml

...and it just worked without any code changes. Apps no longer need to pull in a bunch of transitive dependencies that they don't need.

Please review the following checklist.

  • Docstrings on all new or modified functions / classes
  • Updated documentation
  • Updated CHANGELOG.md (where appropriate)

@darrenburns darrenburns changed the title tree-sitter improvements - lazy loading and allow individual lang ins… TextArea improvements - lazy language import and allow installation of only required languages Mar 12, 2025
@darrenburns darrenburns marked this pull request as ready for review March 12, 2025 18:28
@TomJGooding
Copy link
Contributor

With this PR, users don't need to install the syntax extras which includes a bunch of languages. They can instead just install tree-sitter plus the languages they require.

You obviously have a better understanding of tree-sitter than I do, but could this not be quite brittle if the language package version isn't compatible with the highlight query?

@darrenburns
Copy link
Member Author

It's more brittle than the syntax extras yeah, but you can still supply your own highlight query that aligns with the parser when you register the language. If you're just doing "pip install tree-sitter-python" then relying on the built-in highlight query, then that'd be quite brittle. You should probably be installing a specific version, and registering the language and matching highlight query pair with TextArea.register_language.

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

Successfully merging this pull request may close these issues.

2 participants