aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/check-queries.lua
blob: 1429c55464038199242e45da7e513b827dd7b8db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
-- Execute as `nvim --headless -c "luafile ./scripts/check-queries.lua"`

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

  -- Complete captures for injections.
  local parsers = require 'nvim-treesitter.info'.installed_parsers()
  for _, lang in pairs(parsers) do
    table.insert(captures['injections'], lang)
  end

  return captures
end

local function do_check()
  local parsers = require 'nvim-treesitter.info'.installed_parsers()
  local queries = require 'nvim-treesitter.query'
  local query_types = queries.built_in_query_groups

  local captures = extract_captures()
  local last_error

  for _, lang in pairs(parsers) do
    for _, query_type in pairs(query_types) do
      print('Checking '..lang..' '..query_type)
      local ok, query = pcall(queries.get_query, lang, query_type)
      if not ok then
        vim.api.nvim_err_writeln(query)
        last_error = query
      else
        if query then
          for _, capture in ipairs(query.captures) do
            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
            end
          end
        end
      end
    end
  end
  if last_error then
    print()
    print("Last error: ")
    error(last_error)
  end
end


local ok, err = pcall(do_check)
local allowed_to_fail = vim.split(vim.env.ALLOWED_INSTALLATION_FAILURES or '', ",", true)

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
    if vim.env.CI
      and not v.install_info.requires_generate_from_grammar
      and not vim.tbl_contains(allowed_to_fail, k) then

      print('Error: parser for '..k..' is not installed')
      vim.cmd('cq')
    else
      print('Warning: parser for '..k..' is not installed')
    end
  end
end

if ok then
  print('Check successful!\n')
  vim.cmd('q')
else
  print('Check failed:')
  print(err)
  print('\n')
  vim.cmd('cq')
end