2023-08-15 20:28:55 +03:00
|
|
|
#!/usr/bin/env -S nvim -l
|
2023-06-12 09:54:30 -06:00
|
|
|
vim.opt.runtimepath:append('.')
|
2020-09-07 12:07:17 +02:00
|
|
|
|
2022-01-31 10:00:07 +01:00
|
|
|
-- Equivalent to print(), but this will ensure consistent output regardless of
|
|
|
|
|
-- operating system.
|
|
|
|
|
local function io_print(text)
|
2022-02-04 12:02:01 +01:00
|
|
|
if not text then
|
2023-06-12 09:54:30 -06:00
|
|
|
text = ''
|
2022-02-04 12:02:01 +01:00
|
|
|
end
|
2023-06-12 09:54:30 -06:00
|
|
|
io.write(text, '\n')
|
2022-01-31 10:00:07 +01:00
|
|
|
end
|
|
|
|
|
|
2020-09-07 12:07:17 +02:00
|
|
|
local function extract_captures()
|
|
|
|
|
local captures = {}
|
|
|
|
|
local current_query
|
|
|
|
|
|
2023-06-12 09:54:30 -06:00
|
|
|
for line in io.lines('CONTRIBUTING.md') do
|
|
|
|
|
if vim.startswith(line, '### ') then
|
|
|
|
|
current_query = line:sub(5):lower()
|
|
|
|
|
elseif vim.startswith(line, '@') and current_query then
|
2020-09-07 12:07:17 +02:00
|
|
|
if not captures[current_query] then
|
|
|
|
|
captures[current_query] = {}
|
|
|
|
|
end
|
|
|
|
|
|
2023-06-12 09:54:30 -06:00
|
|
|
table.insert(captures[current_query], vim.split(line:sub(2), ' ', true)[1])
|
2020-09-07 12:07:17 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-05-31 12:03:27 -05:00
|
|
|
-- Complete captures for injections.
|
2023-06-12 09:54:30 -06:00
|
|
|
local parsers = vim.tbl_keys(require('nvim-treesitter.parsers').configs)
|
2021-05-31 12:03:27 -05:00
|
|
|
for _, lang in pairs(parsers) do
|
2023-06-12 09:54:30 -06:00
|
|
|
table.insert(captures['injections'], lang)
|
2021-05-31 12:03:27 -05:00
|
|
|
end
|
|
|
|
|
|
2020-09-07 12:07:17 +02:00
|
|
|
return captures
|
|
|
|
|
end
|
|
|
|
|
|
2020-07-14 16:17:00 +02:00
|
|
|
local function do_check()
|
2022-01-07 21:38:33 +01:00
|
|
|
local timings = {}
|
2023-06-12 09:54:30 -06:00
|
|
|
local parsers = require('nvim-treesitter.config').installed_parsers()
|
|
|
|
|
local query_types = require('nvim-treesitter.health').bundled_queries
|
2020-07-14 16:17:00 +02:00
|
|
|
|
2020-09-07 12:07:17 +02:00
|
|
|
local captures = extract_captures()
|
2024-01-04 19:18:49 +01:00
|
|
|
local errors = {}
|
2020-09-07 12:07:17 +02:00
|
|
|
|
2023-06-12 09:54:30 -06:00
|
|
|
io_print('::group::Check parsers')
|
2022-01-30 22:21:31 +01:00
|
|
|
|
2020-07-14 16:17:00 +02:00
|
|
|
for _, lang in pairs(parsers) do
|
2022-04-15 16:33:01 +02:00
|
|
|
timings[lang] = {}
|
|
|
|
|
for _, query_type in pairs(query_types) do
|
|
|
|
|
local before = vim.loop.hrtime()
|
2023-06-12 09:54:30 -06:00
|
|
|
local ok, query = pcall(vim.treesitter.query.get, lang, query_type)
|
2022-04-15 16:33:01 +02:00
|
|
|
local after = vim.loop.hrtime()
|
|
|
|
|
local duration = after - before
|
|
|
|
|
table.insert(timings, { duration = duration, lang = lang, query_type = query_type })
|
2023-06-12 09:54:30 -06:00
|
|
|
io_print(
|
|
|
|
|
'Checking ' .. lang .. ' ' .. query_type .. string.format(' (%.02fms)', duration * 1e-6)
|
|
|
|
|
)
|
2022-04-15 16:33:01 +02:00
|
|
|
if not ok then
|
2023-06-12 09:54:30 -06:00
|
|
|
local err_msg = lang .. ' (' .. query_type .. '): ' .. query
|
2024-01-04 19:18:49 +01:00
|
|
|
errors[#errors + 1] = err_msg
|
2022-04-15 16:33:01 +02:00
|
|
|
else
|
|
|
|
|
if query then
|
|
|
|
|
for _, capture in ipairs(query.captures) do
|
|
|
|
|
local is_valid = (
|
2023-06-12 09:54:30 -06:00
|
|
|
vim.startswith(capture, '_') -- Helpers.
|
|
|
|
|
or vim.list_contains(captures[query_type], capture)
|
2022-04-15 16:33:01 +02:00
|
|
|
)
|
|
|
|
|
if not is_valid then
|
2023-06-12 09:54:30 -06:00
|
|
|
local error =
|
|
|
|
|
string.format('(x) Invalid capture @%s in %s for %s.', capture, query_type, lang)
|
2024-01-04 19:18:49 +01:00
|
|
|
errors[#errors + 1] = error
|
2021-03-04 13:56:40 +01:00
|
|
|
end
|
2020-09-07 12:07:17 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2020-07-14 16:17:00 +02:00
|
|
|
end
|
|
|
|
|
end
|
2022-01-30 22:21:31 +01:00
|
|
|
|
2023-06-12 09:54:30 -06:00
|
|
|
io_print('::endgroup::')
|
2022-01-30 22:21:31 +01:00
|
|
|
|
2024-01-04 19:18:49 +01:00
|
|
|
if #errors > 0 then
|
2023-06-12 09:54:30 -06:00
|
|
|
io_print('\nCheck failed!\nErrors:')
|
2024-01-04 19:18:49 +01:00
|
|
|
for _, err in ipairs(errors) do
|
|
|
|
|
print(err)
|
|
|
|
|
end
|
|
|
|
|
error()
|
2021-03-04 13:56:40 +01:00
|
|
|
end
|
2022-01-07 21:38:33 +01:00
|
|
|
return timings
|
2020-07-14 16:17:00 +02:00
|
|
|
end
|
|
|
|
|
|
2022-01-07 21:38:33 +01:00
|
|
|
local ok, result = pcall(do_check)
|
2023-06-12 09:54:30 -06:00
|
|
|
local allowed_to_fail = vim.split(vim.env.ALLOWED_INSTALLATION_FAILURES or '', ',', true)
|
2021-03-22 12:33:10 +01:00
|
|
|
|
2023-06-12 09:54:30 -06:00
|
|
|
for k, v in pairs(require('nvim-treesitter.parsers').configs) do
|
2023-05-29 16:52:20 +02:00
|
|
|
if v.install_info then
|
|
|
|
|
-- skip "query only" languages
|
|
|
|
|
if #vim.api.nvim_get_runtime_file('parser/' .. k .. '.*', false) == 0 then
|
|
|
|
|
-- On CI all parsers that can be installed from C files should be installed
|
|
|
|
|
if
|
|
|
|
|
vim.env.CI
|
|
|
|
|
and not v.install_info.requires_generate_from_grammar
|
|
|
|
|
and not vim.list_contains(allowed_to_fail, k)
|
|
|
|
|
then
|
|
|
|
|
io_print('Error: parser for ' .. k .. ' is not installed')
|
|
|
|
|
vim.cmd('cq')
|
|
|
|
|
else
|
|
|
|
|
io_print('Warning: parser for ' .. k .. ' is not installed')
|
|
|
|
|
end
|
2021-03-22 12:33:10 +01:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-07-14 16:17:00 +02:00
|
|
|
if ok then
|
2023-06-12 09:54:30 -06:00
|
|
|
io_print('::group::Timings')
|
2022-01-07 21:38:33 +01:00
|
|
|
table.sort(result, function(a, b)
|
|
|
|
|
return a.duration < b.duration
|
|
|
|
|
end)
|
|
|
|
|
for i, val in ipairs(result) do
|
2023-06-12 09:54:30 -06:00
|
|
|
io_print(
|
|
|
|
|
string.format(
|
|
|
|
|
'%i. %.02fms %s %s',
|
|
|
|
|
#result - i + 1,
|
|
|
|
|
val.duration * 1e-6,
|
|
|
|
|
val.lang,
|
|
|
|
|
val.query_type
|
|
|
|
|
)
|
|
|
|
|
)
|
2022-01-07 21:38:33 +01:00
|
|
|
end
|
2023-06-12 09:54:30 -06:00
|
|
|
io_print('::endgroup::')
|
|
|
|
|
io_print('Check successful!')
|
2020-07-14 16:17:00 +02:00
|
|
|
else
|
2023-06-12 09:54:30 -06:00
|
|
|
vim.cmd('cq')
|
2020-07-14 16:17:00 +02:00
|
|
|
end
|