Skip to content

Commit

Permalink
Add syntax support for toolchain directive
Browse files Browse the repository at this point in the history
So that this directive can be highlighted in `go.mod`

There's not much detail for the syntax of this directive in the grammar
file[1] so this implementation is based on the document linked there,
specifically[2] and glancing at some code[3]. For example support
includes versions like

* go1.21.3
* go1.20
* go1.21rc4
* go1.12.3-somesuffix
* go1.21rc4-somesuffix more stuff

But excludes e.g.:

* go1.20.3rc4
* go1.20abc

The highlight groups this syntax is linked to just follows what is done
for `gomodGoVersion`

[1] https://go.dev/ref/mod#go-mod-file-toolchain
[2] https://go.dev/doc/toolchain#version
[3] https://go.googlesource.com/go/+/165383381199de6632665f43561e8e0dfc96f067/src/cmd/go/internal/gover/toolchain.go#24
  • Loading branch information
matthewhughes934 committed Jan 19, 2024
1 parent 5bed70d commit 90858be
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions syntax/gomod.vim
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,30 @@ syntax case match
" 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
syntax keyword gomodModule module
syntax keyword gomodGo go contained
syntax keyword gomodToolchain toolchain 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
syntax match gomodToolchain '^toolchain .*$' transparent contains=gomodToolchain,gomodToolchainVersion

" 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
highlight default link gomodModule Keyword
highlight default link gomodGo Keyword
highlight default link gomodToolchain 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
Expand All @@ -45,6 +48,16 @@ highlight default link gomodString String
syntax match gomodReplaceOperator "\v\=\>"
highlight default link gomodReplaceOperator Operator

" match toolchain versions, per https://go.dev/doc/toolchain#version, e.g.
"
" * go1.X.Y
" * go1.X
" * go1.XrcN
" * go1.X.Y-somesuffix
" * go1.XrcN-somesuffix more stuff
syntax match gomodToolchainVersion "go1\.\d\+\(\(.\d\+\)\|\(rc\d\+\)\)\?\([ \t-].*\)\?" contained
highlight default link gomodToolchainVersion Identifier

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

0 comments on commit 90858be

Please sign in to comment.