ngalaiko / tree-sitter-go-template

Golang template grammar for tree-sitter
MIT License
69 stars 27 forks source link

How to for Neovim users #19

Open behnamgolds opened 2 months ago

behnamgolds commented 2 months ago

I just managed to import gotmpl to my neovim config, It just took me 5 hours to do it! But thats not the point, Im very new to neovim, so this is for noobs like me . The only thing that I am yet to figure out is how to get highlightings and language injections (html, css, js) to work inside my go templates. If you know how , please let me know.

here is my treesitter.lua config file settings (if you don't use seperate config files for your plugins, just omit the 'return {' at first line and '}' at last line)

return {
  -- Highlight, edit, and navigate code
  'nvim-treesitter/nvim-treesitter',
  build = ':TSUpdate',
  opts = {
    ensure_installed = {
      'bash',
      'c',
      'diff',
      'html',
      'lua',
      'luadoc',
      'markdown',
      'vim',
      'vimdoc',
      'css',
      'go',
      'gomod',
      'gotmpl',
      'gosum',
      'gowork',
      'yaml',
      'python',
      'json',
      'javascript',
      'sql',
      'csv',
    },
    -- Autoinstall languages that are not installed
    auto_install = true,
    highlight = {
      enable = true,
      -- Some languages depend on vim's regex highlighting system (such as Ruby) for indent rules.
      --  If you are experiencing weird indenting issues, add the language to
      --  the list of additional_vim_regex_highlighting and disabled languages for indent.
      additional_vim_regex_highlighting = { 'ruby' },
    },
    indent = { enable = true, disable = { 'ruby' } },
  },
  config = function(_, opts)
    --
    -- For detecting go template files
    vim.filetype.add ( {
      extension = {
        gotmpl = 'gotmpl',
        gohtml = 'gotmpl',
        gohtmltmpl = 'gotmpl',
        gohtxttmpl = 'gotmpl',
        gohtexttmpl = 'gotmpl',
      },
    } )
    local parser_configs = require('nvim-treesitter.parsers').get_parser_configs()
    parser_configs['gotmpl'] = {
      install_info = {
        url = 'https://github.com/ngalaiko/tree-sitter-go-template',
        files = { 'src/parser.c' },
      },
      filetype = 'gotmpl',
      used_by = { 'gohtmltmpl', 'gotexttmpl', 'gotmpl', 'gotxttmpl', 'gohtml' },
    }
    -- For detecting go template files
    --
    require('nvim-treesitter.install').prefer_git = true
    require('nvim-treesitter.configs').setup(opts)
  end,
}
qvalentin commented 4 weeks ago

Hi, nice you got it working for far.

I finally found the time to rework the docs, since the parser is now added to the nvim-tree-sitter repo and you don't need the

parser_configs['gotmpl'] = {
      install_info = {
        url = 'https://github.com/ngalaiko/tree-sitter-go-template',
        files = { 'src/parser.c' },
      },

part anymore.

Maybe you can checkout https://github.com/ngalaiko/tree-sitter-go-template/pull/20