feat(queries): modeline mechanism for base langs

This implements https://github.com/neovim/neovim/pull/13059#issuecomment-704414189

This behaves like modelines and remove the use of the base_language map.
Also, this allows to fine-tune what we actually want to include per
query, which is better IMO.
This commit is contained in:
Thomas Vigouroux 2020-10-06 20:03:39 +02:00
parent 9ad47c65bd
commit c055899dc0
17 changed files with 73 additions and 32 deletions

View file

@ -67,6 +67,18 @@ Here are some global advices :
- Examples of queries can be found in [queries/](queries/)
- Matches in the bottom will override queries that are above of them.
If your language is an extension of a language (TypeScript is an extension of JavaScript for
example), you can include the queries from your base language by adding the following _as the first
line of your file_.
```scheme
; inherits: lang1,(optionallang)
```
If you want to include a language for a given query, but don't want for the queries including the
query you qre writing to include it too, you can mark the language as optional (by putting it
between parenthesis).
### Highlights
As languages differ quite a lot, here is a set of captures available to you when building a `highlights.scm` query.

View file

@ -8,19 +8,9 @@ local M = {}
local query_cache = caching.create_buffer_cache()
-- 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'},
javascript = {'jsx'},
tsx = {'typescript', 'javascript', 'jsx'},
ocaml_interface = {'ocaml'},
}
M.built_in_query_groups = {'highlights', 'locals', 'textobjects', 'folds'}
-- Creates a function that checks whether a certain query exists
-- Creates a function that checks whether a given query exists
-- for a specific language.
local function get_query_guard(query)
return function(lang)
@ -79,19 +69,45 @@ local function filtered_runtime_queries(lang, query_name)
return filter_files(api.nvim_get_runtime_file(string.format('queries/%s/%s.scm', lang, query_name), true) or {})
end
local function runtime_query_exists(lang, query_name)
local files = api.nvim_get_runtime_file(string.format('queries/%s/%s.scm', lang, query_name), false)
return files and #files > 0
end
local function get_query_files(lang, query_name)
local query_files = {}
local function get_query_files(lang, query_name, is_included)
local lang_files = filtered_runtime_queries(lang, query_name)
vim.list_extend(query_files, lang_files)
local query_files = lang_files
for _, base_lang in ipairs(M.base_language_map[lang] or {}) do
local base_files = filtered_runtime_queries(base_lang, query_name)
if #query_files == 0 then return {} end
local base_langs = {}
-- Now get the base languages by looking at the first line of every file
-- The syntax is the folowing :
-- ;+ inherits: ({language},)*{language}
--
-- {language} ::= {lang} | ({lang})
local MODELINE_FORMAT = "^;+%s*inherits%s*:?%s*([a-z_,()]+)%s*$"
for _, file in ipairs(query_files) do
local modeline = vim.fn.readfile(file, "", 1)
if #modeline == 1 then
local langlist = modeline[1]:match(MODELINE_FORMAT)
if langlist then
for _, lang in ipairs(vim.split(langlist, ',', true)) do
local is_optional = lang:match("%(.*%)")
if is_optional then
if not is_included then
table.insert(base_langs, lang:sub(2, #lang - 1))
end
else
table.insert(base_langs, lang)
end
end
end
end
end
for _, base_lang in ipairs(base_langs) do
local base_files = get_query_files(base_lang, query_name, true)
vim.list_extend(query_files, base_files)
end
@ -99,16 +115,8 @@ local function get_query_files(lang, query_name)
end
function M.has_query_files(lang, query_name)
local langs = {lang}
vim.list_extend(langs, M.base_language_map[lang] or {})
for _, lang in ipairs(langs) do
if runtime_query_exists(lang, query_name) then
return true
end
end
return false
local files = get_query_files(lang, query_name)
return files and #files > 0
end
function M.get_query(lang, query_name)

View file

@ -1,3 +1,5 @@
; inherits: c
[
(for_range_loop)
(class_specifier)

View file

@ -1,3 +1,5 @@
; inherits: c
((identifier) @field
(#match? @field "^_"))

View file

@ -1,3 +1,5 @@
; inherits: c
;; Parameters
(variadic_parameter_declaration
declarator: (variadic_declarator

View file

@ -1,3 +1,4 @@
; inherits: (jsx)
[
(for_in_statement)
(for_statement)

View file

@ -1,3 +1,4 @@
; inherits: (jsx)
; Types
; Javascript

View file

@ -1,3 +1,5 @@
; inherits: (jsx)
; Scopes
;-------

View file

@ -0,0 +1 @@
; inherits: ocaml

View file

@ -0,0 +1 @@
; inherits: ocaml

View file

@ -0,0 +1 @@
; inherits: ocaml

1
queries/tsx/folds.scm Normal file
View file

@ -0,0 +1 @@
; inherits: typescript,jsx

View file

@ -0,0 +1 @@
; inherits: typescript,jsx

1
queries/tsx/locals.scm Normal file
View file

@ -0,0 +1 @@
; inherits: typescript,jsx

View file

@ -1,3 +1,5 @@
; inherits: javascript
[
(interface_declaration)
(internal_module)

View file

@ -1,3 +1,4 @@
; inherits: javascript
[
"abstract"
"declare"

View file

@ -1,2 +1,4 @@
; inherits: javascript
(required_parameter (identifier) @definition)
(optional_parameter (identifier) @definition)