Skip to content
This repository has been archived by the owner on Nov 12, 2022. It is now read-only.

Commit

Permalink
add syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
fioncat committed Oct 14, 2021
1 parent 6a30f22 commit 39f0a01
Show file tree
Hide file tree
Showing 4 changed files with 262 additions and 0 deletions.
17 changes: 17 additions & 0 deletions syntax/gohtmltmpl.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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
107 changes: 107 additions & 0 deletions syntax/gomod.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
" gomod.vim: Vim syntax file for go.mod file
"
" Quit when a (custom) syntax file was already loaded
if exists("b:current_syntax")
finish
endif

syntax case match

" Reference documentation:
" https://golang.org/ref/mod#go-mod-file-grammar

" match keywords
syntax keyword gomodModule module
syntax keyword gomodGo go contained
syntax keyword gomodRequire require
syntax keyword gomodExclude exclude
syntax keyword gomodReplace replace
syntax keyword gomodRetract retract

" require, exclude, replace, and go can be also grouped into block
syntax region gomodRequire start='require (' end=')' transparent contains=gomodRequire,gomodVersion
syntax region gomodExclude start='exclude (' end=')' transparent contains=gomodExclude,gomodVersion
syntax region gomodReplace start='replace (' end=')' transparent contains=gomodReplace,gomodVersion
syntax region gomodRetract start='retract (' end=')' transparent contains=gomodVersionRange,gomodVersion
syntax match gomodGo '^go .*$' transparent contains=gomodGo,gomodGoVersion

" set highlights
highlight default link gomodModule Keyword
highlight default link gomodGo Keyword
highlight default link gomodRequire Keyword
highlight default link gomodExclude Keyword
highlight default link gomodReplace Keyword
highlight default link gomodRetract Keyword

" comments are always in form of // ...
syntax region gomodComment start="//" end="$" contains=@Spell
highlight default link gomodComment Comment

" make sure quoted import paths are higlighted
syntax region gomodString start=+"+ skip=+\\\\\|\\"+ end=+"+
highlight default link gomodString String

" replace operator is in the form of '=>'
syntax match gomodReplaceOperator "\v\=\>"
highlight default link gomodReplaceOperator Operator

" match go versions
syntax match gomodGoVersion "1\.\d\+" contained
highlight default link gomodGoVersion Identifier

" highlight versions:
" * vX.Y.Z-pre
" * vX.Y.Z
" * vX.0.0-yyyyymmddhhmmss-abcdefabcdef
" * vX.Y.Z-pre.0.yyyymmddhhmmss-abcdefabcdef
" * vX.Y.(Z+1)-0.yyyymmddhhss-abcdefabcdef
" see https://godoc.org/golang.org/x/tools/internal/semver for more
" information about how semantic versions are parsed and
" https://golang.org/cmd/go/ for how pseudo-versions and +incompatible
" are applied.


" match vX.Y.Z and their prereleases
syntax match gomodVersion "v\d\+\.\d\+\.\d\+\%(-\%([0-9A-Za-z-]\+\)\%(\.[0-9A-Za-z-]\+\)*\)\?\%(+\%([0-9A-Za-z-]\+\)\(\.[0-9A-Za-z-]\+\)*\)\?"
" ^--- version ---^^------------ pre-release ---------------------^^--------------- metadata ---------------------^
" ^--------------------------------------- semantic version -------------------------------------------------------^

" match pseudo versions
" without a major version before the commit (e.g. vX.0.0-yyyymmddhhmmss-abcdefabcdef)
syntax match gomodVersion "v\d\+\.0\.0-\d\{14\}-\x\+"
" when most recent version before target is a pre-release
syntax match gomodVersion "v\d\+\.\d\+\.\d\+-\%([0-9A-Za-z-]\+\)\%(\.[0-9A-Za-z-]\+\)*\%(+\%([0-9A-Za-z-]\+\)\(\.[0-9A-Za-z-]\+\)*\)\?\.0\.\d\{14}-\x\+"
" ^--- version ---^^--- ------ pre-release -----------------^^--------------- metadata ---------------------^
" ^------------------------------------- semantic version --------------------------------------------------^
" most recent version before the target is X.Y.Z
syntax match gomodVersion "v\d\+\.\d\+\.\d\+\%(+\%([0-9A-Za-z-]\+\)\(\.[0-9A-Za-z-]\+\)*\)\?-0\.\d\{14}-\x\+"
" ^--- version ---^^--------------- metadata ---------------------^

