From 44108fe03e5c2cb761090c4abf8e7164225bf62f Mon Sep 17 00:00:00 2001 From: Steven Sojka Date: Fri, 12 Jun 2020 14:11:37 -0500 Subject: feat(queries): allow for user overrides --- lua/nvim-treesitter/query.lua | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'lua') diff --git a/lua/nvim-treesitter/query.lua b/lua/nvim-treesitter/query.lua index 644c33933..b914e5d7d 100644 --- a/lua/nvim-treesitter/query.lua +++ b/lua/nvim-treesitter/query.lua @@ -3,14 +3,21 @@ 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 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) + if #query_files > 0 then - return ts.parse_query(ft, read_query_file(query_files[1])) + return ts.parse_query(ft, read_query_files(query_files)) end end -- cgit v1.2.3-70-g09d2 From 45ea0df21a8494c7a8258bbb98ebd510716148e9 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Mon, 1 Jun 2020 00:22:59 +0200 Subject: Add tree-sitter-regex This might be interesting for injected highlighting --- README.md | 1 + lua/nvim-treesitter/configs.lua | 7 +++++++ queries/regex/highlights.scm | 29 +++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 queries/regex/highlights.scm (limited to 'lua') diff --git a/README.md b/README.md index c31ce8985..233487d7d 100644 --- a/README.md +++ b/README.md @@ -180,6 +180,7 @@ List of currently supported languages: - [ ] yaml - [ ] nix - [ ] markdown +- [x] regex (maintained by @theHamsta) ## Troubleshooting Before doing anything run `:checkhealth nvim_treesitter`. This will help you find where the bug might come from. diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua index f79fed00c..f7b5ff2cf 100644 --- a/lua/nvim-treesitter/configs.lua +++ b/lua/nvim-treesitter/configs.lua @@ -203,6 +203,13 @@ parsers.nix = { } } +parsers.regex = { + install_info = { + url = "https://github.com/tree-sitter/tree-sitter-regex", + files = { "src/parser.c" } + } +} + -- @enable can be true or false -- @disable is a list of languages, only relevant if enable is true -- @keymaps list of user mappings for a given module if relevant diff --git a/queries/regex/highlights.scm b/queries/regex/highlights.scm new file mode 100644 index 000000000..48242543f --- /dev/null +++ b/queries/regex/highlights.scm @@ -0,0 +1,29 @@ +;; Forked from tree-sitter-regex +;; The MIT License (MIT) Copyright (c) 2014 Max Brunsfeld +[ + "(" + ")" + "(?" + "(?:" + "(?<" + ">" + "[" + "]" + "{" + "}" +] @punctuation.bracket + +(group_name) @property + +[ + (identity_escape) + (control_letter_escape) + (character_class_escape) + (control_escape) + (start_assertion) + (end_assertion) + (boundary_assertion) + (non_boundary_assertion) +] @escape + +[ "*" "+" "|" "=" "<=" "!" " 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