Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Add typescriptreact support #9

Merged
merged 1 commit into from
Sep 2, 2022

Conversation

lmburns
Copy link
Contributor

@lmburns lmburns commented Sep 2, 2022

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 ziontee113 merged commit 97f2003 into ziontee113:master Sep 2, 2022
@ziontee113
Copy link
Owner

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

@hrmJ
Copy link

hrmJ commented Sep 3, 2022

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

@Gelio
Copy link

Gelio commented Sep 3, 2022

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 mfussenegger/nvim-treehopper#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 mfussenegger/nvim-treehopper#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
Copy link
Owner

ziontee113 commented Sep 3, 2022

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

@ziontee113
Copy link
Owner

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
Copy link

Gelio commented Sep 3, 2022

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
Copy link
Contributor Author

lmburns commented Sep 3, 2022

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants