2024-04-20 08:44:13 -07:00
|
|
|
local t = require('test.testutil')
|
|
|
|
local n = require('test.functional.testnvim')()
|
2024-08-30 19:27:18 -07:00
|
|
|
local ts_t = require('test.functional.treesitter.testutil')
|
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 dedent = t.dedent
|
|
|
|
local eq = t.eq
|
2024-04-20 08:44:13 -07:00
|
|
|
local insert = n.insert
|
|
|
|
local exec_lua = n.exec_lua
|
2024-04-08 02:03:20 -07:00
|
|
|
local pcall_err = t.pcall_err
|
2024-04-20 08:44:13 -07:00
|
|
|
local feed = n.feed
|
2024-08-30 19:27:18 -07:00
|
|
|
local run_query = ts_t.run_query
|
2021-01-03 06:38:12 -07:00
|
|
|
|
|
|
|
describe('treesitter parser API', function()
|
2023-08-13 00:03:56 -07:00
|
|
|
before_each(function()
|
|
|
|
clear()
|
2024-07-29 03:20:15 -07:00
|
|
|
exec_lua(function()
|
2023-08-13 00:03:56 -07:00
|
|
|
vim.g.__ts_debug = 1
|
2024-07-29 03:20:15 -07:00
|
|
|
end)
|
2023-08-13 00:03:56 -07:00
|
|
|
end)
|
2021-01-03 06:38:12 -07:00
|
|
|
|
|
|
|
it('parses buffer', function()
|
|
|
|
insert([[
|
|
|
|
int main() {
|
|
|
|
int x = 3;
|
|
|
|
}]])
|
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
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)
|
2021-01-03 06:38:12 -07:00
|
|
|
|
|
|
|
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()}'))
|
2024-01-02 18:09:18 -07:00
|
|
|
|
2021-01-03 06:38:12 -07:00
|
|
|
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()}'))
|
2024-01-02 18:09:18 -07:00
|
|
|
|
2021-01-03 06:38:12 -07:00
|
|
|
eq('function_definition', exec_lua('return child:type()'))
|
|
|
|
eq(true, exec_lua('return child:named()'))
|
|
|
|
eq('number', type(exec_lua('return child:symbol()')))
|
2024-09-19 13:08:22 -07:00
|
|
|
eq(true, exec_lua('return lang.symbols[child:type()]'))
|
2024-01-02 18:09:18 -07:00
|
|
|
|
2021-01-03 06:38:12 -07:00
|
|
|
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()')))
|
2024-09-19 13:08:22 -07:00
|
|
|
eq(false, exec_lua([=[return lang.symbols[string.format('"%s"', anon:type())]]=]))
|
2024-01-02 18:09:18 -07:00
|
|
|
|
2021-01-03 06:38:12 -07:00
|
|
|
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()')
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
|
|
|
|
2021-01-03 06:38:12 -07:00
|
|
|
feed('2G7|ay')
|
2024-07-29 03:20:15 -07:00
|
|
|
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)
|
2021-01-03 06:38:12 -07:00
|
|
|
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)
|
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
local res = exec_lua(function()
|
|
|
|
local parser = vim.treesitter.get_parser(0, 'c')
|
2021-01-03 06:38:12 -07:00
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
local func_node = parser:parse()[1]:root():child(0)
|
2021-01-03 06:38:12 -07:00
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
local res = {}
|
2021-01-03 06:38:12 -07:00
|
|
|
for node, field in func_node:iter_children() do
|
2024-01-29 18:51:32 -07:00
|
|
|
table.insert(res, { node:type(), field })
|
2021-01-03 06:38:12 -07:00
|
|
|
end
|
|
|
|
return res
|
2024-07-29 03:20:15 -07:00
|
|
|
end)
|
2021-01-03 06:38:12 -07:00
|
|
|
|
|
|
|
eq({
|
|
|
|
{ 'primitive_type', 'type' },
|
|
|
|
{ 'function_declarator', 'declarator' },
|
|
|
|
{ 'compound_statement', 'body' },
|
|
|
|
}, res)
|
|
|
|
end)
|
|
|
|
|
2023-03-03 02:44:02 -07:00
|
|
|
it('does not get parser for empty filetype', function()
|
|
|
|
insert(test_text)
|
|
|
|
|
2023-03-10 03:25:10 -07:00
|
|
|
eq(
|
2024-09-14 12:57:33 -07:00
|
|
|
'.../treesitter.lua:0: Parser not found for buffer 1: language could not be determined',
|
2023-03-03 02:44:02 -07:00
|
|
|
pcall_err(exec_lua, 'vim.treesitter.get_parser(0)')
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-03-03 02:44:02 -07:00
|
|
|
|
|
|
|
-- Must provide language for buffers with an empty filetype
|
|
|
|
exec_lua("vim.treesitter.get_parser(0, 'c')")
|
|
|
|
end)
|
|
|
|
|
2021-01-03 06:38:12 -07:00
|
|
|
it('allows to get a child by field', function()
|
|
|
|
insert(test_text)
|
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
local res = exec_lua(function()
|
|
|
|
local parser = vim.treesitter.get_parser(0, 'c')
|
2021-01-03 06:38:12 -07:00
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
_G.func_node = parser:parse()[1]:root():child(0)
|
2021-01-03 06:38:12 -07:00
|
|
|
|
|
|
|
local res = {}
|
2024-07-29 03:20:15 -07:00
|
|
|
for _, node in ipairs(_G.func_node:field('type')) do
|
2024-01-29 18:51:32 -07:00
|
|
|
table.insert(res, { node:type(), node:range() })
|
2021-01-03 06:38:12 -07:00
|
|
|
end
|
|
|
|
return res
|
2024-07-29 03:20:15 -07:00
|
|
|
end)
|
2021-01-03 06:38:12 -07:00
|
|
|
|
|
|
|
eq({ { 'primitive_type', 0, 0, 0, 4 } }, res)
|
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
local res_fail = exec_lua(function()
|
|
|
|
vim.treesitter.get_parser(0, 'c')
|
2021-01-03 06:38:12 -07:00
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
return #_G.func_node:field('foo') == 0
|
|
|
|
end)
|
2021-01-03 06:38:12 -07:00
|
|
|
|
|
|
|
assert(res_fail)
|
|
|
|
end)
|
|
|
|
|
2021-12-12 04:05:39 -07:00
|
|
|
it('supports getting text of multiline node', function()
|
|
|
|
insert(test_text)
|
2024-07-29 03:20:15 -07:00
|
|
|
local res = exec_lua(function()
|
|
|
|
local parser = vim.treesitter.get_parser(0, 'c')
|
2021-12-12 04:05:39 -07:00
|
|
|
local tree = parser:parse()[1]
|
|
|
|
return vim.treesitter.get_node_text(tree:root(), 0)
|
2024-07-29 03:20:15 -07:00
|
|
|
end)
|
2021-12-12 04:05:39 -07:00
|
|
|
eq(test_text, res)
|
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
local res2 = exec_lua(function()
|
|
|
|
local parser = vim.treesitter.get_parser(0, 'c')
|
2021-12-12 04:05:39 -07:00
|
|
|
local root = parser:parse()[1]:root()
|
|
|
|
return vim.treesitter.get_node_text(root:child(0):child(0), 0)
|
2024-07-29 03:20:15 -07:00
|
|
|
end)
|
2021-12-12 04:05:39 -07:00
|
|
|
eq('void', res2)
|
|
|
|
end)
|
|
|
|
|
2024-01-29 18:51:32 -07:00
|
|
|
it('supports getting text where start of node is one past EOF', function()
|
2021-12-12 04:05:39 -07:00
|
|
|
local text = [[
|
|
|
|
def run
|
|
|
|
a = <<~E
|
|
|
|
end]]
|
|
|
|
insert(text)
|
2023-03-10 09:40:27 -07:00
|
|
|
eq(
|
|
|
|
'',
|
2024-07-29 03:20:15 -07:00
|
|
|
exec_lua(function()
|
2024-01-29 18:51:32 -07:00
|
|
|
local fake_node = {}
|
|
|
|
function fake_node:start()
|
|
|
|
return 3, 0, 23
|
2023-03-10 09:40:27 -07:00
|
|
|
end
|
2024-01-29 18:51:32 -07:00
|
|
|
function fake_node:end_()
|
|
|
|
return 3, 0, 23
|
|
|
|
end
|
|
|
|
function fake_node:range(bytes)
|
|
|
|
if bytes then
|
|
|
|
return 3, 0, 23, 3, 0, 23
|
|
|
|
end
|
|
|
|
return 3, 0, 3, 0
|
|
|
|
end
|
|
|
|
return vim.treesitter.get_node_text(fake_node, 0)
|
2024-07-29 03:20:15 -07:00
|
|
|
end)
|
2021-12-12 04:05:39 -07:00
|
|
|
)
|
|
|
|
end)
|
|
|
|
|
2024-01-29 18:51:32 -07:00
|
|
|
it('supports getting empty text if node range is zero width', function()
|
2022-02-11 05:59:47 -07:00
|
|
|
local text = [[
|
|
|
|
```lua
|
|
|
|
{}
|
|
|
|
```]]
|
|
|
|
insert(text)
|
2024-07-29 03:20:15 -07:00
|
|
|
local result = exec_lua(function()
|
2022-02-11 05:59:47 -07:00
|
|
|
local fake_node = {}
|
|
|
|
function fake_node:start()
|
|
|
|
return 1, 0, 7
|
|
|
|
end
|
|
|
|
function fake_node:end_()
|
|
|
|
return 1, 0, 7
|
|
|
|
end
|
2022-12-26 14:10:59 -07:00
|
|
|
function fake_node:range()
|
|
|
|
return 1, 0, 1, 0
|
|
|
|
end
|
2022-02-11 05:59:47 -07:00
|
|
|
return vim.treesitter.get_node_text(fake_node, 0) == ''
|
2024-07-29 03:20:15 -07:00
|
|
|
end)
|
2022-02-11 05:59:47 -07:00
|
|
|
eq(true, result)
|
|
|
|
end)
|
|
|
|
|
2021-01-03 06:38:12 -07:00
|
|
|
it('allows to set simple ranges', function()
|
|
|
|
insert(test_text)
|
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
local res = exec_lua(function()
|
|
|
|
_G.parser = vim.treesitter.get_parser(0, 'c')
|
|
|
|
return { _G.parser:parse()[1]:root():range() }
|
|
|
|
end)
|
2021-01-03 06:38:12 -07:00
|
|
|
|
|
|
|
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)
|
2024-07-29 03:20:15 -07:00
|
|
|
local res2 = exec_lua(function()
|
|
|
|
local root = _G.parser:parse()[1]:root()
|
|
|
|
_G.parser:set_included_regions({ { root:child(0) } })
|
|
|
|
_G.parser:invalidate()
|
|
|
|
return { _G.parser:parse(true)[1]:root():range() }
|
|
|
|
end)
|
2021-01-03 06:38:12 -07:00
|
|
|
|
|
|
|
eq({ 0, 0, 18, 1 }, res2)
|
|
|
|
|
2023-08-10 06:21:56 -07:00
|
|
|
eq({ { { 0, 0, 0, 18, 1, 512 } } }, exec_lua [[ return parser:included_regions() ]])
|
2021-01-03 06:38:12 -07:00
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
local range_tbl = exec_lua(function()
|
|
|
|
_G.parser:set_included_regions { { { 0, 0, 17, 1 } } }
|
|
|
|
_G.parser:parse()
|
|
|
|
return _G.parser:included_regions()
|
|
|
|
end)
|
2021-01-03 06:38:12 -07:00
|
|
|
|
2024-03-11 07:23:14 -07:00
|
|
|
eq({ { { 0, 0, 0, 17, 1, 508 } } }, range_tbl)
|
2021-01-03 06:38:12 -07:00
|
|
|
end)
|
2023-05-16 08:41:47 -07:00
|
|
|
|
2021-01-03 06:38:12 -07:00
|
|
|
it('allows to set complex ranges', function()
|
|
|
|
insert(test_text)
|
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
local res = exec_lua(function()
|
|
|
|
local parser = vim.treesitter.get_parser(0, 'c')
|
|
|
|
local query = vim.treesitter.query.parse('c', '(declaration) @decl')
|
2021-01-03 06:38:12 -07:00
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
local nodes = {}
|
|
|
|
for _, node in query:iter_captures(parser:parse()[1]:root(), 0) do
|
|
|
|
table.insert(nodes, node)
|
|
|
|
end
|
2021-01-03 06:38:12 -07:00
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
parser:set_included_regions({ nodes })
|
2021-01-03 06:38:12 -07:00
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
local root = parser:parse(true)[1]:root()
|
2021-01-03 06:38:12 -07:00
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
local res = {}
|
|
|
|
for i = 0, (root:named_child_count() - 1) do
|
|
|
|
table.insert(res, { root:named_child(i):range() })
|
|
|
|
end
|
|
|
|
return res
|
|
|
|
end)
|
2021-01-03 06:38:12 -07:00
|
|
|
|
|
|
|
eq({
|
|
|
|
{ 2, 2, 2, 40 },
|
|
|
|
{ 3, 2, 3, 32 },
|
|
|
|
{ 4, 7, 4, 25 },
|
|
|
|
{ 8, 2, 8, 33 },
|
|
|
|
{ 9, 7, 9, 20 },
|
|
|
|
{ 10, 4, 10, 20 },
|
|
|
|
{ 14, 9, 14, 27 },
|
|
|
|
}, res)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('allows to create string parsers', function()
|
2024-07-29 03:20:15 -07:00
|
|
|
local ret = exec_lua(function()
|
|
|
|
local parser = vim.treesitter.get_string_parser('int foo = 42;', 'c')
|
2021-01-03 06:38:12 -07:00
|
|
|
return { parser:parse()[1]:root():range() }
|
2024-07-29 03:20:15 -07:00
|
|
|
end)
|
2021-01-03 06:38:12 -07:00
|
|
|
|
|
|
|
eq({ 0, 0, 0, 13 }, ret)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('allows to run queries with string parsers', function()
|
|
|
|
local txt = [[
|
|
|
|
int foo = 42;
|
|
|
|
int bar = 13;
|
|
|
|
]]
|
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
local ret = exec_lua(function(str)
|
|
|
|
local parser = vim.treesitter.get_string_parser(str, 'c')
|
2021-01-03 06:38:12 -07:00
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
local nodes = {}
|
|
|
|
local query = vim.treesitter.query.parse('c', '((identifier) @id (#eq? @id "foo"))')
|
2021-01-03 06:38:12 -07:00
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
for _, node in query:iter_captures(parser:parse()[1]:root(), str) do
|
|
|
|
table.insert(nodes, { node:range() })
|
|
|
|
end
|
2021-01-03 06:38:12 -07:00
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
return nodes
|
|
|
|
end, txt)
|
2021-01-03 06:38:12 -07:00
|
|
|
|
|
|
|
eq({ { 0, 10, 0, 13 } }, ret)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('when creating a language tree', function()
|
|
|
|
local function get_ranges()
|
2024-07-29 03:20:15 -07:00
|
|
|
return exec_lua(function()
|
2024-01-29 18:51:32 -07:00
|
|
|
local result = {}
|
2024-07-29 03:20:15 -07:00
|
|
|
_G.parser:for_each_tree(function(tree)
|
|
|
|
table.insert(result, { tree:root():range() })
|
|
|
|
end)
|
2024-01-29 18:51:32 -07:00
|
|
|
return result
|
2024-07-29 03:20:15 -07:00
|
|
|
end)
|
2021-01-03 06:38:12 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
before_each(function()
|
|
|
|
insert([[
|
|
|
|
int x = INT_MAX;
|
2023-04-02 01:11:42 -07:00
|
|
|
#define READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
|
|
|
|
#define READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y))
|
2021-01-03 06:38:12 -07:00
|
|
|
#define VALUE 123
|
|
|
|
#define VALUE1 123
|
|
|
|
#define VALUE2 123
|
|
|
|
]])
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('when parsing regions independently', function()
|
|
|
|
it('should inject a language', function()
|
2024-07-29 03:20:15 -07:00
|
|
|
exec_lua(function()
|
|
|
|
_G.parser = vim.treesitter.get_parser(0, 'c', {
|
2024-01-29 18:51:32 -07:00
|
|
|
injections = {
|
|
|
|
c = (
|
2024-07-29 03:20:15 -07:00
|
|
|
'(preproc_def (preproc_arg) @injection.content (#set! injection.language "c")) '
|
|
|
|
.. '(preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))'
|
|
|
|
),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
_G.parser:parse(true)
|
|
|
|
end)
|
2021-01-03 06:38:12 -07:00
|
|
|
|
|
|
|
eq('table', exec_lua('return type(parser:children().c)'))
|
|
|
|
eq(5, exec_lua('return #parser:children().c:trees()'))
|
|
|
|
eq({
|
|
|
|
{ 0, 0, 7, 0 }, -- root tree
|
|
|
|
{ 3, 14, 3, 17 }, -- VALUE 123
|
|
|
|
{ 4, 15, 4, 18 }, -- VALUE1 123
|
|
|
|
{ 5, 15, 5, 18 }, -- VALUE2 123
|
2023-04-02 01:11:42 -07:00
|
|
|
{ 1, 26, 1, 63 }, -- READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
|
|
|
|
{ 2, 29, 2, 66 }, -- READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y))
|
2021-01-03 06:38:12 -07:00
|
|
|
}, get_ranges())
|
2023-02-23 08:19:52 -07:00
|
|
|
|
2024-04-20 08:44:13 -07:00
|
|
|
n.feed('ggo<esc>')
|
2023-02-23 08:19:52 -07:00
|
|
|
eq(5, exec_lua('return #parser:children().c:trees()'))
|
|
|
|
eq({
|
|
|
|
{ 0, 0, 8, 0 }, -- root tree
|
|
|
|
{ 4, 14, 4, 17 }, -- VALUE 123
|
|
|
|
{ 5, 15, 5, 18 }, -- VALUE1 123
|
|
|
|
{ 6, 15, 6, 18 }, -- VALUE2 123
|
2023-04-02 01:11:42 -07:00
|
|
|
{ 2, 26, 2, 63 }, -- READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
|
|
|
|
{ 3, 29, 3, 66 }, -- READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y))
|
2023-02-23 08:19:52 -07:00
|
|
|
}, get_ranges())
|
2021-01-03 06:38:12 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('when parsing regions combined', function()
|
|
|
|
it('should inject a language', function()
|
2024-07-29 03:20:15 -07:00
|
|
|
exec_lua(function()
|
|
|
|
_G.parser = vim.treesitter.get_parser(0, 'c', {
|
2024-01-29 18:51:32 -07:00
|
|
|
injections = {
|
|
|
|
c = (
|
2024-07-29 03:20:15 -07:00
|
|
|
'(preproc_def (preproc_arg) @injection.content (#set! injection.language "c") (#set! injection.combined)) '
|
|
|
|
.. '(preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c") (#set! injection.combined))'
|
|
|
|
),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
_G.parser:parse(true)
|
|
|
|
end)
|
2021-01-03 06:38:12 -07:00
|
|
|
|
|
|
|
eq('table', exec_lua('return type(parser:children().c)'))
|
|
|
|
eq(2, exec_lua('return #parser:children().c:trees()'))
|
|
|
|
eq({
|
|
|
|
{ 0, 0, 7, 0 }, -- root tree
|
|
|
|
{ 3, 14, 5, 18 }, -- VALUE 123
|
|
|
|
-- VALUE1 123
|
|
|
|
-- VALUE2 123
|
2023-04-02 01:11:42 -07:00
|
|
|
{ 1, 26, 2, 66 }, -- READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
|
|
|
|
-- READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y))
|
2021-01-03 06:38:12 -07:00
|
|
|
}, get_ranges())
|
2023-02-23 08:19:52 -07:00
|
|
|
|
2024-04-20 08:44:13 -07:00
|
|
|
n.feed('ggo<esc>')
|
2023-02-23 08:19:52 -07:00
|
|
|
eq('table', exec_lua('return type(parser:children().c)'))
|
|
|
|
eq(2, exec_lua('return #parser:children().c:trees()'))
|
|
|
|
eq({
|
|
|
|
{ 0, 0, 8, 0 }, -- root tree
|
|
|
|
{ 4, 14, 6, 18 }, -- VALUE 123
|
|
|
|
-- VALUE1 123
|
|
|
|
-- VALUE2 123
|
2023-04-02 01:11:42 -07:00
|
|
|
{ 2, 26, 3, 66 }, -- READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
|
|
|
|
-- READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y))
|
2023-02-23 08:19:52 -07:00
|
|
|
}, get_ranges())
|
2023-12-23 03:45:19 -07:00
|
|
|
|
2024-04-20 08:44:13 -07:00
|
|
|
n.feed('7ggI//<esc>')
|
2023-12-23 03:45:19 -07:00
|
|
|
exec_lua([[parser:parse({6, 7})]])
|
|
|
|
eq('table', exec_lua('return type(parser:children().c)'))
|
|
|
|
eq(2, exec_lua('return #parser:children().c:trees()'))
|
|
|
|
eq({
|
|
|
|
{ 0, 0, 8, 0 }, -- root tree
|
|
|
|
{ 4, 14, 5, 18 }, -- VALUE 123
|
|
|
|
-- VALUE1 123
|
|
|
|
{ 2, 26, 3, 66 }, -- READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
|
|
|
|
-- READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y))
|
|
|
|
}, get_ranges())
|
2021-01-03 06:38:12 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2023-08-21 21:51:38 -07:00
|
|
|
describe('when using injection.self', function()
|
|
|
|
it('should inject the source language', function()
|
2024-07-29 03:20:15 -07:00
|
|
|
exec_lua(function()
|
|
|
|
_G.parser = vim.treesitter.get_parser(0, 'c', {
|
2024-01-29 18:51:32 -07:00
|
|
|
injections = {
|
|
|
|
c = (
|
2024-07-29 03:20:15 -07:00
|
|
|
'(preproc_def (preproc_arg) @injection.content (#set! injection.self)) '
|
|
|
|
.. '(preproc_function_def value: (preproc_arg) @injection.content (#set! injection.self))'
|
|
|
|
),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
_G.parser:parse(true)
|
|
|
|
end)
|
2023-08-21 21:51:38 -07:00
|
|
|
|
|
|
|
eq('table', exec_lua('return type(parser:children().c)'))
|
|
|
|
eq(5, exec_lua('return #parser:children().c:trees()'))
|
|
|
|
eq({
|
|
|
|
{ 0, 0, 7, 0 }, -- root tree
|
|
|
|
{ 3, 14, 3, 17 }, -- VALUE 123
|
|
|
|
{ 4, 15, 4, 18 }, -- VALUE1 123
|
|
|
|
{ 5, 15, 5, 18 }, -- VALUE2 123
|
|
|
|
{ 1, 26, 1, 63 }, -- READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
|
|
|
|
{ 2, 29, 2, 66 }, -- READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y))
|
|
|
|
}, get_ranges())
|
|
|
|
|
2024-04-20 08:44:13 -07:00
|
|
|
n.feed('ggo<esc>')
|
2023-08-21 21:51:38 -07:00
|
|
|
eq(5, exec_lua('return #parser:children().c:trees()'))
|
|
|
|
eq({
|
|
|
|
{ 0, 0, 8, 0 }, -- root tree
|
|
|
|
{ 4, 14, 4, 17 }, -- VALUE 123
|
|
|
|
{ 5, 15, 5, 18 }, -- VALUE1 123
|
|
|
|
{ 6, 15, 6, 18 }, -- VALUE2 123
|
|
|
|
{ 2, 26, 2, 63 }, -- READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
|
|
|
|
{ 3, 29, 3, 66 }, -- READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y))
|
|
|
|
}, get_ranges())
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2021-01-03 06:38:12 -07:00
|
|
|
describe('when using the offset directive', function()
|
|
|
|
it('should shift the range by the directive amount', function()
|
2024-07-29 03:20:15 -07:00
|
|
|
exec_lua(function()
|
|
|
|
_G.parser = vim.treesitter.get_parser(0, 'c', {
|
2024-01-29 18:51:32 -07:00
|
|
|
injections = {
|
|
|
|
c = (
|
2024-07-29 03:20:15 -07:00
|
|
|
'(preproc_def ((preproc_arg) @injection.content (#set! injection.language "c") (#offset! @injection.content 0 2 0 -1))) '
|
|
|
|
.. '(preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))'
|
|
|
|
),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
_G.parser:parse(true)
|
|
|
|
end)
|
2021-01-03 06:38:12 -07:00
|
|
|
|
|
|
|
eq('table', exec_lua('return type(parser:children().c)'))
|
|
|
|
eq({
|
|
|
|
{ 0, 0, 7, 0 }, -- root tree
|
2023-07-27 03:45:08 -07:00
|
|
|
{ 3, 16, 3, 16 }, -- VALUE 123
|
|
|
|
{ 4, 17, 4, 17 }, -- VALUE1 123
|
|
|
|
{ 5, 17, 5, 17 }, -- VALUE2 123
|
2023-04-02 01:11:42 -07:00
|
|
|
{ 1, 26, 1, 63 }, -- READ_STRING(x, y) (char *)read_string((x), (size_t)(y))
|
|
|
|
{ 2, 29, 2, 66 }, -- READ_STRING_OK(x, y) (char *)read_string((x), (size_t)(y))
|
2021-01-03 06:38:12 -07:00
|
|
|
}, get_ranges())
|
|
|
|
end)
|
2021-04-11 11:53:52 -07:00
|
|
|
it('should list all directives', function()
|
2024-07-29 03:20:15 -07:00
|
|
|
local res_list = exec_lua(function()
|
2024-01-29 18:51:32 -07:00
|
|
|
local query = vim.treesitter.query
|
2021-04-11 11:53:52 -07:00
|
|
|
|
2024-01-29 18:51:32 -07:00
|
|
|
local list = query.list_directives()
|
2021-04-11 11:53:52 -07:00
|
|
|
|
2024-01-29 18:51:32 -07:00
|
|
|
table.sort(list)
|
2021-04-11 11:53:52 -07:00
|
|
|
|
2024-01-29 18:51:32 -07:00
|
|
|
return list
|
2024-07-29 03:20:15 -07:00
|
|
|
end)
|
2021-04-11 11:53:52 -07:00
|
|
|
|
2023-08-11 08:05:17 -07:00
|
|
|
eq({ 'gsub!', 'offset!', 'set!', 'trim!' }, res_list)
|
2021-04-11 11:53:52 -07:00
|
|
|
end)
|
2021-01-03 06:38:12 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('when getting the language for a range', function()
|
|
|
|
before_each(function()
|
|
|
|
insert([[
|
|
|
|
int x = INT_MAX;
|
|
|
|
#define VALUE 123456789
|
|
|
|
]])
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('should return the correct language tree', function()
|
2024-07-29 03:20:15 -07:00
|
|
|
local result = exec_lua(function()
|
|
|
|
local parser = vim.treesitter.get_parser(0, 'c', {
|
2024-01-29 18:51:32 -07:00
|
|
|
injections = {
|
2024-07-29 03:20:15 -07:00
|
|
|
c = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c"))',
|
|
|
|
},
|
2024-01-29 18:51:32 -07:00
|
|
|
})
|
|
|
|
parser:parse(true)
|
2021-01-03 06:38:12 -07:00
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
local sub_tree = parser:language_for_range({ 1, 18, 1, 19 })
|
2021-01-03 06:38:12 -07:00
|
|
|
|
2024-01-29 18:51:32 -07:00
|
|
|
return sub_tree == parser:children().c
|
2024-07-29 03:20:15 -07:00
|
|
|
end)
|
2021-01-03 06:38:12 -07:00
|
|
|
|
2024-03-11 07:23:14 -07:00
|
|
|
eq(true, result)
|
2021-01-03 06:38:12 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2024-05-14 07:14:43 -07:00
|
|
|
describe('when setting the node for an injection', function()
|
|
|
|
before_each(function()
|
|
|
|
insert([[
|
|
|
|
print()
|
|
|
|
]])
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('ignores optional captures #23100', function()
|
2024-07-29 03:20:15 -07:00
|
|
|
local result = exec_lua(function()
|
|
|
|
local parser = vim.treesitter.get_parser(0, 'lua', {
|
2024-05-14 07:14:43 -07:00
|
|
|
injections = {
|
|
|
|
lua = (
|
2024-07-29 03:20:15 -07:00
|
|
|
'(function_call '
|
|
|
|
.. '(arguments '
|
|
|
|
.. '(string)? @injection.content '
|
|
|
|
.. '(number)? @injection.content '
|
|
|
|
.. '(#offset! @injection.content 0 1 0 -1) '
|
|
|
|
.. '(#set! injection.language "c")))'
|
|
|
|
),
|
|
|
|
},
|
2024-05-14 07:14:43 -07:00
|
|
|
})
|
|
|
|
parser:parse(true)
|
|
|
|
|
|
|
|
return parser:is_valid()
|
2024-07-29 03:20:15 -07:00
|
|
|
end)
|
2024-05-14 07:14:43 -07:00
|
|
|
|
|
|
|
eq(true, result)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2021-01-03 06:38:12 -07:00
|
|
|
describe('when getting/setting match data', function()
|
|
|
|
describe('when setting for the whole match', function()
|
|
|
|
it('should set/get the data correctly', function()
|
|
|
|
insert([[
|
|
|
|
int x = 3;
|
|
|
|
]])
|
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
local result = exec_lua(function()
|
|
|
|
local query =
|
|
|
|
vim.treesitter.query.parse('c', '((number_literal) @number (#set! "key" "value"))')
|
|
|
|
local parser = vim.treesitter.get_parser(0, 'c')
|
2021-01-03 06:38:12 -07:00
|
|
|
|
2024-09-01 11:01:53 -07:00
|
|
|
local _, _, metadata = query:iter_matches(parser:parse()[1]:root(), 0, 0, -1)()
|
2024-07-29 03:20:15 -07:00
|
|
|
return metadata.key
|
|
|
|
end)
|
2021-01-03 06:38:12 -07:00
|
|
|
|
2024-03-11 07:23:14 -07:00
|
|
|
eq('value', result)
|
2021-01-03 06:38:12 -07:00
|
|
|
end)
|
2021-04-21 07:40:48 -07:00
|
|
|
|
|
|
|
describe('when setting a key on a capture', function()
|
|
|
|
it('it should create the nested table', function()
|
|
|
|
insert([[
|
|
|
|
int x = 3;
|
|
|
|
]])
|
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
local result = exec_lua(function()
|
|
|
|
local query = vim.treesitter.query.parse(
|
|
|
|
'c',
|
|
|
|
'((number_literal) @number (#set! @number "key" "value"))'
|
|
|
|
)
|
|
|
|
local parser = vim.treesitter.get_parser(0, 'c')
|
2021-04-21 07:40:48 -07:00
|
|
|
|
2024-09-01 11:01:53 -07:00
|
|
|
local _, _, metadata = query:iter_matches(parser:parse()[1]:root(), 0, 0, -1)()
|
2024-07-29 03:20:15 -07:00
|
|
|
local _, nested_tbl = next(metadata)
|
|
|
|
return nested_tbl.key
|
|
|
|
end)
|
2021-04-21 07:40:48 -07:00
|
|
|
|
2024-03-11 07:23:14 -07:00
|
|
|
eq('value', result)
|
2021-04-21 07:40:48 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('it should not overwrite the nested table', function()
|
|
|
|
insert([[
|
|
|
|
int x = 3;
|
|
|
|
]])
|
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
local result = exec_lua(function()
|
|
|
|
local query = vim.treesitter.query.parse(
|
|
|
|
'c',
|
|
|
|
'((number_literal) @number (#set! @number "key" "value") (#set! @number "key2" "value2"))'
|
|
|
|
)
|
|
|
|
local parser = vim.treesitter.get_parser(0, 'c')
|
2021-04-21 07:40:48 -07:00
|
|
|
|
2024-09-01 11:01:53 -07:00
|
|
|
local _, _, metadata = query:iter_matches(parser:parse()[1]:root(), 0, 0, -1)()
|
2024-07-29 03:20:15 -07:00
|
|
|
local _, nested_tbl = next(metadata)
|
|
|
|
return nested_tbl
|
|
|
|
end)
|
2021-04-21 07:40:48 -07:00
|
|
|
local expected = {
|
|
|
|
['key'] = 'value',
|
|
|
|
['key2'] = 'value2',
|
|
|
|
}
|
|
|
|
|
|
|
|
eq(expected, result)
|
|
|
|
end)
|
|
|
|
end)
|
2021-01-03 06:38:12 -07:00
|
|
|
end)
|
|
|
|
end)
|
2023-02-23 10:05:20 -07:00
|
|
|
|
2024-08-17 21:05:09 -07:00
|
|
|
describe('trim! directive', function()
|
|
|
|
it('can trim all whitespace', function()
|
|
|
|
-- luacheck: push ignore 611 613
|
|
|
|
insert([=[
|
|
|
|
print([[
|
|
|
|
|
|
|
|
f
|
|
|
|
helllo
|
|
|
|
there
|
|
|
|
asdf
|
|
|
|
asdfassd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
]])
|
|
|
|
print([[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
]])
|
|
|
|
|
|
|
|
print([[]])
|
|
|
|
|
|
|
|
print([[
|
|
|
|
]])
|
|
|
|
|
|
|
|
print([[ hello 😃 ]])
|
|
|
|
]=])
|
|
|
|
-- luacheck: pop
|
|
|
|
|
|
|
|
local query_text = [[
|
|
|
|
; query
|
|
|
|
((string_content) @str
|
|
|
|
(#trim! @str 1 1 1 1))
|
|
|
|
]]
|
|
|
|
|
|
|
|
exec_lua(function()
|
|
|
|
vim.treesitter.start(0, 'lua')
|
|
|
|
end)
|
|
|
|
|
|
|
|
eq({
|
|
|
|
{ 'str', { 2, 12, 6, 10 } },
|
|
|
|
{ 'str', { 11, 10, 11, 10 } },
|
|
|
|
{ 'str', { 17, 10, 17, 10 } },
|
|
|
|
{ 'str', { 19, 10, 19, 10 } },
|
|
|
|
{ 'str', { 22, 15, 22, 25 } },
|
2024-08-30 19:27:18 -07:00
|
|
|
}, run_query('lua', query_text))
|
2024-08-17 21:05:09 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('trims only empty lines by default (backwards compatible)', function()
|
2024-12-07 04:01:59 -07:00
|
|
|
insert(dedent [[
|
2024-08-17 21:05:09 -07:00
|
|
|
## Heading
|
|
|
|
|
|
|
|
With some text
|
|
|
|
|
|
|
|
## And another
|
|
|
|
|
2024-12-07 04:01:59 -07:00
|
|
|
With some more here]])
|
2024-08-17 21:05:09 -07:00
|
|
|
|
|
|
|
local query_text = [[
|
|
|
|
; query
|
|
|
|
((section) @fold
|
|
|
|
(#trim! @fold))
|
|
|
|
]]
|
|
|
|
|
|
|
|
exec_lua(function()
|
|
|
|
vim.treesitter.start(0, 'markdown')
|
|
|
|
end)
|
|
|
|
|
|
|
|
eq({
|
2024-12-07 04:01:59 -07:00
|
|
|
{ 'fold', { 0, 0, 2, 14 } },
|
|
|
|
{ 'fold', { 4, 0, 6, 19 } },
|
|
|
|
}, run_query('markdown', query_text))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('can trim lines', function()
|
|
|
|
insert(dedent [[
|
|
|
|
- Fold list
|
|
|
|
- Fold list
|
|
|
|
- Fold list
|
|
|
|
- Fold list
|
|
|
|
- Fold list
|
|
|
|
- Fold list
|
|
|
|
]])
|
|
|
|
|
|
|
|
local query_text = [[
|
|
|
|
; query
|
|
|
|
((list_item
|
|
|
|
(list)) @fold
|
|
|
|
(#trim! @fold 1 1 1 1))
|
|
|
|
]]
|
|
|
|
|
|
|
|
exec_lua(function()
|
|
|
|
vim.treesitter.start(0, 'markdown')
|
|
|
|
end)
|
|
|
|
|
|
|
|
eq({
|
|
|
|
{ 'fold', { 0, 0, 4, 13 } },
|
|
|
|
{ 'fold', { 1, 2, 3, 15 } },
|
2024-08-30 19:27:18 -07:00
|
|
|
}, run_query('markdown', query_text))
|
2024-08-17 21:05:09 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2023-04-06 07:16:44 -07:00
|
|
|
it('tracks the root range properly (#22911)', function()
|
|
|
|
insert([[
|
|
|
|
int main() {
|
|
|
|
int x = 3;
|
|
|
|
}]])
|
|
|
|
|
|
|
|
local query0 = [[
|
|
|
|
(declaration) @declaration
|
|
|
|
(function_definition) @function
|
|
|
|
]]
|
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
exec_lua(function()
|
2023-04-06 07:16:44 -07:00
|
|
|
vim.treesitter.start(0, 'c')
|
2024-07-29 03:20:15 -07:00
|
|
|
end)
|
2023-04-06 07:16:44 -07:00
|
|
|
|
|
|
|
eq({
|
2024-08-30 19:27:18 -07:00
|
|
|
{ 'function', { 0, 0, 2, 1 } },
|
|
|
|
{ 'declaration', { 1, 2, 1, 12 } },
|
|
|
|
}, run_query('c', query0))
|
2023-04-06 07:16:44 -07:00
|
|
|
|
2024-04-20 08:44:13 -07:00
|
|
|
n.command 'normal ggO'
|
2023-04-06 07:16:44 -07:00
|
|
|
insert('int a;')
|
|
|
|
|
|
|
|
eq({
|
2024-08-30 19:27:18 -07:00
|
|
|
{ 'declaration', { 0, 0, 0, 6 } },
|
|
|
|
{ 'function', { 1, 0, 3, 1 } },
|
|
|
|
{ 'declaration', { 2, 2, 2, 12 } },
|
|
|
|
}, run_query('c', query0))
|
2023-04-06 07:16:44 -07:00
|
|
|
end)
|
2023-05-16 08:41:47 -07:00
|
|
|
|
|
|
|
it('handles ranges when source is a multiline string (#20419)', function()
|
|
|
|
local source = [==[
|
|
|
|
vim.cmd[[
|
|
|
|
set number
|
|
|
|
set cmdheight=2
|
|
|
|
set lastsatus=2
|
|
|
|
]]
|
|
|
|
|
|
|
|
set query = [[;; query
|
|
|
|
((function_call
|
|
|
|
name: [
|
|
|
|
(identifier) @_cdef_identifier
|
|
|
|
(_ _ (identifier) @_cdef_identifier)
|
|
|
|
]
|
|
|
|
arguments: (arguments (string content: _ @injection.content)))
|
|
|
|
(#set! injection.language "c")
|
|
|
|
(#eq? @_cdef_identifier "cdef"))
|
|
|
|
]]
|
|
|
|
]==]
|
|
|
|
|
2024-08-11 01:27:48 -07:00
|
|
|
local r = exec_lua(function()
|
|
|
|
local parser = vim.treesitter.get_string_parser(source, 'lua')
|
2024-07-29 03:20:15 -07:00
|
|
|
parser:parse(true)
|
|
|
|
local ranges = {}
|
|
|
|
parser:for_each_tree(function(tstree, tree)
|
|
|
|
ranges[tree:lang()] = { tstree:root():range(true) }
|
|
|
|
end)
|
|
|
|
return ranges
|
2024-08-11 01:27:48 -07:00
|
|
|
end)
|
2023-05-16 08:41:47 -07:00
|
|
|
|
|
|
|
eq({
|
|
|
|
lua = { 0, 6, 6, 16, 4, 438 },
|
|
|
|
query = { 6, 20, 113, 15, 6, 431 },
|
|
|
|
vim = { 1, 0, 16, 4, 6, 89 },
|
|
|
|
}, r)
|
|
|
|
|
|
|
|
-- The above ranges are provided directly from treesitter, however query directives may mutate
|
|
|
|
-- the ranges but only provide a Range4. Strip the byte entries from the ranges and make sure
|
|
|
|
-- add_bytes() produces the same result.
|
|
|
|
|
2024-08-11 01:27:48 -07:00
|
|
|
local rb = exec_lua(function()
|
2024-07-29 03:20:15 -07:00
|
|
|
local add_bytes = require('vim.treesitter._range').add_bytes
|
2024-08-11 01:27:48 -07:00
|
|
|
for lang, range in pairs(r) do
|
|
|
|
r[lang] = { range[1], range[2], range[4], range[5] }
|
|
|
|
r[lang] = add_bytes(source, r[lang])
|
2024-07-29 03:20:15 -07:00
|
|
|
end
|
2024-08-11 01:27:48 -07:00
|
|
|
return r
|
|
|
|
end)
|
2023-05-16 08:41:47 -07:00
|
|
|
|
|
|
|
eq(rb, r)
|
|
|
|
end)
|
2023-08-07 10:22:36 -07:00
|
|
|
|
|
|
|
it('does not produce empty injection ranges (#23409)', function()
|
|
|
|
insert [[
|
|
|
|
Examples: >lua
|
|
|
|
local a = {}
|
|
|
|
<
|
|
|
|
]]
|
|
|
|
|
|
|
|
-- This is not a valid injection since (code) has children and include-children is not set
|
2024-07-29 03:20:15 -07:00
|
|
|
exec_lua(function()
|
|
|
|
_G.parser1 = require('vim.treesitter.languagetree').new(0, 'vimdoc', {
|
2023-08-07 10:22:36 -07:00
|
|
|
injections = {
|
2024-07-29 03:20:15 -07:00
|
|
|
vimdoc = '((codeblock (language) @injection.language (code) @injection.content))',
|
|
|
|
},
|
2023-08-07 10:22:36 -07:00
|
|
|
})
|
2024-07-29 03:20:15 -07:00
|
|
|
_G.parser1:parse(true)
|
|
|
|
end)
|
2023-08-07 10:22:36 -07:00
|
|
|
|
|
|
|
eq(0, exec_lua('return #vim.tbl_keys(parser1:children())'))
|
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
exec_lua(function()
|
|
|
|
_G.parser2 = require('vim.treesitter.languagetree').new(0, 'vimdoc', {
|
2023-08-07 10:22:36 -07:00
|
|
|
injections = {
|
2024-07-29 03:20:15 -07:00
|
|
|
vimdoc = '((codeblock (language) @injection.language (code) @injection.content) (#set! injection.include-children))',
|
|
|
|
},
|
2023-08-07 10:22:36 -07:00
|
|
|
})
|
2024-07-29 03:20:15 -07:00
|
|
|
_G.parser2:parse(true)
|
|
|
|
end)
|
2023-08-07 10:22:36 -07:00
|
|
|
|
|
|
|
eq(1, exec_lua('return #vim.tbl_keys(parser2:children())'))
|
|
|
|
eq({ { { 1, 0, 21, 2, 0, 42 } } }, exec_lua('return parser2:children().lua:included_regions()'))
|
|
|
|
end)
|
|
|
|
|
2023-08-10 06:21:56 -07:00
|
|
|
it('parsers injections incrementally', function()
|
|
|
|
insert(dedent [[
|
|
|
|
>lua
|
|
|
|
local a = {}
|
|
|
|
<
|
|
|
|
|
|
|
|
>lua
|
|
|
|
local b = {}
|
|
|
|
<
|
|
|
|
|
|
|
|
>lua
|
|
|
|
local c = {}
|
|
|
|
<
|
|
|
|
|
|
|
|
>lua
|
|
|
|
local d = {}
|
|
|
|
<
|
|
|
|
|
|
|
|
>lua
|
|
|
|
local e = {}
|
|
|
|
<
|
|
|
|
|
|
|
|
>lua
|
|
|
|
local f = {}
|
|
|
|
<
|
|
|
|
|
|
|
|
>lua
|
|
|
|
local g = {}
|
|
|
|
<
|
|
|
|
]])
|
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
exec_lua(function()
|
|
|
|
_G.parser = require('vim.treesitter.languagetree').new(0, 'vimdoc', {
|
2023-08-10 06:21:56 -07:00
|
|
|
injections = {
|
2024-07-29 03:20:15 -07:00
|
|
|
vimdoc = '((codeblock (language) @injection.language (code) @injection.content) (#set! injection.include-children))',
|
|
|
|
},
|
2023-08-10 06:21:56 -07:00
|
|
|
})
|
2024-07-29 03:20:15 -07:00
|
|
|
end)
|
2023-08-10 06:21:56 -07:00
|
|
|
|
|
|
|
--- Do not parse injections by default
|
|
|
|
eq(
|
|
|
|
0,
|
2024-07-29 03:20:15 -07:00
|
|
|
exec_lua(function()
|
|
|
|
_G.parser:parse()
|
|
|
|
return #vim.tbl_keys(_G.parser:children())
|
|
|
|
end)
|
2023-08-10 06:21:56 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
--- Only parse injections between lines 0, 2
|
|
|
|
eq(
|
|
|
|
1,
|
2024-07-29 03:20:15 -07:00
|
|
|
exec_lua(function()
|
|
|
|
_G.parser:parse({ 0, 2 })
|
|
|
|
return #_G.parser:children().lua:trees()
|
|
|
|
end)
|
2023-08-10 06:21:56 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
eq(
|
|
|
|
2,
|
2024-07-29 03:20:15 -07:00
|
|
|
exec_lua(function()
|
|
|
|
_G.parser:parse({ 2, 6 })
|
|
|
|
return #_G.parser:children().lua:trees()
|
|
|
|
end)
|
2023-08-10 06:21:56 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
eq(
|
|
|
|
7,
|
2024-07-29 03:20:15 -07:00
|
|
|
exec_lua(function()
|
|
|
|
_G.parser:parse(true)
|
|
|
|
return #_G.parser:children().lua:trees()
|
|
|
|
end)
|
2023-08-10 06:21:56 -07:00
|
|
|
)
|
|
|
|
end)
|
|
|
|
|
2024-01-29 18:51:32 -07:00
|
|
|
describe('languagetree is_valid()', function()
|
2023-11-27 05:34:32 -07:00
|
|
|
before_each(function()
|
|
|
|
insert(dedent [[
|
|
|
|
Treesitter integration *treesitter*
|
|
|
|
|
|
|
|
Nvim integrates the `tree-sitter` library for incremental parsing of buffers:
|
|
|
|
https://tree-sitter.github.io/tree-sitter/
|
|
|
|
|
|
|
|
]])
|
|
|
|
|
|
|
|
feed(':set ft=help<cr>')
|
|
|
|
|
2024-07-29 03:20:15 -07:00
|
|
|
exec_lua(function()
|
|
|
|
vim.treesitter.get_parser(0, 'vimdoc', {
|
2023-11-27 05:34:32 -07:00
|
|
|
injections = {
|
2024-07-29 03:20:15 -07:00
|
|
|
vimdoc = '((codeblock (language) @injection.language (code) @injection.content) (#set! injection.include-children))',
|
|
|
|
},
|
2023-11-27 05:34:32 -07:00
|
|
|
})
|
2024-07-29 03:20:15 -07:00
|
|
|
end)
|
2023-11-27 05:34:32 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('is valid excluding, invalid including children initially', function()
|
|
|
|
eq(true, exec_lua('return vim.treesitter.get_parser():is_valid(true)'))
|
|
|
|
eq(false, exec_lua('return vim.treesitter.get_parser():is_valid()'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('is fully valid after a full parse', function()
|
|
|
|
exec_lua('vim.treesitter.get_parser():parse(true)')
|
|
|
|
eq(true, exec_lua('return vim.treesitter.get_parser():is_valid(true)'))
|
|
|
|
eq(true, exec_lua('return vim.treesitter.get_parser():is_valid()'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('is fully valid after a parsing a range on parsed tree', function()
|
|
|
|
exec_lua('vim.treesitter.get_parser():parse({5, 7})')
|
|
|
|
eq(true, exec_lua('return vim.treesitter.get_parser():is_valid(true)'))
|
|
|
|
eq(true, exec_lua('return vim.treesitter.get_parser():is_valid()'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('when adding content with injections', function()
|
|
|
|
before_each(function()
|
|
|
|
feed('G')
|
|
|
|
insert(dedent [[
|
|
|
|
>lua
|
|
|
|
local a = {}
|
|
|
|
<
|
|
|
|
|
|
|
|
]])
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('is fully invalid after changes', function()
|
|
|
|
eq(false, exec_lua('return vim.treesitter.get_parser():is_valid(true)'))
|
|
|
|
eq(false, exec_lua('return vim.treesitter.get_parser():is_valid()'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('is valid excluding, invalid including children after a rangeless parse', function()
|
|
|
|
exec_lua('vim.treesitter.get_parser():parse()')
|
|
|
|
eq(true, exec_lua('return vim.treesitter.get_parser():is_valid(true)'))
|
|
|
|
eq(false, exec_lua('return vim.treesitter.get_parser():is_valid()'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it(
|
|
|
|
'is fully valid after a range parse that leads to parsing not parsed injections',
|
|
|
|
function()
|
|
|
|
exec_lua('vim.treesitter.get_parser():parse({5, 7})')
|
|
|
|
eq(true, exec_lua('return vim.treesitter.get_parser():is_valid(true)'))
|
|
|
|
eq(true, exec_lua('return vim.treesitter.get_parser():is_valid()'))
|
|
|
|
end
|
|
|
|
)
|
2024-01-02 18:09:18 -07:00
|
|
|
|
2023-11-27 05:34:32 -07:00
|
|
|
it(
|
|
|
|
'is valid excluding, invalid including children after a range parse that does not lead to parsing not parsed injections',
|
|
|
|
function()
|
|
|
|
exec_lua('vim.treesitter.get_parser():parse({2, 4})')
|
|
|
|
eq(true, exec_lua('return vim.treesitter.get_parser():is_valid(true)'))
|
|
|
|
eq(false, exec_lua('return vim.treesitter.get_parser():is_valid()'))
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('when removing content with injections', function()
|
|
|
|
before_each(function()
|
|
|
|
feed('G')
|
|
|
|
insert(dedent [[
|
|
|
|
>lua
|
|
|
|
local a = {}
|
|
|
|
<
|
|
|
|
|
|
|
|
>lua
|
|
|
|
local a = {}
|
|
|
|
<
|
|
|
|
|
|
|
|
]])
|
|
|
|
|
|
|
|
exec_lua('vim.treesitter.get_parser():parse(true)')
|
|
|
|
|
|
|
|
feed('Gd3k')
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('is fully invalid after changes', function()
|
|
|
|
eq(false, exec_lua('return vim.treesitter.get_parser():is_valid(true)'))
|
|
|
|
eq(false, exec_lua('return vim.treesitter.get_parser():is_valid()'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('is valid excluding, invalid including children after a rangeless parse', function()
|
|
|
|
exec_lua('vim.treesitter.get_parser():parse()')
|
|
|
|
eq(true, exec_lua('return vim.treesitter.get_parser():is_valid(true)'))
|
|
|
|
eq(false, exec_lua('return vim.treesitter.get_parser():is_valid()'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('is fully valid after a range parse that leads to parsing modified child tree', function()
|
|
|
|
exec_lua('vim.treesitter.get_parser():parse({5, 7})')
|
|
|
|
eq(true, exec_lua('return vim.treesitter.get_parser():is_valid(true)'))
|
|
|
|
eq(true, exec_lua('return vim.treesitter.get_parser():is_valid()'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it(
|
|
|
|
'is valid excluding, invalid including children after a range parse that does not lead to parsing modified child tree',
|
|
|
|
function()
|
|
|
|
exec_lua('vim.treesitter.get_parser():parse({2, 4})')
|
|
|
|
eq(true, exec_lua('return vim.treesitter.get_parser():is_valid(true)'))
|
|
|
|
eq(false, exec_lua('return vim.treesitter.get_parser():is_valid()'))
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end)
|
|
|
|
end)
|
2021-01-03 06:38:12 -07:00
|
|
|
end)
|