" match incompatible vX.Y.Z and their prereleases
syntax match gomodVersion "v[2-9]\{1}\d*\.\d\+\.\d\+\%(-\%([0-9A-Za-z-]\+\)\%(\.[0-9A-Za-z-]\+\)*\)\?\%(+\%([0-9A-Za-z-]\+\)\(\.[0-9A-Za-z-]\+\)*\)\?+incompatible"
" ^------- version -------^^------------- pre-release ---------------------^^--------------- metadata ---------------------^
" ^------------------------------------------- semantic version -----------------------------------------------------------^

" match incompatible pseudo versions
" incompatible without a major version before the commit (e.g. vX.0.0-yyyymmddhhmmss-abcdefabcdef)
syntax match gomodVersion "v[2-9]\{1}\d*\.0\.0-\d\{14\}-\x\++incompatible"
" when most recent version before target is a pre-release
syntax match gomodVersion "v[2-9]\{1}\d*\.\d\+\.\d\+-\%([0-9A-Za-z-]\+\)\%(\.[0-9A-Za-z-]\+\)*\%(+\%([0-9A-Za-z-]\+\)\(\.[0-9A-Za-z-]\+\)*\)\?\.0\.\d\{14}-\x\++incompatible"
" ^------- version -------^^---------- pre-release -----------------^^--------------- metadata ---------------------^
" ^---------------------------------------- semantic version ------------------------------------------------------^
" most recent version before the target is X.Y.Z
syntax match gomodVersion "v[2-9]\{1}\d*\.\d\+\.\d\+\%(+\%([0-9A-Za-z-]\+\)\%(\.[0-9A-Za-z-]\+\)*\)\?-0\.\d\{14}-\x\++incompatible"
" ^------- version -------^^---------------- metadata ---------------------^
highlight default link gomodVersion Identifier

" match go version ranges in retract directive
" https://golang.org/ref/mod#go-mod-file-retract
syntax region gomodVersionRange start="\[" end="\]" transparent matchgroup=gomodVersionRangeBracket contains=gomodVersion,gomodVersionRangeSeparator
highlight default link gomodVersionRange Operator
syntax match gomodVersionRangeBracket "\[" contained
syntax match gomodVersionRangeBracket "\]" contained
highlight default link gomodVersionRangeBracket Operator
syntax match gomodVersionRangeSeparator "," contained
highlight default link gomodVersionRangeSeparator Operator

let b:current_syntax = "gomod"
55 changes: 55 additions & 0 deletions syntax/gosum.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
" gosum.vim: Vim syntax file for go.sum file
"
" Quit when a (custom) syntax file was already loaded
if exists("b:current_syntax")
finish
endif

syntax case match

" highlight versions:
" * vX.Y.Z-pre
" * vX.Y.Z
" * vX.0.0-yyyyymmddhhmmss-abcdefabcdef
" * vX.Y.Z-pre.0.yyyymmddhhmmss-abcdefabcdef
" * vX.Y.(Z+1)-0.yyyymmddhhss-abcdefabcdef
" see https://godoc.org/golang.org/x/tools/internal/semver for more
" information about how semantic versions are parsed and
" https://golang.org/cmd/go/ for how pseudo-versions and +incompatible
" are applied.


" match vX.Y.Z and their prereleases
syntax match gomodVersion "v\d\+\.\d\+\.\d\+\%(-\%([0-9A-Za-z-]\+\)\%(\.[0-9A-Za-z-]\+\)*\)\?\%(+\%([0-9A-Za-z-]\+\)\(\.[0-9A-Za-z-]\+\)*\)\?"
" ^--- version ---^^------------ pre-release ---------------------^^--------------- metadata ---------------------^
" ^--------------------------------------- semantic version -------------------------------------------------------^

" match pseudo versions
" without a major version before the commit (e.g. vX.0.0-yyyymmddhhmmss-abcdefabcdef)
syntax match gomodVersion "v\d\+\.0\.0-\d\{14\}-\x\+"
" when most recent version before target is a pre-release
syntax match gomodVersion "v\d\+\.\d\+\.\d\+-\%([0-9A-Za-z-]\+\)\%(\.[0-9A-Za-z-]\+\)*\%(+\%([0-9A-Za-z-]\+\)\(\.[0-9A-Za-z-]\+\)*\)\?\.0\.\d\{14}-\x\+"
" ^--- version ---^^--- ------ pre-release -----------------^^--------------- metadata ---------------------^
" ^------------------------------------- semantic version --------------------------------------------------^
" most recent version before the target is X.Y.Z
syntax match gomodVersion "v\d\+\.\d\+\.\d\+\%(+\%([0-9A-Za-z-]\+\)\(\.[0-9A-Za-z-]\+\)*\)\?-0\.\d\{14}-\x\+"
" ^--- version ---^^--------------- metadata ---------------------^

