-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(install): rework NeoVim instructions
The parser and the queries are now added to the nvim-treesitter repo
- Loading branch information
Showing
1 changed file
with
32 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,133 +1,55 @@ | ||
## Announcement | ||
## 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. | ||
The original author stopped using helm (which he 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. | ||
Some people have been found to keep the repo going, so if you have ideas for improvements feel free to open issues or pull requests. | ||
|
||
# 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][]. | ||
|
||
[tree-sitter]: https://github.com/tree-sitter/tree-sitter | ||
[Golang templates]: https://golang.org/pkg/text/template/ | ||
Also includes a [helm][] dialect. The concept of a dialect is that it uses the same parser, but can have different queries (e.g. for different injections). | ||
More dialects could be added in the future (e.g. for html templates). | ||
|
||
## 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 | ||
- Setup filetype detection: | ||
|
||
; Builtin functions | ||
- for [helm][] the [vim-helm] plugin is recommended | ||
- for gotmpl you can add your own filetype in `~/.config/nvim/ftdetect/gotmpl.vim`: | ||
|
||
((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)$")) | ||
```vimscript | ||
au BufRead,BufNewFile *.gotmpl set filetype=gotmpl | ||
``` | ||
; Delimiters | ||
or using [lua](https://neovim.io/doc/user/lua.html#vim.filetype): | ||
"." @punctuation.delimiter | ||
"," @punctuation.delimiter | ||
```lua | ||
vim.filetype.add({ | ||
extension = { | ||
gotmpl = 'gotmpl', | ||
}, | ||
}) | ||
``` | ||
"{{" @punctuation.bracket | ||
"}}" @punctuation.bracket | ||
"{{-" @punctuation.bracket | ||
"-}}" @punctuation.bracket | ||
")" @punctuation.bracket | ||
"(" @punctuation.bracket | ||
- Install [nvim-treesitter][] as described here: [nvim-treesitter-install][] | ||
- Install the parser you want to use: | ||
; Keywords | ||
```vim | ||
:TSInstall gotmpl | ||
:TSInstall helm | ||
``` | ||
|
||
[ | ||
"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 | ||
``` | ||
> [!NOTE] | ||
> | ||
> If you are using helm, you should also install the yaml parser (:TSInstall yaml) to get [language injection][] for yaml | ||
[tree-sitter]: https://github.com/tree-sitter/tree-sitter | ||
[Golang templates]: https://golang.org/pkg/text/template/ | ||
[nvim-treesitter instructions]: https://github.com/nvim-treesitter/nvim-treesitter#adding-parsers | ||
[nvim-treesitter-install]: https://github.com/nvim-treesitter/nvim-treesitter?tab=readme-ov-file#installation | ||
[nvim-treesitter]: https://github.com/nvim-treesitter/nvim-treesitter | ||
[helm]: https://helm.sh | ||
[vim-helm]: https://github.com/towolf/vim-helm | ||
[language injection]: https://tree-sitter.github.io/tree-sitter/syntax-highlighting#language-injection |