From b0758d354148e6d452a0fe97f146deabb44fcbe3 Mon Sep 17 00:00:00 2001 From: Kai Moschcau Date: Thu, 4 Jan 2024 16:59:28 +0100 Subject: [PATCH 1/7] feat!: change the parser repo to an installable plugin First, makes this repo installable via plugin manager. In addition it already takes care of most setup for the user: - file type detection - parser registration - injections queries Second, it defines `helm` as a language that uses the gotmpl language, but with different injection queries. This allows the standard gotmpl language to use simple text for its text nodes and only injects `yaml` as a language in `helm` templates. The injections are also set to combined, to fix some highlighting issues. Third, it increases the priority of gotmpl highlight queries, so that the gotmpl highlights are always visible above the highlights of any injection language. --- README.md | 136 +++++---------------------- ftplugin/helm.lua | 5 + lua/tree-sitter-go-template/init.lua | 29 ++++++ queries/gotmpl/highlights.scm | 90 ++++++++++++++++++ queries/helm/highlights.scm | 1 + queries/helm/injections.scm | 3 + queries/highlights.scm | 75 --------------- 7 files changed, 150 insertions(+), 189 deletions(-) create mode 100644 ftplugin/helm.lua create mode 100644 lua/tree-sitter-go-template/init.lua create mode 100644 queries/gotmpl/highlights.scm create mode 100644 queries/helm/highlights.scm create mode 100644 queries/helm/injections.scm delete mode 100644 queries/highlights.scm diff --git a/README.md b/README.md index b094abf..b9688c6 100644 --- a/README.md +++ b/README.md @@ -1,133 +1,41 @@ -## Announcement - -I've stopped using helm (which I mostly written this package for) on a daily basis, so I don't really keep up with the development of tree sitter, and nvim-tree-sitter. So this project won't receive any attention from me in the near future. - -If you are interested in mainaining the project, feel free to fork the repo, and if the fork is maintained I will gladly delegate you this repository or link it. - # tree-sitter-go-template [![Build/test](https://github.com/ngalaiko/tree-sitter-go-template/actions/workflows/ci.yaml/badge.svg)](https://github.com/ngalaiko/tree-sitter-go-template/actions/workflows/ci.yaml) [Golang templates][] grammar for [tree-sitter][]. +This includes languages that use Go templates like [helm][]. [tree-sitter]: https://github.com/tree-sitter/tree-sitter [Golang templates]: https://golang.org/pkg/text/template/ -## NeoVim integration using [nvim-treesitter][] - -* Add gotmpl parser following [nvim-treesitter instructions][] - ```lua - local parser_config = require'nvim-treesitter.parsers'.get_parser_configs() - parser_config.gotmpl = { - install_info = { - url = "https://github.com/ngalaiko/tree-sitter-go-template", - files = {"src/parser.c"} - }, - filetype = "gotmpl", - used_by = {"gohtmltmpl", "gotexttmpl", "gotmpl", "yaml"} - } - ``` - Note that `yaml` is listed under `used_by`. I've set this to highlight [helm][] templates as Go Templates instead of yaml. - To ensure that yaml highlighting is still working, you should set up [language injection][] for gotmpl filetypes. - -* Run `:TSInstallFromGrammar gotmpl` to download and compile the grammar into your tree-sitter installation -* Setup filetype detection in `~/.config/nvim/ftdetect/gotmpl.vim`: - ```vimscript - autocmd BufNewFile,BufRead * if search('{{.\+}}', 'nw') | setlocal filetype=gotmpl | endif - ``` -* Define language injection for yaml in ~/.config/nvim/queries/gotmpl/injections.scm: - ```scheme - (text) @yaml - ``` -* Define highlights in `~/.config/nvim/queries/gotmpl/highlights.scm`, for example: - ```scheme - ; Identifiers - - [ - (field) - (field_identifier) - ] @property - - (variable) @variable - - ; Function calls - - (function_call - function: (identifier) @function) - - (method_call - method: (selector_expression - field: (field_identifier) @method)) - - ; Operators - - "|" @operator - ":=" @operator - - ; Builtin functions - - ((identifier) @function.builtin - (#match? @function.builtin "^(and|call|html|index|slice|js|len|not|or|print|printf|println|urlquery|eq|ne|lt|ge|gt|ge)$")) - - ; Delimiters - - "." @punctuation.delimiter - "," @punctuation.delimiter - - "{{" @punctuation.bracket - "}}" @punctuation.bracket - "{{-" @punctuation.bracket - "-}}" @punctuation.bracket - ")" @punctuation.bracket - "(" @punctuation.bracket - - ; Keywords - - [ - "else" - "else if" - "if" - "with" - ] @conditional - - [ - "range" - "end" - "template" - "define" - "block" - ] @keyword +## Announcement - ; Literals +I've stopped using helm (which I mostly written this package for) on a daily +basis, so I don't really keep up with the development of tree sitter, and +nvim-tree-sitter. So this project won't receive any attention from me in the +near future. - [ - (interpreted_string_literal) - (raw_string_literal) - (rune_literal) - ] @string +If you are interested in mainaining the project, feel free to fork the repo, and +if the fork is maintained I will gladly delegate you this repository or link it. - (escape_sequence) @string.special +## Installation - [ - (int_literal) - (float_literal) - (imaginary_literal) - ] @number +Install with your favorite package manager. - [ - (true) - (false) - ] @boolean +Lazy: - [ - (nil) - ] @constant.builtin +```lua +{ + "ngalaiko/tree-sitter-go-template", + dependencies = "nvim-treesitter/nvim-treesitter", + config = true, +} +``` - (comment) @comment - (ERROR) @error - ``` +Then either open a `.gotmpl` file or run `:TSInstall gotmpl` once. Afterwards +you should be able to open helm files and the helm highlighting should work +after installing. If you get an error message after the parser has been +installed, just reload the file and it should work. -[nvim-treesitter instructions]: https://github.com/nvim-treesitter/nvim-treesitter#adding-parsers [nvim-treesitter]: https://github.com/nvim-treesitter/nvim-treesitter [helm]: https://helm.sh -[language injection]: https://tree-sitter.github.io/tree-sitter/syntax-highlighting#language-injection diff --git a/ftplugin/helm.lua b/ftplugin/helm.lua new file mode 100644 index 0000000..9e6bce9 --- /dev/null +++ b/ftplugin/helm.lua @@ -0,0 +1,5 @@ +-- Load the gotmpl parser for helm with helm queries. +vim.treesitter.language.add("helm", { + filetype = "helm", + symbol_name = "gotmpl", +}) diff --git a/lua/tree-sitter-go-template/init.lua b/lua/tree-sitter-go-template/init.lua new file mode 100644 index 0000000..6d17610 --- /dev/null +++ b/lua/tree-sitter-go-template/init.lua @@ -0,0 +1,29 @@ +local M = {} + +M.setup = function(options) + vim.filetype.add { + extension = { + gotmpl = "gotmpl", + }, + pattern = { + [".*/templates/.*%.tpl"] = "helm", + [".*/templates/.*%.yaml"] = "helm", + ["helmfile.*%.yaml"] = "helm", + }, + } + + local path = debug.getinfo(1).source:sub(2):match("(.*[/\\])") or "./" + + local parser_config = require("nvim-treesitter.parsers").get_parser_configs() + + for _, lang in ipairs { "gotmpl", "helm" } do + parser_config[lang] = { + install_info = { + url = path .. "../..", + files = { "src/parser.c" }, + }, + } + end +end + +return M diff --git a/queries/gotmpl/highlights.scm b/queries/gotmpl/highlights.scm new file mode 100644 index 0000000..34b29b5 --- /dev/null +++ b/queries/gotmpl/highlights.scm @@ -0,0 +1,90 @@ +; Identifiers + +([ + (field) + (field_identifier) + ] @property (#set! priority 101)) + +((variable) @variable (#set! priority 101)) + +; Function calls + +(function_call + function: (identifier) @function (#set! priority 101)) + +(method_call + method: (selector_expression + field: (field_identifier) @function (#set! priority 101))) + +; Operators + +([ + "|" + ":=" + ] @operator (#set! priority 101)) + +; Builtin functions + +((identifier) @function.builtin + (#set! priority 101) + (#match? @function.builtin "^(and|call|html|index|slice|js|len|not|or|print|printf|println|urlquery|eq|ne|lt|ge|gt|ge)$")) + +; Delimiters + +([ + "." + "," + ] @punctuation.delimiter (#set! priority 101)) + +([ + "{{" + "}}" + "{{-" + "-}}" + ")" + "(" + ] @punctuation.bracket (#set! priority 101)) + +; Keywords + +([ + "else" + "if" + ] @conditional (#set! priority 101)) + +([ + "range" + "with" + "end" + "template" + "define" + "block" + ] @keyword (#set! priority 101)) + +; Literals + +([ + (interpreted_string_literal) + (raw_string_literal) + (rune_literal) + ] @string (#set! priority 101)) + +((escape_sequence) @string.special (#set! priority 101)) + +([ + (int_literal) + (float_literal) + (imaginary_literal) + ] @number (#set! priority 101)) + +([ + (true) + (false) + ] @boolean (#set! priority 101)) + +([ + (nil) + ] @constant.builtin (#set! priority 101)) + +((comment) @comment (#set! priority 101)) +((ERROR) @error (#set! priority 101)) diff --git a/queries/helm/highlights.scm b/queries/helm/highlights.scm new file mode 100644 index 0000000..6ffb4b7 --- /dev/null +++ b/queries/helm/highlights.scm @@ -0,0 +1 @@ +; inherits: gotmpl diff --git a/queries/helm/injections.scm b/queries/helm/injections.scm new file mode 100644 index 0000000..53e150a --- /dev/null +++ b/queries/helm/injections.scm @@ -0,0 +1,3 @@ +((text) @injection.content + (#set! injection.language "yaml") + (#set! injection.combined)) diff --git a/queries/highlights.scm b/queries/highlights.scm deleted file mode 100644 index 78b9804..0000000 --- a/queries/highlights.scm +++ /dev/null @@ -1,75 +0,0 @@ -; Identifiers - -[ - (field) - (field_identifier) -] @property - -(variable) @variable - -; Function calls - -(function_call - function: (identifier) @function) - -(method_call - method: (selector_expression - field: (field_identifier) @function)) - -; Operators - -"|" @operator -":=" @operator - -; Builtin functions - -((identifier) @function.builtin - (#match? @function.builtin "^(and|call|html|index|slice|js|len|not|or|print|printf|println|urlquery|eq|ne|lt|ge|gt|ge)$")) - -; Delimiters - -"." @punctuation.delimiter -"," @punctuation.delimiter - -"{{" @punctuation.bracket -"}}" @punctuation.bracket -"{{-" @punctuation.bracket -"-}}" @punctuation.bracket -")" @punctuation.bracket -"(" @punctuation.bracket - -; Keywords - -"else" @keyword -"if" @keyword -"range" @keyword -"with" @keyword -"end" @keyword -"template" @keyword -"define" @keyword -"block" @keyword - -; Literals - -[ - (interpreted_string_literal) - (raw_string_literal) - (rune_literal) -] @string - -(escape_sequence) @string.special - -[ - (int_literal) - (float_literal) - (imaginary_literal) -] @number - -[ - (true) - (false) - (nil) -] @constant.builtin - -(comment) @comment -(ERROR) @error From b07319ebb7b37ac20a5a2c550d92aab791485aa2 Mon Sep 17 00:00:00 2001 From: Kai Moschcau Date: Fri, 5 Jan 2024 11:39:44 +0100 Subject: [PATCH 2/7] feat(stylua): add config --- stylua.toml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 stylua.toml diff --git a/stylua.toml b/stylua.toml new file mode 100644 index 0000000..8e011e5 --- /dev/null +++ b/stylua.toml @@ -0,0 +1,4 @@ +column_width = 80 +indent_type = "Spaces" +indent_width = 2 +call_parentheses = "None" From 79084d6b2b08f151f7b6e914be5d9196889fee73 Mon Sep 17 00:00:00 2001 From: Kai Moschcau Date: Fri, 5 Jan 2024 11:40:12 +0100 Subject: [PATCH 3/7] feat(install): fix installation problems and allow config This fixes installation problems and allows the user to pick which additional languages to install and configure. --- README.md | 36 ++++++++++--- lua/tree-sitter-go-template/init.lua | 76 ++++++++++++++++++++++++---- 2 files changed, 95 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index b9688c6..0d71804 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,6 @@ [Golang templates][] grammar for [tree-sitter][]. This includes languages that use Go templates like [helm][]. -[tree-sitter]: https://github.com/tree-sitter/tree-sitter -[Golang templates]: https://golang.org/pkg/text/template/ - ## Announcement I've stopped using helm (which I mostly written this package for) on a daily @@ -18,6 +15,10 @@ near future. If you are interested in mainaining the project, feel free to fork the repo, and if the fork is maintained I will gladly delegate you this repository or link it. +## Dependencies + +- [nvim-treesitter][] + ## Installation Install with your favorite package manager. @@ -32,10 +33,29 @@ Lazy: } ``` -Then either open a `.gotmpl` file or run `:TSInstall gotmpl` once. Afterwards -you should be able to open helm files and the helm highlighting should work -after installing. If you get an error message after the parser has been -installed, just reload the file and it should work. +The parsers will be installed once you restart neovim. -[nvim-treesitter]: https://github.com/nvim-treesitter/nvim-treesitter +## Configuration + +If you only want to install a subset of the additional languages or none at all, +you can pass these via the `additional_langs` option. By default all additional +languages are installed. + +```lua +{ + "ngalaiko/tree-sitter-go-template", + dependencies = "nvim-treesitter/nvim-treesitter", + opts = { + additional_langs = { "helm" }, + }, +} +``` + +## Available additional languages + +- [helm][] + +[Golang templates]: https://golang.org/pkg/text/template/ [helm]: https://helm.sh +[nvim-treesitter]: https://github.com/nvim-treesitter/nvim-treesitter +[tree-sitter]: https://github.com/tree-sitter/tree-sitter diff --git a/lua/tree-sitter-go-template/init.lua b/lua/tree-sitter-go-template/init.lua index 6d17610..256dc8c 100644 --- a/lua/tree-sitter-go-template/init.lua +++ b/lua/tree-sitter-go-template/init.lua @@ -1,22 +1,68 @@ local M = {} -M.setup = function(options) +local lang_name = "gotmpl" + +local known_additional_langs = { + "helm", +} + +local path = debug.getinfo(1).source:sub(2):match "(.*[/\\])" or "./" +local parser_config = require("nvim-treesitter.parsers").get_parser_configs() + +--- @param additional_langs string[] +local function configure_filetype(additional_langs) vim.filetype.add { extension = { - gotmpl = "gotmpl", + [lang_name] = lang_name }, - pattern = { - [".*/templates/.*%.tpl"] = "helm", - [".*/templates/.*%.yaml"] = "helm", - ["helmfile.*%.yaml"] = "helm", + } + + if vim.tbl_contains(additional_langs, "helm") then + vim.filetype.add { + pattern = { + [".*/templates/.*%.tpl"] = "helm", + [".*/templates/.*%.yaml"] = "helm", + ["helmfile.*%.yaml"] = "helm", + }, + } + end +end + +local function register_and_install_gotmpl() + parser_config[lang_name] = { + install_info = { + url = path .. "../..", + files = { "src/parser.c" }, }, } - local path = debug.getinfo(1).source:sub(2):match("(.*[/\\])") or "./" + require("nvim-treesitter.install").ensure_installed_sync(lang_name) +end - local parser_config = require("nvim-treesitter.parsers").get_parser_configs() +--- @param additional_langs string[] +local function register_and_install_additional(additional_langs) + local unknown_langs = {} - for _, lang in ipairs { "gotmpl", "helm" } do + local langs_to_install = vim.tbl_filter(function(l) + local is_known = vim.tbl_contains(known_additional_langs, l) + + if not is_known then + table.insert(unknown_langs, l) + end + + return is_known + end, additional_langs) + + if not vim.tbl_isempty(unknown_langs) then + vim.notify( + "Unknown languages: " + .. table.concat(unknown_langs, ", ", 1, #unknown_langs), + vim.log.levels.WARN, + { title = "tree-sitter-go-template" } + ) + end + + for _, lang in ipairs(langs_to_install) do parser_config[lang] = { install_info = { url = path .. "../..", @@ -24,6 +70,18 @@ M.setup = function(options) }, } end + + if not vim.tbl_isempty(langs_to_install) then + require("nvim-treesitter.install").ensure_installed(langs_to_install) + end +end + +M.setup = function(options) + local additional_langs = options.additional_langs or { "helm" } + + configure_filetype(additional_langs) + register_and_install_gotmpl() + register_and_install_additional(additional_langs) end return M From 4429458282877143e7e13de7c8eef0cd69b3eabf Mon Sep 17 00:00:00 2001 From: Kai Moschcau Date: Sat, 6 Jan 2024 11:40:04 +0100 Subject: [PATCH 4/7] feat(ftplugin): set commentstring local option --- ftplugin/gotmpl.lua | 2 ++ ftplugin/helm.lua | 3 +++ 2 files changed, 5 insertions(+) create mode 100644 ftplugin/gotmpl.lua diff --git a/ftplugin/gotmpl.lua b/ftplugin/gotmpl.lua new file mode 100644 index 0000000..47025ca --- /dev/null +++ b/ftplugin/gotmpl.lua @@ -0,0 +1,2 @@ +-- set up the gotmpl commentstring +vim.opt_local.commentstring = "{{/* %s */}}" diff --git a/ftplugin/helm.lua b/ftplugin/helm.lua index 9e6bce9..e2a9a02 100644 --- a/ftplugin/helm.lua +++ b/ftplugin/helm.lua @@ -3,3 +3,6 @@ vim.treesitter.language.add("helm", { filetype = "helm", symbol_name = "gotmpl", }) + +-- set up the gotmpl commentstring +vim.opt_local.commentstring = "{{/* %s */}}" From e7d770eab225dba0eaa2c7983aca5c3f3226f2c2 Mon Sep 17 00:00:00 2001 From: Kai Moschcau Date: Mon, 29 Jan 2024 17:12:10 +0100 Subject: [PATCH 5/7] feat(queries): update to new capture names and improve keywords --- queries/gotmpl/highlights.scm | 68 ++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 21 deletions(-) diff --git a/queries/gotmpl/highlights.scm b/queries/gotmpl/highlights.scm index 34b29b5..fdfbd37 100644 --- a/queries/gotmpl/highlights.scm +++ b/queries/gotmpl/highlights.scm @@ -16,6 +16,13 @@ method: (selector_expression field: (field_identifier) @function (#set! priority 101))) +; Builtin functions + +(function_call + function: (identifier) @function.builtin + (#set! priority 101) + (#match? @function.builtin "^(and|call|html|index|slice|js|len|not|or|print|printf|println|urlquery|eq|ne|lt|ge|gt|ge)$")) + ; Operators ([ @@ -23,12 +30,6 @@ ":=" ] @operator (#set! priority 101)) -; Builtin functions - -((identifier) @function.builtin - (#set! priority 101) - (#match? @function.builtin "^(and|call|html|index|slice|js|len|not|or|print|printf|println|urlquery|eq|ne|lt|ge|gt|ge)$")) - ; Delimiters ([ @@ -45,21 +46,46 @@ "(" ] @punctuation.bracket (#set! priority 101)) -; Keywords - -([ - "else" - "if" - ] @conditional (#set! priority 101)) - -([ - "range" - "with" - "end" - "template" - "define" - "block" - ] @keyword (#set! priority 101)) +; Actions + +(if_action + [ + "if" + "else" + "else if" + "end" + ] @keyword.conditional (#set! priority 101)) + +(range_action + [ + "range" + "else" + "end" + ] @keyword.repeat (#set! priority 101)) + +(template_action + [ + "template" + ] @function.builtin (#set! priority 101)) + +(block_action + [ + "block" + "end" + ] @keyword.directive (#set! priority 101)) + +(define_action + [ + "define" + "end" + ] @keyword.directive.define (#set! priority 101)) + +(with_action + [ + "with" + "else" + "end" + ] @keyword.conditional (#set! priority 101)) ; Literals From 0e65a8f5bcedf104696633dd3d25038048cec2be Mon Sep 17 00:00:00 2001 From: Kai Moschcau Date: Mon, 4 Mar 2024 09:57:48 +0100 Subject: [PATCH 6/7] feat!: remove the parser installer logic The parsers are now part of nvim-treesitter core. --- ftplugin/helm.lua | 6 --- lua/tree-sitter-go-template/init.lua | 61 +--------------------------- 2 files changed, 1 insertion(+), 66 deletions(-) diff --git a/ftplugin/helm.lua b/ftplugin/helm.lua index e2a9a02..47025ca 100644 --- a/ftplugin/helm.lua +++ b/ftplugin/helm.lua @@ -1,8 +1,2 @@ --- Load the gotmpl parser for helm with helm queries. -vim.treesitter.language.add("helm", { - filetype = "helm", - symbol_name = "gotmpl", -}) - -- set up the gotmpl commentstring vim.opt_local.commentstring = "{{/* %s */}}" diff --git a/lua/tree-sitter-go-template/init.lua b/lua/tree-sitter-go-template/init.lua index 256dc8c..1360609 100644 --- a/lua/tree-sitter-go-template/init.lua +++ b/lua/tree-sitter-go-template/init.lua @@ -1,19 +1,10 @@ local M = {} -local lang_name = "gotmpl" - -local known_additional_langs = { - "helm", -} - -local path = debug.getinfo(1).source:sub(2):match "(.*[/\\])" or "./" -local parser_config = require("nvim-treesitter.parsers").get_parser_configs() - --- @param additional_langs string[] local function configure_filetype(additional_langs) vim.filetype.add { extension = { - [lang_name] = lang_name + gotmpl = "gotmpl" }, } @@ -28,60 +19,10 @@ local function configure_filetype(additional_langs) end end -local function register_and_install_gotmpl() - parser_config[lang_name] = { - install_info = { - url = path .. "../..", - files = { "src/parser.c" }, - }, - } - - require("nvim-treesitter.install").ensure_installed_sync(lang_name) -end - ---- @param additional_langs string[] -local function register_and_install_additional(additional_langs) - local unknown_langs = {} - - local langs_to_install = vim.tbl_filter(function(l) - local is_known = vim.tbl_contains(known_additional_langs, l) - - if not is_known then - table.insert(unknown_langs, l) - end - - return is_known - end, additional_langs) - - if not vim.tbl_isempty(unknown_langs) then - vim.notify( - "Unknown languages: " - .. table.concat(unknown_langs, ", ", 1, #unknown_langs), - vim.log.levels.WARN, - { title = "tree-sitter-go-template" } - ) - end - - for _, lang in ipairs(langs_to_install) do - parser_config[lang] = { - install_info = { - url = path .. "../..", - files = { "src/parser.c" }, - }, - } - end - - if not vim.tbl_isempty(langs_to_install) then - require("nvim-treesitter.install").ensure_installed(langs_to_install) - end -end - M.setup = function(options) local additional_langs = options.additional_langs or { "helm" } configure_filetype(additional_langs) - register_and_install_gotmpl() - register_and_install_additional(additional_langs) end return M From 2292c402598428eb490465409b53af2bfb93c390 Mon Sep 17 00:00:00 2001 From: Kai Moschcau Date: Mon, 4 Mar 2024 09:58:31 +0100 Subject: [PATCH 7/7] feat: align the queries with the ones in nvim-treesitter core --- queries/gotmpl/highlights.scm | 53 ++++++++++++++++++----------------- queries/gotmpl/injections.scm | 34 ++++++++++++++++++++++ queries/helm/highlights.scm | 48 +++++++++++++++++++++++++++++++ queries/helm/injections.scm | 40 ++++++++++++++++++++++++-- 4 files changed, 147 insertions(+), 28 deletions(-) create mode 100644 queries/gotmpl/injections.scm diff --git a/queries/gotmpl/highlights.scm b/queries/gotmpl/highlights.scm index fdfbd37..f65ae6a 100644 --- a/queries/gotmpl/highlights.scm +++ b/queries/gotmpl/highlights.scm @@ -3,39 +3,41 @@ ([ (field) (field_identifier) - ] @property (#set! priority 101)) + ] @variable.member (#set! priority 105)) -((variable) @variable (#set! priority 101)) +((variable) @variable (#set! priority 105)) ; Function calls (function_call - function: (identifier) @function (#set! priority 101)) + function: (identifier) @function (#set! priority 105)) (method_call method: (selector_expression - field: (field_identifier) @function (#set! priority 101))) + field: (field_identifier) @function (#set! priority 105))) ; Builtin functions (function_call function: (identifier) @function.builtin - (#set! priority 101) - (#match? @function.builtin "^(and|call|html|index|slice|js|len|not|or|print|printf|println|urlquery|eq|ne|lt|ge|gt|ge)$")) + (#set! priority 105) + (#any-of? @function.builtin + "and" "call" "html" "index" "slice" "js" "len" "not" "or" "print" "printf" "println" "urlquery" + "eq" "ne" "lt" "ge" "gt" "ge")) ; Operators ([ "|" ":=" - ] @operator (#set! priority 101)) + ] @operator (#set! priority 105)) ; Delimiters ([ "." "," - ] @punctuation.delimiter (#set! priority 101)) + ] @punctuation.delimiter (#set! priority 105)) ([ "{{" @@ -44,7 +46,7 @@ "-}}" ")" "(" - ] @punctuation.bracket (#set! priority 101)) + ] @punctuation.bracket (#set! priority 105)) ; Actions @@ -54,63 +56,62 @@ "else" "else if" "end" - ] @keyword.conditional (#set! priority 101)) + ] @keyword.conditional (#set! priority 105)) (range_action [ "range" "else" "end" - ] @keyword.repeat (#set! priority 101)) + ] @keyword.repeat (#set! priority 105)) (template_action [ "template" - ] @function.builtin (#set! priority 101)) + ] @function.builtin (#set! priority 105)) (block_action [ "block" "end" - ] @keyword.directive (#set! priority 101)) + ] @keyword.directive (#set! priority 105)) (define_action [ "define" "end" - ] @keyword.directive.define (#set! priority 101)) + ] @keyword.directive.define (#set! priority 105)) (with_action [ "with" "else" "end" - ] @keyword.conditional (#set! priority 101)) + ] @keyword.conditional (#set! priority 105)) ; Literals ([ (interpreted_string_literal) (raw_string_literal) - (rune_literal) - ] @string (#set! priority 101)) + ] @string (#set! priority 105)) -((escape_sequence) @string.special (#set! priority 101)) +((rune_literal) @string.special.symbol (#set! priority 105)) + +((escape_sequence) @string.escape (#set! priority 105)) ([ (int_literal) - (float_literal) (imaginary_literal) - ] @number (#set! priority 101)) + ] @number (#set! priority 105)) + +((float_literal) @number.float (#set! priority 105)) ([ (true) (false) - ] @boolean (#set! priority 101)) + ] @boolean (#set! priority 105)) -([ - (nil) - ] @constant.builtin (#set! priority 101)) +((nil) @constant.builtin (#set! priority 105)) -((comment) @comment (#set! priority 101)) -((ERROR) @error (#set! priority 101)) +((comment) @comment @spell (#set! priority 105)) diff --git a/queries/gotmpl/injections.scm b/queries/gotmpl/injections.scm new file mode 100644 index 0000000..787ca99 --- /dev/null +++ b/queries/gotmpl/injections.scm @@ -0,0 +1,34 @@ +((comment) @injection.content + (#set! injection.language "comment")) + +; {{"put" | printf "%s%s" "out" | printf "%q"}} +(function_call + function: (identifier) @_function + arguments: + (argument_list + . + (interpreted_string_literal) @injection.content) + (#eq? @_function "printf") + (#set! injection.language "printf")) + +; {{ js "var a = 1 + 1" }} +(function_call + function: (identifier) @_function + arguments: + (argument_list + . + (interpreted_string_literal) @injection.content) + (#eq? @_function "js") + (#offset! @injection.content 0 1 0 -1) + (#set! injection.language "javascript")) + +; {{ html "

