aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/treesitter/_query_linter.lua
blob: 3dd0177a81b2dc4a7f543242b96c69be494a0092 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
local api = vim.api

local namespace = api.nvim_create_namespace('vim.treesitter.query_linter')
-- those node names exist for every language
local BUILT_IN_NODE_NAMES = { '_', 'ERROR' }

local M = {}

--- @class QueryLinterNormalizedOpts
--- @field langs string[]
--- @field clear boolean

--- @private
--- Caches parse results for queries for each language.
--- Entries of parse_cache[lang][query_text] will either be true for successful parse or contain the
--- error message of the parse
--- @type table<string,table<string,string|true>>
local parse_cache = {}

--- Contains language dependent context for the query linter
--- @class QueryLinterLanguageContext
--- @field lang string? Current `lang` of the targeted parser
--- @field parser_info table? Parser info returned by vim.treesitter.language.inspect
--- @field is_first_lang boolean Whether this is the first language of a linter run checking queries for multiple `langs`

--- @private
--- Adds a diagnostic for node in the query buffer
--- @param diagnostics Diagnostic[]
--- @param node TSNode
--- @param buf integer
--- @param lint string
--- @param lang string?
local function add_lint_for_node(diagnostics, node, buf, lint, lang)
  local node_text = vim.treesitter.get_node_text(node, buf):gsub('\n', ' ')
  --- @type string
  local message = lint .. ': ' .. node_text
  local error_range = { node:range() }
  diagnostics[#diagnostics + 1] = {
    lnum = error_range[1],
    end_lnum = error_range[3],
    col = error_range[2],
    end_col = error_range[4],
    severity = vim.diagnostic.ERROR,
    message = message,
    source = lang,
  }
end

--- @private
--- Determines the target language of a query file by its path: <lang>/<query_type>.scm
--- @param buf integer
--- @return string?
local function guess_query_lang(buf)
  local filename = api.nvim_buf_get_name(buf)
  if filename ~= '' then
    return vim.F.npcall(vim.fn.fnamemodify, filename, ':p:h:t')
  end
end

--- @private
--- @param buf integer
--- @param opts QueryLinterOpts|QueryLinterNormalizedOpts|nil
--- @return QueryLinterNormalizedOpts
local function normalize_opts(buf, opts)
  opts = opts or {}
  if not opts.langs then
    opts.langs = guess_query_lang(buf)
  end

  if type(opts.langs) ~= 'table' then
    --- @diagnostic disable-next-line:assign-type-mismatch
    opts.langs = { opts.langs }
  end

  --- @cast opts QueryLinterNormalizedOpts
  opts.langs = opts.langs or {}
  return opts
end

local lint_query = [[;; query
  (program [(named_node) (list) (grouping)] @toplevel)
  (named_node
    name: _ @node.named)
  (anonymous_node
    name: _ @node.anonymous)
  (field_definition
    name: (identifier) @field)
  (predicate
    name: (identifier) @predicate.name
    type: (predicate_type) @predicate.type)
  (ERROR) @error
]]

--- @private
--- @param node TSNode
--- @param buf integer
--- @param lang string
--- @param diagnostics Diagnostic[]
local function check_toplevel(node, buf, lang, diagnostics)
  local query_text = vim.treesitter.get_node_text(node, buf)

  if not parse_cache[lang] then
    parse_cache[lang] = {}
  end

  local lang_cache = parse_cache[lang]

  if lang_cache[query_text] == nil then
    local ok, err = pcall(vim.treesitter.query.parse, lang, query_text)

    if not ok and type(err) == 'string' then
      err = err:match('.-:%d+: (.+)')
    end

    lang_cache[query_text] = ok or err
  end

  local cache_entry = lang_cache[query_text]

  if type(cache_entry) == 'string' then
    add_lint_for_node(diagnostics, node, buf, cache_entry, lang)
  end
end

--- @private
--- @param node TSNode
--- @param buf integer
--- @param lang string
--- @param parser_info table
--- @param diagnostics Diagnostic[]
local function check_field(node, buf, lang, parser_info, diagnostics)
  local field_name = vim.treesitter.get_node_text(node, buf)
  if not vim.tbl_contains(parser_info.fields, field_name) then
    add_lint_for_node(diagnostics, node, buf, 'Invalid field', lang)
  end
end

--- @private
--- @param node TSNode
--- @param buf integer
--- @param lang string
--- @param parser_info (table)
--- @param diagnostics Diagnostic[]
local function check_node(node, buf, lang, parser_info, diagnostics)
  local node_type = vim.treesitter.get_node_text(node, buf)
  local is_named = node_type:sub(1, 1) ~= '"'

  if not is_named then
    node_type = node_type:gsub('"(.*)".*$', '%1'):gsub('\\(.)', '%1')
  end

  local found = vim.tbl_contains(BUILT_IN_NODE_NAMES, node_type)
    or vim.tbl_contains(parser_info.symbols, function(s)
      return vim.deep_equal(s, { node_type, is_named })
    end, { predicate = true })

  if not found then
    add_lint_for_node(diagnostics, node, buf, 'Invalid node type', lang)
  end
end

--- @private
--- @param node TSNode
--- @param buf integer
--- @param is_predicate boolean
--- @return string
local function get_predicate_name(node, buf, is_predicate)
  local name = vim.treesitter.get_node_text(node, buf)
  if is_predicate then
    if vim.startswith(name, 'not-') then
      --- @type string
      name = name:sub(string.len('not-') + 1)
    end
    return name .. '?'
  end
  return name .. '!'
