georgewfraser / vscode-tree-sitter

Accurate syntax coloring for VSCode using tree-sitter
MIT License
175 stars 25 forks source link

[Question] How did you make the extension that depends on tree-sitter work cross-platform? #40

Closed Symbolk closed 3 years ago

Symbolk commented 3 years ago

Hi, I am very excited to find this project! I am also developing a VSCode extension based on LSP and tree-sitter, which is used to help developers to resolve merge conflicts: https://github.com/Symbolk/somanyconflicts/

However, now I am in front of a severe issue: I am developing on mac, and I built and published it in the VSCode market, but according to my users, this version cannot be run on windows, for the error: "tree-sitter-runtime is not a valid Win32 Application". Therefore, I tried to rebuild the module and extension on windows, now it runs but the query will report error that do not exist on mac: 'Query error of type TSQueryErrorSyntax at position 1'

I saw yours depends on web-tree-sitter and somehow uses the WASM, but I failed to figure whether and how can this solve the cross-platform issue, can you help me? Ideally I do not want to have two versions for different OSs.

sogaiu commented 3 years ago

I learned a bit more detail about this here.

IIUC, the reason the cross-platform issue is addressed is because the WASM bits are platform-independent.

Symbolk commented 3 years ago

I learned a bit more detail about this here.

IIUC, the reason the cross-platform issue is addressed is because the WASM bits are platform-independent.

Thanks! I will check it out.

sogaiu commented 3 years ago

@Symbolk jeff-hykin mentioned here about another VSCode extension that uses tree-sitter and WASM.

I think it's this one: https://github.com/EvgeniyPeshkov/syntax-highlighter

If you haven't had a look, may be it would be of interest to you -- it seems like it might be better maintained at this point in time.

jeff-hykin commented 3 years ago

@Symbolk here's the commit for when this extension switched to using wasm, which made it work cross platform with no extra effort.

Commit: https://github.com/georgewfraser/vscode-tree-sitter/commit/7ef1ce95f6b8c53b20564fa91e8737a522f856a8 createParser is the function you'll probably want to talk a look at

Symbolk commented 3 years ago

@jeff-hykin @sogaiu Hi, I managed to switch to web-tree-sitter in this way:

https://github.com/Symbolk/somanyconflicts/commit/47a94dede81fc29066ead23348b3aca7814c80f8 https://github.com/Symbolk/somanyconflicts/commit/3412c8e683e9d26cecd12230db3ab31e3e4aa3f7 https://github.com/Symbolk/somanyconflicts/commit/3e19b56c77d67e1dcd2059ae221590e6030c66b5

I am not sure whether I am doing it correctly, though the debugging and running under macOS&Win10 seem basically good!

However, there are some remaining issues that I need your help:

  1. In my old version which depends on the node binding, I need to init a Query object with 2 arguments (https://github.com/Symbolk/somanyconflicts/blob/0834d4e9c903b4d3512d34c8f1cfde5e81b219aa/src/SoManyConflicts.ts#L91). However, the web-tree-sitter the TreeSitter.Query() expects 0 arguments, so I have to temporarily remove arguments to build:

    -      const treeQuery = new TreeSitter.Query(specification, queryString)
    +      const treeQuery = new TreeSitter.Query()

    But the query is necessary for my extension, is there anyway to use Query with query string?

  2. I am not sure whether the parser is loaded correctly, but the TreeSitter.parse() seems to return nothing, I tried to print the parsed tree, but no results printed in any cases:

https://github.com/Symbolk/somanyconflicts/blob/3e19b56c77d67e1dcd2059ae221590e6030c66b5/src/SoManyConflicts.ts#L100

Any hint is a great help!

sogaiu commented 3 years ago

@Symbolk It's been quite a while since I've looked at these details so I'm not sure how much help I can be.

There are a few things that occur to me though.

For 1., IIUC, the web-tree-sitter tests seem to indicate that querying can be handed a string: https://github.com/tree-sitter/tree-sitter/blob/master/lib/binding_web/test/query-test.js#L57-L60 -- may be studying how that's done in that file could be helpful?

For 2., IIUC, the elm folks use web-tree-sitter in their language server which is written in TypeScript: https://github.com/elm-tooling/elm-language-server/blob/main/package.json#L31 -- may be you can get some hints from looking in their source?

One more thing is that the tree-sitter repository has a discussions area: https://github.com/tree-sitter/tree-sitter/discussions -- it might be that there are folks who can help you better there.

Good luck!

Sorry I don't have good answers for you.

Symbolk commented 3 years ago

@sogaiu No, your answer is amazing! I think it solves the both problems!

Symbolk commented 3 years ago

Thanks for the nice answers from you, loving Github!