summaryrefslogtreecommitdiff
path: root/lua/nvim-treesitter/query.lua
diff options
context:
space:
mode:
authorSteven Sojka <Steven.Sojka@tdameritrade.com>2020-06-15 07:05:20 -0500
committerSteven Sojka <Steven.Sojka@tdameritrade.com>2020-06-15 07:05:20 -0500
commit4551b0e1c914f8f0dabdec4010c86c163c08fee2 (patch)
tree09f781e5970756fb5a092a37fcbc4274f9664b10 /lua/nvim-treesitter/query.lua
parentac8ae3b1c7b5644f8317cdc88d604cdb558b6296 (diff)
parentc452d4a91c341a1057b638d520e7cc75999a6b3b (diff)
Merge branch 'master' into feat/typescript-queries
* master: Change regexes in C/C++ highlights Update C/C++ highlights to new query syntax Add better highlighting for preprocessor functions in C highlights Add operators /=,*=,|=,&= to C highlights Add compound_statement to c queries Add punctuation.bracket/punctuation.delimiter to C highlights Make =,~,! operators in C highlights Add cpp/locals.scm Add @error highlight to c/highlights.scm Add C++ highlights.scm Introduce base languages for queries Add tree-sitter-regex feat(queries): allow for user overrides Update issue templates
Diffstat (limited to 'lua/nvim-treesitter/query.lua')
-rw-r--r--lua/nvim-treesitter/query.lua35
1 files changed, 31 insertions, 4 deletions
diff --git a/lua/nvim-treesitter/query.lua b/lua/nvim-treesitter/query.lua
index 644c3393..788bfa12 100644
--- a/lua/nvim-treesitter/query.lua
+++ b/lua/nvim-treesitter/query.lua
@@ -3,14 +3,41 @@ local ts = vim.treesitter
local M = {}
-local function read_query_file(fname)
- return table.concat(vim.fn.readfile(fname), '\n')
+local function read_query_files(filenames)
+ local contents = {}
+
+ for _,filename in ipairs(filenames) do
+ vim.list_extend(contents, vim.fn.readfile(filename))
+ end
+
+ 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), false)
+ 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_file(query_files[1]))
+ 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