end

--- @private
--- @param predicate_node TSNode
--- @param predicate_type_node TSNode
--- @param buf integer
--- @param lang string?
--- @param diagnostics Diagnostic[]
local function check_predicate(predicate_node, predicate_type_node, buf, lang, diagnostics)
  local type_string = vim.treesitter.get_node_text(predicate_type_node, buf)

  -- Quirk of the query grammar that directives are also predicates!
  if type_string == '?' then
    if
      not vim.tbl_contains(
        vim.treesitter.query.list_predicates(),
        get_predicate_name(predicate_node, buf, true)
      )
    then
      add_lint_for_node(diagnostics, predicate_node, buf, 'Unknown predicate', lang)
    end
  elseif type_string == '!' then
    if
      not vim.tbl_contains(
        vim.treesitter.query.list_directives(),
        get_predicate_name(predicate_node, buf, false)
      )
    then
      add_lint_for_node(diagnostics, predicate_node, buf, 'Unknown directive', lang)
    end
  end
end

--- @private
--- @param buf integer
--- @param match table<integer,TSNode>
--- @param query Query
--- @param lang_context QueryLinterLanguageContext
--- @param diagnostics Diagnostic[]
local function lint_match(buf, match, query, lang_context, diagnostics)
  local predicate --- @type TSNode
  local predicate_type --- @type TSNode
  local lang = lang_context.lang
  local parser_info = lang_context.parser_info

  for id, node in pairs(match) do
    local cap_id = query.captures[id]

    -- perform language-independent checks only for first lang
    if lang_context.is_first_lang then
      if cap_id == 'error' then
        add_lint_for_node(diagnostics, node, buf, 'Syntax error')
      elseif cap_id == 'predicate.name' then
        predicate = node
      elseif cap_id == 'predicate.type' then
        predicate_type = node
      end
    end

    -- other checks rely on Neovim parser introspection
    if lang and parser_info then
      if cap_id == 'toplevel' then
        check_toplevel(node, buf, lang, diagnostics)
      elseif cap_id == 'field' then
        check_field(node, buf, lang, parser_info, diagnostics)
      elseif cap_id == 'node.named' or cap_id == 'node.anonymous' then
        check_node(node, buf, lang, parser_info, diagnostics)
      end
    end
  end

  if predicate and predicate_type then
    check_predicate(predicate, predicate_type, buf, lang, diagnostics)
  end
end

--- @private
--- @param buf integer Buffer to lint
--- @param opts QueryLinterOpts|QueryLinterNormalizedOpts|nil Options for linting
function M.lint(buf, opts)
  if buf == 0 then
    buf = api.nvim_get_current_buf()
  end

  local diagnostics = {}
  local query = vim.treesitter.query.parse('query', lint_query)

  opts = normalize_opts(buf, opts)

  -- perform at least one iteration even with no langs to perform language independent checks
  for i = 1, math.max(1, #opts.langs) do
    local lang = opts.langs[i]

    --- @type (table|nil)
    local parser_info = vim.F.npcall(vim.treesitter.language.inspect, lang)

    local parser = vim.treesitter.get_parser(buf)
    parser:parse()
    parser:for_each_tree(function(tree, ltree)
      if ltree:lang() == 'query' then
        for _, match, _ in query:iter_matches(tree:root(), buf, 0, -1) do
          local lang_context = {
            lang = lang,
            parser_info = parser_info,
            is_first_lang = i == 1,
          }
          lint_match(buf, match, query, lang_context, diagnostics)
        end
      end
    end)
  end

  vim.diagnostic.set(namespace, buf, diagnostics)
end

--- @private
--- @param buf integer
function M.clear(buf)
  vim.diagnostic.reset(namespace, buf)
end

--- @private
--- @param findstart integer
--- @param base string
function M.omnifunc(findstart, base)
  if findstart == 1 then
    local result =
      api.nvim_get_current_line():sub(1, api.nvim_win_get_cursor(0)[2]):find('["#%-%w]*$')
    return result - 1
  end

  local buf = api.nvim_get_current_buf()
  local query_lang = guess_query_lang(buf)

  local ok, parser_info = pcall(vim.treesitter.language.inspect, query_lang)
  if not ok then
    return -2
  end

  local items = {}
  for _, f in pairs(parser_info.fields) do
    if f:find(base, 1, true) then
      table.insert(items, f .. ':')
    end
  end
  for _, p in pairs(vim.treesitter.query.list_predicates()) do
    local text = '#' .. p
    local found = text:find(base, 1, true)
    if found and found <= 2 then -- with or without '#'
      table.insert(items, text)
    end
    text = '#not-' .. p
    found = text:find(base, 1, true)
    if found and found <= 2 then -- with or without '#'
      table.insert(items, text)
    end
  end
  for _, p in pairs(vim.treesitter.query.list_directives()) do
    local text = '#' .. p
    local found = text:find(base, 1, true)
    if found and found <= 2 then -- with or without '#'
      table.insert(items, text)
    end
  end
  for _, s in pairs(parser_info.symbols) do
    local text = s[2] and s[1] or '"' .. s[1]:gsub([[\]], [[\\]]) .. '"'
    if text:find(base, 1, true) then
      table.insert(items, text)
    end
  end
  return { words = items, refresh = 'always' }
end

return M