From a2818819bb5afc3283e935727d5a1b0e9d1cf6b3 Mon Sep 17 00:00:00 2001 From: “jvgrootveld” <“justin.vangrootveld@gmail.com”> Date: Fri, 15 Jan 2021 21:44:40 +0100 Subject: 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. --- runtime/lua/vim/treesitter/query.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'runtime/lua/vim/treesitter/query.lua') 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() -- cgit From 9bed991cfb2d40911eaf149b705da4d3b77a9c28 Mon Sep 17 00:00:00 2001 From: “jvgrootveld” <“justin.vangrootveld@gmail.com”> Date: Mon, 18 Jan 2021 08:17:12 +0100 Subject: treesitter: Fix linter warning and add helper function to remove duplicated logic This function returns the start and stop value if set else the node's range is used When the node's range is used, the stop is incremented by 1 to make the search inclusive --- runtime/lua/vim/treesitter/query.lua | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'runtime/lua/vim/treesitter/query.lua') diff --git a/runtime/lua/vim/treesitter/query.lua b/runtime/lua/vim/treesitter/query.lua index 9d1f265e54..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 @@ -354,10 +367,7 @@ function Query:iter_captures(node, source, start, stop) 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 + start, stop = value_or_node_range(start, stop, node) local raw_iter = node:_rawquery(self.query, true, start, stop) local function iter() @@ -392,10 +402,7 @@ function Query:iter_matches(node, source, start, stop) 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 + start, stop = value_or_node_range(start, stop, node) local raw_iter = node:_rawquery(self.query, false, start, stop) local function iter() -- cgit