Skip to content

ngalaiko/tree-sitter-go-template

Repository files navigation

Announcement

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

Golang templates grammar for tree-sitter.

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

  • Setup filetype detection:

    vim.filetype.add({
      extension = {
        gotmpl = 'gotmpl',
      },
      pattern = {
        [".*/templates/.*%.tpl"] = "helm",
        [".*/templates/.*%.ya?ml"] = "helm",
        ["helmfile.*%.ya?ml"] = "helm",
      },
    })
  • Install nvim-treesitter as described here: nvim-treesitter-install

  • Install the parser you want to use:

:TSInstall gotmpl
:TSInstall helm

Highlighting for the templated language

To get proper highlighting for the language that is templated (e.g. html) you need to add injections for that language to .config/nvim/queries/gotmpl/injections.scm:

((text) @injection.content
 (#set! injection.language "html")
 (#set! injection.combined))

Replace html with the tree-sitter name of your required language and make sure to install it with :TSInstall.

More advanced use cases would require adding more dialects (as done for helm).

Note

If you are using helm, you should also install the yaml parser (:TSInstall yaml) to get language injection for yaml.

In case you are using AstroNvim you can just install the astrocommunity helm pack or when using LazyVim its LazyVimHelm plugin.