Closed lmburns closed 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:
Great to see someone tackle this! I've just been manually setting the filetype to tsx 😅
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
Thank you @Gelio for your insight. I'll look into the nvim treesitter approach.
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.
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
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
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 thattypescriptreact
is not the name of the treesitter parser, it is actuallytsx
.I check whether the filetype is
typescriptreact
and if it is, then pass the language argument astsx
.Note, this wasn't checked for
javascriptreact
andjsx
. I don't think the same problem exists since thejavascriptreact
parser comes bundled with thejavascript
parser.