aboutsummaryrefslogtreecommitdiff
path: root/test/functional/treesitter/parser_spec.lua
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2024-01-03 02:09:18 +0100
committerJustin M. Keyes <justinkz@gmail.com>2024-01-03 02:09:29 +0100
commit04f2f864e270e772c6326cefdf24947f0130e492 (patch)
tree46f83f909b888a66c741032ab955afc6eab84292 /test/functional/treesitter/parser_spec.lua
parent59d117ec99b6037cb9fad5bbfb6d0b18f5012927 (diff)
downloadrneovim-04f2f864e270e772c6326cefdf24947f0130e492.tar.gz
rneovim-04f2f864e270e772c6326cefdf24947f0130e492.tar.bz2
rneovim-04f2f864e270e772c6326cefdf24947f0130e492.zip
refactor: format test/*
Diffstat (limited to 'test/functional/treesitter/parser_spec.lua')
-rw-r--r--test/functional/treesitter/parser_spec.lua639
1 files changed, 363 insertions, 276 deletions
diff --git a/test/functional/treesitter/parser_spec.lua b/test/functional/treesitter/parser_spec.lua
index 94642552d9..e63d424622 100644
--- a/test/functional/treesitter/parser_spec.lua
+++ b/test/functional/treesitter/parser_spec.lua
@@ -12,7 +12,7 @@ local is_os = helpers.is_os
describe('treesitter parser API', function()
before_each(function()
clear()
- exec_lua[[
+ exec_lua [[
vim.g.__ts_debug = 1
]]
end)
@@ -30,59 +30,62 @@ describe('treesitter parser API', function()
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")
+ 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(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"))
+ 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(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()"))
+ 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"))
+ 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"))
+ eq(true, exec_lua('return parser:parse()[1] == tree2'))
end)
local test_text = [[
@@ -107,7 +110,7 @@ void ui_refresh(void)
}]]
it('allows to iterate over nodes children', function()
- insert(test_text);
+ insert(test_text)
local res = exec_lua([[
parser = vim.treesitter.get_parser(0, "c")
@@ -122,26 +125,28 @@ void ui_refresh(void)
]])
eq({
- {"primitive_type", "type"},
- {"function_declarator", "declarator"},
- {"compound_statement", "body"}
+ { 'primitive_type', 'type' },
+ { 'function_declarator', 'declarator' },
+ { 'compound_statement', 'body' },
}, res)
end)
it('does not get parser for empty filetype', function()
- insert(test_text);
+ 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)'))
+ 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(test_text);
+ insert(test_text)
local res = exec_lua([[
parser = vim.treesitter.get_parser(0, "c")
@@ -155,7 +160,7 @@ void ui_refresh(void)
return res
]])
- eq({{ "primitive_type", 0, 0, 0, 4 }}, res)
+ eq({ { 'primitive_type', 0, 0, 0, 4 } }, res)
local res_fail = exec_lua([[
parser = vim.treesitter.get_parser(0, "c")
@@ -173,7 +178,7 @@ void ui_refresh(void)
(field_expression argument: (identifier) @fieldarg)
]]
- it("supports runtime queries", function()
+ it('supports runtime queries', function()
local ret = exec_lua [[
return vim.treesitter.query.get("c", "highlights").captures[1]
]]
@@ -181,10 +186,11 @@ void ui_refresh(void)
eq('variable', ret)
end)
- it("supports caching queries", function()
+ it('supports caching queries', function()
local long_query = query:rep(100)
local function q(n)
- return exec_lua ([[
+ return exec_lua(
+ [[
local query, n = ...
local before = vim.uv.hrtime()
for i=1,n,1 do
@@ -192,7 +198,10 @@ void ui_refresh(void)
end
local after = vim.uv.hrtime()
return after - before
- ]], long_query, n)
+ ]],
+ long_query,
+ n
+ )
end
local firstrun = q(1)
@@ -200,13 +209,17 @@ void ui_refresh(void)
-- First run should be at least 200x slower than an 100 subsequent runs.
local factor = is_os('win') and 100 or 200
- assert(factor * manyruns < firstrun, ('firstrun: %f ms, manyruns: %f ms'):format(firstrun / 1e6, manyruns / 1e6))
+ assert(
+ factor * manyruns < firstrun,
+ ('firstrun: %f ms, manyruns: %f ms'):format(firstrun / 1e6, manyruns / 1e6)
+ )
end)
it('support query and iter by capture', function()
insert(test_text)
- local res = exec_lua([[
+ local res = exec_lua(
+ [[
cquery = vim.treesitter.query.parse("c", ...)
parser = vim.treesitter.get_parser(0, "c")
tree = parser:parse()[1]
@@ -216,26 +229,29 @@ void ui_refresh(void)
table.insert(res, {cquery.captures[cid], node:type(), node:range()})
end
return res
- ]], query)
+ ]],
+ query
+ )
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 },
+ { '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 },
}, res)
end)
it('support query and iter by match', function()
insert(test_text)
- local res = exec_lua([[
+ local res = exec_lua(
+ [[
cquery = vim.treesitter.query.parse("c", ...)
parser = vim.treesitter.get_parser(0, "c")
tree = parser:parse()[1]
@@ -249,17 +265,25 @@ void ui_refresh(void)
table.insert(res, {pattern, mrepr})
end
return res
- ]], query)
+ ]],
+ 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 } } }
+ { 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)
@@ -286,7 +310,9 @@ def run
a = <<~E
end]]
insert(text)
- eq('', exec_lua[[
+ eq(
+ '',
+ exec_lua [[
local fake_node = {}
function fake_node:start()
return 3, 0, 23
@@ -301,7 +327,8 @@ end]]
return 3, 0, 3, 0
end
return vim.treesitter.get_node_text(fake_node, 0)
- ]])
+ ]]
+ )
end)
it('support getting empty text if node range is zero width', function()
@@ -350,12 +377,12 @@ end]]
]])
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)
@@ -394,32 +421,38 @@ end]]
end
]])
- local res0 = exec_lua([[return get_query_result(...)]],
- [[((primitive_type) @c-keyword (#any-of? @c-keyword "int" "float"))]])
+ local res0 = exec_lua(
+ [[return get_query_result(...)]],
+ [[((primitive_type) @c-keyword (#any-of? @c-keyword "int" "float"))]]
+ )
eq({
- { "c-keyword", "primitive_type", { 2, 2, 2, 5 }, "int" },
- { "c-keyword", "primitive_type", { 3, 4, 3, 7 }, "int" },
+ { 'c-keyword', 'primitive_type', { 2, 2, 2, 5 }, 'int' },
+ { 'c-keyword', 'primitive_type', { 3, 4, 3, 7 }, 'int' },
}, res0)
- local res1 = exec_lua([[return get_query_result(...)]],
+ local res1 = exec_lua(
+ [[return get_query_result(...)]],
[[
((string_literal) @fizzbuzz-strings (#any-of? @fizzbuzz-strings
"\"number= %d FizzBuzz\\n\""
"\"number= %d Fizz\\n\""
"\"number= %d Buzz\\n\""
))
- ]])
+ ]]
+ )
eq({
- { "fizzbuzz-strings", "string_literal", { 6, 15, 6, 38 }, "\"number= %d FizzBuzz\\n\""},
- { "fizzbuzz-strings", "string_literal", { 8, 15, 8, 34 }, "\"number= %d Fizz\\n\""},
- { "fizzbuzz-strings", "string_literal", { 10, 15, 10, 34 }, "\"number= %d Buzz\\n\""},
+ { 'fizzbuzz-strings', 'string_literal', { 6, 15, 6, 38 }, '"number= %d FizzBuzz\\n"' },
+ { 'fizzbuzz-strings', 'string_literal', { 8, 15, 8, 34 }, '"number= %d Fizz\\n"' },
+ { 'fizzbuzz-strings', 'string_literal', { 10, 15, 10, 34 }, '"number= %d Buzz\\n"' },
}, res1)
end)
- it('allow loading query with escaped quotes and capture them with `lua-match?` and `vim-match?`', function()
- insert('char* astring = "Hello World!";')
+ it(
+ 'allow loading query with escaped quotes and capture them with `lua-match?` and `vim-match?`',
+ function()
+ insert('char* astring = "Hello World!";')
- local res = exec_lua([[
+ local res = exec_lua([[
cquery = vim.treesitter.query.parse("c", '([_] @quote (#vim-match? @quote "^\\"$")) ([_] @quote (#lua-match? @quote "^\\"$"))')
parser = vim.treesitter.get_parser(0, "c")
tree = parser:parse()[1]
@@ -435,13 +468,14 @@ 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 } } },
- }, res)
- end)
+ 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 } } },
+ }, res)
+ end
+ )
it('allows to add predicates', function()
insert([[
@@ -450,9 +484,10 @@ end]]
}
]])
- local custom_query = "((identifier) @main (#is-main? @main))"
+ local custom_query = '((identifier) @main (#is-main? @main))'
- local res = exec_lua([[
+ local res = exec_lua(
+ [[
local query = vim.treesitter.query
local function is_main(match, pattern, bufnr, predicate)
@@ -473,11 +508,13 @@ end]]
end
return nodes
- ]], custom_query)
+ ]],
+ custom_query
+ )
- eq({{0, 4, 0, 8}}, res)
+ eq({ { 0, 4, 0, 8 } }, res)
- local res_list = exec_lua[[
+ local res_list = exec_lua [[
local query = vim.treesitter.query
local list = query.list_predicates()
@@ -487,7 +524,17 @@ end]]
return list
]]
- eq({ 'any-of?', 'contains?', 'eq?', 'has-ancestor?', 'has-parent?', 'is-main?', 'lua-match?', 'match?', 'vim-match?' }, res_list)
+ eq({
+ 'any-of?',
+ 'contains?',
+ 'eq?',
+ 'has-ancestor?',
+ 'has-parent?',
+ 'is-main?',
+ 'lua-match?',
+ 'match?',
+ 'vim-match?',
+ }, res_list)
end)
it('allows to set simple ranges', function()
@@ -498,7 +545,7 @@ end]]
return { parser:parse()[1]:root():range() }
]]
- eq({0, 0, 19, 0}, res)
+ 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)
@@ -509,7 +556,7 @@ end]]
return { parser:parse(true)[1]:root():range() }
]]
- eq({0, 0, 18, 1}, res2)
+ eq({ 0, 0, 18, 1 }, res2)
eq({ { { 0, 0, 0, 18, 1, 512 } } }, exec_lua [[ return parser:included_regions() ]])
@@ -522,7 +569,7 @@ end]]
eq(range_tbl, { { { 0, 0, 0, 17, 1, 508 } } })
end)
- it("allows to set complex ranges", function()
+ it('allows to set complex ranges', function()
insert(test_text)
local res = exec_lua [[
@@ -552,10 +599,11 @@ end]]
{ 8, 2, 8, 33 },
{ 9, 7, 9, 20 },
{ 10, 4, 10, 20 },
- { 14, 9, 14, 27 } }, res)
+ { 14, 9, 14, 27 },
+ }, res)
end)
- it("allows to create string parsers", function()
+ it('allows to create string parsers', function()
local ret = exec_lua [[
local parser = vim.treesitter.get_string_parser("int foo = 42;", "c")
return { parser:parse()[1]:root():range() }
@@ -564,13 +612,14 @@ end]]
eq({ 0, 0, 0, 13 }, ret)
end)
- it("allows to run queries with string parsers", function()
+ it('allows to run queries with string parsers', function()
local txt = [[
int foo = 42;
int bar = 13;
]]
- local ret = exec_lua([[
+ local ret = exec_lua(
+ [[
local str = ...
local parser = vim.treesitter.get_string_parser(str, "c")
@@ -581,18 +630,21 @@ end]]
table.insert(nodes, { node:range() })
end
- return nodes]], txt)
+ return nodes]],
+ txt
+ )
- eq({ {0, 10, 0, 13} }, ret)
+ eq({ { 0, 10, 0, 13 } }, ret)
end)
- it("should use node range when omitted", function()
+ it('should use node range when omitted', function()
local txt = [[
int foo = 42;
int bar = 13;
]]
- local ret = exec_lua([[
+ local ret = exec_lua(
+ [[
local str = ...
local parser = vim.treesitter.get_string_parser(str, "c")
@@ -604,12 +656,14 @@ end]]
table.insert(nodes, { node:range() })
end
- return nodes]], txt)
+ return nodes]],
+ txt
+ )
- eq({ {1, 10, 1, 13} }, ret)
+ eq({ { 1, 10, 1, 13 } }, ret)
end)
- describe("when creating a language tree", function()
+ describe('when creating a language tree', function()
local function get_ranges()
return exec_lua([[
local result = {}
@@ -629,8 +683,8 @@ int x = INT_MAX;
]])
end)
- describe("when parsing regions independently", function()
- it("should inject a language", function()
+ describe('when parsing regions independently', function()
+ it('should inject a language', function()
exec_lua([[
parser = vim.treesitter.get_parser(0, "c", {
injections = {
@@ -638,32 +692,32 @@ int x = INT_MAX;
parser:parse(true)
]])
- eq("table", exec_lua("return type(parser:children().c)"))
- eq(5, exec_lua("return #parser:children().c:trees()"))
+ 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))
+ { 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())
helpers.feed('ggo<esc>')
- eq(5, exec_lua("return #parser:children().c:trees()"))
+ 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))
+ { 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)
- describe("when parsing regions combined", function()
- it("should inject a language", function()
+ describe('when parsing regions combined', function()
+ it('should inject a language', function()
exec_lua([[
parser = vim.treesitter.get_parser(0, "c", {
injections = {
@@ -671,45 +725,45 @@ int x = INT_MAX;
parser:parse(true)
]])
- eq("table", exec_lua("return type(parser:children().c)"))
- eq(2, exec_lua("return #parser:children().c:trees()"))
+ 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
- {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))
+ { 0, 0, 7, 0 }, -- root tree
+ { 3, 14, 5, 18 }, -- VALUE 123
+ -- VALUE1 123
+ -- VALUE2 123
+ { 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))
}, get_ranges())
helpers.feed('ggo<esc>')
- eq("table", exec_lua("return type(parser:children().c)"))
- eq(2, exec_lua("return #parser:children().c:trees()"))
+ 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
- {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))
+ { 0, 0, 8, 0 }, -- root tree
+ { 4, 14, 6, 18 }, -- VALUE 123
+ -- VALUE1 123
+ -- VALUE2 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())
helpers.feed('7ggI//<esc>')
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('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))
+ { 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())
end)
end)
- describe("when using injection.self", function()
- it("should inject the source language", function()
+ describe('when using injection.self', function()
+ it('should inject the source language', function()
exec_lua([[
parser = vim.treesitter.get_parser(0, "c", {
injections = {
@@ -717,32 +771,32 @@ int x = INT_MAX;
parser:parse(true)
]])
- eq("table", exec_lua("return type(parser:children().c)"))
- eq(5, exec_lua("return #parser:children().c:trees()"))
+ 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))
+ { 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())
helpers.feed('ggo<esc>')
- eq(5, exec_lua("return #parser:children().c:trees()"))
+ 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))
+ { 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)
- describe("when using the offset directive", function()
- it("should shift the range by the directive amount", function()
+ describe('when using the offset directive', function()
+ it('should shift the range by the directive amount', function()
exec_lua([[
parser = vim.treesitter.get_parser(0, "c", {
injections = {
@@ -750,18 +804,18 @@ int x = INT_MAX;
parser:parse(true)
]])
- eq("table", exec_lua("return type(parser:children().c)"))
+ eq('table', exec_lua('return type(parser:children().c)'))
eq({
- {0, 0, 7, 0}, -- root tree
- {3, 16, 3, 16}, -- VALUE 123
- {4, 17, 4, 17}, -- VALUE1 123
- {5, 17, 5, 17}, -- 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))
+ { 0, 0, 7, 0 }, -- root tree
+ { 3, 16, 3, 16 }, -- VALUE 123
+ { 4, 17, 4, 17 }, -- VALUE1 123
+ { 5, 17, 5, 17 }, -- 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())
end)
- it("should list all directives", function()
- local res_list = exec_lua[[
+ it('should list all directives', function()
+ local res_list = exec_lua [[
local query = vim.treesitter.query
local list = query.list_directives()
@@ -776,7 +830,7 @@ int x = INT_MAX;
end)
end)
- describe("when getting the language for a range", function()
+ describe('when getting the language for a range', function()
before_each(function()
insert([[
int x = INT_MAX;
@@ -784,7 +838,7 @@ int x = INT_MAX;
]])
end)
- it("should return the correct language tree", function()
+ 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"))'}})
@@ -799,9 +853,9 @@ int x = INT_MAX;
end)
end)
- describe("when getting/setting match data", function()
- describe("when setting for the whole match", function()
- it("should set/get the data correctly", function()
+ 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;
]])
@@ -819,11 +873,11 @@ int x = INT_MAX;
return result
]])
- eq(result, "value")
+ eq(result, 'value')
end)
- describe("when setting a key on a capture", function()
- it("it should create the nested table", function()
+ describe('when setting a key on a capture', function()
+ it('it should create the nested table', function()
insert([[
int x = 3;
]])
@@ -842,10 +896,10 @@ int x = INT_MAX;
end
]])
- eq(result, "value")
+ eq(result, 'value')
end)
- it("it should not overwrite the nested table", function()
+ it('it should not overwrite the nested table', function()
insert([[
int x = 3;
]])
@@ -864,8 +918,8 @@ int x = INT_MAX;
end
]])
local expected = {
- ["key"] = "value",
- ["key2"] = "value2",
+ ['key'] = 'value',
+ ['key2'] = 'value2',
}
eq(expected, result)
@@ -890,7 +944,8 @@ int x = INT_MAX;
]])
local function run_query()
- return exec_lua([[
+ return exec_lua(
+ [[
local query = vim.treesitter.query.parse("c", ...)
parser = vim.treesitter.get_parser()
tree = parser:parse()[1]
@@ -899,23 +954,24 @@ int x = INT_MAX;
table.insert(res, {query.captures[id], node:range()})
end
return res
- ]], query0)
+ ]],
+ query0
+ )
end
eq({
{ 'function', 0, 0, 2, 1 },
- { 'declaration', 1, 2, 1, 12 }
+ { 'declaration', 1, 2, 1, 12 },
}, run_query())
- helpers.command'normal ggO'
+ helpers.command 'normal ggO'
insert('int a;')
eq({
{ 'declaration', 0, 0, 0, 6 },
{ 'function', 1, 0, 3, 1 },
- { 'declaration', 2, 2, 2, 12 }
+ { 'declaration', 2, 2, 2, 12 },
}, run_query())
-
end)
it('handles ranges when source is a multiline string (#20419)', function()
@@ -938,7 +994,8 @@ int x = INT_MAX;
]]
]==]
- local r = exec_lua([[
+ local r = exec_lua(
+ [[
local parser = vim.treesitter.get_string_parser(..., 'lua')
parser:parse(true)
local ranges = {}
@@ -946,19 +1003,22 @@ int x = INT_MAX;
ranges[tree:lang()] = { tstree:root():range(true) }
end)
return ranges
- ]], source)
+ ]],
+ source
+ )
eq({
lua = { 0, 6, 6, 16, 4, 438 },
query = { 6, 20, 113, 15, 6, 431 },
- vim = { 1, 0, 16, 4, 6, 89 }
+ 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.
- local rb = exec_lua([[
+ local rb = exec_lua(
+ [[
local r, source = ...
local add_bytes = require('vim.treesitter._range').add_bytes
for lang, range in pairs(r) do
@@ -966,13 +1026,15 @@ int x = INT_MAX;
r[lang] = add_bytes(source, r[lang])
end
return r
- ]], r, source)
+ ]],
+ r,
+ source
+ )
eq(rb, r)
-
end)
- it("does not produce empty injection ranges (#23409)", function()
+ it('does not produce empty injection ranges (#23409)', function()
insert [[
Examples: >lua
local a = {}
@@ -989,7 +1051,7 @@ int x = INT_MAX;
parser1:parse(true)
]]
- eq(0, exec_lua("return #vim.tbl_keys(parser1:children())"))
+ eq(0, exec_lua('return #vim.tbl_keys(parser1:children())'))
exec_lua [[
parser2 = require('vim.treesitter.languagetree').new(0, "vimdoc", {
@@ -1000,13 +1062,12 @@ int x = INT_MAX;
parser2:parse(true)
]]
- 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()"))
-
+ 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)
- it("parsers injections incrementally", function()
- insert(dedent[[
+ it('parsers injections incrementally', function()
+ insert(dedent [[
>lua
local a = {}
<
@@ -1045,26 +1106,38 @@ int x = INT_MAX;
]]
--- Do not parse injections by default
- eq(0, exec_lua [[
+ eq(
+ 0,
+ exec_lua [[
parser:parse()
return #vim.tbl_keys(parser:children())
- ]])
+ ]]
+ )
--- Only parse injections between lines 0, 2
- eq(1, exec_lua [[
+ eq(
+ 1,
+ exec_lua [[
parser:parse({0, 2})
return #parser:children().lua:trees()
- ]])
+ ]]
+ )
- eq(2, exec_lua [[
+ eq(
+ 2,
+ exec_lua [[
parser:parse({2, 6})
return #parser:children().lua:trees()
- ]])
+ ]]
+ )
- eq(7, exec_lua [[
+ eq(
+ 7,
+ exec_lua [[
parser:parse(true)
return #parser:children().lua:trees()
- ]])
+ ]]
+ )
end)
it('fails to load queries', function()
@@ -1074,43 +1147,48 @@ int x = INT_MAX;
-- Invalid node type
test(
- '.../query.lua:0: Query error at 1:2. Invalid node type "dentifier":\n'..
- '(dentifier) @variable\n'..
- ' ^',
- '(dentifier) @variable')
+ '.../query.lua:0: Query error at 1:2. Invalid node type "dentifier":\n'
+ .. '(dentifier) @variable\n'
+ .. ' ^',
+ '(dentifier) @variable'
+ )
-- Impossible pattern
test(
- '.../query.lua:0: Query error at 1:13. Impossible pattern:\n'..
- '(identifier (identifier) @variable)\n'..
- ' ^',
- '(identifier (identifier) @variable)')
+ '.../query.lua:0: Query error at 1:13. Impossible pattern:\n'
+ .. '(identifier (identifier) @variable)\n'
+ .. ' ^',
+ '(identifier (identifier) @variable)'
+ )
-- Invalid syntax
test(
- '.../query.lua:0: Query error at 1:13. Invalid syntax:\n'..
- '(identifier @variable\n'..
- ' ^',
- '(identifier @variable')
+ '.../query.lua:0: Query error at 1:13. Invalid syntax:\n'
+ .. '(identifier @variable\n'
+ .. ' ^',
+ '(identifier @variable'
+ )
-- Invalid field name
test(
- '.../query.lua:0: Query error at 1:15. Invalid field name "invalid_field":\n'..
- '((identifier) invalid_field: (identifier))\n'..
- ' ^',
- '((identifier) invalid_field: (identifier))')
+ '.../query.lua:0: Query error at 1:15. Invalid field name "invalid_field":\n'
+ .. '((identifier) invalid_field: (identifier))\n'
+ .. ' ^',
+ '((identifier) invalid_field: (identifier))'
+ )
-- Invalid capture name
test(
- '.../query.lua:0: Query error at 3:2. Invalid capture name "ok.capture":\n'..
- '@ok.capture\n'..
- ' ^',
- '((identifier) @id \n(#eq? @id\n@ok.capture\n))')
+ '.../query.lua:0: Query error at 3:2. Invalid capture name "ok.capture":\n'
+ .. '@ok.capture\n'
+ .. ' ^',
+ '((identifier) @id \n(#eq? @id\n@ok.capture\n))'
+ )
end)
describe('is_valid()', function()
before_each(function()
- insert(dedent[[
+ insert(dedent [[
Treesitter integration *treesitter*
Nvim integrates the `tree-sitter` library for incremental parsing of buffers:
@@ -1149,7 +1227,7 @@ int x = INT_MAX;
describe('when adding content with injections', function()
before_each(function()
feed('G')
- insert(dedent[[
+ insert(dedent [[
>lua
local a = {}
<
@@ -1168,23 +1246,29 @@ int x = INT_MAX;
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)
-
- 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)
+ 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
+ )
+
+ 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[[
+ insert(dedent [[
>lua
local a = {}
<
@@ -1217,11 +1301,14 @@ int x = INT_MAX;
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)
+ 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)
end)