From ac18403d6e58a08956f9465998f2223df4e19108 Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux Date: Sun, 14 Jun 2020 18:50:22 +0200 Subject: treesitter: test newly added set_included_ranges --- test/functional/lua/treesitter_spec.lua | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'test/functional') diff --git a/test/functional/lua/treesitter_spec.lua b/test/functional/lua/treesitter_spec.lua index ecee471386..1fd93d5f56 100644 --- a/test/functional/lua/treesitter_spec.lua +++ b/test/functional/lua/treesitter_spec.lua @@ -404,4 +404,44 @@ static int nlua_schedule(lua_State *const lstate) end eq({true,true}, {has_named,has_anonymous}) end) + it('allows to set ranges', function() + if not check_parser() then return end + + insert(test_text) + + local res = exec_lua([[ + parser = vim.treesitter.get_parser(0, "c") + return { parser:parse():root():range() } + ]]) + + eq({0, 0, 19, 0}, res) + + local res = exec_lua([[ + parser:set_included_ranges({{0, 0, 1, 0}}) + parser.valid = false + return { parser:parse():root():range() } + ]]) + + eq({0, 0, 1, 0}, res) + + -- Pick random samples + local res = exec_lua([[ + parser:set_included_ranges({{8, 0, 9, 0}, {12, 0, 13 ,0}}) + local root = parser:parse():root() + return {{root:child(0):range()}, {root:child(1):range()}} + ]]) + + eq({{ + 8, + 2, + 8, + 33 + }, + { + 12, + 4, + 12, + 37 + }}, res) + end) end) -- cgit From b652f74ca3a6722ad0d185a0f4093907a6af65d7 Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux Date: Tue, 16 Jun 2020 08:17:25 +0200 Subject: treesitter: use nodes to mark ranges --- test/functional/lua/treesitter_spec.lua | 45 +++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 16 deletions(-) (limited to 'test/functional') diff --git a/test/functional/lua/treesitter_spec.lua b/test/functional/lua/treesitter_spec.lua index 1fd93d5f56..539c2b5e7c 100644 --- a/test/functional/lua/treesitter_spec.lua +++ b/test/functional/lua/treesitter_spec.lua @@ -417,31 +417,44 @@ static int nlua_schedule(lua_State *const lstate) eq({0, 0, 19, 0}, res) local res = exec_lua([[ - parser:set_included_ranges({{0, 0, 1, 0}}) + local root = parser:parse():root() + parser:set_included_ranges({{root:child(0), root:child(0)}}) parser.valid = false return { parser:parse():root():range() } ]]) - eq({0, 0, 1, 0}, res) + eq({0, 0, 18, 1}, res) -- Pick random samples local res = exec_lua([[ - parser:set_included_ranges({{8, 0, 9, 0}, {12, 0, 13 ,0}}) + query = vim.treesitter.parse_query("c", "(declaration) @decl") + + local nodes = {} + for _, node in query:iter_captures(parser:parse():root(), 0, 0, 19) do + table.insert(nodes, { node, node }) + end + + parser:set_included_ranges(nodes) + local root = parser:parse():root() - return {{root:child(0):range()}, {root:child(1):range()}} + + local res = {} + for i=0,(root:named_child_count() - 1) do + table.insert(res, { root:named_child(i):range() }) + end + return res ]]) - eq({{ - 8, - 2, - 8, - 33 - }, - { - 12, - 4, - 12, - 37 - }}, res) + eq({ + { 2, 2, 2, 40 }, + { 3, 3, 3, 32 }, + { 4, 7, 4, 8 }, + { 4, 8, 4, 25 }, + { 8, 2, 8, 6 }, + { 8, 7, 8, 33 }, + { 9, 8, 9, 20 }, + { 10, 4, 10, 5 }, + { 10, 5, 10, 20 }, + { 14, 9, 14, 27 } }, res) end) end) -- cgit From 66af35fc85fb2e14fe8f91449289af06b9104dd4 Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux Date: Fri, 26 Jun 2020 20:11:16 +0200 Subject: treesitter: separate tests into smaller pieces --- test/functional/lua/treesitter_spec.lua | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'test/functional') diff --git a/test/functional/lua/treesitter_spec.lua b/test/functional/lua/treesitter_spec.lua index 539c2b5e7c..16a7f365a8 100644 --- a/test/functional/lua/treesitter_spec.lua +++ b/test/functional/lua/treesitter_spec.lua @@ -404,7 +404,7 @@ static int nlua_schedule(lua_State *const lstate) end eq({true,true}, {has_named,has_anonymous}) end) - it('allows to set ranges', function() + it('allows to set simple ranges', function() if not check_parser() then return end insert(test_text) @@ -416,6 +416,8 @@ static int nlua_schedule(lua_State *const lstate) eq({0, 0, 19, 0}, res) + -- The following sets the included ranges for the current parser + -- As stated here, this only includes the function (thus the whole buffer, without the last line) local res = exec_lua([[ local root = parser:parse():root() parser:set_included_ranges({{root:child(0), root:child(0)}}) @@ -424,9 +426,15 @@ static int nlua_schedule(lua_State *const lstate) ]]) eq({0, 0, 18, 1}, res) + end) + it("allows to set complex ranges", function() + if not check_parser() then return end + + insert(test_text) + - -- Pick random samples local res = exec_lua([[ + parser = vim.treesitter.get_parser(0, "c") query = vim.treesitter.parse_query("c", "(declaration) @decl") local nodes = {} -- cgit From 69816f5e134f0e965352367b5928835794da1698 Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux Date: Mon, 29 Jun 2020 23:02:30 +0200 Subject: treesitter: use single nodes in set_ranges fixup! treesitter: fix lint --- test/functional/lua/treesitter_spec.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'test/functional') diff --git a/test/functional/lua/treesitter_spec.lua b/test/functional/lua/treesitter_spec.lua index 16a7f365a8..ab0224a911 100644 --- a/test/functional/lua/treesitter_spec.lua +++ b/test/functional/lua/treesitter_spec.lua @@ -418,14 +418,14 @@ static int nlua_schedule(lua_State *const lstate) -- The following sets the included ranges for the current parser -- As stated here, this only includes the function (thus the whole buffer, without the last line) - local res = exec_lua([[ + local res2 = exec_lua([[ local root = parser:parse():root() - parser:set_included_ranges({{root:child(0), root:child(0)}}) + parser:set_included_ranges({root:child(0)}) parser.valid = false return { parser:parse():root():range() } ]]) - eq({0, 0, 18, 1}, res) + eq({0, 0, 18, 1}, res2) end) it("allows to set complex ranges", function() if not check_parser() then return end @@ -439,7 +439,7 @@ static int nlua_schedule(lua_State *const lstate) local nodes = {} for _, node in query:iter_captures(parser:parse():root(), 0, 0, 19) do - table.insert(nodes, { node, node }) + table.insert(nodes, node) end parser:set_included_ranges(nodes) -- cgit