local helpers = require('test.functional.helpers')(after_each)
local clear = helpers.clear
local dedent = helpers.dedent
local eq = helpers.eq
local insert = helpers.insert
local exec_lua = helpers.exec_lua
local pcall_err = helpers.pcall_err
local feed = helpers.feed
local is_os = helpers.is_os
describe('treesitter parser API', function()
before_each(function()
clear()
exec_lua[[
vim.g.__ts_debug = 1
]]
end)
it('parses buffer', function()
insert([[
int main() {
int x = 3;
}]])
exec_lua([[
parser = vim.treesitter.get_parser(0, "c")
tree = parser:parse()[1]
root = tree:root()
lang = vim.treesitter.language.inspect('c')
]])
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({'function_definition', true}, exec_lua("return lang.symbols[child:symbol()]"))
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[anon:symbol()]"))
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([[
tree2 = parser:parse()[1]
root2 = tree2:root()
descendant2 = root2:descendant_for_range(1,2,1,13)
]])
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)
local test_text = [[
void ui_refresh(void)
{
int width = INT_MAX, height = INT_MAX;
bool ext_widgets[kUIExtCount];
for (UIExtension i = 0; (int)i < kUIExtCount; i++) {
ext_widgets[i] = true;
}
bool inclusive = ui_override();
for (size_t i = 0; i < ui_count; i++) {
UI *ui = uis[i];
width = MIN(ui->width, width);
height = MIN(ui->height, height);
foo = BAR(ui->bazaar, bazaar);
for (UIExtension j = 0; (int)j < kUIExtCount; j++) {
ext_widgets[j] &= (ui->ui_ext[j] || inclusive);
}
}
}]]
it('allows to iterate over nodes children', function()
insert(test_text);
local res = exec_lua([[
parser = vim.treesitter.get_parser(0, "c")
func_node = parser:parse()[1]: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('does not get parser for empty filetype', function()
insert(test_text);
eq('.../treesitter.lua:0: There is no parser available for buffer 1 and one'
.. ' could not be created because lang could not be determined. Either'
.. ' pass lang or set the buffer filetype',
pcall_err(exec_lua, 'vim.treesitter.get_parser(0)'))
-- Must provide language for buffers with an empty filetype
exec_lua("vim.treesitter.get_parser(0, 'c')")
end)
it('allows to get a child by field', function()
insert<
|