|
|
|
|
|
|
|
|
|
|
|
local api = vim.api |
|
|
local language = require('vim.treesitter.language') |
|
|
local memoize = vim.func._memoize |
|
|
|
|
|
local MODELINE_FORMAT = '^;+%s*inherits%s*:?%s*([a-z_,()]+)%s*$' |
|
|
local EXTENDS_FORMAT = '^;+%s*extends%s*$' |
|
|
|
|
|
local M = {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local Query = {} |
|
|
Query.__index = Query |
|
|
|
|
|
local function is_directive(name) |
|
|
return string.sub(name, -1) == '!' |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function Query:_process_patterns() |
|
|
self._processed_patterns = {} |
|
|
|
|
|
for k, pattern_list in pairs(self.info.patterns) do |
|
|
|
|
|
local predicates = {} |
|
|
|
|
|
local directives = {} |
|
|
|
|
|
for _, pattern in ipairs(pattern_list) do |
|
|
|
|
|
local pred_name = pattern[1] |
|
|
|
|
|
|
|
|
if is_directive(pred_name) then |
|
|
table.insert(directives, pattern) |
|
|
if vim.deep_equal(pattern, { 'set!', 'injection.combined' }) then |
|
|
self.has_combined_injections = true |
|
|
end |
|
|
if vim.deep_equal(pattern, { 'set!', 'conceal_lines', '' }) then |
|
|
self.has_conceal_line = true |
|
|
end |
|
|
else |
|
|
local should_match = true |
|
|
if pred_name:match('^not%-') then |
|
|
pred_name = pred_name:sub(5) |
|
|
should_match = false |
|
|
end |
|
|
table.insert(predicates, { pred_name, should_match, pattern }) |
|
|
end |
|
|
end |
|
|
|
|
|
self._processed_patterns[k] = { predicates = predicates, directives = directives } |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function Query.new(lang, ts_query) |
|
|
local self = setmetatable({}, Query) |
|
|
local query_info = ts_query:inspect() |
|
|
self.query = ts_query |
|
|
self.lang = lang |
|
|
self.info = { |
|
|
captures = query_info.captures, |
|
|
patterns = query_info.patterns, |
|
|
} |
|
|
self.captures = self.info.captures |
|
|
self:_process_patterns() |
|
|
return self |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function dedupe_files(files) |
|
|
local result = {} |
|
|
|
|
|
local seen = {} |
|
|
|
|
|
for _, path in ipairs(files) do |
|
|
if not seen[path] then |
|
|
table.insert(result, path) |
|
|
seen[path] = true |
|
|
end |
|
|
end |
|
|
|
|
|
return result |
|
|
end |
|
|
|
|
|
local function safe_read(filename, read_quantifier) |
|
|
local file, err = io.open(filename, 'r') |
|
|
if not file then |
|
|
error(err) |
|
|
end |
|
|
local content = file:read(read_quantifier) |
|
|
io.close(file) |
|
|
return content |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function add_included_lang(base_langs, lang, ilang) |
|
|
if lang == ilang then |
|
|
return true |
|
|
end |
|
|
table.insert(base_langs, ilang) |
|
|
return false |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function M.get_files(lang, query_name, is_included) |
|
|
local query_path = string.format('queries/%s/%s.scm', lang, query_name) |
|
|
local lang_files = dedupe_files(api.nvim_get_runtime_file(query_path, true)) |
|
|
|
|
|
if #lang_files == 0 then |
|
|
return {} |
|
|
end |
|
|
|
|
|
local base_query = nil |
|
|
local extensions = {} |
|
|
|
|
|
local base_langs = {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _, filename in ipairs(lang_files) do |
|
|
local file, err = io.open(filename, 'r') |
|
|
if not file then |
|
|
error(err) |
|
|
end |
|
|
|
|
|
local extension = false |
|
|
|
|
|
for modeline in |
|
|
|
|
|
function() |
|
|
return file:read('*l') |
|
|
end |
|
|
do |
|
|
if not vim.startswith(modeline, ';') then |
|
|
break |
|
|
end |
|
|
|
|
|
local langlist = modeline:match(MODELINE_FORMAT) |
|
|
if langlist then |
|
|
|
|
|
for _, incllang in ipairs(vim.split(langlist, ',', true)) do |
|
|
local is_optional = incllang:match('%(.*%)') |
|
|
|
|
|
if is_optional then |
|
|
if not is_included then |
|
|
if add_included_lang(base_langs, lang, incllang:sub(2, #incllang - 1)) then |
|
|
extension = true |
|
|
end |
|
|
end |
|
|
else |
|
|
if add_included_lang(base_langs, lang, incllang) then |
|
|
extension = true |
|
|
end |
|
|
end |
|
|
end |
|
|
elseif modeline:match(EXTENDS_FORMAT) then |
|
|
extension = true |
|
|
end |
|
|
end |
|
|
|
|
|
if extension then |
|
|
table.insert(extensions, filename) |
|
|
elseif base_query == nil then |
|
|
base_query = filename |
|
|
end |
|
|
io.close(file) |
|
|
end |
|
|
|
|
|
local query_files = {} |
|
|
for _, base_lang in ipairs(base_langs) do |
|
|
local base_files = M.get_files(base_lang, query_name, true) |
|
|
vim.list_extend(query_files, base_files) |
|
|
end |
|
|
vim.list_extend(query_files, { base_query }) |
|
|
vim.list_extend(query_files, extensions) |
|
|
|
|
|
return query_files |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
local function read_query_files(filenames) |
|
|
local contents = {} |
|
|
|
|
|
for _, filename in ipairs(filenames) do |
|
|
table.insert(contents, safe_read(filename, '*a')) |
|
|
end |
|
|
|
|
|
return table.concat(contents, '') |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
local explicit_queries = setmetatable({}, { |
|
|
__index = function(t, k) |
|
|
local lang_queries = {} |
|
|
rawset(t, k, lang_queries) |
|
|
|
|
|
return lang_queries |
|
|
end, |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function M.set(lang, query_name, text) |
|
|
|
|
|
M.get:clear(lang, query_name) |
|
|
explicit_queries[lang][query_name] = text |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
M.get = memoize('concat-2', function(lang, query_name) |
|
|
local query_string |
|
|
|
|
|
if explicit_queries[lang][query_name] then |
|
|
local query_files = {} |
|
|
local base_langs = {} |
|
|
|
|
|
for line in explicit_queries[lang][query_name]:gmatch('([^\n]*)\n?') do |
|
|
if not vim.startswith(line, ';') then |
|
|
break |
|
|
end |
|
|
|
|
|
local lang_list = line:match(MODELINE_FORMAT) |
|
|
if lang_list then |
|
|
for _, incl_lang in ipairs(vim.split(lang_list, ',')) do |
|
|
local is_optional = incl_lang:match('%(.*%)') |
|
|
|
|
|
if is_optional then |
|
|
add_included_lang(base_langs, lang, incl_lang:sub(2, #incl_lang - 1)) |
|
|
else |
|
|
add_included_lang(base_langs, lang, incl_lang) |
|
|
end |
|
|
end |
|
|
elseif line:match(EXTENDS_FORMAT) then |
|
|
table.insert(base_langs, lang) |
|
|
end |
|
|
end |
|
|
|
|
|
for _, base_lang in ipairs(base_langs) do |
|
|
local base_files = M.get_files(base_lang, query_name, true) |
|
|
vim.list_extend(query_files, base_files) |
|
|
end |
|
|
|
|
|
query_string = read_query_files(query_files) .. explicit_queries[lang][query_name] |
|
|
else |
|
|
local query_files = M.get_files(lang, query_name) |
|
|
query_string = read_query_files(query_files) |
|
|
end |
|
|
|
|
|
if #query_string == 0 then |
|
|
return nil |
|
|
end |
|
|
|
|
|
return M.parse(lang, query_string) |
|
|
end, false) |
|
|
|
|
|
api.nvim_create_autocmd('OptionSet', { |
|
|
pattern = { 'runtimepath' }, |
|
|
group = api.nvim_create_augroup('nvim.treesitter.query_cache_reset', { clear = true }), |
|
|
callback = function() |
|
|
|
|
|
M.get:clear() |
|
|
end, |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
M.parse = memoize('concat-2', function(lang, query) |
|
|
assert(language.add(lang)) |
|
|
local ts_query = vim._ts_parse_query(lang, query) |
|
|
return Query.new(lang, ts_query) |
|
|
end, false) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local impl = { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
['eq'] = function(match, source, predicate, any) |
|
|
local nodes = match[predicate[2]] |
|
|
if not nodes or #nodes == 0 then |
|
|
return true |
|
|
end |
|
|
|
|
|
for _, node in ipairs(nodes) do |
|
|
local node_text = vim.treesitter.get_node_text(node, source) |
|
|
|
|
|
local str |
|
|
if type(predicate[3]) == 'string' then |
|
|
|
|
|
str = predicate[3] |
|
|
else |
|
|
|
|
|
local other = assert(match[predicate[3]]) |
|
|
assert(#other == 1, '#eq? does not support comparison with captures on multiple nodes') |
|
|
str = vim.treesitter.get_node_text(other[1], source) |
|
|
end |
|
|
|
|
|
local res = str ~= nil and node_text == str |
|
|
if any and res then |
|
|
return true |
|
|
elseif not any and not res then |
|
|
return false |
|
|
end |
|
|
end |
|
|
|
|
|
return not any |
|
|
end, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
['lua-match'] = function(match, source, predicate, any) |
|
|
local nodes = match[predicate[2]] |
|
|
if not nodes or #nodes == 0 then |
|
|
return true |
|
|
end |
|
|
|
|
|
for _, node in ipairs(nodes) do |
|
|
local regex = predicate[3] |
|
|
local res = string.find(vim.treesitter.get_node_text(node, source), regex) ~= nil |
|
|
if any and res then |
|
|
return true |
|
|
elseif not any and not res then |
|
|
return false |
|
|
end |
|
|
end |
|
|
|
|
|
return not any |
|
|
end, |
|
|
|
|
|
['match'] = (function() |
|
|
local magic_prefixes = { ['\\v'] = true, ['\\m'] = true, ['\\M'] = true, ['\\V'] = true } |
|
|
local function check_magic(str) |
|
|
if string.len(str) < 2 or magic_prefixes[string.sub(str, 1, 2)] then |
|
|
return str |
|
|
end |
|
|
return '\\v' .. str |
|
|
end |
|
|
|
|
|
local compiled_vim_regexes = setmetatable({}, { |
|
|
__index = function(t, pattern) |
|
|
local res = vim.regex(check_magic(pattern)) |
|
|
rawset(t, pattern, res) |
|
|
return res |
|
|
end, |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return function(match, source, predicate, any) |
|
|
local nodes = match[predicate[2]] |
|
|
if not nodes or #nodes == 0 then |
|
|
return true |
|
|
end |
|
|
|
|
|
for _, node in ipairs(nodes) do |
|
|
local regex = compiled_vim_regexes[predicate[3]] |
|
|
local res = regex:match_str(vim.treesitter.get_node_text(node, source)) |
|
|
if any and res then |
|
|
return true |
|
|
elseif not any and not res then |
|
|
return false |
|
|
end |
|
|
end |
|
|
return not any |
|
|
end |
|
|
end)(), |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
['contains'] = function(match, source, predicate, any) |
|
|
local nodes = match[predicate[2]] |
|
|
if not nodes or #nodes == 0 then |
|
|
return true |
|
|
end |
|
|
|
|
|
for _, node in ipairs(nodes) do |
|
|
local node_text = vim.treesitter.get_node_text(node, source) |
|
|
|
|
|
for i = 3, #predicate do |
|
|
local res = string.find(node_text, predicate[i], 1, true) |
|
|
if any and res then |
|
|
return true |
|
|
elseif not any and not res then |
|
|
return false |
|
|
end |
|
|
end |
|
|
end |
|
|
|
|
|
return not any |
|
|
end, |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local predicate_handlers = { |
|
|
['eq?'] = function(match, _, source, predicate) |
|
|
return impl['eq'](match, source, predicate, false) |
|
|
end, |
|
|
|
|
|
['any-eq?'] = function(match, _, source, predicate) |
|
|
return impl['eq'](match, source, predicate, true) |
|
|
end, |
|
|
|
|
|
['lua-match?'] = function(match, _, source, predicate) |
|
|
return impl['lua-match'](match, source, predicate, false) |
|
|
end, |
|
|
|
|
|
['any-lua-match?'] = function(match, _, source, predicate) |
|
|
return impl['lua-match'](match, source, predicate, true) |
|
|
end, |
|
|
|
|
|
['match?'] = function(match, _, source, predicate) |
|
|
return impl['match'](match, source, predicate, false) |
|
|
end, |
|
|
|
|
|
['any-match?'] = function(match, _, source, predicate) |
|
|
return impl['match'](match, source, predicate, true) |
|
|
end, |
|
|
|
|
|
['contains?'] = function(match, _, source, predicate) |
|
|
return impl['contains'](match, source, predicate, false) |
|
|
end, |
|
|
|
|
|
['any-contains?'] = function(match, _, source, predicate) |
|
|
return impl['contains'](match, source, predicate, true) |
|
|
end, |
|
|
|
|
|
['any-of?'] = function(match, _, source, predicate) |
|
|
local nodes = match[predicate[2]] |
|
|
if not nodes or #nodes == 0 then |
|
|
return true |
|
|
end |
|
|
|
|
|
for _, node in ipairs(nodes) do |
|
|
local node_text = vim.treesitter.get_node_text(node, source) |
|
|
|
|
|
|
|
|
|
|
|
local string_set = predicate['string_set'] |
|
|
if not string_set then |
|
|
string_set = {} |
|
|
for i = 3, #predicate do |
|
|
string_set[predicate[i]] = true |
|
|
end |
|
|
predicate['string_set'] = string_set |
|
|
end |
|
|
|
|
|
if string_set[node_text] then |
|
|
return true |
|
|
end |
|
|
end |
|
|
|
|
|
return false |
|
|
end, |
|
|
|
|
|
['has-ancestor?'] = function(match, _, _, predicate) |
|
|
local nodes = match[predicate[2]] |
|
|
if not nodes or #nodes == 0 then |
|
|
return true |
|
|
end |
|
|
|
|
|
for _, node in ipairs(nodes) do |
|
|
if node:__has_ancestor(predicate) then |
|
|
return true |
|
|
end |
|
|
end |
|
|
return false |
|
|
end, |
|
|
|
|
|
['has-parent?'] = function(match, _, _, predicate) |
|
|
local nodes = match[predicate[2]] |
|
|
if not nodes or #nodes == 0 then |
|
|
return true |
|
|
end |
|
|
|
|
|
for _, node in ipairs(nodes) do |
|
|
if vim.list_contains({ unpack(predicate, 3) }, node:parent():type()) then |
|
|
return true |
|
|
end |
|
|
end |
|
|
return false |
|
|
end, |
|
|
} |
|
|
|
|
|
|
|
|
predicate_handlers['vim-match?'] = predicate_handlers['match?'] |
|
|
predicate_handlers['any-vim-match?'] = predicate_handlers['any-match?'] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local directive_handlers = { |
|
|
['set!'] = function(_, _, _, pred, metadata) |
|
|
if #pred >= 3 and type(pred[2]) == 'number' then |
|
|
|
|
|
local capture_id, key, value = pred[2], pred[3], pred[4] |
|
|
if not metadata[capture_id] then |
|
|
metadata[capture_id] = {} |
|
|
end |
|
|
metadata[capture_id][key] = value |
|
|
else |
|
|
|
|
|
local key, value = pred[2], pred[3] |
|
|
metadata[key] = value or true |
|
|
end |
|
|
end, |
|
|
|
|
|
|
|
|
['offset!'] = function(match, _, _, pred, metadata) |
|
|
local capture_id = pred[2] |
|
|
local nodes = match[capture_id] |
|
|
if not nodes or #nodes == 0 then |
|
|
return |
|
|
end |
|
|
assert(#nodes == 1, '#offset! does not support captures on multiple nodes') |
|
|
|
|
|
local node = nodes[1] |
|
|
|
|
|
if not metadata[capture_id] then |
|
|
metadata[capture_id] = {} |
|
|
end |
|
|
|
|
|
local range = metadata[capture_id].range or { node:range() } |
|
|
local start_row_offset = pred[3] or 0 |
|
|
local start_col_offset = pred[4] or 0 |
|
|
local end_row_offset = pred[5] or 0 |
|
|
local end_col_offset = pred[6] or 0 |
|
|
|
|
|
range[1] = range[1] + start_row_offset |
|
|
range[2] = range[2] + start_col_offset |
|
|
range[3] = range[3] + end_row_offset |
|
|
range[4] = range[4] + end_col_offset |
|
|
|
|
|
|
|
|
if range[1] < range[3] or (range[1] == range[3] and range[2] <= range[4]) then |
|
|
metadata[capture_id].range = range |
|
|
end |
|
|
end, |
|
|
|
|
|
|
|
|
['gsub!'] = function(match, _, bufnr, pred, metadata) |
|
|
assert(#pred == 4) |
|
|
|
|
|
local id = pred[2] |
|
|
assert(type(id) == 'number') |
|
|
|
|
|
local nodes = match[id] |
|
|
if not nodes or #nodes == 0 then |
|
|
return |
|
|
end |
|
|
assert(#nodes == 1, '#gsub! does not support captures on multiple nodes') |
|
|
local node = nodes[1] |
|
|
local text = vim.treesitter.get_node_text(node, bufnr, { metadata = metadata[id] }) or '' |
|
|
|
|
|
if not metadata[id] then |
|
|
metadata[id] = {} |
|
|
end |
|
|
|
|
|
local pattern, replacement = pred[3], pred[4] |
|
|
assert(type(pattern) == 'string') |
|
|
assert(type(replacement) == 'string') |
|
|
|
|
|
metadata[id].text = text:gsub(pattern, replacement) |
|
|
end, |
|
|
|
|
|
|
|
|
['trim!'] = function(match, _, bufnr, pred, metadata) |
|
|
local capture_id = pred[2] |
|
|
assert(type(capture_id) == 'number') |
|
|
|
|
|
local trim_start_lines = pred[3] == '1' |
|
|
local trim_start_cols = pred[4] == '1' |
|
|
local trim_end_lines = pred[5] == '1' or not pred[3] |
|
|
local trim_end_cols = pred[6] == '1' |
|
|
|
|
|
local nodes = match[capture_id] |
|
|
if not nodes or #nodes == 0 then |
|
|
return |
|
|
end |
|
|
assert(#nodes == 1, '#trim! does not support captures on multiple nodes') |
|
|
local node = nodes[1] |
|
|
|
|
|
local start_row, start_col, end_row, end_col = node:range() |
|
|
|
|
|
local node_text = vim.split(vim.treesitter.get_node_text(node, bufnr), '\n') |
|
|
if end_col == 0 then |
|
|
|
|
|
node_text[#node_text + 1] = '' |
|
|
end |
|
|
|
|
|
local end_idx = #node_text |
|
|
local start_idx = 1 |
|
|
|
|
|
if trim_end_lines then |
|
|
while end_idx > 0 and node_text[end_idx]:find('^%s*$') do |
|
|
end_idx = end_idx - 1 |
|
|
end_row = end_row - 1 |
|
|
|
|
|
|
|
|
end_col = end_idx > 0 and #node_text[end_idx] or 0 |
|
|
end |
|
|
end |
|
|
if trim_end_cols then |
|
|
if end_idx == 0 then |
|
|
end_row = start_row |
|
|
end_col = start_col |
|
|
else |
|
|
local whitespace_start = node_text[end_idx]:find('(%s*)$') |
|
|
end_col = (whitespace_start - 1) + (end_idx == 1 and start_col or 0) |
|
|
end |
|
|
end |
|
|
|
|
|
if trim_start_lines then |
|
|
while start_idx <= end_idx and node_text[start_idx]:find('^%s*$') do |
|
|
start_idx = start_idx + 1 |
|
|
start_row = start_row + 1 |
|
|
start_col = 0 |
|
|
end |
|
|
end |
|
|
if trim_start_cols and node_text[start_idx] then |
|
|
local _, whitespace_end = node_text[start_idx]:find('^(%s*)') |
|
|
whitespace_end = whitespace_end or 0 |
|
|
start_col = (start_idx == 1 and start_col or 0) + whitespace_end |
|
|
end |
|
|
|
|
|
|
|
|
if start_row < end_row or (start_row == end_row and start_col <= end_col) then |
|
|
metadata[capture_id] = metadata[capture_id] or {} |
|
|
metadata[capture_id].range = { start_row, start_col, end_row, end_col } |
|
|
end |
|
|
end, |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function M.add_predicate(name, handler, opts) |
|
|
|
|
|
if type(opts) == 'boolean' then |
|
|
opts = { force = opts } |
|
|
end |
|
|
|
|
|
opts = opts or {} |
|
|
|
|
|
if predicate_handlers[name] and not opts.force then |
|
|
error(string.format('Overriding existing predicate %s', name)) |
|
|
end |
|
|
|
|
|
if opts.all ~= false then |
|
|
predicate_handlers[name] = handler |
|
|
else |
|
|
|
|
|
local function wrapper(match, ...) |
|
|
local m = {} |
|
|
for k, v in pairs(match) do |
|
|
if type(k) == 'number' then |
|
|
m[k] = v[#v] |
|
|
end |
|
|
end |
|
|
return handler(m, ...) |
|
|
end |
|
|
predicate_handlers[name] = wrapper |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function M.add_directive(name, handler, opts) |
|
|
|
|
|
if type(opts) == 'boolean' then |
|
|
opts = { force = opts } |
|
|
end |
|
|
|
|
|
opts = opts or {} |
|
|
|
|
|
if directive_handlers[name] and not opts.force then |
|
|
error(string.format('Overriding existing directive %s', name)) |
|
|
end |
|
|
|
|
|
if opts.all then |
|
|
directive_handlers[name] = handler |
|
|
else |
|
|
|
|
|
local function wrapper(match, ...) |
|
|
local m = {} |
|
|
for k, v in pairs(match) do |
|
|
m[k] = v[#v] |
|
|
end |
|
|
handler(m, ...) |
|
|
end |
|
|
directive_handlers[name] = wrapper |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
function M.list_directives() |
|
|
return vim.tbl_keys(directive_handlers) |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
function M.list_predicates() |
|
|
return vim.tbl_keys(predicate_handlers) |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function Query:_match_predicates(predicates, pattern_i, captures, source) |
|
|
for _, predicate in ipairs(predicates) do |
|
|
local processed_name = predicate[1] |
|
|
local should_match = predicate[2] |
|
|
local orig_predicate = predicate[3] |
|
|
|
|
|
local handler = predicate_handlers[processed_name] |
|
|
if not handler then |
|
|
error(string.format('No handler for %s', orig_predicate[1])) |
|
|
return false |
|
|
end |
|
|
|
|
|
local does_match = handler(captures, pattern_i, source, orig_predicate) |
|
|
if does_match ~= should_match then |
|
|
return false |
|
|
end |
|
|
end |
|
|
return true |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function Query:_apply_directives(directives, pattern_i, captures, source) |
|
|
|
|
|
local metadata = {} |
|
|
|
|
|
for _, directive in pairs(directives) do |
|
|
local handler = directive_handlers[directive[1]] |
|
|
|
|
|
if not handler then |
|
|
error(string.format('No handler for %s', directive[1])) |
|
|
end |
|
|
|
|
|
handler(captures, pattern_i, source, directive, metadata) |
|
|
end |
|
|
|
|
|
return metadata |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function value_or_node_range(start, stop, node) |
|
|
if start == nil then |
|
|
start = node:start() |
|
|
end |
|
|
if stop == nil then |
|
|
stop = node:end_() + 1 |
|
|
end |
|
|
|
|
|
return start, stop |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function Query:iter_captures(node, source, start, stop, opts) |
|
|
opts = opts or {} |
|
|
opts.match_limit = opts.match_limit or 256 |
|
|
|
|
|
if type(source) == 'number' and source == 0 then |
|
|
source = api.nvim_get_current_buf() |
|
|
end |
|
|
|
|
|
start, stop = value_or_node_range(start, stop, node) |
|
|
|
|
|
local tree = node:tree() |
|
|
local cursor = vim._create_ts_querycursor(node, self.query, start, stop, opts) |
|
|
|
|
|
|
|
|
local highest_cached_match_id = -1 |
|
|
|
|
|
local match_cache = {} |
|
|
|
|
|
local function iter(end_line) |
|
|
local capture, captured_node, match = cursor:next_capture() |
|
|
|
|
|
if not capture then |
|
|
return |
|
|
end |
|
|
|
|
|
local match_id, pattern_i = match:info() |
|
|
|
|
|
|
|
|
local metadata |
|
|
if match_id <= highest_cached_match_id then |
|
|
metadata = match_cache[match_id] |
|
|
end |
|
|
|
|
|
if not metadata then |
|
|
metadata = {} |
|
|
|
|
|
local processed_pattern = self._processed_patterns[pattern_i] |
|
|
if processed_pattern then |
|
|
local captures = match:captures() |
|
|
|
|
|
local predicates = processed_pattern.predicates |
|
|
if not self:_match_predicates(predicates, pattern_i, captures, source) then |
|
|
cursor:remove_match(match_id) |
|
|
if end_line and captured_node:range() > end_line then |
|
|
return nil, captured_node, nil, nil |
|
|
end |
|
|
return iter(end_line) |
|
|
end |
|
|
|
|
|
local directives = processed_pattern.directives |
|
|
metadata = self:_apply_directives(directives, pattern_i, captures, source) |
|
|
end |
|
|
|
|
|
highest_cached_match_id = math.max(highest_cached_match_id, match_id) |
|
|
match_cache[match_id] = metadata |
|
|
end |
|
|
|
|
|
return capture, captured_node, metadata, match, tree |
|
|
end |
|
|
return iter |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function Query:iter_matches(node, source, start, stop, opts) |
|
|
opts = opts or {} |
|
|
opts.match_limit = opts.match_limit or 256 |
|
|
|
|
|
if type(source) == 'number' and source == 0 then |
|
|
source = api.nvim_get_current_buf() |
|
|
end |
|
|
|
|
|
start, stop = value_or_node_range(start, stop, node) |
|
|
|
|
|
local tree = node:tree() |
|
|
local cursor = vim._create_ts_querycursor(node, self.query, start, stop, opts) |
|
|
|
|
|
local function iter() |
|
|
local match = cursor:next_match() |
|
|
|
|
|
if not match then |
|
|
return |
|
|
end |
|
|
|
|
|
local match_id, pattern_i = match:info() |
|
|
local processed_pattern = self._processed_patterns[pattern_i] |
|
|
local captures = match:captures() |
|
|
|
|
|
|
|
|
local metadata = {} |
|
|
if processed_pattern then |
|
|
local predicates = processed_pattern.predicates |
|
|
if not self:_match_predicates(predicates, pattern_i, captures, source) then |
|
|
cursor:remove_match(match_id) |
|
|
return iter() |
|
|
end |
|
|
local directives = processed_pattern.directives |
|
|
metadata = self:_apply_directives(directives, pattern_i, captures, source) |
|
|
end |
|
|
|
|
|
if opts.all == false then |
|
|
|
|
|
|
|
|
|
|
|
local old_match = {} |
|
|
for k, v in pairs(captures or {}) do |
|
|
old_match[k] = v[#v] |
|
|
end |
|
|
return pattern_i, old_match, metadata |
|
|
end |
|
|
|
|
|
|
|
|
return pattern_i, captures, metadata, tree |
|
|
end |
|
|
return iter |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function M.lint(buf, opts) |
|
|
if opts and opts.clear then |
|
|
vim.treesitter._query_linter.clear(buf) |
|
|
else |
|
|
vim.treesitter._query_linter.lint(buf, opts) |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function M.omnifunc(findstart, base) |
|
|
return vim.treesitter._query_linter.omnifunc(findstart, base) |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function M.edit(lang) |
|
|
assert(vim.treesitter.dev.edit_query(lang)) |
|
|
end |
|
|
|
|
|
return M |
|
|
|