2024-04-20 08:44:13 -07:00
|
|
|
local t = require('test.testutil')
|
|
|
|
local n = require('test.functional.testnvim')()
|
2021-01-03 06:38:12 -07:00
|
|
|
|
2024-04-20 08:44:13 -07:00
|
|
|
local clear = n.clear
|
2024-04-08 02:03:20 -07:00
|
|
|
local eq = t.eq
|
2024-04-20 08:44:13 -07:00
|
|
|
local command = n.command
|
|
|
|
local exec_lua = n.exec_lua
|
2024-04-08 02:03:20 -07:00
|
|
|
local pcall_err = t.pcall_err
|
|
|
|
local matches = t.matches
|
2024-04-20 08:44:13 -07:00
|
|
|
local insert = n.insert
|
2024-09-13 05:09:11 -07:00
|
|
|
local NIL = vim.NIL
|
2021-01-03 06:38:12 -07:00
|
|
|
|
|
|
|
before_each(clear)
|
|
|
|
|
2022-07-25 03:23:04 -07:00
|
|
|
describe('treesitter language API', function()
|
2021-01-03 06:38:12 -07:00
|
|
|
-- error tests not requiring a parser library
|
|
|
|
it('handles missing language', function()
|
2022-11-14 03:01:35 -07:00
|
|
|
eq(
|
2024-09-14 12:57:33 -07:00
|
|
|
'.../treesitter.lua:0: Parser could not be created for buffer 1 and language "borklang"',
|
2021-01-03 06:38:12 -07:00
|
|
|
pcall_err(exec_lua, "parser = vim.treesitter.get_parser(0, 'borklang')")
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2021-01-03 06:38:12 -07:00
|
|
|
|
2024-09-14 12:57:33 -07:00
|
|
|
eq(NIL, exec_lua("return vim.treesitter.get_parser(0, 'borklang', { error = false })"))
|
2024-09-13 05:09:11 -07:00
|
|
|
|
2021-01-03 06:38:12 -07:00
|
|
|
-- actual message depends on platform
|
2023-01-22 08:51:17 -07:00
|
|
|
matches(
|
|
|
|
"Failed to load parser for language 'borklang': uv_dlopen: .+",
|
2023-03-24 07:43:14 -07:00
|
|
|
pcall_err(
|
|
|
|
exec_lua,
|
|
|
|
"parser = vim.treesitter.language.add('borklang', { path = 'borkbork.so' })"
|
|
|
|
)
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2021-01-03 06:38:12 -07:00
|
|
|
|
2024-09-15 05:19:08 -07:00
|
|
|
eq(NIL, exec_lua("return vim.treesitter.language.add('borklang')"))
|
2023-02-21 10:09:18 -07:00
|
|
|
|
2023-03-24 07:43:14 -07:00
|
|
|
eq(
|
|
|
|
false,
|
|
|
|
exec_lua("return pcall(vim.treesitter.language.add, 'borklang', { path = 'borkbork.so' })")
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2021-01-03 06:38:12 -07:00
|
|
|
|
2022-11-14 03:01:35 -07:00
|
|
|
matches(
|
|
|
|
'Failed to load parser: uv_dlsym: .+',
|
2023-03-24 07:43:14 -07:00
|
|
|
pcall_err(exec_lua, 'vim.treesitter.language.add("c", { symbol_name = "borklang" })')
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2021-01-03 06:38:12 -07:00
|
|
|
end)
|
|
|
|
|
2024-09-15 05:19:08 -07:00
|
|
|
it('does not load parser for invalid language name', function()
|
|
|
|
eq(NIL, exec_lua('vim.treesitter.language.add("/foo/")'))
|
2023-03-03 02:44:02 -07:00
|
|
|
end)
|
|
|
|
|
2021-01-03 06:38:12 -07:00
|
|
|
it('inspects language', function()
|
2024-07-29 03:20:15 -07:00
|
|
|
local keys, fields, symbols = unpack(exec_lua(function()
|
2023-03-24 07:43:14 -07:00
|
|
|
local lang = vim.treesitter.language.inspect('c')
|
2021-01-03 06:38:12 -07:00
|
|
|
local keys, symbols = {}, {}
|
2024-04-19 08:04:57 -07:00
|
|
|
for k, v in pairs(lang) do
|
|
|
|
if type(v) == 'boolean' then
|
|
|
|
keys[k] = v
|
|
|
|
else
|
|
|
|
keys[k] = true
|
|
|
|
end
|
2021-01-03 06:38:12 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
-- symbols array can have "holes" and is thus not a valid msgpack array
|
|
|
|
-- but we don't care about the numbers here (checked in the parser test)
|
|
|
|
for _, v in pairs(lang.symbols) do
|
|
|
|
table.insert(symbols, v)
|
|
|
|
end
|
2024-07-29 03:20:15 -07:00
|
|
|
return { keys, lang.fields, symbols }
|
|
|
|
end))
|
2021-01-03 06:38:12 -07:00
|
|
|
|
2024-04-19 08:04:57 -07:00
|
|
|
eq({ fields = true, symbols = true, _abi_version = true, _wasm = false }, keys)
|
2021-01-03 06:38:12 -07:00
|
|
|
|
|
|
|
local fset = {}
|
|
|
|
for _, f in pairs(fields) do
|
|
|
|
eq('string', type(f))
|
|
|
|
fset[f] = true
|
|
|
|
end
|
|
|
|
eq(true, fset['directive'])
|
|
|
|
eq(true, fset['initializer'])
|
|
|
|
|
|
|
|
local has_named, has_anonymous
|
|
|
|
for _, s in pairs(symbols) do
|
|
|
|
eq('string', type(s[1]))
|
|
|
|
eq('boolean', type(s[2]))
|
|
|
|
if s[1] == 'for_statement' and s[2] == true then
|
|
|
|
has_named = true
|
|
|
|
elseif s[1] == '|=' and s[2] == false then
|
|
|
|
has_anonymous = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
eq({ true, true }, { has_named, has_anonymous })
|
|
|
|
end)
|
2022-04-22 07:15:28 -07:00
|
|
|
|
|
|
|
it(
|
|
|
|
'checks if vim.treesitter.get_parser tries to create a new parser on filetype change',
|
|
|
|
function()
|
|
|
|
command('set filetype=c')
|
|
|
|
-- Should not throw an error when filetype is c
|
|
|
|
eq('c', exec_lua('return vim.treesitter.get_parser(0):lang()'))
|
|
|
|
command('set filetype=borklang')
|
|
|
|
-- Should throw an error when filetype changes to borklang
|
2022-11-14 03:01:35 -07:00
|
|
|
eq(
|
2024-09-14 12:57:33 -07:00
|
|
|
'.../treesitter.lua:0: Parser could not be created for buffer 1 and language "borklang"',
|
2023-02-09 09:08:22 -07:00
|
|
|
pcall_err(exec_lua, "new_parser = vim.treesitter.get_parser(0, 'borklang')")
|
2022-04-22 07:15:28 -07:00
|
|
|
)
|
2024-09-14 12:57:33 -07:00
|
|
|
eq(NIL, exec_lua("return vim.treesitter.get_parser(0, 'borklang', { error = false })"))
|
2022-04-22 07:15:28 -07:00
|
|
|
end
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2022-04-30 01:43:26 -07:00
|
|
|
|
|
|
|
it('retrieve the tree given a range', function()
|
|
|
|
insert([[
|
|
|
|
int main() {
|
|
|
|
int x = 3;
|
|
|
|
}]])
|
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
eq(
|
|
|
|
'<node translation_unit>',
|
|
|
|
exec_lua(function()
|
|
|
|
local langtree = vim.treesitter.get_parser(0, 'c')
|
|
|
|
local tree = langtree:tree_for_range({ 1, 3, 1, 3 })
|
|
|
|
return tostring(tree:root())
|
|
|
|
end)
|
|
|
|
)
|
2022-04-30 01:43:26 -07:00
|
|
|
end)
|
|
|
|
|
2024-04-10 01:52:51 -07:00
|
|
|
it('retrieve the tree given a range when range is out of bounds relative to buffer', function()
|
|
|
|
insert([[
|
|
|
|
int main() {
|
|
|
|
int x = 3;
|
|
|
|
}]])
|
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
eq(
|
|
|
|
'<node translation_unit>',
|
|
|
|
exec_lua(function()
|
|
|
|
local langtree = vim.treesitter.get_parser(0, 'c')
|
|
|
|
local tree = langtree:tree_for_range({ 10, 10, 10, 10 })
|
|
|
|
return tostring(tree:root())
|
|
|
|
end)
|
|
|
|
)
|
2024-04-10 01:52:51 -07:00
|
|
|
end)
|
|
|
|
|
2022-04-30 01:43:26 -07:00
|
|
|
it('retrieve the node given a range', function()
|
|
|
|
insert([[
|
|
|
|
int main() {
|
|
|
|
int x = 3;
|
|
|
|
}]])
|
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
eq(
|
|
|
|
'<node primitive_type>',
|
|
|
|
exec_lua(function()
|
|
|
|
local langtree = vim.treesitter.get_parser(0, 'c')
|
|
|
|
local node = langtree:named_node_for_range({ 1, 3, 1, 3 })
|
|
|
|
return tostring(node)
|
|
|
|
end)
|
|
|
|
)
|
2022-04-30 01:43:26 -07:00
|
|
|
end)
|
2024-07-28 13:23:40 -07:00
|
|
|
|
|
|
|
it('retrieve an anonymous node given a range', function()
|
|
|
|
insert([[vim.fn.input()]])
|
|
|
|
|
2024-08-11 01:27:48 -07:00
|
|
|
exec_lua(function()
|
|
|
|
_G.langtree = vim.treesitter.get_parser(0, 'lua')
|
|
|
|
_G.node = _G.langtree:node_for_range({ 0, 3, 0, 3 })
|
|
|
|
end)
|
2024-07-28 13:23:40 -07:00
|
|
|
|
|
|
|
eq('.', exec_lua('return node:type()'))
|
|
|
|
end)
|
2021-01-03 06:38:12 -07:00
|
|
|
end)
|