EvgeniyPeshkov / syntax-highlighter

Syntax Highlighter extension for Visual Studio Code (VSCode). Based on Tree-sitter.
https://marketplace.visualstudio.com/items?itemName=evgeniypeshkov.syntax-highlighter
MIT License
215 stars 43 forks source link

Make use of tree-sitter's incremental parsing #46

Open Menci opened 4 years ago

Menci commented 4 years ago

Tree-sitter supports incremental parsing, which can be accessed via tree.edit. But {Syntax Highlighter} just parse the code and generate a new tree every time. This will lost the efficiency advantage from tree-sitter.

Also, instad of rebuild every time, buildDecor should be done incrementally.

https://github.com/EvgeniyPeshkov/syntax-highlighter/blob/5cfdaf7f4c545d49417bb157b6ea73b16fa253b0/src/extension.ts#L200

Menci commented 4 years ago

This tree-sitter extension works well on updating tree-sitter's tree:

https://github.com/georgewfraser/vscode-tree-sitter/blob/26dbce4d3c8cf42f9b7894fbff14ca6f3d97b5b8/src/extension.ts#L106

EvgeniyPeshkov commented 4 years ago

Hello @Menci, Initially I utilized partial updates of syntax tree. But sometimes it broke highlighting after multi-line edits (formatting or copy-pasting), like for example in #19 and #20. So I had decided that it would be better to update entire syntax tree 7495135. I'll look into this, maybe issue is resolved and partial updates will not break highlighting. Thank you for reminding.

Menci commented 4 years ago

I'm currently working on porting your highlighter from vscode to monaco-editor, in order to have fancy highlights in the web. I built a demo here. I used tree.edit and I didn't find any issue with multi-line editing.

Maybe it's a bug in tree-sitter and already be fixed? Or maybe it's a vscode bug?

milahu commented 2 years ago

Initially I utilized partial updates of syntax tree. But sometimes it broke highlighting after multi-line edits (formatting or copy-pasting), like for example in #19 and #20.

should be reported to tree-sitter ... may be a duplicate of https://github.com/tree-sitter/tree-sitter/issues/1444

So I had decided that it would be better to update entire syntax tree 7495135.

as a "less brutal" solution, we could use incremental parsing for "small" updates (say +-5 characters per edit), and full parse on "large" updates (more than 5 characters were changed)

i guess it should be possible to estimate the size of a changeset, as produced by monaco-editor

edit:

use incremental parsing for "small" updates

seems the issue is not the size of the update, but syntax errors, which break the parse tree on further updates

→ when user is editing, do a full parse every 5 seconds

milahu commented 2 years ago

Initially I utilized partial updates of syntax tree. But sometimes it broke highlighting after multi-line edits

could be fixed in tree-sitter 0.20.1