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

Add Injected Language Support #150

Merged
merged 13 commits into from
Mar 24, 2023

Conversation

thecoolwinter
Copy link
Collaborator

@thecoolwinter thecoolwinter commented Feb 28, 2023

Description

This PR adds support for injected languages using tree-sitter as described in #16 and in the tree-sitter documentation. Languages can contain injected languages which are described in a language's injections.scm file. Some examples of injected languages are:

  • HTML contains CSS and Javascript in style and script tags
  • Javascript contains Regex literals
  • PHP contains HTML between the <php tags
  • C++ can contain rawstring literals of arbitrary languages

Details

This PR is a rework of the TreeSitterClient class. Specifically it:

  • Adds a layers array and primaryLayer property. layers contains all language layers in the document, and primaryLayer is the ID of the document's primary language.
  • Each layer is a LanguageLayer object. These objects represent an injected language-range(s) combination. Each layer can have one or more range associated with it depending on if it should be parsed as one document or multiple.
  • When editing:
    • Each layer's ranges are updated using a similar algorithm to tree-sitter, and edits are applied to make use of the incremental parsing tree-sitter gives.
    • Each layer is checked for any injections, inserting any new injected layers and keeping track of any 'untouched' layers to remove.
    • Any layers that were not touched are removed.

The highlight query algorithm is largely the same, but keeps track of any ranges not used by any injected layers, and only queries the primary layer for those ranges so as not to override any injected highlights.

Related Issues

Screenshots

Before, other languages were detected but parsed and highlighted as normal text.
Screenshot 2023-02-28 at 3 08 52 PM

With Injected languages, in this case CSS and JS embedded in HTML and a second layer of Regex embedded in JS embedded in HTML:
Screenshot 2023-03-24 at 2 45 15 PM

@lukepistrol
Copy link
Member

Is this still WIP?

@thecoolwinter
Copy link
Collaborator Author

Is this still WIP?

Yeah it is. There are still some glaring issues right now that I thought would be trivial to fix but are ending up being trickier than expected.

@thecoolwinter thecoolwinter marked this pull request as ready for review March 24, 2023 20:08
Copy link
Member

@lukepistrol lukepistrol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple of code style considerations about function parameter indentation. I provided fixes for most of them.

Refs: Code Style

Copy link
Member

@lukepistrol lukepistrol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@lukepistrol lukepistrol requested a review from 0xWDG March 24, 2023 21:26
Copy link
Collaborator

@austincondiff austincondiff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really great work @thecoolwinter! 👏

@lukepistrol
Copy link
Member

@thecoolwinter Are there any breaking changes regarding the public API? Need to know when releasing a new version.

@thecoolwinter
Copy link
Collaborator Author

@thecoolwinter Are there any breaking changes regarding the public API? Need to know when releasing a new version.

Yes, there's an additional method on HighlightProvider which external highlight providers will have to implement when updating.

@lukepistrol lukepistrol merged commit b11be4d into CodeEditApp:main Mar 24, 2023
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.

✨ Injected Languages
4 participants