Skip to content
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

docs(install): rework NeoVim instructions #20

Merged
merged 1 commit into from
Aug 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading