-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
feat(query): extracts get_range_text from get_node_text #20158
base: master
Are you sure you want to change the base?
Conversation
I think we want to park this until after 0.8, when we get a cleaner picture of which utilities are generally useful and which only in the context of nvim-treesitter. This should also be a part of a more general refactor on how ranges are handled; ideally, we can reduce the surface API of these utility functions, not increase them (e.g., by marking some of them as private, or combining them). In any case, this behavior should be controlled by an option as you may want the original node range. |
runtime/lua/vim/treesitter/query.lua
Outdated
return M.get_range_text({start_row, start_col, start_byte, end_row, end_col, end_byte}, source, opts) | ||
end | ||
|
||
--- Gets the text corresponding to a given rangen |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- should there be some (extremely brief) hint about the fact that a "range" maps to the input text (which is usually a buffer)? Or is that understood? Maybe we need a tag like
treesitter-range
to link to? - typo : "rangen"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it would be good to also specify why this is different than nvim_buf_get_text
, at least if source is a number? I wonder if it's more clear if instead there is a get_vim_range
as in nvim-treesitter and then we use that with nvim_buf_get_text
directly?
Fixed the typo at least.
Thanks for your feedback! After sleeping on this, I'm wondering if the better approach (at least for the usecase I had) would be to support a third optional argument to eg Then one could do for example: for id, node, metadata in query:iter_captures(tree:root(), bufnr, first, last) do
local text = get_node_text(node, bufnr, metadata) -- text here then takes #offset! into account
end How does that sound? Then the user does not need to explicitly touch the ranges anyway. |
We usually treat optional arguments as an But, yes, adding options is preferable to adding another function. |
Why make it only the { "offset!", 1, "0", "1", "0", "-1" } whereas metadata would include {range = { 14, 7, 14, 14 }} |
I meant "design pattern". |
Does it makes sense if I make an update to this MR with a suggestion for an optional |
This would be necessary in any case, but I don't think we want to merge this before 0.8 (end of month) either way. (Full transparency: which doesn't guarantee that we'll merge it afterwards, although I'm not opposed to this PR on principle.) |
Based on what we discussed in nvim-treesitter/nvim-treesitter#3487 I no longer think it makes sense to add |
f25e7e2
to
31f441b
Compare
That sounds like a potentially more powerful/generalized approach if it will avoid lots of extra functions. |
Now that Nvim 0.8 is released and the dust has settled somewhat, we should circle back to this (in particular because the next step is to update nvim-treesitter to use the newly upstreamed core APIs, and I want to do this only once, so any significant refactoring should happen before). But before we make any changes, we need to take a step back and discuss what interface we want here. The various Basically, what we want is to be able to extract the text corresponding to some tree-sitter object. For this, we need to find the buffer positions corresponding to the object. In particular, this requires translating from "tree-sitter coordinates" to "buffer coordinates". The last is related to similar functions for LSP and Lua in general (especially involving multibyte characters), so we should treat this as part of a general-purpose utility ("find buffer range from coordinates", extending Regarding objects, we want to handle (at least) these individually:
The goal would be to compose the current features via a minimal set of functions (and possibly fill out the "feature matrix"). For commonly used functions (like In general, refactoring everything to return/accept a @neovim/treesitter |
(patterns/matches are related to predicates and directives, so that's more of question towards generalizing those @lewis6991 ) |
@AckslD do you plan to continue this? |
@justinmk, I'd be happy to continue this but I'm not fully sure if there are more recent views on what this api should look like. Happy to discuss this though. However, I won't really have any time the coming month or so. |
Currently we have
get_node_text
which gets the text corresponding to a node using the values fromnode:start()
andnode:end_()
. When#offset!
is used in a query (for example for injections or text objects) the range of the match will not equal the range of the node.In nvim-treesitter/nvim-treesitter#3486 for example the metadata is now included in the matches of the query. So with this change here one could then simply do
get_range_text(match.metadata.range)
to get the text using the same logic asget_node_text
.Tagging @clason since I briefly asked about this on matrix.