" match incompatible vX.Y.Z and their prereleases
syntax match gomodVersion "v[2-9]\{1}\d*\.\d\+\.\d\+\%(-\%([0-9A-Za-z-]\+\)\%(\.[0-9A-Za-z-]\+\)*\)\?\%(+\%([0-9A-Za-z-]\+\)\(\.[0-9A-Za-z-]\+\)*\)\?+incompatible"
" ^------- version -------^^------------- pre-release ---------------------^^--------------- metadata ---------------------^
" ^------------------------------------------- semantic version -----------------------------------------------------------^

" match incompatible pseudo versions
" incompatible without a major version before the commit (e.g. vX.0.0-yyyymmddhhmmss-abcdefabcdef)
syntax match gomodVersion "v[2-9]\{1}\d*\.0\.0-\d\{14\}-\x\++incompatible"
" when most recent version before target is a pre-release
syntax match gomodVersion "v[2-9]\{1}\d*\.\d\+\.\d\+-\%([0-9A-Za-z-]\+\)\%(\.[0-9A-Za-z-]\+\)*\%(+\%([0-9A-Za-z-]\+\)\(\.[0-9A-Za-z-]\+\)*\)\?\.0\.\d\{14}-\x\++incompatible"
" ^------- version -------^^---------- pre-release -----------------^^--------------- metadata ---------------------^
" ^---------------------------------------- semantic version ------------------------------------------------------^
" most recent version before the target is X.Y.Z
syntax match gomodVersion "v[2-9]\{1}\d*\.\d\+\.\d\+\%(+\%([0-9A-Za-z-]\+\)\%(\.[0-9A-Za-z-]\+\)*\)\?-0\.\d\{14}-\x\++incompatible"
" ^------- version -------^^---------------- metadata ---------------------^
highlight default link gomodVersion Identifier

let b:current_syntax = "gomod"
83 changes: 83 additions & 0 deletions syntax/gotexttmpl.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
" 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
if exists("b:current_syntax")
finish
endif

syn case match

" Go escapes
syn match goEscapeOctal display contained "\\[0-7]\{3}"
syn match goEscapeC display contained +\\[abfnrtv\\'"]+
syn match goEscapeX display contained "\\x\x\{2}"
syn match goEscapeU display contained "\\u\x\{4}"
syn match goEscapeBigU display contained "\\U\x\{8}"
syn match goEscapeError display contained +\\[^0-7xuUabfnrtv\\'"]+

hi def link goEscapeOctal goSpecialString
hi def link goEscapeC goSpecialString
hi def link goEscapeX goSpecialString
hi def link goEscapeU goSpecialString
hi def link goEscapeBigU goSpecialString
hi def link goSpecialString Special
hi def link goEscapeError Error

" Strings and their contents
syn cluster goStringGroup contains=goEscapeOctal,goEscapeC,goEscapeX,goEscapeU,goEscapeBigU,goEscapeError
syn region goString contained start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@goStringGroup
syn region goRawString contained start=+`+ end=+`+

hi def link goString String
hi def link goRawString String

" Characters; their contents
syn cluster goCharacterGroup contains=goEscapeOctal,goEscapeC,goEscapeX,goEscapeU,goEscapeBigU
syn region goCharacter contained start=+'+ skip=+\\\\\|\\'+ end=+'+ contains=@goCharacterGroup

hi def link goCharacter Character

" Integers
syn match goDecimalInt contained "\<\d\+\([Ee]\d\+\)\?\>"
syn match goHexadecimalInt contained "\<0x\x\+\>"
syn match goOctalInt contained "\<0\o\+\>"
syn match goOctalError contained "\<0\o*[89]\d*\>"
syn cluster goInt contains=goDecimalInt,goHexadecimalInt,goOctalInt
" Floating point
syn match goFloat contained "\<\d\+\.\d*\([Ee][-+]\d\+\)\?\>"
syn match goFloat contained "\<\.\d\+\([Ee][-+]\d\+\)\?\>"
syn match goFloat contained "\<\d\+[Ee][-+]\d\+\>"
" Imaginary literals
syn match goImaginary contained "\<\d\+i\>"
syn match goImaginary contained "\<\d\+\.\d*\([Ee][-+]\d\+\)\?i\>"
syn match goImaginary contained "\<\.\d\+\([Ee][-+]\d\+\)\?i\>"
syn match goImaginary contained "\<\d\+[Ee][-+]\d\+i\>"

hi def link goInt Number
hi def link goFloat Number
hi def link goImaginary Number

" 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

0 comments on commit 39f0a01

Please sign in to comment.