diff --git a/README.md b/README.md index 4096309..a7f1a74 100644 --- a/README.md +++ b/README.md @@ -37,13 +37,14 @@ The plugin covers most features required for a gopher. - Inlay hints: gopls (version 0.9.x or greater) inlay hints; version 0.10.x inlay hints are enabled by default. - luasnip: go.nvim included a feature rich luasnips you definitally need to try. - Treesitter highlight injection: go.nvim included a treesitter highlight injection for SQL and JSON. +- Treesitter also injects highlight for `go template`, `gohtmltmpl` - Generate return value for current function - Generate go file with template - Generate go struct from json ## Installation -Use your favorite package manager to install. The dependency `treesitter` (and optionally, treesitter-objects) +Use your favorite package manager to install. The dependency ` treesitter ` (and optionally, treesitter-objects) should be installed the first time you use it. Also Run `TSInstall go` to install the go parser if not installed yet. `sed` is recommended to run this plugin. @@ -1167,6 +1168,39 @@ require('lspconfig').gopls.setup(cfg) ``` +## Highlighting for gomod, gosum, gohtmltmpl, gotmpl, gotexttmpl + +You can install treesitter parser for gomod and gosum +```vim +:TSInstall gomod gosum +``` + +As for go template, the plugin has not been merge to treeistter master yet, you need to install +[treesitter-go-template](https://github.com/ngalaiko/tree-sitter-go-template) + +```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"} +} +``` + +And run + +```vim +:TSInstall gotmpl +``` + +The plugin injects the tmpl to html so you should see this: +image + + + ## Integrate null-ls ### The plugin provides: diff --git a/after/queries/gomod/highlights.scm b/after/queries/gomod/highlights.scm new file mode 100644 index 0000000..d8c667a --- /dev/null +++ b/after/queries/gomod/highlights.scm @@ -0,0 +1,2 @@ +(module_path) @namespace +(version) @keyword diff --git a/queries/gotmpl/highlights.scm b/queries/gotmpl/highlights.scm new file mode 100644 index 0000000..8a09ba5 --- /dev/null +++ b/queries/gotmpl/highlights.scm @@ -0,0 +1,85 @@ +; Copyright: https://github.com/ngalaiko/tree-sitter-go-template +; 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 diff --git a/queries/gotmpl/injections.scm b/queries/gotmpl/injections.scm new file mode 100644 index 0000000..4d15dba --- /dev/null +++ b/queries/gotmpl/injections.scm @@ -0,0 +1,9 @@ +(text) @yaml + +((text) @injection.content + (#set! injection.language "html") + (#set! injection.combined)) + +((text) @injection.content + (#set! injection.language "javascript") + (#set! injection.combined)) diff --git a/syntax/gohtmltmpl.vim b/syntax/gohtmltmpl.vim deleted file mode 100644 index a3db44f..0000000 --- a/syntax/gohtmltmpl.vim +++ /dev/null @@ -1,18 +0,0 @@ -" this file is copied from vim-go -if exists("b:current_syntax") - finish -endif - -if !exists("g:main_syntax") - let g:main_syntax = 'html' -endif - -runtime! syntax/gotexttmpl.vim -runtime! syntax/html.vim -unlet b:current_syntax - -syn cluster htmlPreproc add=gotplAction,goTplComment - -let b:current_syntax = "gohtmltmpl" - -" vim: sw=2 ts=2 et diff --git a/syntax/gotexttmpl.vim b/syntax/gotexttmpl.vim deleted file mode 100644 index ad6a092..0000000 --- a/syntax/gotexttmpl.vim +++ /dev/null @@ -1,36 +0,0 @@ -" Copyright 2011 The Go Authors. All rights reserved. -" Use of this source code is governed by a BSD-style -" license that can be found in the LICENSE file. -" -" gotexttmpl.vim: Vim syntax file for Go templates. - -" Quit when a (custom) syntax file was already loaded -" a modified version of gotexttmpl.vim -if exists("b:current_syntax") - finish -endif - - -runtime! syntax/go.vim -unlet b:current_syntax - -" Token groups -syn cluster gotplLiteral contains=goString,goRawString,goCharacter,@goInt,goFloat,goImaginary -syn keyword gotplControl contained if else end range with template -syn keyword gotplFunctions contained and html index js len not or print printf println urlquery eq ne lt le gt ge -syn match gotplVariable contained /\$[a-zA-Z0-9_]*\>/ -syn match goTplIdentifier contained /\.[^[:blank:]}]\+\>/ - -hi def link gotplControl Keyword -hi def link gotplFunctions Function -hi def link goTplVariable Special - -syn region gotplAction start="{{" end="}}" contains=@gotplLiteral,gotplControl,gotplFunctions,gotplVariable,goTplIdentifier display -syn region goTplComment start="{{\(- \)\?/\*" end="\*/\( -\)\?}}" display - -hi def link gotplAction PreProc -hi def link goTplComment Comment - -let b:current_syntax = "gotexttmpl" - -" vim: sw=2 ts=2 et