-
Notifications
You must be signed in to change notification settings - Fork 33
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
Highlight ROxygen tags #68
Comments
+1 for this! |
Another +1! |
I'm not that familiar with treesitter syntax or how it operates, but I wonder if prior art from other languages that have doc comments would help (e.g. rust seems to have the hang of it) |
If we were to support this, I do think we'd use the Rust approach linked above The key points are:
I think that is as far as tree-sitter-r would go. We currently don't have any rules in tree-sitter-r that rely on community conventions and external packages, so I am somewhat hesitant to even do this! I'm not entirely sure, but I think according to tree-sitter/tree-sitter-rust#212 one thing the neovim community could do is create a Alternatively you could probably use an If someone who has some neovim developer experience wants to chime in on this plan, that would be helpful! It would be particularly useful to hear if you can already work around this using a match based query to find Relevant links: |
Hey 👋
That's a fair point. I have roxygen2 so ingrained into my brain that I wouldn't even bat an eye! With that in mind, I don't think that adding a special treatment for
I think this is the way to go. Once tree-sitter-r-roxygen2 grammar exists, all that is left to do is creating
References:
I am interested in giving it a go and creating the grammar for roxygen2. I won't give any ETA, but I will report back if it takes off! However, my experiments didn't go in vain. If you don't want to wait for tree-sitter-r-roxygen2 to come around, then I have a Lua-based solution that at a glance replicates Lua-based Solution for NeovimThis approach relies on an autocommand. The upside? It works. The downside? Manual setup1. This approach could be expanded to handle things like DemoCleanShot.2024-09-17.at.11.02.36.mp4CodeImportant Place this code in local get_root = function(bufnr, lang)
local parser = vim.treesitter.get_parser(bufnr, lang, {})
local tree = parser:parse()[1]
return tree:root()
end
local highlight_roxygen2_tags = function(bufnr)
local query = [[
((comment) @comment.roxygen2
(#lua-match? @comment.roxygen2 "^#' (@%a+).*$")
(#gsub! @comment.roxygen2 "^#' (@%a+).*$" "%1"))
]]
local root = get_root(bufnr, "r")
local ts_query = vim.treesitter.query.parse("r", query)
local ns = vim.api.nvim_create_namespace("r.comments.roxygen2")
for id, node, metadata in ts_query:iter_captures(root, bufnr) do
if ts_query.captures[id] == "comment.roxygen2" then
local start_row, _, end_row, _ = vim.treesitter.get_node_range(node)
local start_col = 3 -- skip leading "#' "
local end_col = start_col + #metadata[id].text -- add tag length
vim.highlight.range(bufnr, ns, "@operator", { start_row, start_col }, { end_row, end_col })
end
end
end
vim.api.nvim_create_autocmd({ "BufWinEnter", "TextChanged", "TextChangedI" }, {
desc = "Highlight roxygen2 tags",
buffer = 0,
callback = function(args)
highlight_roxygen2_tags(args.buf)
end,
}) Footnotes
|
For
it feels wrong to me to assert that every comment now has the language of r_roxygen2. That's what that says, right? That was why I was suggesting that you'd only do that on |
Right. It could be changed to:
I suppose this comes down to roxygen2 grammar design. For the above to work, the grammar would have to handle I just did a quick scan through other "doc" grammars. luadoc grammar doesn't include the leading comment string. On the other hand, jsdoc and phpdoc both include it. Personally, I don't have a strong opinion which approach should a roxygen2 grammar take. Nor I see any immediate benefits of using one approach over the other. |
Personally I think if we can make something like
work then that would be greatly preferable to keep tree-sitter-r agnostic to any R packages. What you have there is pretty close to what I thought was possible with the existing setup. And its nice to see that there is some prior art like jsdoc that pretty much does it exactly this way. I see that tree-sitter-javascript even has an injection query for jsdoc (i don't think jsdoc has any marker character like Note that the exact rule for "this is a roxygen2 comment" is more flexible than just
|
In theory the roxygen2 grammar could also give us the ability to mark contents inside an
i.e. #' @param x a param
#'
#' @examples # the lines after this one are R code if you strip the leading `#'`
#' 1 + 1
#' fn(
#' a,
#' b
#' ) Which could allow highlighting of R code in Also worth looking at
|
The comment block has to start with It also looks like as soon as it doesn't match
Today I learned! Thanks for pointing that out. I think
That's what I also thought :-)
Yes! I would definitely take a look on how to efficiently pass things around. My intuition is to try passing continuous block of comments to roxygen2 parser and treating them as one document (similarly to what jsdoc does). I suppose the next step would be sketching an outline of how the roxygen2 grammar would be structured... :-) |
I have been experimenting with how to approach tree-sitter-roxygen2 and ran into an issue. R uses line comments, and tree-sitter cannot combine multiple nodes into a single capture. This means it's not possible to properly inject the roxygen2 parser into the R parser. For example, given the following comment:
The result would be:
While this is not a showstopper, it means that tree-sitter-roxygen2 won't be as robust as we imagined, because it will work at the line level. Unless it is possible to make the R grammar join |
I thought this was what Does that not combine the sequential comments into 1 "document" that tree-sitter-roxygen2 gets? |
...
I think this would be great, as I assume then there would also be a cleaner solution for R-nvim/R.nvim#106 dealing with the fallout of trying to work around such tags not being recognized by |
Are ROxygen tag highlighting something that can be added? With treesitter disabled they get highlighted from the main neovim runtime:
https://github.com/neovim/neovim/blob/c651a0f643e7bd34eb740069a7b5b8c9f8759ecc/runtime/syntax/r.vim#L96-L159
Thanks for your work maintaining this project!
The text was updated successfully, but these errors were encountered: