mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-01 19:17:02 -04:00
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:
parent
9ad47c65bd
commit
c055899dc0
17 changed files with 73 additions and 32 deletions
|
|
@ -67,6 +67,18 @@ Here are some global advices :
|
||||||
- Examples of queries can be found in [queries/](queries/)
|
- Examples of queries can be found in [queries/](queries/)
|
||||||
- Matches in the bottom will override queries that are above of them.
|
- 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
|
### Highlights
|
||||||
|
|
||||||
As languages differ quite a lot, here is a set of captures available to you when building a `highlights.scm` query.
|
As languages differ quite a lot, here is a set of captures available to you when building a `highlights.scm` query.
|
||||||
|
|
|
||||||
|
|
@ -8,19 +8,9 @@ local M = {}
|
||||||
|
|
||||||
local query_cache = caching.create_buffer_cache()
|
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'}
|
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.
|
-- for a specific language.
|
||||||
local function get_query_guard(query)
|
local function get_query_guard(query)
|
||||||
return function(lang)
|
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 {})
|
return filter_files(api.nvim_get_runtime_file(string.format('queries/%s/%s.scm', lang, query_name), true) or {})
|
||||||
end
|
end
|
||||||
|
|
||||||
local function runtime_query_exists(lang, query_name)
|
local function get_query_files(lang, query_name, is_included)
|
||||||
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 lang_files = filtered_runtime_queries(lang, query_name)
|
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
|
if #query_files == 0 then return {} end
|
||||||
local base_files = filtered_runtime_queries(base_lang, query_name)
|
|
||||||
|
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)
|
vim.list_extend(query_files, base_files)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -99,16 +115,8 @@ local function get_query_files(lang, query_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.has_query_files(lang, query_name)
|
function M.has_query_files(lang, query_name)
|
||||||
local langs = {lang}
|
local files = get_query_files(lang, query_name)
|
||||||
vim.list_extend(langs, M.base_language_map[lang] or {})
|
return files and #files > 0
|
||||||
|
|
||||||
for _, lang in ipairs(langs) do
|
|
||||||
if runtime_query_exists(lang, query_name) then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return false
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.get_query(lang, query_name)
|
function M.get_query(lang, query_name)
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
; inherits: c
|
||||||
|
|
||||||
[
|
[
|
||||||
(for_range_loop)
|
(for_range_loop)
|
||||||
(class_specifier)
|
(class_specifier)
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
; inherits: c
|
||||||
|
|
||||||
((identifier) @field
|
((identifier) @field
|
||||||
(#match? @field "^_"))
|
(#match? @field "^_"))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
; inherits: c
|
||||||
|
|
||||||
;; Parameters
|
;; Parameters
|
||||||
(variadic_parameter_declaration
|
(variadic_parameter_declaration
|
||||||
declarator: (variadic_declarator
|
declarator: (variadic_declarator
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
; inherits: (jsx)
|
||||||
[
|
[
|
||||||
(for_in_statement)
|
(for_in_statement)
|
||||||
(for_statement)
|
(for_statement)
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
; inherits: (jsx)
|
||||||
; Types
|
; Types
|
||||||
|
|
||||||
; Javascript
|
; Javascript
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
; inherits: (jsx)
|
||||||
|
|
||||||
; Scopes
|
; Scopes
|
||||||
;-------
|
;-------
|
||||||
|
|
||||||
|
|
|
||||||
1
queries/ocaml_interface/folds.scm
Normal file
1
queries/ocaml_interface/folds.scm
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
; inherits: ocaml
|
||||||
1
queries/ocaml_interface/highlights.scm
Normal file
1
queries/ocaml_interface/highlights.scm
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
; inherits: ocaml
|
||||||
1
queries/ocaml_interface/locals.scm
Normal file
1
queries/ocaml_interface/locals.scm
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
; inherits: ocaml
|
||||||
1
queries/tsx/folds.scm
Normal file
1
queries/tsx/folds.scm
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
; inherits: typescript,jsx
|
||||||
1
queries/tsx/highlights.scm
Normal file
1
queries/tsx/highlights.scm
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
; inherits: typescript,jsx
|
||||||
1
queries/tsx/locals.scm
Normal file
1
queries/tsx/locals.scm
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
; inherits: typescript,jsx
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
; inherits: javascript
|
||||||
|
|
||||||
[
|
[
|
||||||
(interface_declaration)
|
(interface_declaration)
|
||||||
(internal_module)
|
(internal_module)
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
; inherits: javascript
|
||||||
[
|
[
|
||||||
"abstract"
|
"abstract"
|
||||||
"declare"
|
"declare"
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,4 @@
|
||||||
|
; inherits: javascript
|
||||||
|
|
||||||
(required_parameter (identifier) @definition)
|
(required_parameter (identifier) @definition)
|
||||||
(optional_parameter (identifier) @definition)
|
(optional_parameter (identifier) @definition)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue