mirror of
https://github.com/neovim/neovim.git
synced 2026-04-23 07:45:32 +00:00
tree-sitter: improve parser API (shared parser between plugins)
This commit is contained in:
@@ -8,6 +8,7 @@ local insert = helpers.insert
|
||||
local meth_pcall = helpers.meth_pcall
|
||||
local exec_lua = helpers.exec_lua
|
||||
local iswin = helpers.iswin
|
||||
local feed = helpers.feed
|
||||
|
||||
before_each(clear)
|
||||
|
||||
@@ -46,12 +47,11 @@ describe('tree-sitter API', function()
|
||||
}]])
|
||||
|
||||
exec_lua([[
|
||||
parser = vim.treesitter.create_parser(0, "c")
|
||||
tree = parser:parse_tree()
|
||||
parser = vim.treesitter.get_parser(0, "c")
|
||||
tree = parser:parse()
|
||||
root = tree:root()
|
||||
]])
|
||||
|
||||
--eq("<parser>", exec_lua("return tostring(parser)"))
|
||||
eq("<tree>", exec_lua("return tostring(tree)"))
|
||||
eq("<node translation_unit>", exec_lua("return tostring(root)"))
|
||||
eq({0,0,3,0}, exec_lua("return {root:range()}"))
|
||||
@@ -60,6 +60,27 @@ describe('tree-sitter API', function()
|
||||
exec_lua("child = root:child(0)")
|
||||
eq("<node function_definition>", exec_lua("return tostring(child)"))
|
||||
eq({0,0,2,1}, exec_lua("return {child:range()}"))
|
||||
|
||||
exec_lua("descendant = root:descendant_for_point_range(1,2,1,12)")
|
||||
eq("<node declaration>", exec_lua("return tostring(descendant)"))
|
||||
eq({1,2,1,12}, exec_lua("return {descendant:range()}"))
|
||||
eq("(declaration (primitive_type) (init_declarator (identifier) (number_literal)))", exec_lua("return descendant:sexpr()"))
|
||||
|
||||
feed("2G7|ay")
|
||||
exec_lua([[
|
||||
tree2 = parser:parse()
|
||||
root2 = tree2:root()
|
||||
descendant2 = root2:descendant_for_point_range(1,2,1,13)
|
||||
]])
|
||||
eq(false, exec_lua("return tree2 == tree1"))
|
||||
eq("<node declaration>", exec_lua("return tostring(descendant2)"))
|
||||
eq({1,2,1,13}, exec_lua("return {descendant2:range()}"))
|
||||
|
||||
-- orginal tree did not change
|
||||
eq({1,2,1,12}, exec_lua("return {descendant:range()}"))
|
||||
|
||||
-- unchanged buffer: return the same tree
|
||||
eq(true, exec_lua("return parser:parse() == tree2"))
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
Reference in New Issue
Block a user