ajaxorg / ace

Ace (Ajax.org Cloud9 Editor)
https://ace.c9.io
Other
26.54k stars 5.27k forks source link

Custom syntax highlighting using tree-sitter #4674

Open ajeetdsouza opened 2 years ago

ajeetdsouza commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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[bot] commented 1 year ago

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 commented 1 year ago

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

github-actions[bot] commented 1 month ago

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 commented 1 month ago

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

InspiredGuy commented 1 month ago

Reopening per request from the community above

Dixtosa commented 3 weeks ago

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?