Skip to content

Custom syntax highlighting using tree-sitter #4674

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
ajeetdsouza opened this issue Apr 11, 2022 · 8 comments
Open

Custom syntax highlighting using tree-sitter #4674

ajeetdsouza opened this issue Apr 11, 2022 · 8 comments

Comments

@ajeetdsouza
Copy link

Hi, I'm trying to use Ace as an online editor for a new language. I already have a grammar written in tree-sitter, I'm wondering if I can somehow reuse that for highlighting rather than writing yet another grammar for Ace.

Is it possible to use tree-sitter with Ace directly, or bypass the builtin highlighter and manually define my own one?

@nightwing
Copy link
Member

For syntax highlighting Ace needs an object with getLineTokens method https://github.com/ajaxorg/ace/blob/v1.4.14/lib/ace/background_tokenizer.js#L234 which returns an object like {state : "string", tokens: [{value: "text", type: "classname"}, ...]}

The state is needed for editor to know if highlighting have changed after that line and editor needs to rerender more lines or not.

It appears that it should be possible to convert output of tree sitter into a format usable by ace, but if tree-sitter provides only async api, it is also possible to replace bg-tokenizer entirely and rely on tree sitter itself ofr caching and incremental highlighting.

Atom code can be a useful starting point in implementing this https://github.com/atom/atom/blob/9826355da0a52509a84448d86c2c9a935e447aab/src/tree-sitter-language-mode.js

@ajeetdsouza
Copy link
Author

Thanks @nightwing! I don't think tree-sitter supports parsing line-by-line, could you elaborate more on how I could replace the BackgroundTokenizer entirely?

@github-actions
Copy link

This issue has not received any attention in 1 year. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@ajeetdsouza
Copy link
Author

I'd still love to see tree-sitter support (or custom syntax highlighting support) in Ace.

Copy link

This issue has not received any attention in 1 year. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@ajeetdsouza
Copy link
Author

@nightwing @andrewnester can this be reopened? It is still relevant

@InspiredGuy
Copy link
Contributor

Reopening per request from the community above

@InspiredGuy InspiredGuy reopened this May 13, 2024
@Dixtosa
Copy link

Dixtosa commented Jun 10, 2024

I would like to use this library to make mustache documents editable. I guess I would also benefit from this library supporting highlighting any grammar-provided languages?

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

No branches or pull requests

5 participants