Skip to content

Conversation

clason
Copy link
Member

@clason clason commented Dec 10, 2022

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:

  1. allow get_{node,captures}_at_pos() to be called with no arguments to return the full(!) node/captures at the cursor
  2. add show_captures_at_cursor() to print(!) the correctly highlighted captures at the cursor
  3. remove get_{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)

@github-actions github-actions bot requested review from bfredl and vigoux December 10, 2022 16:16
@clason clason changed the title feat(treesitter)!: replace get_captures_at_cursor with show feat(treesitter)!: consolidate get_{node,captures} functions Dec 11, 2022
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.
end

return captures
a.nvim_echo(msg, false, {})
Copy link
Member Author

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

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?

Copy link
Member Author

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.)

Copy link
Member

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.

Copy link
Member Author

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?

Copy link
Member

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.

Copy link
Member Author

@clason clason Dec 12, 2022

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.)

@clason
Copy link
Member Author

clason commented Dec 12, 2022

@gpanders or do we want to go full breaking change here and also mess with the signature of get_{captures,node}_at_pos?

@justinmk
Copy link
Member

Thanks for the cleanup! Similar to numerous other PRs of late, I hope we can avoid the _at_cursor variants here in favor of functions that take a position.

  • Even if there's not an immediate use case, the question is "if we can be more flexible without much cost, why not?"
  • _at_cursor functions risk having to add mostly-redundant _at_pos variants in the future.

@clason clason closed this Dec 13, 2022
@github-actions github-actions bot removed request for bfredl and vigoux December 13, 2022 05:07
@clason clason deleted the ts-show-captures branch December 23, 2022 11:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants