uncenter / tree-sitter-tera

⛩️ Tree-sitter grammar for the Tera templating language
MIT License
6 stars 1 forks source link
helix neovim tera tree-sitter

tree-sitter-tera

Tree-sitter grammar and syntax highlight queries for the Tera templating language.

[!TIP] This repository provides support for Helix and Neovim. For Visual Studio Code support, see uncenter/vscode-tera. For Zed support, see uncenter/zed-tera.

Screenshot of highlighted sample Tera code in Helix with the tree-sitter-tera grammar installed

Helix with tree-sitter-tera. Screenshot taken with the catppuccin/helix theme.

Screenshot of highlighted sample Tera code in Neovim with the tree-sitter-tera grammar installed

Neovim with tree-sitter-tera. Screenshot taken with the catppuccin/nvim theme.

Usage

Helix

  1. Add the following language and grammar configuration to your own languages.toml configuration file:
[[grammar]]
name = "tera"

[grammar.source]
git = "https://github.com/uncenter/tree-sitter-tera"
rev = "main"

[[language]]
file-types = ["tera"]
grammar = "tera"
injection-regex = "tera"
name = "tera"
scope = "source.tera"
block-comment-tokens = [
  { start = "{#", end = "#}" },
  { start = "{#-", end = "-#}" },
  { start = "{#", end = "-#}" },
  { start = "{#-", end = "#}" },
]

[language.auto-pairs]
"\"" = "\""
"'" = "'"
"`" = "`"
"(" = ")"
"[" = "]"
"{" = "}"
"%" = "%"
  1. Run hx --grammar fetch to fetch the grammar from this repository, and then hx --grammar build to build the grammars.
  2. Run just helix sync or just helix link to copy or symlink the Helix-specific queries to Helix's runtime directory, as unfortunately queries are not used from the grammar repository by default.

[!IMPORTANT] Helix uses the reverse query precedence ordering, meaning that the first matching highlight query is used rather than last; this is the opposite behavior of Neovim and Zed, both of which use the last matching highlight query - think of it like CSS, where queries gain precedence by being located further down. Helix does seemingly plan to change this soon, but for now this repository has a separate folder for Helix-converted queries (built with just helix build, using tree-sitter-query-reverser.

Neovim

Lazy.nvim

Extend your nvim-treesitter plugin with this Tera plugin.

{
    "nvim-treesitter/nvim-treesitter",
    config = function()
        -- setup treesitter with config
    end,
    dependencies = {
        ...
        { "uncenter/tree-sitter-tera", build = ":TSUpdate tera" },
        ...
    },
    build = ":TSUpdate",
},

Manual

local parser_config = require("nvim-treesitter.parsers").get_parser_configs()

parser_config.tera = {
  install_info = {
    url = "https://github.com/uncenter/tree-sitter-tera",
    files = { "src/parser.c" },
    branch = "main",
  },
  filetype = "tera",
}

Run :TSInstall tera in Neovim to install the above parser.

License

MIT