2020-07-14 16:17:00 +02:00
|
|
|
-- Execute as `nvim --headless -c "luafile ./scripts/check-queries.lua"`
|
2020-09-07 12:07:17 +02:00
|
|
|
|
|
|
|
|
local function extract_captures()
|
|
|
|
|
local lines = vim.fn.readfile("CONTRIBUTING.md")
|
|
|
|
|
local captures = {}
|
|
|
|
|
local current_query
|
|
|
|
|
|
|
|
|
|
for _, line in ipairs(lines) do
|
|
|
|
|
if vim.startswith(line, "### ") then
|
|
|
|
|
current_query = vim.fn.tolower(line:sub(5))
|
|
|
|
|
elseif vim.startswith(line, "@") and current_query then
|
|
|
|
|
if not captures[current_query] then
|
|
|
|
|
captures[current_query] = {}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
table.insert(captures[current_query], vim.split(line:sub(2), " ", true)[1])
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-05-31 12:03:27 -05:00
|
|
|
-- Complete captures for injections.
|
|
|
|
|
local parsers = require 'nvim-treesitter.info'.installed_parsers()
|
|
|
|
|
for _, lang in pairs(parsers) do
|
|
|
|
|
table.insert(captures['injections'], lang)
|
|
|
|
|
end
|
|
|
|
|
|
2020-09-07 12:07:17 +02:00
|
|
|
return captures
|
|
|
|
|
end
|
|
|
|
|
|
2020-07-14 16:17:00 +02:00
|
|
|
local function do_check()
|
2021-03-07 19:36:08 +01:00
|
|
|
local parsers = require 'nvim-treesitter.info'.installed_parsers()
|
2020-07-14 16:17:00 +02:00
|
|
|
local queries = require 'nvim-treesitter.query'
|
2020-07-15 00:38:09 +02:00
|
|
|
local query_types = queries.built_in_query_groups
|
2020-07-14 16:17:00 +02:00
|
|
|
|
2020-09-07 12:07:17 +02:00
|
|
|
local captures = extract_captures()
|
2021-03-04 13:56:40 +01:00
|
|
|
local last_error
|
2020-09-07 12:07:17 +02:00
|
|
|
|
2020-07-14 16:17:00 +02:00
|
|
|
for _, lang in pairs(parsers) do
|
2021-03-07 19:36:08 +01:00
|
|
|
for _, query_type in pairs(query_types) do
|
|
|
|
|
print('Checking '..lang..' '..query_type)
|
2021-05-31 12:03:27 -05:00
|
|
|
local ok, query = pcall(queries.get_query, lang, query_type)
|
2021-03-07 19:36:08 +01:00
|
|
|
if not ok then
|
|
|
|
|
vim.api.nvim_err_writeln(query)
|
|
|
|
|
last_error = query
|
|
|
|
|
else
|
|
|
|
|
if query then
|
|
|
|
|
for _, capture in ipairs(query.captures) do
|
2021-05-31 12:03:27 -05:00
|
|
|
local is_valid = (
|
|
|
|
|
vim.startswith(capture, "_") -- Helpers.
|
|
|
|
|
or vim.tbl_contains(captures[query_type], capture)
|
|
|
|
|
)
|
|
|
|
|
if not is_valid then
|
|
|
|
|
local error = string.format("(x) Invalid capture @%s in %s for %s.", capture, query_type, lang)
|
|
|
|
|
print(error)
|
|
|
|
|
last_error = 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
|
2021-03-04 13:56:40 +01:00
|
|
|
if last_error then
|
2021-03-07 19:10:15 +01:00
|
|
|
print()
|
|
|
|
|
print("Last error: ")
|
2021-03-04 13:56:40 +01:00
|
|
|
error(last_error)
|
|
|
|
|
end
|
2020-07-14 16:17:00 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local ok, err = pcall(do_check)
|
2021-04-22 18:13:09 +02:00
|
|
|
local allowed_to_fail = vim.split(vim.env.ALLOWED_INSTALLATION_FAILURES or '', ",", true)
|
2021-03-22 12:33:10 +01:00
|
|
|
|
|
|
|
|
for k, v in pairs(require 'nvim-treesitter.parsers'.get_parser_configs()) do
|
|
|
|
|
if not require 'nvim-treesitter.parsers'.has_parser(k) then
|
|
|
|
|
-- On CI all parsers that can be installed from C files should be installed
|
2021-04-22 18:13:09 +02:00
|
|
|
if vim.env.CI
|
|
|
|
|
and not v.install_info.requires_generate_from_grammar
|
|
|
|
|
and not vim.tbl_contains(allowed_to_fail, k) then
|
|
|
|
|
|
2021-03-22 12:33:10 +01:00
|
|
|
print('Error: parser for '..k..' is not installed')
|
|
|
|
|
vim.cmd('cq')
|
|
|
|
|
else
|
|
|
|
|
print('Warning: parser for '..k..' is not installed')
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-07-14 16:17:00 +02:00
|
|
|
if ok then
|
|
|
|
|
print('Check successful!\n')
|
|
|
|
|
vim.cmd('q')
|
|
|
|
|
else
|
|
|
|
|
print('Check failed:')
|
|
|
|
|
print(err)
|
|
|
|
|
print('\n')
|
|
|
|
|
vim.cmd('cq')
|
|
|
|
|
end
|