-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
feat(treesitter)!: consolidate get_{node,captures}
functions
#21374
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
Conversation
get_{node,captures}
functions
b474e04
to
6421631
Compare
Instead of returning a table, print the highlighted captures directly.
Remove `vim.treesitter.get_node_at_cursor`, since its use case is now better covered by `vim.treesitter.show_tree()`. To make up for it, allow `get_node_at_pos` and `get_captures_at_pos` to be called with no arguments to return the full(!) node and captures, respectively, at the cursor.
6421631
to
364dd27
Compare
end | ||
|
||
return captures | ||
a.nvim_echo(msg, false, {}) |
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.
I set {history}
to false
since people may want to use this in a CursorHold
autocommand without spamming into their :messages
list.
--- | ||
---@return string[] List of capture names | ||
function M.get_captures_at_cursor(winnr) | ||
function M.show_captures_at_cursor(winnr) |
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.
Would it make sense to make this show_captures_at_pos
that defaults to the cursor position when arguments are omitted, just like the other ones? Seems like that could be more generally useful.
Most likely getting captures at the cursor position will be the most common use case, but if it's easy to support showing captures at an arbitrary position then why not?
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.
What would be the use case? (Adding arbitrary position would complicate/duplicate the code somewhat, so I'm a bit hesitant.)
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.
Would it? I think you would just add a row
and col
parameter like you have for get_captures_at_pos
and forward those directly to get_captures_at_pos
on line 267.
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.
Ah, you're right, I was overcomplicating this.
But still, it would be helpful for me to understand when you would like to "pretty print" to the message area the capture at an arbitrary position?
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.
I don't have a use case on top of mind. But if it's easy to make the interface more flexible while also keeping the common case simple that seems like a win to me.
The symmetry with get_captures_at_pos
is nice too.
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.
Conversely, do you have a use case for getting the captures/node at the current cursor position? Would that simplify vim.treesitter.show_tree()
?
(I'm thinking here about the winnr
in particular, which we pass for show
but not get
; messing with the signature of the later would be (too) breaking.)
@gpanders or do we want to go full breaking change here and also mess with the signature of |
Thanks for the cleanup! Similar to numerous other PRs of late, I hope we can avoid the
|
Problem: The
vim.treesitter
API has too many similar functions around returning/showing nodes and captures.Solution: Consolidate the API by moving functionality into existing functions or better differentiation:
get_{node,captures}_at_pos()
to be called with no arguments to return the full(!) node/captures at the cursorshow_captures_at_cursor()
to print(!) the correctly highlighted captures at the cursorget_{node,captures}_at_cursor()
As the treesitter API is still marked as experimental and the two removed functions were meant to be used for interactive debugging, do we need to go through a full deprecation cycle here?
@gpanders (who asked about 1.) @justinmk (who likes a tidy API)