Skip to content

Commit

Permalink
Merge pull request #20 from qvalentin/feature/rework-nvim-docs
Browse files Browse the repository at this point in the history
docs(install): rework NeoVim instructions
  • Loading branch information
qvalentin authored Aug 25, 2024
2 parents 9d3f6e5 + 9ca36d6 commit fd9e1c6
Showing 1 changed file with 37 additions and 111 deletions.
148 changes: 37 additions & 111 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,133 +1,59 @@
## 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 wrote this package for) on a daily basis, so he didn'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
- using [lua](https://neovim.io/doc/user/lua.html#vim.filetype):

((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)$"))
```lua
vim.filetype.add({
extension = {
gotmpl = 'gotmpl',
},
pattern = {
[".*/templates/.*%.tpl"] = "helm",
[".*/templates/.*%.ya?ml"] = "helm",
["helmfile.*%.ya?ml"] = "helm",
},
})
```

; Delimiters
- Install [nvim-treesitter][] as described here: [nvim-treesitter-install][]
- Install the parser you want to use:

"." @punctuation.delimiter
"," @punctuation.delimiter
```vim
:TSInstall gotmpl
:TSInstall helm
```

"{{" @punctuation.bracket
"}}" @punctuation.bracket
"{{-" @punctuation.bracket
"-}}" @punctuation.bracket
")" @punctuation.bracket
"(" @punctuation.bracket
> [!NOTE]
>
> If you are using helm, you should also install the yaml parser (:TSInstall yaml) to get [language injection][] for yaml
; 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
```
> [!TIP]
>
> If you are using [AstroNvim][] you can just install the [astrocommunity][] helm pack.
[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
[AstroNvim]: https://github.com/AstroNvim/AstroNvim
[astrocommunity]: https://github.com/AstroNvim/astrocommunity

0 comments on commit fd9e1c6

Please sign in to comment.