robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
3.2 KiB113 linesraw
1local M = {}
2local ts = vim.treesitter
3local health = vim.health
4
5--- Performs a healthcheck for treesitter integration
6function M.check()
7  health.start('Treesitter features')
8
9  health.info(
10    string.format(
11      'Treesitter ABI support: min %d, max %d',
12      ts.minimum_language_version,
13      ts.language_version
14    )
15  )
16
17  local can_wasm = vim._ts_add_language_from_wasm ~= nil
18  health.info(string.format('WASM parser support: %s', tostring(can_wasm)))
19
20  health.start('Treesitter parsers')
21  local parsers = vim.api.nvim_get_runtime_file('parser/*', true)
22
23  ---@class ParserEntry
24  ---@field name string
25  ---@field path string
26  ---@field index integer runtime path index (unique)
27
28  local sorted_parsers = {} ---@type ParserEntry[]
29
30  for i, parser in ipairs(parsers) do
31    local parsername = vim.fn.fnamemodify(parser, ':t:r')
32    table.insert(sorted_parsers, { name = parsername, path = parser, index = i })
33  end
34
35  table.sort(sorted_parsers, function(a, b)
36    if a.name == b.name then
37      return a.index < b.index -- if names are the same sort by rtpath index (unique)
38    else
39      return a.name < b.name
40    end
41  end)
42
43  for i, parser in ipairs(sorted_parsers) do
44    local previous = sorted_parsers[i - 1]
45    local is_loadable, err_or_nil = pcall(ts.language.add, parser.name)
46
47    if not is_loadable then
48      health.error(
49        string.format(
50          'Parser "%s" failed to load (path: %s): %s',
51          parser.name,
52          parser.path,
53          err_or_nil or '?'
54        )
55      )
56    elseif previous and previous.name == parser.name then
57      -- Sorted by runtime path order (load order), thus, if the previous parser has the same name,
58      -- the current parser will not be loaded and `ts.language.inspect(parser.name)` with have
59      -- incorrect information.
60      health.ok(string.format('Parser: %-20s (not loaded), path: %s', parser.name, parser.path))
61    else
62      local lang = ts.language.inspect(parser.name)
63      health.ok(
64        string.format('Parser: %-25s ABI: %d, path: %s', parser.name, lang.abi_version, parser.path)
65      )
66    end
67  end
68
69  health.start('Treesitter queries')
70  local query_files = vim.api.nvim_get_runtime_file('queries/**/*.scm', true)
71
72  ---@class QueryEntry
73  ---@field lang string
74  ---@field type string
75  ---@field path string
76  ---@field index integer
77
78  local queries_by_lang = {} ---@type table<string, QueryEntry[]>
79
80  for i, query_file in ipairs(query_files) do
81    local lang, query_type = query_file:match('queries/([^/]+)/([^/]+)%.scm$')
82    if lang and query_type then
83      if not queries_by_lang[lang] then
84        queries_by_lang[lang] = {}
85      end
86      table.insert(queries_by_lang[lang], {
87        lang = lang,
88        type = query_type,
89        path = query_file,
90        index = i,
91      })
92    end
93  end
94
95  if not vim.tbl_isempty(queries_by_lang) then
96    for lang, queries in vim.spairs(queries_by_lang) do
97      table.sort(queries, function(a, b)
98        if a.type == b.type then
99          return a.index < b.index
100        else
101          return a.type < b.type
102        end
103      end)
104
105      for _, query in ipairs(queries) do
106        local dir = vim.fs.dirname(query.path)
107        health.ok(string.format('%-15s %-15s %s', lang, query.type, dir))
108      end
109    end
110  end
111end
112
113return M