test(treesitter): improve the style of treesitter/parser_spec

General refactoring, including:

- Improve whitespace and indentation
- Prefix captures with `@`
- Add more comments on `iter_capture()` tests
- Move `test_query` up closer to the fixture source string

No behavioral changes are made.
This commit is contained in:
Jongwook Choi 2024-01-29 20:51:32 -05:00
parent d3e51603bc
commit 921aa52b8f

View File

@ -111,6 +111,20 @@ void ui_refresh(void)
}
}]]
local test_query = [[
((call_expression
function: (identifier) @minfunc
(argument_list (identifier) @min_id))
(#eq? @minfunc "MIN")
)
"for" @keyword
(primitive_type) @type
(field_expression argument: (identifier) @fieldarg)
]]
it('allows to iterate over nodes children', function()
insert(test_text)
@ -121,7 +135,7 @@ void ui_refresh(void)
res = {}
for node, field in func_node:iter_children() do
table.insert(res, {node:type(), field})
table.insert(res, { node:type(), field })
end
return res
]])
@ -157,7 +171,7 @@ void ui_refresh(void)
local res = {}
for _, node in ipairs(func_node:field("type")) do
table.insert(res, {node:type(), node:range()})
table.insert(res, { node:type(), node:range() })
end
return res
]])
@ -173,29 +187,26 @@ void ui_refresh(void)
assert(res_fail)
end)
local test_query = [[
((call_expression function: (identifier) @minfunc (argument_list (identifier) @min_id)) (#eq? @minfunc "MIN"))
"for" @keyword
(primitive_type) @type
(field_expression argument: (identifier) @fieldarg)
]]
it('supports runtime queries', function()
---@type string[]
local ret = exec_lua [[
return vim.treesitter.query.get("c", "highlights").captures[1]
return vim.treesitter.query.get("c", "highlights").captures
]]
eq('variable', ret)
-- see $VIMRUNTIME/queries/c/highlights.scm
eq('variable', ret[1])
eq('keyword', ret[2])
end)
it('supports caching queries', function()
local long_query = test_query:rep(100)
---@return number
local function q(n)
return exec_lua(
[[
local query, n = ...
local before = vim.uv.hrtime()
for i=1,n,1 do
for i=1, n, 1 do
cquery = vim.treesitter.query.parse("c", ...)
end
local after = vim.uv.hrtime()
@ -217,7 +228,7 @@ void ui_refresh(void)
)
end)
it('support query and iter by capture', function()
it('supports query and iter by capture (iter_captures)', function()
insert(test_text)
local res = exec_lua(
@ -228,7 +239,7 @@ void ui_refresh(void)
res = {}
for cid, node in cquery:iter_captures(tree:root(), 0, 7, 14) do
-- can't transmit node over RPC. just check the name and range
table.insert(res, {cquery.captures[cid], node:type(), node:range()})
table.insert(res, { '@' .. cquery.captures[cid], node:type(), node:range() })
end
return res
]],
@ -236,20 +247,94 @@ void ui_refresh(void)
)
eq({
{ 'type', 'primitive_type', 8, 2, 8, 6 },
{ 'keyword', 'for', 9, 2, 9, 5 },
{ 'type', 'primitive_type', 9, 7, 9, 13 },
{ 'minfunc', 'identifier', 11, 12, 11, 15 },
{ 'fieldarg', 'identifier', 11, 16, 11, 18 },
{ 'min_id', 'identifier', 11, 27, 11, 32 },
{ 'minfunc', 'identifier', 12, 13, 12, 16 },
{ 'fieldarg', 'identifier', 12, 17, 12, 19 },
{ 'min_id', 'identifier', 12, 29, 12, 35 },
{ 'fieldarg', 'identifier', 13, 14, 13, 16 },
{ '@type', 'primitive_type', 8, 2, 8, 6 }, -- bool
{ '@keyword', 'for', 9, 2, 9, 5 }, -- for
{ '@type', 'primitive_type', 9, 7, 9, 13 }, -- size_t
{ '@minfunc', 'identifier', 11, 12, 11, 15 }, -- "MIN"(ui->width, width);
{ '@fieldarg', 'identifier', 11, 16, 11, 18 }, -- ui
{ '@min_id', 'identifier', 11, 27, 11, 32 }, -- width
{ '@minfunc', 'identifier', 12, 13, 12, 16 }, -- "MIN"(ui->height, height);
{ '@fieldarg', 'identifier', 12, 17, 12, 19 }, -- ui
{ '@min_id', 'identifier', 12, 29, 12, 35 }, -- height
{ '@fieldarg', 'identifier', 13, 14, 13, 16 }, -- ui ; in BAR(..)
}, res)
end)
it('support query and iter by match', function()
it('supports query and iter by match (iter_matches)', function()
insert(test_text)
---@type table
local res = exec_lua(
[[
cquery = vim.treesitter.query.parse("c", ...)
parser = vim.treesitter.get_parser(0, "c")
tree = parser:parse()[1]
res = {}
for pattern, match in cquery:iter_matches(tree:root(), 0, 7, 14, { all = true }) do
-- can't transmit node over RPC. just check the name and range
local mrepr = {}
for cid, nodes in pairs(match) do
for _, node in ipairs(nodes) do
table.insert(mrepr, { '@' .. cquery.captures[cid], node:type(), node:range() })
end
end
table.insert(res, { pattern, mrepr })
end
return res
]],
test_query
)
eq({
{ 3, { { '@type', 'primitive_type', 8, 2, 8, 6 } } },
{ 2, { { '@keyword', 'for', 9, 2, 9, 5 } } },
{ 3, { { '@type', 'primitive_type', 9, 7, 9, 13 } } },
{ 4, { { '@fieldarg', 'identifier', 11, 16, 11, 18 } } },
{
1,
{
{ '@minfunc', 'identifier', 11, 12, 11, 15 },
{ '@min_id', 'identifier', 11, 27, 11, 32 },
},
},
{ 4, { { '@fieldarg', 'identifier', 12, 17, 12, 19 } } },
{
1,
{
{ '@minfunc', 'identifier', 12, 13, 12, 16 },
{ '@min_id', 'identifier', 12, 29, 12, 35 },
},
},
{ 4, { { '@fieldarg', 'identifier', 13, 14, 13, 16 } } },
}, res)
end)
it('supports query and iter by capture for quantifiers', function()
insert(test_text)
local res = exec_lua(
[[
cquery = vim.treesitter.query.parse("c", ...)
parser = vim.treesitter.get_parser(0, "c")
tree = parser:parse()[1]
res = {}
for cid, node in cquery:iter_captures(tree:root(), 0, 7, 14) do
-- can't transmit node over RPC. just check the name and range
table.insert(res, { '@' .. cquery.captures[cid], node:type(), node:range() })
end
return res
]],
'(expression_statement (assignment_expression (call_expression)))+ @funccall'
)
eq({
{ '@funccall', 'expression_statement', 11, 4, 11, 34 },
{ '@funccall', 'expression_statement', 12, 4, 12, 37 },
{ '@funccall', 'expression_statement', 13, 4, 13, 34 },
}, res)
end)
it('supports query and iter by match for quantifiers', function()
insert(test_text)
local res = exec_lua(
@ -263,74 +348,7 @@ void ui_refresh(void)
local mrepr = {}
for cid, nodes in pairs(match) do
for _, node in ipairs(nodes) do
table.insert(mrepr, {cquery.captures[cid], node:type(), node:range()})
end
end
table.insert(res, {pattern, mrepr})
end
return res
]],
test_query
)
eq({
{ 3, { { 'type', 'primitive_type', 8, 2, 8, 6 } } },
{ 2, { { 'keyword', 'for', 9, 2, 9, 5 } } },
{ 3, { { 'type', 'primitive_type', 9, 7, 9, 13 } } },
{ 4, { { 'fieldarg', 'identifier', 11, 16, 11, 18 } } },
{
1,
{ { 'minfunc', 'identifier', 11, 12, 11, 15 }, { 'min_id', 'identifier', 11, 27, 11, 32 } },
},
{ 4, { { 'fieldarg', 'identifier', 12, 17, 12, 19 } } },
{
1,
{ { 'minfunc', 'identifier', 12, 13, 12, 16 }, { 'min_id', 'identifier', 12, 29, 12, 35 } },
},
{ 4, { { 'fieldarg', 'identifier', 13, 14, 13, 16 } } },
}, res)
end)
it('support query and iter by capture for quantifiers', function()
insert(test_text)
local res = exec_lua(
[[
cquery = vim.treesitter.query.parse("c", ...)
parser = vim.treesitter.get_parser(0, "c")
tree = parser:parse()[1]
res = {}
for cid, node in cquery:iter_captures(tree:root(), 0, 7, 14) do
-- can't transmit node over RPC. just check the name and range
table.insert(res, {cquery.captures[cid], node:type(), node:range()})
end
return res
]],
'(expression_statement (assignment_expression (call_expression)))+ @funccall'
)
eq({
{ 'funccall', 'expression_statement', 11, 4, 11, 34 },
{ 'funccall', 'expression_statement', 12, 4, 12, 37 },
{ 'funccall', 'expression_statement', 13, 4, 13, 34 },
}, res)
end)
it('support query and iter by match for quantifiers', function()
insert(test_text)
local res = exec_lua(
[[
cquery = vim.treesitter.query.parse("c", ...)
parser = vim.treesitter.get_parser(0, "c")
tree = parser:parse()[1]
res = {}
for pattern, match in cquery:iter_matches(tree:root(), 0, 7, 14, { all = true }) do
-- can't transmit node over RPC. just check the name and range
local mrepr = {}
for cid, nodes in pairs(match) do
for _, node in ipairs(nodes) do
table.insert(mrepr, {cquery.captures[cid], node:type(), node:range()})
table.insert(mrepr, { '@' .. cquery.captures[cid], node:type(), node:range() })
end
end
table.insert(res, {pattern, mrepr})
@ -344,9 +362,9 @@ void ui_refresh(void)
{
1,
{
{ 'funccall', 'expression_statement', 11, 4, 11, 34 },
{ 'funccall', 'expression_statement', 12, 4, 12, 37 },
{ 'funccall', 'expression_statement', 13, 4, 13, 34 },
{ '@funccall', 'expression_statement', 11, 4, 11, 34 },
{ '@funccall', 'expression_statement', 12, 4, 12, 37 },
{ '@funccall', 'expression_statement', 13, 4, 13, 34 },
},
},
}, res)
@ -369,7 +387,7 @@ void ui_refresh(void)
eq('void', res2)
end)
it('support getting text where start of node is one past EOF', function()
it('supports getting text where start of node is one past EOF', function()
local text = [[
def run
a = <<~E
@ -396,7 +414,7 @@ end]]
)
end)
it('support getting empty text if node range is zero width', function()
it('supports getting empty text if node range is zero width', function()
local text = [[
```lua
{}
@ -421,12 +439,16 @@ end]]
it('can match special regex characters like \\ * + ( with `vim-match?`', function()
insert('char* astring = "\\n"; (1 + 1) * 2 != 2;')
---@type table
local res = exec_lua([[
cquery = vim.treesitter.query.parse("c", '([_] @plus (#vim-match? @plus "^\\\\+$"))'..
'([_] @times (#vim-match? @times "^\\\\*$"))'..
'([_] @paren (#vim-match? @paren "^\\\\($"))'..
'([_] @escape (#vim-match? @escape "^\\\\\\\\n$"))'..
'([_] @string (#vim-match? @string "^\\"\\\\\\\\n\\"$"))')
query = (
'([_] @plus (#vim-match? @plus "^\\\\+$"))' ..
'([_] @times (#vim-match? @times "^\\\\*$"))' ..
'([_] @paren (#vim-match? @paren "^\\\\($"))' ..
'([_] @escape (#vim-match? @escape "^\\\\\\\\n$"))' ..
'([_] @string (#vim-match? @string "^\\"\\\\\\\\n\\"$"))'
)
cquery = vim.treesitter.query.parse("c", query)
parser = vim.treesitter.get_parser(0, "c")
tree = parser:parse()[1]
res = {}
@ -435,21 +457,21 @@ end]]
local mrepr = {}
for cid, nodes in pairs(match) do
for _, node in ipairs(nodes) do
table.insert(mrepr, {cquery.captures[cid], node:type(), node:range()})
table.insert(mrepr, { '@' .. cquery.captures[cid], node:type(), node:range() })
end
end
table.insert(res, {pattern, mrepr})
table.insert(res, { pattern, mrepr })
end
return res
]])
eq({
{ 2, { { 'times', '*', 0, 4, 0, 5 } } },
{ 5, { { 'string', 'string_literal', 0, 16, 0, 20 } } },
{ 4, { { 'escape', 'escape_sequence', 0, 17, 0, 19 } } },
{ 3, { { 'paren', '(', 0, 22, 0, 23 } } },
{ 1, { { 'plus', '+', 0, 25, 0, 26 } } },
{ 2, { { 'times', '*', 0, 30, 0, 31 } } },
{ 2, { { '@times', '*', 0, 4, 0, 5 } } },
{ 5, { { '@string', 'string_literal', 0, 16, 0, 20 } } },
{ 4, { { '@escape', 'escape_sequence', 0, 17, 0, 19 } } },
{ 3, { { '@paren', '(', 0, 22, 0, 23 } } },
{ 1, { { '@plus', '+', 0, 25, 0, 26 } } },
{ 2, { { '@times', '*', 0, 30, 0, 31 } } },
}, res)
end)
@ -482,7 +504,7 @@ end]]
-- can't transmit node over RPC. just check the name, range, and text
local text = vim.treesitter.get_node_text(node, 0)
local range = {node:range()}
table.insert(res, {cquery.captures[cid], node:type(), range, text})
table.insert(res, { cquery.captures[cid], node:type(), range, text })
end
return res
end
@ -514,9 +536,7 @@ end]]
}, res1)
end)
it(
'allow loading query with escaped quotes and capture them with `lua-match?` and `vim-match?`',
function()
it('allows loading query with escaped quotes and capture them `#{lua,vim}-match`?', function()
insert('char* astring = "Hello World!";')
local res = exec_lua([[
@ -529,22 +549,21 @@ end]]
local mrepr = {}
for cid, nodes in pairs(match) do
for _, node in ipairs(nodes) do
table.insert(mrepr, {cquery.captures[cid], node:type(), node:range()})
table.insert(mrepr, { '@' .. cquery.captures[cid], node:type(), node:range() })
end
end
table.insert(res, {pattern, mrepr})
table.insert(res, { pattern, mrepr })
end
return res
]])
eq({
{ 1, { { 'quote', '"', 0, 16, 0, 17 } } },
{ 2, { { 'quote', '"', 0, 16, 0, 17 } } },
{ 1, { { 'quote', '"', 0, 29, 0, 30 } } },
{ 2, { { 'quote', '"', 0, 29, 0, 30 } } },
{ 1, { { '@quote', '"', 0, 16, 0, 17 } } },
{ 2, { { '@quote', '"', 0, 16, 0, 17 } } },
{ 1, { { '@quote', '"', 0, 29, 0, 30 } } },
{ 2, { { '@quote', '"', 0, 29, 0, 30 } } },
}, res)
end
)
end)
it('allows to add predicates', function()
insert([[
@ -772,9 +791,9 @@ end]]
for pattern, match in cquery:iter_matches(tree:root(), 0, 7, 14) do
local mrepr = {}
for cid, node in pairs(match) do
table.insert(mrepr, {cquery.captures[cid], node:type(), node:range()})
table.insert(mrepr, { '@' .. cquery.captures[cid], node:type(), node:range() })
end
table.insert(res, {pattern, mrepr})
table.insert(res, { pattern, mrepr })
end
return res
]],
@ -782,20 +801,26 @@ end]]
)
eq({
{ 3, { { 'type', 'primitive_type', 8, 2, 8, 6 } } },
{ 2, { { 'keyword', 'for', 9, 2, 9, 5 } } },
{ 3, { { 'type', 'primitive_type', 9, 7, 9, 13 } } },
{ 4, { { 'fieldarg', 'identifier', 11, 16, 11, 18 } } },
{ 3, { { '@type', 'primitive_type', 8, 2, 8, 6 } } },
{ 2, { { '@keyword', 'for', 9, 2, 9, 5 } } },
{ 3, { { '@type', 'primitive_type', 9, 7, 9, 13 } } },
{ 4, { { '@fieldarg', 'identifier', 11, 16, 11, 18 } } },
{
1,
{ { 'minfunc', 'identifier', 11, 12, 11, 15 }, { 'min_id', 'identifier', 11, 27, 11, 32 } },
{
{ '@minfunc', 'identifier', 11, 12, 11, 15 },
{ '@min_id', 'identifier', 11, 27, 11, 32 },
},
{ 4, { { 'fieldarg', 'identifier', 12, 17, 12, 19 } } },
},
{ 4, { { '@fieldarg', 'identifier', 12, 17, 12, 19 } } },
{
1,
{ { 'minfunc', 'identifier', 12, 13, 12, 16 }, { 'min_id', 'identifier', 12, 29, 12, 35 } },
{
{ '@minfunc', 'identifier', 12, 13, 12, 16 },
{ '@min_id', 'identifier', 12, 29, 12, 35 },
},
{ 4, { { 'fieldarg', 'identifier', 13, 14, 13, 16 } } },
},
{ 4, { { '@fieldarg', 'identifier', 13, 14, 13, 16 } } },
}, res)
end)
@ -892,7 +917,8 @@ end]]
table.insert(nodes, { node:range() })
end
return nodes]],
return nodes
]],
txt
)
@ -918,7 +944,8 @@ end]]
table.insert(nodes, { node:range() })
end
return nodes]],
return nodes
]],
txt
)
@ -927,11 +954,11 @@ end]]
describe('when creating a language tree', function()
local function get_ranges()
return exec_lua([[
return exec_lua [[
local result = {}
parser:for_each_tree(function(tree) table.insert(result, {tree:root():range()}) end)
return result
]])
]]
end
before_each(function()
@ -950,7 +977,11 @@ int x = INT_MAX;
exec_lua([[
parser = vim.treesitter.get_parser(0, "c", {
injections = {
c = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c")) (preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))'}})
c = (
'(preproc_def (preproc_arg) @injection.content (#set! injection.language "c")) ' ..
'(preproc_function_def value: (preproc_arg) @injection.content (#set! injection.language "c"))'
)
}})
parser:parse(true)
]])
@ -983,7 +1014,11 @@ int x = INT_MAX;
exec_lua([[
parser = vim.treesitter.get_parser(0, "c", {
injections = {
c = '(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))'}})
c = (
'(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))'
)
}})
parser:parse(true)
]])
@ -1029,7 +1064,11 @@ int x = INT_MAX;
exec_lua([[
parser = vim.treesitter.get_parser(0, "c", {
injections = {
c = '(preproc_def (preproc_arg) @injection.content (#set! injection.self)) (preproc_function_def value: (preproc_arg) @injection.content (#set! injection.self))'}})
c = (
'(preproc_def (preproc_arg) @injection.content (#set! injection.self)) ' ..
'(preproc_function_def value: (preproc_arg) @injection.content (#set! injection.self))'
)
}})
parser:parse(true)
]])
@ -1062,7 +1101,11 @@ int x = INT_MAX;
exec_lua([[
parser = vim.treesitter.get_parser(0, "c", {
injections = {
c = '(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"))'}})
c = (
'(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"))'
)
}})
parser:parse(true)
]])
@ -1103,7 +1146,10 @@ int x = INT_MAX;
it('should return the correct language tree', function()
local result = exec_lua([[
parser = vim.treesitter.get_parser(0, "c", {
injections = { c = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c"))'}})
injections = {
c = '(preproc_def (preproc_arg) @injection.content (#set! injection.language "c"))'
}
})
parser:parse(true)
local sub_tree = parser:language_for_range({1, 18, 1, 19})
@ -1448,7 +1494,7 @@ int x = INT_MAX;
)
end)
describe('is_valid()', function()
describe('languagetree is_valid()', function()
before_each(function()
insert(dedent [[
Treesitter integration *treesitter*