ziontee113 / syntax-tree-surfer

A plugin for Neovim that helps you surf through your document and move elements around using the nvim-treesitter API.
MIT License
469 stars 9 forks source link

Add typescriptreact support #9

Closed lmburns closed 2 years ago

lmburns commented 2 years ago

When in a typescriptreact file, an error occurs when trying to use this plugin.

This is because vim.treesitter.get_parser takes a language as a second argument. However, there is no language argument that is given in this plugins source code. When no language is given, the filetype is taken to be the language. The problem here is that typescriptreact is not the name of the treesitter parser, it is actually tsx.

I check whether the filetype is typescriptreact and if it is, then pass the language argument as tsx.

Note, this wasn't checked for javascriptreact and jsx. I don't think the same problem exists since the javascriptreact parser comes bundled with the javascript parser.

ziontee113 commented 2 years ago

Thank you very much @lmburns for the PR :+1: I haven't got the change to test out typescriptreact yet so I haven't ran into this issue. Thank you for fixing it :smile:

hrmJ commented 2 years ago

Great to see someone tackle this! I've just been manually setting the filetype to tsx 😅

Gelio commented 2 years ago

While I appreciate the fix, it is just a band aid for a single filetype. There are more filetypes for which the treesitter parser name is different than the filetype.

I was tackling the same problem for nvim-treehopper in https://github.com/mfussenegger/nvim-treehopper/pull/14

The maintainer of that plugin didn't end up merging my PR as he did not want the plugin to rely on https://github.com/nvim-treesitter/nvim-treesitter. I see that https://github.com/ziontee113/syntax-tree-surfer already requires nvim-treesitter, so it should not be a problem. We can use the same approach that I used in https://github.com/mfussenegger/nvim-treehopper/pull/14 to get the parser:

Instead of using the native treesitter method to get the TS parser, we can use require('nvim-treesitter.parsers').get_parser(0). This should work for more than just typescriptreact filetype

ziontee113 commented 2 years ago

Thank you @Gelio for your insight. I'll look into the nvim treesitter approach.

ziontee113 commented 2 years ago

Although it may sound ugly, having a table that hold the file type / parsers name doesn't sound awful to me.

And I really get tree sitter hopper plug-in author decision. Changes in nvim tree sitter broke this plug-in more than one time. I had to find an old nvim tree sitter commit and copy the old code.

Gelio commented 2 years ago

I see. I would probably rely on nvim-treesitter, but I am not a plugin author and I haven't been burned by breaking changes in that plugin. I understand your point of view

lmburns commented 2 years ago

Yeah, whenever I first made this PR and made that comment, I was going to suggest adding a table of file type-parser mappings. I will work on this here soon and create another pull request. There are a couple other plugins using treesitter than could maybe be looked at as a guide