mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-01 19:17:02 -04:00
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:
parent
6b26674ba0
commit
df06026110
1 changed files with 21 additions and 1 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue