Code Monkey home page Code Monkey logo

tree-sitter-go-template's Introduction

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.

tree-sitter-go-template

Build/test

Golang templates grammar for tree-sitter.

NeoVim integration using nvim-treesitter

  • Add gotmpl parser following nvim-treesitter instructions

    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:

    autocmd BufNewFile,BufRead * if search('{{.\+}}', 'nw') | setlocal filetype=gotmpl | endif
    
  • Define language injection for yaml in ~/.config/nvim/queries/gotmpl/injections.scm:

    (text) @yaml
  • Define highlights in ~/.config/nvim/queries/gotmpl/highlights.scm, for example:

    ; 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
    
    ; Builtin functions
    
    ((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)$"))
    
    ; Delimiters
    
    "." @punctuation.delimiter
    "," @punctuation.delimiter
    
    "{{" @punctuation.bracket
    "}}" @punctuation.bracket
    "{{-" @punctuation.bracket
    "-}}" @punctuation.bracket
    ")" @punctuation.bracket
    "(" @punctuation.bracket
    
    ; 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

tree-sitter-go-template's People

Contributors

ngalaiko avatar reegnz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.