hello

" }} +(function_call + function: (identifier) @_function + arguments: + (argument_list + . + (interpreted_string_literal) @injection.content) + (#eq? @_function "html") + (#offset! @injection.content 0 1 0 -1) + (#set! injection.language "html")) diff --git a/queries/helm/highlights.scm b/queries/helm/highlights.scm index 6ffb4b7..d27a0e4 100644 --- a/queries/helm/highlights.scm +++ b/queries/helm/highlights.scm @@ -1 +1,49 @@ ; inherits: gotmpl + +; Builtin functions +(function_call + function: (identifier) @function.builtin + (#set! priority 105) + (#any-of? @function.builtin + "and" "or" "not" "eq" "ne" "lt" "le" "gt" "ge" "default" "required" "empty" "fail" "coalesce" + "ternary" "print" "println" "printf" "trim" "trimAll" "trimPrefix" "trimSuffix" "lower" "upper" + "title" "untitle" "repeat" "substr" "nospace" "trunc" "abbrev" "abbrevboth" "initials" + "randAlphaNum" "randAlpha" "randNumeric" "randAscii" "wrap" "wrapWith" "contains" "hasPrefix" + "hasSuffix" "quote" "squote" "cat" "indent" "nindent" "replace" "plural" "snakecase" "camelcase" + "kebabcase" "swapcase" "shuffle" "toStrings" "toDecimal" "toJson" "mustToJson" "toPrettyJson" + "mustToPrettyJson" "toRawJson" "mustToRawJson" "fromYaml" "fromJson" "fromJsonArray" + "fromYamlArray" "toYaml" "regexMatch" "mustRegexMatch" "regexFindAll" "mustRegexFinDall" + "regexFind" "mustRegexFind" "regexReplaceAll" "mustRegexReplaceAll" "regexReplaceAllLiteral" + "mustRegexReplaceAllLiteral" "regexSplit" "mustRegexSplit" "sha1sum" "sha256sum" "adler32sum" + "htpasswd" "derivePassword" "genPrivateKey" "buildCustomCert" "genCA" "genSelfSignedCert" + "genSignedCert" "encryptAES" "decryptAES" "now" "ago" "date" "dateInZone" "duration" + "durationRound" "unixEpoch" "dateModify" "mustDateModify" "htmlDate" "htmlDateInZone" "toDate" + "mustToDate" "dict" "get" "set" "unset" "hasKey" "pluck" "dig" "merge" "mustMerge" + "mergeOverwrite" "mustMergeOverwrite" "keys" "pick" "omit" "values" "deepCopy" "mustDeepCopy" + "b64enc" "b64dec" "b32enc" "b32dec" "list" "first" "mustFirst" "rest" "mustRest" "last" + "mustLast" "initial" "mustInitial" "append" "mustAppend" "prepend" "mustPrepend" "concat" + "reverse" "mustReverse" "uniq" "mustUniq" "without" "mustWithout" "has" "mustHas" "compact" + "mustCompact" "index" "slice" "mustSlice" "until" "untilStep" "seq" "add" "add1" "sub" "div" + "mod" "mul" "max" "min" "len" "addf" "add1f" "subf" "divf" "mulf" "maxf" "minf" "floor" "ceil" + "round" "getHostByName" "base" "dir" "clean" "ext" "isAbs" "kindOf" "kindIs" "typeOf" "typeIs" + "typeIsLike" "deepequal" "semver" "semverCompare" "urlParse" "urlJoin" "urlquery" "lookup" + "include") + ) + +; {{ .Values.test }} +(selector_expression + operand: + (field + name: (identifier) @constant.builtin + (#set! priority 105) + (#any-of? @constant.builtin + "Values" "Chart" "Release" "Capabilities" "Files" "Subcharts" "Template")) + (field_identifier)) + +; {{ $.Values.test }} +(selector_expression + operand: (variable) + field: (field_identifier) @constant.builtin + (#set! priority 105) + (#any-of? @constant.builtin + "Values" "Chart" "Release" "Capabilities" "Files" "Subcharts" "Template")) diff --git a/queries/helm/injections.scm b/queries/helm/injections.scm index 53e150a..12f1b7c 100644 --- a/queries/helm/injections.scm +++ b/queries/helm/injections.scm @@ -1,3 +1,39 @@ +; inherits: gotmpl + ((text) @injection.content - (#set! injection.language "yaml") - (#set! injection.combined)) + (#set! injection.language "yaml") + (#set! injection.combined)) + +; {{ regexFind "[a-zA-Z][1-9]" "abcd1234" }} +(function_call + function: (identifier) @_function + arguments: + (argument_list + . + (interpreted_string_literal) @injection.content) + (#any-of? @_function + "regexMatch" "mustRegexMatch" "regexFindAll" "mustRegexFinDall" "regexFind" "mustRegexFind" + "regexReplaceAll" "mustRegexReplaceAll" "regexReplaceAllLiteral" "mustRegexReplaceAllLiteral" + "regexSplit" "mustRegexSplit") + (#offset! @injection.content 0 1 0 -1) + (#set! injection.language "regex")) + +(function_call + function: (identifier) @_function + arguments: + (argument_list + . + (interpreted_string_literal) @injection.content) + (#any-of? @_function "fromYaml" "fromYamlArray") + (#offset! @injection.content 0 1 0 -1) + (#set! injection.language "yaml")) + +(function_call + function: (identifier) @_function + arguments: + (argument_list + . + (interpreted_string_literal) @injection.content) + (#any-of? @_function "fromJson" "fromJsonArray") + (#offset! @injection.content 0 1 0 -1) + (#set! injection.language "json"))