From df060261106a3570dbae04ae83879cdcbc9a9907 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sat, 23 May 2020 20:29:08 +0200 Subject: 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. --- lua/nvim-treesitter/query.lua | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'lua') diff --git a/lua/nvim-treesitter/query.lua b/lua/nvim-treesitter/query.lua index b914e5d7d..788bfa126 100644 --- a/lua/nvim-treesitter/query.lua +++ b/lua/nvim-treesitter/query.lua @@ -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 -- cgit v1.2.3-70-g09d2