local t = require('test.testutil')
local n = require('test.functional.testnvim')()
local ts_t = require('test.functional.treesitter.testutil')
local clear = n.clear
local dedent = t.dedent
local eq = t.eq
local insert = n.insert
local exec_lua = n.exec_lua
local pcall_err = t.pcall_err
local feed = n.feed
local run_query = ts_t.run_query
local assert_alive = n.assert_alive
describe('treesitter parser API', function()
before_each(function()
clear()
exec_lua(function()
vim.g.__ts_debug = 1
end)
end)
it('parses buffer', function()
insert([[
int main() {
int x = 3;
}]])
exec_lua(function()
_G.parser = vim.treesitter.get_parser(0, 'c')
_G.tree = _G.parser:parse()[1]
_G.root = _G.tree:root()
_G.lang = vim.treesitter.language.inspect('c')
end)
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()}'))
eq(1, exec_lua('return root:child_count()'))
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()}'))
eq('function_definition', exec_lua('return child:type()'))
eq(true, exec_lua('return child:named()'))
eq('number', type(exec_lua('return child:symbol()')))
eq(true, exec_lua('return lang.symbols[child:type()]'))
exec_lua('anon = root:descendant_for_range(0,8,0,9)')
eq('(', exec_lua('return anon:type()'))
eq(false, exec_lua('return anon:named()'))
eq('number', type(exec_lua('return anon:symbol()')))
eq(false, exec_lua([=[return lang.symbols[string.format('"%s"', anon:type())]]=]))
exec_lua('descendant = root:descendant_for_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 type: (primitive_type) declarator: (init_declarator declarator: (identifier) value: (number_literal)))',
exec_lua('return descendant:sexpr()')
)
feed('2G7|ay')
exec_lua(function()
_G.tree2 = _G.parser:parse()[1]
_G.root2 = _G.tree2:root()
_G.descendant2 = _G.root2:descendant_for_range(1, 2, 1, 13)
end)
eq(false, exec_lua('return tree2 == tree1'))
eq(false, exec_lua('return root2 == root'))
eq('<node declaration>', exec_lua('return tostring(descendant2)'))
eq({ 1, 2, 1, 13 }, exec_lua('return {descendant2:range()}'))
eq(true, exec_lua('return child == child'))
-- separate lua object, but represents same node
eq(true, exec_lua('return child == root:child(0)'))
eq(false, exec_lua('return child == descendant2'))
eq(false, exec_lua('return child == nil'))
eq(false, exec_lua('return child == tree'))
eq('string', exec_lua('return type(child:id())'))
eq(true, exec_lua('return child:id() == child:id()'))
-- separate lua object, but represents same node
eq(true, exec_lua('return child:id() == root:child(0):id()'))
eq(false, exec_lua('return child:id() == descendant2:id()'))
eq(false, exec_lua('return child:id() == nil'))
eq(false, exec_lua('return child:id() == tree'))
-- unchanged buffer: return the same tree
eq(true, exec_lua('return parser:parse()[1] == tree2'))
end)
it('parses buffer asynchronously', function()
insert([[
int main() {
int x = 3;
}]])
exec_lua(function()
_G.parser = vim.treesitter.get_parser(0, 'c')
_G.lang = vim.treesitter.language.inspect('c')
_G.parser:parse(nil, function(_, trees)
_G.tree = trees[1]
_G.root = _G.tree:root()
end)
vim.wait(100, function() end)
end)
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()}'))
eq(1, exec_lua('return root:child_count()'))
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()}'))
eq('function_definition', exec_lua('return child:type()'))
eq(true, exec_lua('return child:named()'))
eq('number', type(exec_lua('return child:symbol()')))
eq(true, exec_lua('return lang.symbols[child:type()]'))
exec_lua('anon = root:descendant_for_range(0,8,0,9)')
eq('(', exec_lua('return anon:type()'))
eq(false, exec_lua('return anon:named()'))
eq('number', type(exec_lua('return anon:symbol()')))
eq(false, exec_lua([=[return lang.symbols[string.format('"%s"', anon:type())]]=]))
exec_lua('descendant = root:descendant_for_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 type: (primitive_type) declarator: (init_declarator declarator: (identifier) value: (number_literal)))',
exec_lua('return descendant:sexpr()')
)
feed('2G7|ay')
exec_lua(function()
_G.parser:parse(nil, function(_, trees)
_G.tree2 = trees[1]
_G.root2 = _G.tree2:root()
_G.descendant2 = _G.root2:descendant_for_range(1, 2, 1, 13)
end)
vim.wait(100, function() end)
end)
eq(false, exec_lua('return tree2 == tree1'))
eq(false, exec_lua('return root2 == root'))
eq('<node declaration>', exec_lua('return tostring(descendant2)'))
eq({ 1, 2, 1, 13 }, exec_lua('return {descendant2:range()}'))
eq(true, exec_lua('return child == child'))
-- separate lua object, but represents same node
eq(true, exec_lua('return child == root:child(0)'))
eq(false, exec_lua('return child == descendant2'))
eq(false, exec_lua('return child == nil'))
eq(false, exec_lua('return child == tree'))
eq('string', exec_lua('return type(child:id())'))
eq(true, exec_lua('return child:id() == child:id()'))
-- separate lua object, but represents same node
eq(true, exec_lua('return child:id() == root:child(0):id()'))
eq(false, exec_lua('return child:id() == descendant2:id()'))
eq(false, exec_lua('return child:id() == nil'))
eq(false, exec_lua('return child:id() == tree'))
-- unchanged buffer: return the same tree
eq(true, exec_lua('return parser:parse()[1] == tree2'))
end)
it('does not crash when editing large files', function()
insert([[printf(&quo
|