From 3aba4ba37859e4407eff2bb3f4d99c44b108ed79 Mon Sep 17 00:00:00 2001 From: Quentin Rasmont Date: Fri, 22 Apr 2022 21:50:52 +0200 Subject: feat(treesitter): upstream is_parent() Util from the nvim-treesitter project. Renamed is_parent to is_ancestor for clarity. --- test/functional/treesitter/utils_spec.lua | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 test/functional/treesitter/utils_spec.lua (limited to 'test') diff --git a/test/functional/treesitter/utils_spec.lua b/test/functional/treesitter/utils_spec.lua new file mode 100644 index 0000000000..c5609501a7 --- /dev/null +++ b/test/functional/treesitter/utils_spec.lua @@ -0,0 +1,29 @@ +local helpers = require('test.functional.helpers')(after_each) + +local clear = helpers.clear +local insert = helpers.insert +local eq = helpers.eq +local exec_lua = helpers.exec_lua + +before_each(clear) + +describe('treesitter utils', function() + clear() + it('can find an ancestor', function() + insert([[ + int main() { + int x = 3; + }]]) + + exec_lua([[ + parser = vim.treesitter.get_parser(0, "c") + tree = parser:parse()[1] + root = tree:root() + ancestor = root:child(0) + child = ancestor:child(0) + ]]) + + eq(true, exec_lua('return vim.treesitter.is_ancestor(ancestor, child)')) + eq(false, exec_lua('return vim.treesitter.is_ancestor(child, ancestor)')) + end) +end) -- cgit From 6b2d42eb0352d01923e4bf2e3ce0824c662b7be4 Mon Sep 17 00:00:00 2001 From: Quentin Rasmont Date: Sat, 30 Apr 2022 10:43:26 +0200 Subject: feat(treesitter): add ability to retreive a tree/node given a range --- test/functional/treesitter/language_spec.lua | 29 ++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'test') diff --git a/test/functional/treesitter/language_spec.lua b/test/functional/treesitter/language_spec.lua index 8aa8524a26..b66a56264f 100644 --- a/test/functional/treesitter/language_spec.lua +++ b/test/functional/treesitter/language_spec.lua @@ -7,6 +7,7 @@ local exec_lua = helpers.exec_lua local pcall_err = helpers.pcall_err local matches = helpers.matches local pending_c_parser = helpers.pending_c_parser +local insert = helpers.insert before_each(clear) @@ -84,5 +85,33 @@ describe('treesitter language API', function() eq("Error executing lua: .../language.lua:0: no parser for 'borklang' language, see :help treesitter-parsers", pcall_err(exec_lua, "new_parser = vim.treesitter.get_parser(0)")) end) + + it('retrieve the tree given a range', function () + insert([[ + int main() { + int x = 3; + }]]) + + exec_lua([[ + langtree = vim.treesitter.get_parser(0, "c") + tree = langtree:tree_for_range({1, 3, 1, 3}) + ]]) + + eq('', exec_lua('return tostring(tree:root())')) + end) + + it('retrieve the node given a range', function () + insert([[ + int main() { + int x = 3; + }]]) + + exec_lua([[ + langtree = vim.treesitter.get_parser(0, "c") + node = langtree:named_node_for_range({1, 3, 1, 3}) + ]]) + + eq('', exec_lua('return tostring(node)')) + end) end) -- cgit From a577fb778addb6eb305ade82a229b52673ced234 Mon Sep 17 00:00:00 2001 From: Quentin Rasmont Date: Sat, 30 Apr 2022 20:54:25 +0200 Subject: feat(treesitter): upstream get_named_children() as a node method Util from the nvim-treesitter project. --- test/functional/treesitter/node_spec.lua | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'test') diff --git a/test/functional/treesitter/node_spec.lua b/test/functional/treesitter/node_spec.lua index 21c287644e..b9ad4925c9 100644 --- a/test/functional/treesitter/node_spec.lua +++ b/test/functional/treesitter/node_spec.lua @@ -59,4 +59,22 @@ describe('treesitter node API', function() exec_lua 'node = node:prev_named_sibling()' eq('int x', lua_eval('node_text(node)')) end) + + it('can retrieve the children of a node', function() + insert([[ + int main() { + int x = 3; + }]]) + + local len = exec_lua([[ + tree = vim.treesitter.get_parser(0, "c"):parse()[1] + node = tree:root():child(0) + children = node:named_children() + + return #children + ]]) + + eq(3, len) + eq('', lua_eval('tostring(children[3])')) + end) end) -- cgit From baba43681e792db30318bdedc3e73e4fe12482a6 Mon Sep 17 00:00:00 2001 From: Quentin Rasmont Date: Sun, 1 May 2022 11:35:12 +0200 Subject: feat(treesitter): upstream get_root_for_node() as a node method Util from the nvim-treesitter project. --- test/functional/treesitter/node_spec.lua | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'test') diff --git a/test/functional/treesitter/node_spec.lua b/test/functional/treesitter/node_spec.lua index b9ad4925c9..9450526257 100644 --- a/test/functional/treesitter/node_spec.lua +++ b/test/functional/treesitter/node_spec.lua @@ -77,4 +77,19 @@ describe('treesitter node API', function() eq(3, len) eq('', lua_eval('tostring(children[3])')) end) + + it('can retrieve the tree root given a node', function() + insert([[ + int main() { + int x = 3; + }]]) + + exec_lua([[ + tree = vim.treesitter.get_parser(0, "c"):parse()[1] + root = tree:root() + node = root:child(0):child(2) + ]]) + + eq(lua_eval('tostring(root)'), lua_eval('tostring(node:root())')) + end) end) -- cgit From f57341a4b69398ff0c58686e66c2f4138be164aa Mon Sep 17 00:00:00 2001 From: Quentin Rasmont Date: Sun, 1 May 2022 21:13:47 +0200 Subject: feat(treesitter): upstream node_length() as a node method Util from the nvim-treesitter project. --- test/functional/treesitter/node_spec.lua | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'test') diff --git a/test/functional/treesitter/node_spec.lua b/test/functional/treesitter/node_spec.lua index 9450526257..87ce1b973c 100644 --- a/test/functional/treesitter/node_spec.lua +++ b/test/functional/treesitter/node_spec.lua @@ -92,4 +92,20 @@ describe('treesitter node API', function() eq(lua_eval('tostring(root)'), lua_eval('tostring(node:root())')) end) + + it('can compute the byte length of a node', function() + insert([[ + int main() { + int x = 3; + }]]) + + exec_lua([[ + tree = vim.treesitter.get_parser(0, "c"):parse()[1] + root = tree:root() + child = root:child(0):child(0) + ]]) + + eq(28, lua_eval('root:byte_length()')) + eq(3, lua_eval('child:byte_length()')) + end) end) -- cgit From 73ee2b35d1fc738be9bd4b99f49ce8f84351fbe6 Mon Sep 17 00:00:00 2001 From: bfredl Date: Thu, 25 Aug 2022 20:49:27 +0200 Subject: fix(tests): use pending_c_parser when needed --- test/functional/treesitter/language_spec.lua | 2 ++ test/functional/treesitter/utils_spec.lua | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/functional/treesitter/language_spec.lua b/test/functional/treesitter/language_spec.lua index b66a56264f..8e9941d797 100644 --- a/test/functional/treesitter/language_spec.lua +++ b/test/functional/treesitter/language_spec.lua @@ -87,6 +87,7 @@ describe('treesitter language API', function() end) it('retrieve the tree given a range', function () + if pending_c_parser(pending) then return end insert([[ int main() { int x = 3; @@ -101,6 +102,7 @@ describe('treesitter language API', function() end) it('retrieve the node given a range', function () + if pending_c_parser(pending) then return end insert([[ int main() { int x = 3; diff --git a/test/functional/treesitter/utils_spec.lua b/test/functional/treesitter/utils_spec.lua index c5609501a7..4f4c18a748 100644 --- a/test/functional/treesitter/utils_spec.lua +++ b/test/functional/treesitter/utils_spec.lua @@ -4,12 +4,16 @@ local clear = helpers.clear local insert = helpers.insert local eq = helpers.eq local exec_lua = helpers.exec_lua +local pending_c_parser = helpers.pending_c_parser before_each(clear) describe('treesitter utils', function() - clear() + before_each(clear) + it('can find an ancestor', function() + if pending_c_parser(pending) then return end + insert([[ int main() { int x = 3; -- cgit