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

Explore using tree sitter for syntax highlighting #210475

Open
alexr00 opened this issue Apr 16, 2024 · 8 comments
Open

Explore using tree sitter for syntax highlighting #210475

alexr00 opened this issue Apr 16, 2024 · 8 comments
Assignees
Labels
exploration grammar Syntax highlighting grammar

Comments

@alexr00
Copy link
Member

alexr00 commented Apr 16, 2024

Goals of the explorations:

@alexr00
Copy link
Member Author

alexr00 commented May 3, 2024

Some notes on incremental parsing:

  • Per Can web-tree-sitter actually do async parsing? tree-sitter/tree-sitter#2118, it looks like we can set a timeout so that each call to parse times-out, with the parsing progress preserved for the next call to parse.
  • I've confirmed that this is the case; however, it appears to be broken in the latest version of web-tree-sitter. Going back to 0.22.2 works (latest is 0.22.5).
  • There does not appear to be any significant time penalty for setting a timeout.

@hediet FYI.

@alexr00
Copy link
Member Author

alexr00 commented May 6, 2024

Opened tree-sitter/tree-sitter#3341 for the above issue.

@alexr00
Copy link
Member Author

alexr00 commented May 28, 2024

After some discussion, we've decided to first understand the performance of using tree-sitter to parse files. #213565 contains that work.

@alexr00
Copy link
Member Author

alexr00 commented May 28, 2024

We also need new internal editor API to write a more optimal parse function, which @rebornix may help with.

@alexr00
Copy link
Member Author

alexr00 commented Jul 22, 2024

Progress in July:

alexr00 added a commit that referenced this issue Aug 19, 2024
* Simple tree sitter based syntax highlighting for typescript

Part of #210475

* Implement onceIf using filter

* Mix monaco

* Fix tests

* Switch back to classic

* PR feedback

* Fix bug causing test failure

* Update tree-sitter-wasm

* Fix monaco editor tree shaking

* Fix running monaco checks on Windows

* Use dummy tree sitter service in standalone

* PR feedback
alexr00 added a commit that referenced this issue Aug 20, 2024
alexr00 added a commit that referenced this issue Aug 20, 2024
* Include highlights files in build
Part of #210475

* Also add for non-esm
alexr00 added a commit that referenced this issue Aug 21, 2024
alexr00 added a commit that referenced this issue Aug 21, 2024
alexr00 added a commit that referenced this issue Aug 21, 2024
alexr00 added a commit that referenced this issue Aug 21, 2024
@alexr00 alexr00 modified the milestones: August 2024, September 2024 Aug 26, 2024
@alexr00
Copy link
Member Author

alexr00 commented Aug 26, 2024

Syntax highlighting for typescript using Tree-Sitter can be enabled with:

  "editor.experimental.preferTreeSitter": [
    "typescript"
  ],

There are still bugs, and many themes will not work right.

Next steps:

  • Enable colorization tests just like we have for textmate
  • Test top themes
  • Give the tokenization feature a way to signal that tokens have changed
  • Enable nested grammars (ex. regex highlighting in typescript)

@RedCMD
Copy link
Contributor

RedCMD commented Aug 30, 2024

I don't believe this is correct

const newEndPosition = model.getPositionAt(newEndOffset);

model.getPositionAt() is relative to the final document state
when it should be relative to the document during each separate change state

I was having problems in my implementation when having many edits across many lines all at once
I ended up needing to make my own wack .getPositionAt() code
RedCMD/TmLanguage-Syntax-Highlighter@8681d5e#diff-9914030256b6efbf5184ce635cb026f18f21fc51ed548740a07e4972b34ee1a8R380-R389
haven't had an error since

@savetheclocktower
Copy link

As a fellow Tree-sitter implementer, I'm duty-bound to point out the Tree-sitter implementation I did for Pulsar (a fork of Atom). Maybe it'll be useful as a second opinion when it's not clear how Zed is doing things.

I had Atom's original Tree-sitter implementation from 2017ish to refer to when writing mine, so it's only fair.

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

No branches or pull requests

3 participants