diff options
author | Thomas Vigouroux <tomvig38@gmail.com> | 2021-01-20 10:32:48 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-20 10:32:48 +0100 |
commit | d9c1586e4a6f741174a277bd75470da478ede3f3 (patch) | |
tree | 43e9aa456b2cf422daf947da7c81f67269b5a34a /runtime | |
parent | d6d4e3d1ae88fd037aadb12a3237fdecfb1c692d (diff) | |
parent | 9bed991cfb2d40911eaf149b705da4d3b77a9c28 (diff) | |
download | rneovim-d9c1586e4a6f741174a277bd75470da478ede3f3.tar.gz rneovim-d9c1586e4a6f741174a277bd75470da478ede3f3.tar.bz2 rneovim-d9c1586e4a6f741174a277bd75470da478ede3f3.zip |
Merge pull request #13765 from jvgrootveld/ts-default-start-end-row-on-capture
treesitter: default start and end row when omitted
Diffstat (limited to 'runtime')
-rw-r--r-- | runtime/doc/treesitter.txt | 3 | ||||
-rw-r--r-- | runtime/lua/vim/treesitter/query.lua | 19 |
2 files changed, 21 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..e49f54681d 100644 --- a/runtime/lua/vim/treesitter/query.lua +++ b/runtime/lua/vim/treesitter/query.lua @@ -340,6 +340,19 @@ function Query:apply_directives(match, pattern, source, metadata) end end + +--- Returns the start and stop value if set else the node's range. +-- When the node's range is used, the stop is incremented by 1 +-- to make the search inclusive. +local function value_or_node_range(start, stop, node) + if start == nil and stop == nil then + local node_start, _, node_stop, _ = node:range() + return node_start, node_stop + 1 -- Make stop inclusive + end + + return start, stop +end + --- Iterates of the captures of self on a given range. -- -- @param node The node under witch the search will occur @@ -353,6 +366,9 @@ function Query:iter_captures(node, source, start, stop) if type(source) == "number" and source == 0 then source = vim.api.nvim_get_current_buf() end + + start, stop = value_or_node_range(start, stop, node) + local raw_iter = node:_rawquery(self.query, true, start, stop) local function iter() local capture, captured_node, match = raw_iter() @@ -385,6 +401,9 @@ function Query:iter_matches(node, source, start, stop) if type(source) == "number" and source == 0 then source = vim.api.nvim_get_current_buf() end + + start, stop = value_or_node_range(start, stop, node) + local raw_iter = node:_rawquery(self.query, false, start, stop) local function iter() local pattern, match = raw_iter() |