diff options
author | Björn Linse <bjorn.linse@gmail.com> | 2020-09-01 10:52:55 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-01 10:52:55 +0200 |
commit | 04ab26b88591ce5afa427c9a8badb9fcf1b95685 (patch) | |
tree | 71ec6f46ca21eaf1bb623f5510020410590e4d3b /test/functional/lua/treesitter_spec.lua | |
parent | c14a9f6fcc0e74294e98913699c8a4a79f1f7321 (diff) | |
parent | 18217b987f79afa2f389262d7eb155b1c064f97c (diff) | |
download | rneovim-04ab26b88591ce5afa427c9a8badb9fcf1b95685.tar.gz rneovim-04ab26b88591ce5afa427c9a8badb9fcf1b95685.tar.bz2 rneovim-04ab26b88591ce5afa427c9a8badb9fcf1b95685.zip |
Merge pull request #12736 from vigoux/ts-iter-children
treesitter: allow to iterate over node children
Diffstat (limited to 'test/functional/lua/treesitter_spec.lua')
-rw-r--r-- | test/functional/lua/treesitter_spec.lua | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/test/functional/lua/treesitter_spec.lua b/test/functional/lua/treesitter_spec.lua index b0ac9e079a..12b0e6ef9f 100644 --- a/test/functional/lua/treesitter_spec.lua +++ b/test/functional/lua/treesitter_spec.lua @@ -127,6 +127,58 @@ void ui_refresh(void) } }]] + it('allows to iterate over nodes children', function() + if not check_parser() then return end + + insert(test_text); + + local res = exec_lua([[ + parser = vim.treesitter.get_parser(0, "c") + + func_node = parser:parse():root():child(0) + + res = {} + for node, field in func_node:iter_children() do + table.insert(res, {node:type(), field}) + end + return res + ]]) + + eq({ + {"primitive_type", "type"}, + {"function_declarator", "declarator"}, + {"compound_statement", "body"} + }, res) + end) + + it('allows to get a child by field', function() + if not check_parser() then return end + + insert(test_text); + + local res = exec_lua([[ + parser = vim.treesitter.get_parser(0, "c") + + func_node = parser:parse():root():child(0) + + local res = {} + for _, node in ipairs(func_node:field("type")) do + table.insert(res, {node:type(), node:range()}) + end + return res + ]]) + + eq({{ "primitive_type", 0, 0, 0, 4 }}, res) + + local res_fail = exec_lua([[ + parser = vim.treesitter.get_parser(0, "c") + + return #func_node:field("foo") == 0 + ]]) + + assert(res_fail) + end) + local query = [[ ((call_expression function: (identifier) @minfunc (argument_list (identifier) @min_id)) (eq? @minfunc "MIN")) "for" @keyword |