From 3dfb9e6f60d9ca27ff140a9300cc1a43e38aa2ee Mon Sep 17 00:00:00 2001 From: Riley Bruins Date: Wed, 11 Dec 2024 04:34:24 -0800 Subject: feat(treesitter): include capture id in return value of `get_captures_at_pos()` #30559 **Problem:** Currently, it is difficult to get node(s)-level metadata for a capture returned by `get_captures_at_pos()`. This is because it is stored in `metadata[id]` and we do not have access to the value of `id`, so to get this value we have to iterate over the keys of `metadata`. See [this commit](https://github.com/neovim/neovim/commit/d63622930001b39b12f14112fc3abb55b760c447#diff-8bd4742121c2f359d0345f3c6c253a58220f1a28670cc4e1c957992232059a6cR16). Things would be much simpler if we were given the `id` of the capture so we could use it to just index `metadata` directly. **Solution:** Include `id` in the data returned by `get_captures_at_pos()` --- runtime/doc/news.txt | 1 + runtime/doc/treesitter.txt | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) (limited to 'runtime/doc') diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt index 71ec84c2f2..07b1b8646a 100644 --- a/runtime/doc/news.txt +++ b/runtime/doc/news.txt @@ -289,6 +289,7 @@ TREESITTER false, which allows it to return anonymous nodes as well as named nodes. • |treesitter-directive-trim!| can trim all whitespace (not just empty lines) from both sides of a node. +• |vim.treesitter.get_captures_at_pos()| now returns the `id` of each capture TUI diff --git a/runtime/doc/treesitter.txt b/runtime/doc/treesitter.txt index 877d90a3b7..a0860c60a6 100644 --- a/runtime/doc/treesitter.txt +++ b/runtime/doc/treesitter.txt @@ -902,8 +902,8 @@ get_captures_at_pos({bufnr}, {row}, {col}) Returns a list of highlight captures at the given position Each capture is represented by a table containing the capture name as a - string as well as a table of metadata (`priority`, `conceal`, ...; empty - if none are defined). + string, the capture's language, a table of metadata (`priority`, + `conceal`, ...; empty if none are defined), and the id of the capture. Parameters: ~ • {bufnr} (`integer`) Buffer number (0 for current buffer) @@ -911,7 +911,7 @@ get_captures_at_pos({bufnr}, {row}, {col}) • {col} (`integer`) Position column Return: ~ - (`{capture: string, lang: string, metadata: vim.treesitter.query.TSMetadata}[]`) + (`{capture: string, lang: string, metadata: vim.treesitter.query.TSMetadata, id: integer}[]`) get_node({opts}) *vim.treesitter.get_node()* Returns the smallest named node at the given position -- cgit