mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-01 19:17:02 -04:00
feat: improve check-queries
This commit is contained in:
parent
178c6a84c1
commit
e77506bde3
1 changed files with 39 additions and 58 deletions
|
|
@ -1,18 +1,15 @@
|
||||||
#!/usr/bin/env -S nvim -l
|
#!/usr/bin/env -S nvim -l
|
||||||
vim.opt.runtimepath:append('.')
|
vim.opt.runtimepath:append('.')
|
||||||
|
|
||||||
-- Equivalent to print(), but this will ensure consistent output regardless of
|
local query_types = require('nvim-treesitter.health').bundled_queries
|
||||||
-- operating system.
|
local configs = require('nvim-treesitter.parsers').configs
|
||||||
local function io_print(text)
|
local parsers = #_G.arg > 0 and { unpack(_G.arg) }
|
||||||
if not text then
|
or require('nvim-treesitter.config').installed_parsers()
|
||||||
text = ''
|
|
||||||
end
|
|
||||||
io.write(text, '\n')
|
|
||||||
end
|
|
||||||
|
|
||||||
local function extract_captures()
|
-- Extract captures from documentation for validation
|
||||||
local captures = {}
|
local captures = {}
|
||||||
local current_query
|
do
|
||||||
|
local current_query ---@type string
|
||||||
|
|
||||||
for line in io.lines('CONTRIBUTING.md') do
|
for line in io.lines('CONTRIBUTING.md') do
|
||||||
if vim.startswith(line, '### ') then
|
if vim.startswith(line, '### ') then
|
||||||
|
|
@ -22,45 +19,33 @@ local function extract_captures()
|
||||||
captures[current_query] = {}
|
captures[current_query] = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
table.insert(captures[current_query], vim.split(line:sub(2), ' ', true)[1])
|
table.insert(captures[current_query], vim.split(line:sub(2), ' ')[1])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Complete captures for injections.
|
-- Complete captures for injections.
|
||||||
local parsers = vim.tbl_keys(require('nvim-treesitter.parsers').configs)
|
for _, lang in pairs(vim.tbl_keys(configs)) do
|
||||||
for _, lang in pairs(parsers) do
|
|
||||||
table.insert(captures['injections'], lang)
|
table.insert(captures['injections'], lang)
|
||||||
end
|
end
|
||||||
|
|
||||||
return captures
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function do_check()
|
-- Check queries for each installed parser in parsers
|
||||||
local timings = {}
|
local errors = {} ---@type string[]
|
||||||
local parsers = require('nvim-treesitter.config').installed_parsers()
|
local timings = {} ---@type number[][]
|
||||||
local query_types = require('nvim-treesitter.health').bundled_queries
|
do
|
||||||
local configs = require('nvim-treesitter.parsers').configs
|
print('::group::Check parsers')
|
||||||
|
|
||||||
local captures = extract_captures()
|
|
||||||
local errors = {}
|
|
||||||
|
|
||||||
io_print('::group::Check parsers')
|
|
||||||
|
|
||||||
for _, lang in pairs(parsers) do
|
for _, lang in pairs(parsers) do
|
||||||
if configs[lang].install_info then
|
if configs[lang] and configs[lang].install_info then
|
||||||
timings[lang] = {}
|
timings[lang] = {}
|
||||||
for _, query_type in pairs(query_types) do
|
for _, query_type in pairs(query_types) do
|
||||||
local before = vim.uv.hrtime()
|
local before = vim.uv.hrtime()
|
||||||
local ok, query = pcall(vim.treesitter.query.get, lang, query_type)
|
local ok, query = pcall(vim.treesitter.query.get, lang, query_type)
|
||||||
local after = vim.uv.hrtime()
|
local duration = vim.uv.hrtime() - before
|
||||||
local duration = after - before
|
|
||||||
table.insert(timings, { duration = duration, lang = lang, query_type = query_type })
|
table.insert(timings, { duration = duration, lang = lang, query_type = query_type })
|
||||||
io_print(
|
print(string.format('Checking %s %s (%.02fms)', lang, query_type, duration * 1e-6))
|
||||||
'Checking ' .. lang .. ' ' .. query_type .. string.format(' (%.02fms)', duration * 1e-6)
|
|
||||||
)
|
|
||||||
if not ok then
|
if not ok then
|
||||||
local err_msg = lang .. ' (' .. query_type .. '): ' .. query
|
errors[#errors + 1] = string.format('%s (%s): %s', lang, query_type, query)
|
||||||
errors[#errors + 1] = err_msg
|
|
||||||
else
|
else
|
||||||
if query then
|
if query then
|
||||||
for _, capture in ipairs(query.captures) do
|
for _, capture in ipairs(query.captures) do
|
||||||
|
|
@ -69,9 +54,8 @@ local function do_check()
|
||||||
or vim.list_contains(captures[query_type], capture)
|
or vim.list_contains(captures[query_type], capture)
|
||||||
)
|
)
|
||||||
if not is_valid then
|
if not is_valid then
|
||||||
local error =
|
errors[#errors + 1] =
|
||||||
string.format('(x) Invalid capture @%s in %s for %s.', capture, query_type, lang)
|
string.format('%s (%s): invalid capture "@%s"', lang, query_type, capture)
|
||||||
errors[#errors + 1] = error
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -80,37 +64,34 @@ local function do_check()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
io_print('::endgroup::')
|
print('::endgroup::')
|
||||||
|
|
||||||
if #errors > 0 then
|
|
||||||
io_print('\nCheck failed!\nErrors:')
|
|
||||||
for _, err in ipairs(errors) do
|
|
||||||
print(err)
|
|
||||||
end
|
|
||||||
error()
|
|
||||||
end
|
|
||||||
return timings
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local ok, result = pcall(do_check)
|
-- Output
|
||||||
if ok then
|
if #errors > 0 then
|
||||||
io_print('::group::Timings')
|
print('::group::Errors')
|
||||||
table.sort(result, function(a, b)
|
for _, err in ipairs(errors) do
|
||||||
|
print(err)
|
||||||
|
end
|
||||||
|
print('::endgroup::')
|
||||||
|
print('Check failed!\n')
|
||||||
|
vim.cmd.cq()
|
||||||
|
else
|
||||||
|
print('::group::Timings')
|
||||||
|
table.sort(timings, function(a, b)
|
||||||
return a.duration < b.duration
|
return a.duration < b.duration
|
||||||
end)
|
end)
|
||||||
for i, val in ipairs(result) do
|
for i, val in ipairs(timings) do
|
||||||
io_print(
|
print(
|
||||||
string.format(
|
string.format(
|
||||||
'%i. %.02fms %s %s',
|
'%i. %.02fms %s %s',
|
||||||
#result - i + 1,
|
#timings - i + 1,
|
||||||
val.duration * 1e-6,
|
val.duration * 1e-6,
|
||||||
val.lang,
|
val.lang,
|
||||||
val.query_type
|
val.query_type
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
io_print('::endgroup::')
|
print('::endgroup::')
|
||||||
io_print('Check successful!')
|
print('Check successful!')
|
||||||
else
|
|
||||||
vim.cmd('cq')
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue