From c1f61d4ca1319747c27ec97b5f050b3d936e0a55 Mon Sep 17 00:00:00 2001 From: Santos Gallegos Date: Mon, 31 May 2021 12:03:27 -0500 Subject: [PATCH] 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. --- CONTRIBUTING.md | 14 ++++++++++++-- lua/nvim-treesitter/query.lua | 2 +- queries/julia/injections.scm | 5 +++-- queries/rst/injections.scm | 9 +++++---- scripts/check-queries.lua | 21 +++++++++++++++------ 5 files changed, 36 insertions(+), 15 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index cec51bdc7..909ad9eeb 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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 ; dynamic detection of the injection language (i.e. the text of the captured node describes the language) -@content ; region for the dynamically detected 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. +@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 ``` diff --git a/lua/nvim-treesitter/query.lua b/lua/nvim-treesitter/query.lua index 07896f22f..397d733f4 100644 --- a/lua/nvim-treesitter/query.lua +++ b/lua/nvim-treesitter/query.lua @@ -9,7 +9,7 @@ local M = {} 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 -- for a specific language. diff --git a/queries/julia/injections.scm b/queries/julia/injections.scm index 5ae30dfa7..be2412c06 100644 --- a/queries/julia/injections.scm +++ b/queries/julia/injections.scm @@ -1,4 +1,5 @@ -((triple_string) @markdown - (#offset! @markdown 0 3 0 -3)) +; TODO: re-add when markdown is added. +; ((triple_string) @markdown +; (#offset! @markdown 0 3 0 -3)) (comment) @comment diff --git a/queries/rst/injections.scm b/queries/rst/injections.scm index 6d62550ad..50c3b46b7 100644 --- a/queries/rst/injections.scm +++ b/queries/rst/injections.scm @@ -40,10 +40,11 @@ body: (body (content) @latex)) (#eq? @_type "math")) -((directive - name: (type) @_type - body: (body (content) @csv)) - (#eq? @_type "csv-table")) +; TODO: re-add when a parser for csv is added. +; ((directive +; name: (type) @_type +; body: (body (content) @csv)) +; (#eq? @_type "csv-table")) ;; Special roles - prefix diff --git a/scripts/check-queries.lua b/scripts/check-queries.lua index 623ad6bfd..1429c5546 100755 --- a/scripts/check-queries.lua +++ b/scripts/check-queries.lua @@ -17,6 +17,12 @@ local function extract_captures() 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 end @@ -31,18 +37,21 @@ local function do_check() for _, lang in pairs(parsers) do for _, query_type in pairs(query_types) do 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 vim.api.nvim_err_writeln(query) last_error = query else if query then for _, capture in ipairs(query.captures) do - if not vim.startswith(capture, "_") -- We ignore things like _helper - and captures[query_type] - and not capture:find("^[A-Z]") -- Highlight groups - and not vim.tbl_contains(captures[query_type], capture) then - error(string.format("Invalid capture @%s in %s for %s.", capture, query_type, lang)) + local is_valid = ( + vim.startswith(capture, "_") -- Helpers. + or vim.tbl_contains(captures[query_type], capture) + ) + 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