diff options
author | Lewis Russell <lewis6991@gmail.com> | 2023-03-08 23:45:43 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-08 23:45:43 +0000 |
commit | be0461e3c216c2e4e2c3397c739b7727a5bf6df8 (patch) | |
tree | 8f3dfe67b10aa52fd77667ec01ed58cda8f86fa6 | |
parent | ae70e946eeaec792c9a87a89fea7141b5ee6a33c (diff) | |
download | rneovim-be0461e3c216c2e4e2c3397c739b7727a5bf6df8.tar.gz rneovim-be0461e3c216c2e4e2c3397c739b7727a5bf6df8.tar.bz2 rneovim-be0461e3c216c2e4e2c3397c739b7727a5bf6df8.zip |
fix(treesitter): is_in_node_range (#22582)
TS ranges are end column exclusive, so fix is_in_node_range
to account for that.
-rw-r--r-- | runtime/lua/vim/treesitter.lua | 5 | ||||
-rw-r--r-- | test/functional/treesitter/utils_spec.lua | 17 |
2 files changed, 20 insertions, 2 deletions
diff --git a/runtime/lua/vim/treesitter.lua b/runtime/lua/vim/treesitter.lua index d13824076e..ab9f8968c8 100644 --- a/runtime/lua/vim/treesitter.lua +++ b/runtime/lua/vim/treesitter.lua @@ -191,7 +191,7 @@ end --- ---@return boolean True if the position is in node range function M.is_in_node_range(node, line, col) - return M.node_contains(node, { line, col, line, col }) + return M.node_contains(node, { line, col, line, col + 1 }) end --- Determines if a node contains a range @@ -202,7 +202,8 @@ end ---@return boolean True if the {node} contains the {range} function M.node_contains(node, range) vim.validate({ - node = { node, 'userdata' }, + -- allow a table so nodes can be mocked + node = { node, { 'userdata', 'table' } }, range = { range, Range.validate, 'integer list with 4 or 6 elements' }, }) return Range.contains({ node:range() }, range) diff --git a/test/functional/treesitter/utils_spec.lua b/test/functional/treesitter/utils_spec.lua index 7f5a864c3d..9c07959098 100644 --- a/test/functional/treesitter/utils_spec.lua +++ b/test/functional/treesitter/utils_spec.lua @@ -28,4 +28,21 @@ describe('treesitter utils', function() eq(true, exec_lua('return vim.treesitter.is_ancestor(ancestor, child)')) eq(false, exec_lua('return vim.treesitter.is_ancestor(child, ancestor)')) end) + + it('can detect if a position is contained in a node', function() + exec_lua([[ + node = { + range = function() + return 0, 4, 0, 8 + end, + } + ]]) + + eq(false, exec_lua('return vim.treesitter.is_in_node_range(node, 0, 3)')) + for i = 4, 7 do + eq(true, exec_lua('return vim.treesitter.is_in_node_range(node, 0, ...)', i)) + end + -- End column exclusive + eq(false, exec_lua('return vim.treesitter.is_in_node_range(node, 0, 8)')) + end) end) |