Skip to content

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

Merged
willmcgugan merged 5 commits into
mainfrom
tree-sitter-improvements
Mar 26, 2025
Merged

TextArea improvements - lazy language import and allow installation of only required languages#5639
willmcgugan merged 5 commits into
mainfrom
tree-sitter-improvements

Conversation

@darrenburns

@darrenburns darrenburns commented Mar 12, 2025

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
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.

@willmcgugan

Copy link
Copy Markdown
Member

Thanks

@willmcgugan willmcgugan merged commit 64cba2f into main Mar 26, 2025
@willmcgugan willmcgugan deleted the tree-sitter-improvements branch March 26, 2025 14:40
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.

3 participants