mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-19 11:50:09 -04:00
Improve check-queries (#1253)
- Add checks for injections. - Allow queries that start with [A-Z] for highlights only. - Don't stop on the first error, finish checking all queries.
This commit is contained in:
parent
972f70956a
commit
c1f61d4ca1
5 changed files with 36 additions and 15 deletions
|
|
@ -257,6 +257,16 @@ the node describing the language and `@content` to describe the injection region
|
||||||
```
|
```
|
||||||
@{language} ; e.g. @html to describe a html region
|
@{language} ; e.g. @html to describe a html region
|
||||||
|
|
||||||
@language ; dynamic detection of the injection language (i.e. the text of the captured node describes the language)
|
@language ; dynamic detection of the injection language (i.e. the text of the captured node describes the language).
|
||||||
@content ; region for the dynamically detected language
|
@content ; region for the dynamically detected language.
|
||||||
|
@combined ; This will combine all matches of a pattern as one single block of content.
|
||||||
|
```
|
||||||
|
|
||||||
|
### Indents
|
||||||
|
|
||||||
|
```
|
||||||
|
@indent ; Indent when matching this node
|
||||||
|
@branch ; Dedent when matching this node
|
||||||
|
@return ; Dedent when matching this node
|
||||||
|
@ignore ; Skip this node when calculating the indentation level
|
||||||
```
|
```
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ local M = {}
|
||||||
|
|
||||||
local EMPTY_ITER = function() end
|
local EMPTY_ITER = function() end
|
||||||
|
|
||||||
M.built_in_query_groups = {'highlights', 'locals', 'folds', 'indents'}
|
M.built_in_query_groups = {'highlights', 'locals', 'folds', 'indents', 'injections'}
|
||||||
|
|
||||||
-- Creates a function that checks whether a given query exists
|
-- Creates a function that checks whether a given query exists
|
||||||
-- for a specific language.
|
-- for a specific language.
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
((triple_string) @markdown
|
; TODO: re-add when markdown is added.
|
||||||
(#offset! @markdown 0 3 0 -3))
|
; ((triple_string) @markdown
|
||||||
|
; (#offset! @markdown 0 3 0 -3))
|
||||||
|
|
||||||
(comment) @comment
|
(comment) @comment
|
||||||
|
|
|
||||||
|
|
@ -40,10 +40,11 @@
|
||||||
body: (body (content) @latex))
|
body: (body (content) @latex))
|
||||||
(#eq? @_type "math"))
|
(#eq? @_type "math"))
|
||||||
|
|
||||||
((directive
|
; TODO: re-add when a parser for csv is added.
|
||||||
name: (type) @_type
|
; ((directive
|
||||||
body: (body (content) @csv))
|
; name: (type) @_type
|
||||||
(#eq? @_type "csv-table"))
|
; body: (body (content) @csv))
|
||||||
|
; (#eq? @_type "csv-table"))
|
||||||
|
|
||||||
;; Special roles - prefix
|
;; Special roles - prefix
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,12 @@ local function extract_captures()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Complete captures for injections.
|
||||||
|
local parsers = require 'nvim-treesitter.info'.installed_parsers()
|
||||||
|
for _, lang in pairs(parsers) do
|
||||||
|
table.insert(captures['injections'], lang)
|
||||||
|
end
|
||||||
|
|
||||||
return captures
|
return captures
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -31,18 +37,21 @@ local function do_check()
|
||||||
for _, lang in pairs(parsers) do
|
for _, lang in pairs(parsers) do
|
||||||
for _, query_type in pairs(query_types) do
|
for _, query_type in pairs(query_types) do
|
||||||
print('Checking '..lang..' '..query_type)
|
print('Checking '..lang..' '..query_type)
|
||||||
local ok, query = pcall(queries.get_query,lang, query_type)
|
local ok, query = pcall(queries.get_query, lang, query_type)
|
||||||
if not ok then
|
if not ok then
|
||||||
vim.api.nvim_err_writeln(query)
|
vim.api.nvim_err_writeln(query)
|
||||||
last_error = query
|
last_error = query
|
||||||
else
|
else
|
||||||
if query then
|
if query then
|
||||||
for _, capture in ipairs(query.captures) do
|
for _, capture in ipairs(query.captures) do
|
||||||
if not vim.startswith(capture, "_") -- We ignore things like _helper
|
local is_valid = (
|
||||||
and captures[query_type]
|
vim.startswith(capture, "_") -- Helpers.
|
||||||
and not capture:find("^[A-Z]") -- Highlight groups
|
or vim.tbl_contains(captures[query_type], capture)
|
||||||
and not vim.tbl_contains(captures[query_type], capture) then
|
)
|
||||||
error(string.format("Invalid capture @%s in %s for %s.", capture, query_type, lang))
|
if not is_valid then
|
||||||
|
local error = string.format("(x) Invalid capture @%s in %s for %s.", capture, query_type, lang)
|
||||||
|
print(error)
|
||||||
|
last_error = error
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue