danipen / TextMateSharp

A port of tm4e to bring TextMate grammars to dotnet ecosystem
MIT License
87 stars 15 forks source link

Textmate thread safe #24

Closed spf-oss closed 2 years ago

spf-oss commented 2 years ago

Calling "grammar.TokenizeLine(line,rulestack)" by two threads at the same time will cause thread safety problems. How do you solve them?

danipen commented 2 years ago

Each AvaloniaEdit instance uses one intance of the TMModel class to manage multithreading.

It launches only one thread per grammar.

spf-oss commented 2 years ago

If I want to achieve the effect of vscode, when the code is very long, first of all, one thread of vscode will refresh the code color from the code in the visual area (the color may be wrong at this time), and then another thread will refresh the color from changing the position. At this time, there will be a phenomenon that the syntax starts two threads.

In this way, you can achieve the rapid response of code color and ensure the correctness after refreshing.

What should I do to achieve this effect?

danipen commented 2 years ago

Well this behavior is already implemented in AvaloniaEdit and it's working fine. Take a look.

It parses the viewport in the UI thread and starts parsing the document in a bg thread. Every time the viewport is scrolled, the view port is re-parsed.