cursorless-dev / vscode-parse-tree

Syntax trees for VSCode using tree-sitter
MIT License
40 stars 35 forks source link

Race condition on new language calling getNodeAtLocation #4

Closed AndreasArvidsson closed 2 years ago

AndreasArvidsson commented 2 years ago

The tree map is dependent on the events onDidChangeVisibleTextEditors and onDidChangeTextDocument to add new languages when needed.

Quickly changing language on a file or switching to a file with a different language and immediately calling getNodeAtLocation throws an exception "Can't read property rootNode of undefined".

One solution would be that in getNodeAtLocation do the following:

Similar thoughts might be applied to the function getTree

pokey commented 2 years ago

This approach seems reasonable, the only slight annoyance being that getNodeAtLocation would have to become async, because it might need to load the wasm, which is async. This async-ness then becomes contagious, so eg in cursorless, transformSelection becomes async as well, and then everything up the chain from there 😕. Not sure I see a better alternative tho. Any ideas?

AndreasArvidsson commented 2 years ago

I have no better solution. If you really want to keep it synchronous I guess you could always do a busy wait via a loop.

pokey commented 2 years ago

In #5, I opted to just raise a special exception (LanguageStillLoadingError) for this case. Client code can catch the exception and try again. If desired, I could add a separate version of the function that is async, but I think I'd prefer to keep the default implementation sync and just throw an exception