diff options
author | “jvgrootveld” <“justin.vangrootveld@gmail.com”> | 2021-01-15 21:44:40 +0100 |
---|---|---|
committer | “jvgrootveld” <“justin.vangrootveld@gmail.com”> | 2021-01-15 21:44:40 +0100 |
commit | a2818819bb5afc3283e935727d5a1b0e9d1cf6b3 (patch) | |
tree | 2427250f0453c01faa3d69a9c1a353cedbb01971 /runtime | |
parent | 0fad952a2b7d90d2c3215e8e3c50ae62233e2158 (diff) | |
download | rneovim-a2818819bb5afc3283e935727d5a1b0e9d1cf6b3.tar.gz rneovim-a2818819bb5afc3283e935727d5a1b0e9d1cf6b3.tar.bz2 rneovim-a2818819bb5afc3283e935727d5a1b0e9d1cf6b3.zip |
treesitter: default start and end row when omitted
Add support for default start and end row when omitted in the
query:iter_captures and query:iter_matches functions.
When the start and end row values are omitted, the values of the given
node is used. The end row value is incremented by 1 to include the node end
row in the match.
Updated tests and docs accordingly.
Diffstat (limited to 'runtime')
-rw-r--r-- | runtime/doc/treesitter.txt | 3 | ||||
-rw-r--r-- | runtime/lua/vim/treesitter/query.lua | 12 |
2 files changed, 14 insertions, 1 deletions
diff --git a/runtime/doc/treesitter.txt b/runtime/doc/treesitter.txt index 911e7b8b47..1696d3b9ba 100644 --- a/runtime/doc/treesitter.txt +++ b/runtime/doc/treesitter.txt @@ -195,7 +195,8 @@ query:iter_captures({node}, {bufnr}, {start_row}, {end_row}) text of the buffer. {start_row} and {end_row} can be used to limit matches inside a row range (this is typically used with root node as the node, i e to get syntax highlight matches in the current - viewport) + viewport). When omitted the start and end row values are used from + the given node. The iterator returns three values, a numeric id identifying the capture, the captured node, and metadata from any directives processing the match. diff --git a/runtime/lua/vim/treesitter/query.lua b/runtime/lua/vim/treesitter/query.lua index 5a27d740a2..9d1f265e54 100644 --- a/runtime/lua/vim/treesitter/query.lua +++ b/runtime/lua/vim/treesitter/query.lua @@ -353,6 +353,12 @@ function Query:iter_captures(node, source, start, stop) if type(source) == "number" and source == 0 then source = vim.api.nvim_get_current_buf() end + + if start == nil and stop == nil then + start, _, stop, _ = node:range() + stop = stop + 1 -- Make stop inclusive + end + local raw_iter = node:_rawquery(self.query, true, start, stop) local function iter() local capture, captured_node, match = raw_iter() @@ -385,6 +391,12 @@ function Query:iter_matches(node, source, start, stop) if type(source) == "number" and source == 0 then source = vim.api.nvim_get_current_buf() end + + if start == nil and stop == nil then + start, _, stop, _ = node:range() + stop = stop + 1 -- Make stop inclusive + end + local raw_iter = node:_rawquery(self.query, false, start, stop) local function iter() local pattern, match = raw_iter() |