-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change the parser repo to an installable plugin and update queries/injections #13
base: master
Are you sure you want to change the base?
Changes from all commits
b0758d3
b07319e
79084d6
4429458
e7d770e
0e65a8f
2292c40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,133 +1,61 @@ | ||
## 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/ | ||
## Announcement | ||
|
||
## 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 | ||
|
||
; Literals | ||
|
||
[ | ||
(interpreted_string_literal) | ||
(raw_string_literal) | ||
(rune_literal) | ||
] @string | ||
|
||
(escape_sequence) @string.special | ||
|
||
[ | ||
(int_literal) | ||
(float_literal) | ||
(imaginary_literal) | ||
] @number | ||
|
||
[ | ||
(true) | ||
(false) | ||
] @boolean | ||
|
||
[ | ||
(nil) | ||
] @constant.builtin | ||
|
||
(comment) @comment | ||
(ERROR) @error | ||
``` | ||
|
||
[nvim-treesitter instructions]: https://github.com/nvim-treesitter/nvim-treesitter#adding-parsers | ||
[nvim-treesitter]: https://github.com/nvim-treesitter/nvim-treesitter | ||
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. | ||
|
||
## Dependencies | ||
|
||
- [nvim-treesitter][] | ||
|
||
## Installation | ||
|
||
Install with your favorite package manager. | ||
|
||
Lazy: | ||
|
||
```lua | ||
{ | ||
"ngalaiko/tree-sitter-go-template", | ||
dependencies = "nvim-treesitter/nvim-treesitter", | ||
config = true, | ||
} | ||
``` | ||
|
||
The parsers will be installed once you restart neovim. | ||
|
||
## 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 | ||
[language injection]: https://tree-sitter.github.io/tree-sitter/syntax-highlighting#language-injection | ||
[nvim-treesitter]: https://github.com/nvim-treesitter/nvim-treesitter | ||
[tree-sitter]: https://github.com/tree-sitter/tree-sitter |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- set up the gotmpl commentstring | ||
vim.opt_local.commentstring = "{{/* %s */}}" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- set up the gotmpl commentstring | ||
vim.opt_local.commentstring = "{{/* %s */}}" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
local M = {} | ||
|
||
--- @param additional_langs string[] | ||
local function configure_filetype(additional_langs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This functions seems to be generic, but then it contains details for the helm filetype. Maybe we should just make one There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this is just a misleading name. I named it after |
||
vim.filetype.add { | ||
extension = { | ||
gotmpl = "gotmpl" | ||
}, | ||
} | ||
|
||
if vim.tbl_contains(additional_langs, "helm") then | ||
vim.filetype.add { | ||
kmoschcau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pattern = { | ||
[".*/templates/.*%.tpl"] = "helm", | ||
[".*/templates/.*%.yaml"] = "helm", | ||
["helmfile.*%.yaml"] = "helm", | ||
}, | ||
} | ||
end | ||
end | ||
|
||
M.setup = function(options) | ||
local additional_langs = options.additional_langs or { "helm" } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we just just add the helm filetype always and remove the whole config option to keep things simple. |
||
|
||
configure_filetype(additional_langs) | ||
end | ||
|
||
return M |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
; Identifiers | ||
|
||
([ | ||
(field) | ||
(field_identifier) | ||
] @variable.member (#set! priority 105)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess we can now move the priorities to the nivm-treesitter repo and delete the queries here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I opened a PR for the queries in nvim-treesitter. |
||
|
||
((variable) @variable (#set! priority 105)) | ||
|
||
; Function calls | ||
|
||
(function_call | ||
function: (identifier) @function (#set! priority 105)) | ||
|
||
(method_call | ||
method: (selector_expression | ||
field: (field_identifier) @function (#set! priority 105))) | ||
|
||
; Builtin functions | ||
|
||
(function_call | ||
function: (identifier) @function.builtin | ||
(#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 105)) | ||
|
||
; Delimiters | ||
|
||
([ | ||
"." | ||
"," | ||
] @punctuation.delimiter (#set! priority 105)) | ||
|
||
([ | ||
"{{" | ||
"}}" | ||
"{{-" | ||
"-}}" | ||
")" | ||
"(" | ||
] @punctuation.bracket (#set! priority 105)) | ||
|
||
; Actions | ||
|
||
(if_action | ||
[ | ||
"if" | ||
"else" | ||
"else if" | ||
"end" | ||
] @keyword.conditional (#set! priority 105)) | ||
|
||
(range_action | ||
[ | ||
"range" | ||
"else" | ||
"end" | ||
] @keyword.repeat (#set! priority 105)) | ||
|
||
(template_action | ||
[ | ||
"template" | ||
] @function.builtin (#set! priority 105)) | ||
|
||
(block_action | ||
[ | ||
"block" | ||
"end" | ||
] @keyword.directive (#set! priority 105)) | ||
|
||
(define_action | ||
[ | ||
"define" | ||
"end" | ||
] @keyword.directive.define (#set! priority 105)) | ||
|
||
(with_action | ||
[ | ||
"with" | ||
"else" | ||
"end" | ||
] @keyword.conditional (#set! priority 105)) | ||
|
||
; Literals | ||
|
||
([ | ||
(interpreted_string_literal) | ||
(raw_string_literal) | ||
] @string (#set! priority 105)) | ||
|
||
((rune_literal) @string.special.symbol (#set! priority 105)) | ||
|
||
((escape_sequence) @string.escape (#set! priority 105)) | ||
|
||
([ | ||
(int_literal) | ||
(imaginary_literal) | ||
] @number (#set! priority 105)) | ||
|
||
((float_literal) @number.float (#set! priority 105)) | ||
|
||
([ | ||
(true) | ||
(false) | ||
] @boolean (#set! priority 105)) | ||
|
||
((nil) @constant.builtin (#set! priority 105)) | ||
|
||
((comment) @comment @spell (#set! priority 105)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 "<h1>hello</h1>" }} | ||
(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")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this should be more like:
:TSInstall (gotmpl|helm)