Skip to content

Use treesitter to get code language #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 16, 2024
Merged

Use treesitter to get code language #187

merged 7 commits into from
Jul 16, 2024

Conversation

jalvesaq
Copy link
Member

This pull request replaces the b:IsInRCode functions with a single tree-sitter based function. It seems to work well with Rmarkdown or Quarto, but not with Rnoweb. We still need a specific is_in_R_code function for the rhelp file type because there is no tree-sitter grammar for it.

@jalvesaq
Copy link
Member Author

I forgot to delete a call to vim.notify() inside the r.utils.get_lang() function, but I think we might leave it there for now to more easily check if the returned language is correct.

@jalvesaq
Copy link
Member Author

I fixed get_lang() by not using tree-sitter for rnoweb and rhelp. I also removed the vim.notify().

- Bring back parend_diff() and has_op().
@jalvesaq jalvesaq marked this pull request as ready for review July 16, 2024 01:33
@jalvesaq
Copy link
Member Author

This pull request is ready for review now. It:

  • Avoids the complication of Lua functions called via buffer variables (b:IsInRCode) which was OK in VimScript but unusual in Lua. We still have two functions specific to rnoweb and rhelp in utils.lua. The rnoweb function can be deleted if either the rnoweb parser is improved or we find the correct way of getting the language at the cursor position. The rhelp function can be deleted if someone creates a parser for the language.
  • Fixes sending code from rhelp files to R. There is an additional function specific to rhelp with two auxiliary functions in send.lua. The three functions can be deleted if a parser for rhelp is created.

@jalvesaq
Copy link
Member Author

Note: the tests fail to run correctly (make test) and I don't know how to fix them.

jalvesaq added 2 commits July 15, 2024 23:05
Also: bring back parend_diff() and has_op() to fix sending code to Rhelp.
@PMassicotte
Copy link
Collaborator

Excellent, it simplifies the code a lot. Testing/reviewing it right now.

Copy link
Collaborator

@PMassicotte PMassicotte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@jalvesaq
Copy link
Member Author

The documentation of vim.treesitter.get_parser() says:

Returns the parser for a specific buffer and attaches it to the buffer

So, it seems that each time that vim.treesitter.get_parser() is called a new parser is used (although the same one). I will put some vim.uv.hrtime() in the code to see if it becomes faster if the parser is stored.

@PMassicotte
Copy link
Collaborator

So, it seems that each time that vim.treesitter.get_parser() is called a new parser is used (although the same one). I will put some vim.uv.hrtime() in the code to see if it becomes faster if the parser is stored.

Curious to see if it would make a significant difference.

@jalvesaq
Copy link
Member Author

No difference in a quick test.

@jalvesaq
Copy link
Member Author

This is faster than using treesitter (but more complicated): https://github.com/R-nvim/cmp-r/blob/main/lua/cmp_r/init.lua#L328-L396

We are talking about processes that take less than a millisecond, but it's good to know that it's faster to get a range of lines and call string.find() on each line than to make a single call to vim.fn.search().

@PMassicotte
Copy link
Collaborator

This is all good on my side. Ready to merge?

@jalvesaq jalvesaq merged commit 1d3f8e9 into main Jul 16, 2024
2 checks passed
@jalvesaq
Copy link
Member Author

Thank you!

@jalvesaq jalvesaq deleted the get_lang branch July 16, 2024 17:37
@PMassicotte
Copy link
Collaborator

I really like these changes, code is simpler.

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

Successfully merging this pull request may close these issues.

2 participants