Introduce base languages for queries

Some treesitter grammars just extend another treesitter grammar.
This enables us to use the C queries also for C++.
We only need to put additional queries in the C++ files.
This commit is contained in:
Stephan Seitz 2020-05-23 20:29:08 +02:00
parent 6b26674ba0
commit df06026110

View file

@ -13,11 +13,31 @@ local function read_query_files(filenames)
return table.concat(contents, '\n')
end
-- Some treesitter grammars extend others.
-- We can use that to import the queries of the base language
M.base_language_map = {
cpp = {'c'},
typescript = {'javascript'},
tsx = {'typescript', 'javascript'},
}
function M.get_query(ft, query_name)
local query_files = api.nvim_get_runtime_file(string.format('queries/%s/%s.scm', ft, query_name), true)
local query_string = ''
if #query_files > 0 then
return ts.parse_query(ft, read_query_files(query_files))
query_string = read_query_files(query_files)..query_string
end
for _, base_lang in ipairs(M.base_language_map[ft] or {}) do
local base_files = api.nvim_get_runtime_file(string.format('queries/%s/%s.scm', base_lang, query_name), false)
if base_files and #base_files > 0 then
query_string = read_query_files(base_files)..query_string
end
end
if #query_string > 0 then
return ts.parse_query(ft, query_string)
end
end