blob: 4272e807f3236f48815f4380be06c15c6ed2fcb5 [file] [log] [blame]
Bram Moolenaar90df4b92021-07-07 20:26:08 +02001" Copyright 2009 The Go Authors. All rights reserved.
2" Use of this source code is governed by a BSD-style
3" license that can be found in the LICENSE file.
Bram Moolenaarfb539272014-08-22 19:21:47 +02004"
Bram Moolenaar90df4b92021-07-07 20:26:08 +02005" go.vim: Vim syntax file for Go.
6" Language: Go
7" Maintainer: Billie Cleek <bhcleek@gmail.com>
José-Paul Db0d584d2023-08-22 20:29:19 +02008" Latest Revision: 2023-08-21
Bram Moolenaar90df4b92021-07-07 20:26:08 +02009" License: BSD-style. See LICENSE file in source repository.
10" Repository: https://github.com/fatih/vim-go
Bram Moolenaarfb539272014-08-22 19:21:47 +020011
12" Quit when a (custom) syntax file was already loaded
Bram Moolenaar90df4b92021-07-07 20:26:08 +020013if exists("b:current_syntax")
Bram Moolenaarfb539272014-08-22 19:21:47 +020014 finish
15endif
16
Bram Moolenaar90df4b92021-07-07 20:26:08 +020017let s:keepcpo = &cpo
18set cpo&vim
19
20function! s:FoldEnable(...) abort
21 if a:0 > 0
22 return index(s:FoldEnable(), a:1) > -1
23 endif
24 return get(g:, 'go_fold_enable', ['block', 'import', 'varconst', 'package_comment'])
25endfunction
26
27function! s:HighlightArrayWhitespaceError() abort
28 return get(g:, 'go_highlight_array_whitespace_error', 0)
29endfunction
30
31function! s:HighlightChanWhitespaceError() abort
32 return get(g:, 'go_highlight_chan_whitespace_error', 0)
33endfunction
34
35function! s:HighlightExtraTypes() abort
36 return get(g:, 'go_highlight_extra_types', 0)
37endfunction
38
39function! s:HighlightSpaceTabError() abort
40 return get(g:, 'go_highlight_space_tab_error', 0)
41endfunction
42
43function! s:HighlightTrailingWhitespaceError() abort
44 return get(g:, 'go_highlight_trailing_whitespace_error', 0)
45endfunction
46
47function! s:HighlightOperators() abort
48 return get(g:, 'go_highlight_operators', 0)
49endfunction
50
51function! s:HighlightFunctions() abort
52 return get(g:, 'go_highlight_functions', 0)
53endfunction
54
55function! s:HighlightFunctionParameters() abort
56 return get(g:, 'go_highlight_function_parameters', 0)
57endfunction
58
59function! s:HighlightFunctionCalls() abort
60 return get(g:, 'go_highlight_function_calls', 0)
61endfunction
62
63function! s:HighlightFields() abort
64 return get(g:, 'go_highlight_fields', 0)
65endfunction
66
67function! s:HighlightTypes() abort
68 return get(g:, 'go_highlight_types', 0)
69endfunction
70
71function! s:HighlightBuildConstraints() abort
72 return get(g:, 'go_highlight_build_constraints', 0)
73endfunction
74
75function! s:HighlightStringSpellcheck() abort
76 return get(g:, 'go_highlight_string_spellcheck', 1)
77endfunction
78
79function! s:HighlightFormatStrings() abort
80 return get(g:, 'go_highlight_format_strings', 1)
81endfunction
82
83function! s:HighlightGenerateTags() abort
84 return get(g:, 'go_highlight_generate_tags', 0)
85endfunction
86
87function! s:HighlightVariableAssignments() abort
88 return get(g:, 'go_highlight_variable_assignments', 0)
89endfunction
90
91function! s:HighlightVariableDeclarations() abort
92 return get(g:, 'go_highlight_variable_declarations', 0)
93endfunction
Bram Moolenaarfb539272014-08-22 19:21:47 +020094
95syn case match
96
Bram Moolenaar90df4b92021-07-07 20:26:08 +020097syn keyword goPackage package
98syn keyword goImport import contained
99syn keyword goVar var contained
100syn keyword goConst const contained
Bram Moolenaarfb539272014-08-22 19:21:47 +0200101
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200102hi def link goPackage Statement
103hi def link goImport Statement
104hi def link goVar Keyword
105hi def link goConst Keyword
Bram Moolenaarfb539272014-08-22 19:21:47 +0200106hi def link goDeclaration Keyword
Bram Moolenaarfb539272014-08-22 19:21:47 +0200107
108" Keywords within functions
109syn keyword goStatement defer go goto return break continue fallthrough
110syn keyword goConditional if else switch select
111syn keyword goLabel case default
112syn keyword goRepeat for range
113
114hi def link goStatement Statement
115hi def link goConditional Conditional
116hi def link goLabel Label
117hi def link goRepeat Repeat
118
119" Predefined types
Bram Moolenaard13166e2022-11-18 21:49:57 +0000120syn keyword goType chan map bool string error any comparable
Bram Moolenaarfb539272014-08-22 19:21:47 +0200121syn keyword goSignedInts int int8 int16 int32 int64 rune
122syn keyword goUnsignedInts byte uint uint8 uint16 uint32 uint64 uintptr
123syn keyword goFloats float32 float64
124syn keyword goComplexes complex64 complex128
125
126hi def link goType Type
127hi def link goSignedInts Type
128hi def link goUnsignedInts Type
129hi def link goFloats Type
130hi def link goComplexes Type
131
Bram Moolenaarfb539272014-08-22 19:21:47 +0200132" Predefined functions and values
José-Paul Db0d584d2023-08-22 20:29:19 +0200133syn keyword goBuiltins append cap clear close complex copy delete imag len
134syn keyword goBuiltins make max min new panic print println real recover
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200135syn keyword goBoolean true false
136syn keyword goPredefinedIdentifiers nil iota
Bram Moolenaarfb539272014-08-22 19:21:47 +0200137
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200138hi def link goBuiltins Identifier
Bram Moolenaar938ae282023-02-20 20:44:55 +0000139hi def link goPredefinedIdentifiers Constant
140" Boolean links to Constant by default by vim: goBoolean and goPredefinedIdentifiers
141" will be highlighted the same, but having the separate groups allows users to
142" have separate highlighting for them if they desire.
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200143hi def link goBoolean Boolean
Bram Moolenaarfb539272014-08-22 19:21:47 +0200144
145" Comments; their contents
146syn keyword goTodo contained TODO FIXME XXX BUG
147syn cluster goCommentGroup contains=goTodo
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200148
149syn region goComment start="//" end="$" contains=goGenerate,@goCommentGroup,@Spell
150if s:FoldEnable('comment')
151 syn region goComment start="/\*" end="\*/" contains=@goCommentGroup,@Spell fold
152 syn match goComment "\v(^\s*//.*\n)+" contains=goGenerate,@goCommentGroup,@Spell fold
153else
154 syn region goComment start="/\*" end="\*/" contains=@goCommentGroup,@Spell
155endif
Bram Moolenaarfb539272014-08-22 19:21:47 +0200156
157hi def link goComment Comment
158hi def link goTodo Todo
159
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200160if s:HighlightGenerateTags()
161 syn match goGenerateVariables contained /\%(\$GOARCH\|\$GOOS\|\$GOFILE\|\$GOLINE\|\$GOPACKAGE\|\$DOLLAR\)\>/
162 syn region goGenerate start="^\s*//go:generate" end="$" contains=goGenerateVariables
163 hi def link goGenerate PreProc
164 hi def link goGenerateVariables Special
165endif
166
Bram Moolenaarfb539272014-08-22 19:21:47 +0200167" Go escapes
168syn match goEscapeOctal display contained "\\[0-7]\{3}"
169syn match goEscapeC display contained +\\[abfnrtv\\'"]+
170syn match goEscapeX display contained "\\x\x\{2}"
171syn match goEscapeU display contained "\\u\x\{4}"
172syn match goEscapeBigU display contained "\\U\x\{8}"
173syn match goEscapeError display contained +\\[^0-7xuUabfnrtv\\'"]+
174
175hi def link goEscapeOctal goSpecialString
176hi def link goEscapeC goSpecialString
177hi def link goEscapeX goSpecialString
178hi def link goEscapeU goSpecialString
179hi def link goEscapeBigU goSpecialString
180hi def link goSpecialString Special
181hi def link goEscapeError Error
182
183" Strings and their contents
184syn cluster goStringGroup contains=goEscapeOctal,goEscapeC,goEscapeX,goEscapeU,goEscapeBigU,goEscapeError
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200185if s:HighlightStringSpellcheck()
186 syn region goString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@goStringGroup,@Spell
187 syn region goRawString start=+`+ end=+`+ contains=@Spell
188else
189 syn region goString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@goStringGroup
190 syn region goRawString start=+`+ end=+`+
191endif
192
Bram Moolenaard13166e2022-11-18 21:49:57 +0000193syn match goImportString /^\%(\s\+\|import \)\(\h\w* \)\?\zs"[^"]\+"$/ contained containedin=goImport
194
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200195if s:HighlightFormatStrings()
196 " [n] notation is valid for specifying explicit argument indexes
197 " 1. Match a literal % not preceded by a %.
198 " 2. Match any number of -, #, 0, space, or +
199 " 3. Match * or [n]* or any number or nothing before a .
200 " 4. Match * or [n]* or any number or nothing after a .
201 " 5. Match [n] or nothing before a verb
202 " 6. Match a formatting verb
203 syn match goFormatSpecifier /\
204 \%([^%]\%(%%\)*\)\
205 \@<=%[-#0 +]*\
206 \%(\%(\%(\[\d\+\]\)\=\*\)\|\d\+\)\=\
207 \%(\.\%(\%(\%(\[\d\+\]\)\=\*\)\|\d\+\)\=\)\=\
208 \%(\[\d\+\]\)\=[vTtbcdoqxXUeEfFgGspw]/ contained containedin=goString,goRawString
209 hi def link goFormatSpecifier goSpecialString
210endif
Bram Moolenaarfb539272014-08-22 19:21:47 +0200211
Bram Moolenaard13166e2022-11-18 21:49:57 +0000212hi def link goImportString String
Bram Moolenaarfb539272014-08-22 19:21:47 +0200213hi def link goString String
214hi def link goRawString String
215
216" Characters; their contents
217syn cluster goCharacterGroup contains=goEscapeOctal,goEscapeC,goEscapeX,goEscapeU,goEscapeBigU
218syn region goCharacter start=+'+ skip=+\\\\\|\\'+ end=+'+ contains=@goCharacterGroup
219
220hi def link goCharacter Character
221
222" Regions
Bram Moolenaarfb539272014-08-22 19:21:47 +0200223syn region goParen start='(' end=')' transparent
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200224if s:FoldEnable('block')
225 syn region goBlock start="{" end="}" transparent fold
226else
227 syn region goBlock start="{" end="}" transparent
228endif
229
230" import
231if s:FoldEnable('import')
Bram Moolenaard13166e2022-11-18 21:49:57 +0000232 syn region goImport start='import (' end=')' transparent fold contains=goImport,goImportString,goComment
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200233else
Bram Moolenaard13166e2022-11-18 21:49:57 +0000234 syn region goImport start='import (' end=')' transparent contains=goImport,goImportString,goComment
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200235endif
236
237" var, const
238if s:FoldEnable('varconst')
239 syn region goVar start='var (' end='^\s*)$' transparent fold
240 \ contains=ALLBUT,goParen,goBlock,goFunction,goTypeName,goReceiverType,goReceiverVar,goParamName,goParamType,goSimpleParams,goPointerOperator
241 syn region goConst start='const (' end='^\s*)$' transparent fold
242 \ contains=ALLBUT,goParen,goBlock,goFunction,goTypeName,goReceiverType,goReceiverVar,goParamName,goParamType,goSimpleParams,goPointerOperator
243else
244 syn region goVar start='var (' end='^\s*)$' transparent
245 \ contains=ALLBUT,goParen,goBlock,goFunction,goTypeName,goReceiverType,goReceiverVar,goParamName,goParamType,goSimpleParams,goPointerOperator
246 syn region goConst start='const (' end='^\s*)$' transparent
247 \ contains=ALLBUT,goParen,goBlock,goFunction,goTypeName,goReceiverType,goReceiverVar,goParamName,goParamType,goSimpleParams,goPointerOperator
248endif
249
250" Single-line var, const, and import.
251syn match goSingleDecl /\%(import\|var\|const\) [^(]\@=/ contains=goImport,goVar,goConst
Bram Moolenaarfb539272014-08-22 19:21:47 +0200252
253" Integers
Bram Moolenaard13166e2022-11-18 21:49:57 +0000254syn match goDecimalInt "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)\>"
255syn match goHexadecimalInt "\<-\=0[xX]_\?\%(\x\|\x_\x\)\+\>"
256syn match goOctalInt "\<-\=0[oO]\?_\?\%(\o\|\o_\o\)\+\>"
257syn match goBinaryInt "\<-\=0[bB]_\?\%([01]\|[01]_[01]\)\+\>"
Bram Moolenaarfb539272014-08-22 19:21:47 +0200258
259hi def link goDecimalInt Integer
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200260hi def link goDecimalError Error
Bram Moolenaarfb539272014-08-22 19:21:47 +0200261hi def link goHexadecimalInt Integer
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200262hi def link goHexadecimalError Error
Bram Moolenaarfb539272014-08-22 19:21:47 +0200263hi def link goOctalInt Integer
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200264hi def link goOctalError Error
265hi def link goBinaryInt Integer
266hi def link goBinaryError Error
Bram Moolenaarfb539272014-08-22 19:21:47 +0200267hi def link Integer Number
268
269" Floating point
Bram Moolenaard13166e2022-11-18 21:49:57 +0000270"float_lit = decimal_float_lit | hex_float_lit .
271"
272"decimal_float_lit = decimal_digits "." [ decimal_digits ] [ decimal_exponent ] |
273" decimal_digits decimal_exponent |
274" "." decimal_digits [ decimal_exponent ] .
275"decimal_exponent = ( "e" | "E" ) [ "+" | "-" ] decimal_digits .
276"
277"hex_float_lit = "0" ( "x" | "X" ) hex_mantissa hex_exponent .
278"hex_mantissa = [ "_" ] hex_digits "." [ hex_digits ] |
279" [ "_" ] hex_digits |
280" "." hex_digits .
281"hex_exponent = ( "p" | "P" ) [ "+" | "-" ] decimal_digits .
282" decimal floats with a decimal point
283syn match goFloat "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)\.\%(\%(\%(\d\|\d_\d\)\+\)\=\%([Ee][-+]\=\%(\d\|\d_\d\)\+\)\=\>\)\="
284syn match goFloat "\s\zs-\=\.\%(\d\|\d_\d\)\+\%(\%([Ee][-+]\=\%(\d\|\d_\d\)\+\)\>\)\="
285" decimal floats without a decimal point
286syn match goFloat "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)[Ee][-+]\=\%(\d\|\d_\d\)\+\>"
287" hexadecimal floats with a decimal point
288syn match goHexadecimalFloat "\<-\=0[xX]\%(_\x\|\x\)\+\.\%(\%(\x\|\x_\x\)\+\)\=\%([Pp][-+]\=\%(\d\|\d_\d\)\+\)\=\>"
289syn match goHexadecimalFloat "\<-\=0[xX]\.\%(\x\|\x_\x\)\+\%([Pp][-+]\=\%(\d\|\d_\d\)\+\)\=\>"
290" hexadecimal floats without a decimal point
291syn match goHexadecimalFloat "\<-\=0[xX]\%(_\x\|\x\)\+[Pp][-+]\=\%(\d\|\d_\d\)\+\>"
Bram Moolenaarfb539272014-08-22 19:21:47 +0200292
293hi def link goFloat Float
Bram Moolenaard13166e2022-11-18 21:49:57 +0000294hi def link goHexadecimalFloat Float
Bram Moolenaarfb539272014-08-22 19:21:47 +0200295
296" Imaginary literals
Bram Moolenaard13166e2022-11-18 21:49:57 +0000297syn match goImaginaryDecimal "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)i\>"
298syn match goImaginaryHexadecimal "\<-\=0[xX]_\?\%(\x\|\x_\x\)\+i\>"
299syn match goImaginaryOctal "\<-\=0[oO]\?_\?\%(\o\|\o_\o\)\+i\>"
300syn match goImaginaryBinary "\<-\=0[bB]_\?\%([01]\|[01]_[01]\)\+i\>"
Bram Moolenaarfb539272014-08-22 19:21:47 +0200301
Bram Moolenaard13166e2022-11-18 21:49:57 +0000302" imaginary decimal floats with a decimal point
303syn match goImaginaryFloat "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)\.\%(\%(\%(\d\|\d_\d\)\+\)\=\%([Ee][-+]\=\%(\d\|\d_\d\)\+\)\=\)\=i\>"
304syn match goImaginaryFloat "\s\zs-\=\.\%(\d\|\d_\d\)\+\%([Ee][-+]\=\%(\d\|\d_\d\)\+\)\=i\>"
305" imaginary decimal floats without a decimal point
306syn match goImaginaryFloat "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)[Ee][-+]\=\%(\d\|\d_\d\)\+i\>"
307" imaginary hexadecimal floats with a decimal point
308syn match goImaginaryHexadecimalFloat "\<-\=0[xX]\%(_\x\|\x\)\+\.\%(\%(\x\|\x_\x\)\+\)\=\%([Pp][-+]\=\%(\d\|\d_\d\)\+\)\=i\>"
309syn match goImaginaryHexadecimalFloat "\<-\=0[xX]\.\%(\x\|\x_\x\)\+\%([Pp][-+]\=\%(\d\|\d_\d\)\+\)\=i\>"
310" imaginary hexadecimal floats without a decimal point
311syn match goImaginaryHexadecimalFloat "\<-\=0[xX]\%(_\x\|\x\)\+[Pp][-+]\=\%(\d\|\d_\d\)\+i\>"
312
313hi def link goImaginaryDecimal Number
314hi def link goImaginaryHexadecimal Number
315hi def link goImaginaryOctal Number
316hi def link goImaginaryBinary Number
317hi def link goImaginaryFloat Float
318hi def link goImaginaryHexadecimalFloat Float
Bram Moolenaarfb539272014-08-22 19:21:47 +0200319
320" Spaces after "[]"
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200321if s:HighlightArrayWhitespaceError()
322 syn match goSpaceError display "\%(\[\]\)\@<=\s\+"
Bram Moolenaarfb539272014-08-22 19:21:47 +0200323endif
324
325" Spacing errors around the 'chan' keyword
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200326if s:HighlightChanWhitespaceError()
Bram Moolenaarfb539272014-08-22 19:21:47 +0200327 " receive-only annotation on chan type
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200328 "
329 " \(\<chan\>\)\@<!<- (only pick arrow when it doesn't come after a chan)
330 " this prevents picking up 'chan<- chan<-' but not '<- chan'
331 syn match goSpaceError display "\%(\%(\<chan\>\)\@<!<-\)\@<=\s\+\%(\<chan\>\)\@="
332
Bram Moolenaarfb539272014-08-22 19:21:47 +0200333 " send-only annotation on chan type
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200334 "
335 " \(<-\)\@<!\<chan\> (only pick chan when it doesn't come after an arrow)
336 " this prevents picking up '<-chan <-chan' but not 'chan <-'
337 syn match goSpaceError display "\%(\%(<-\)\@<!\<chan\>\)\@<=\s\+\%(<-\)\@="
338
Bram Moolenaarfb539272014-08-22 19:21:47 +0200339 " value-ignoring receives in a few contexts
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200340 syn match goSpaceError display "\%(\%(^\|[={(,;]\)\s*<-\)\@<=\s\+"
Bram Moolenaarfb539272014-08-22 19:21:47 +0200341endif
342
343" Extra types commonly seen
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200344if s:HighlightExtraTypes()
345 syn match goExtraType /\<bytes\.\%(Buffer\)\>/
346 syn match goExtraType /\<context\.\%(Context\)\>/
347 syn match goExtraType /\<io\.\%(Reader\|ReadSeeker\|ReadWriter\|ReadCloser\|ReadWriteCloser\|Writer\|WriteCloser\|Seeker\)\>/
348 syn match goExtraType /\<reflect\.\%(Kind\|Type\|Value\)\>/
Bram Moolenaarfb539272014-08-22 19:21:47 +0200349 syn match goExtraType /\<unsafe\.Pointer\>/
350endif
351
352" Space-tab error
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200353if s:HighlightSpaceTabError()
Bram Moolenaarfb539272014-08-22 19:21:47 +0200354 syn match goSpaceError display " \+\t"me=e-1
355endif
356
357" Trailing white space error
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200358if s:HighlightTrailingWhitespaceError()
Bram Moolenaarfb539272014-08-22 19:21:47 +0200359 syn match goSpaceError display excludenl "\s\+$"
360endif
361
362hi def link goExtraType Type
363hi def link goSpaceError Error
364
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200365
366
367" included from: https://github.com/athom/more-colorful.vim/blob/master/after/syntax/go.vim
368"
369" Comments; their contents
370syn keyword goTodo contained NOTE
371hi def link goTodo Todo
372
373syn match goVarArgs /\.\.\./
374
375" Operators;
376if s:HighlightOperators()
377 " match single-char operators: - + % < > ! & | ^ * =
378 " and corresponding two-char operators: -= += %= <= >= != &= |= ^= *= ==
379 syn match goOperator /[-+%<>!&|^*=]=\?/
380 " match / and /=
381 syn match goOperator /\/\%(=\|\ze[^/*]\)/
382 " match two-char operators: << >> &^
383 " and corresponding three-char operators: <<= >>= &^=
384 syn match goOperator /\%(<<\|>>\|&^\)=\?/
385 " match remaining two-char operators: := && || <- ++ --
386 syn match goOperator /:=\|||\|<-\|++\|--/
Bram Moolenaard13166e2022-11-18 21:49:57 +0000387 " match ~
388 syn match goOperator /\~/
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200389 " match ...
390
391 hi def link goPointerOperator goOperator
392 hi def link goVarArgs goOperator
393endif
394hi def link goOperator Operator
395
Bram Moolenaard13166e2022-11-18 21:49:57 +0000396" -> type constraint opening bracket
397" |-> start non-counting group
398" || -> any word character
399" || | -> at least one, as many as possible
400" || | | -> start non-counting group
401" || | | | -> match ~
402" || | | | | -> at most once
403" || | | | | | -> allow a slice type
404" || | | | | | | -> any word character
405" || | | | | | | | -> start a non-counting group
406" || | | | | | | | | -> that matches word characters and |
407" || | | | | | | | | | -> close the non-counting group
408" || | | | | | | | | | | -> close the non-counting group
409" || | | | | | | | | | | |-> any number of matches
410" || | | | | | | | | | | || -> start a non-counting group
411" || | | | | | | | | | | || | -> a comma and whitespace
412" || | | | | | | | | | | || | | -> at most once
413" || | | | | | | | | | | || | | | -> close the non-counting group
414" || | | | | | | | | | | || | | | | -> at least one of those non-counting groups, as many as possible
415" || | | | | | -------- | | | | || | | | | | -> type constraint closing bracket
416" || | | | | || | | | | | || | | | | | |
417syn match goTypeParams /\[\%(\w\+\s\+\%(\~\?\%(\[]\)\?\w\%(\w\||\)\)*\%(,\s*\)\?\)\+\]/ nextgroup=goSimpleParams,goDeclType contained
418
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200419" Functions;
420if s:HighlightFunctions() || s:HighlightFunctionParameters()
421 syn match goDeclaration /\<func\>/ nextgroup=goReceiver,goFunction,goSimpleParams skipwhite skipnl
Bram Moolenaard13166e2022-11-18 21:49:57 +0000422 syn match goReceiverDecl /(\s*\zs\%(\%(\w\+\s\+\)\?\*\?\w\+\%(\[\%(\%(\[\]\)\?\w\+\%(,\s*\)\?\)\+\]\)\?\)\ze\s*)/ contained contains=goReceiverVar,goReceiverType,goPointerOperator
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200423 syn match goReceiverVar /\w\+\ze\s\+\%(\w\|\*\)/ nextgroup=goPointerOperator,goReceiverType skipwhite skipnl contained
424 syn match goPointerOperator /\*/ nextgroup=goReceiverType contained skipwhite skipnl
Bram Moolenaard13166e2022-11-18 21:49:57 +0000425 syn match goFunction /\w\+/ nextgroup=goSimpleParams,goTypeParams contained skipwhite skipnl
426 syn match goReceiverType /\w\+\%(\[\%(\%(\[\]\)\?\w\+\%(,\s*\)\?\)\+\]\)\?\ze\s*)/ contained
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200427 if s:HighlightFunctionParameters()
428 syn match goSimpleParams /(\%(\w\|\_s\|[*\.\[\],\{\}<>-]\)*)/ contained contains=goParamName,goType nextgroup=goFunctionReturn skipwhite skipnl
429 syn match goFunctionReturn /(\%(\w\|\_s\|[*\.\[\],\{\}<>-]\)*)/ contained contains=goParamName,goType skipwhite skipnl
430 syn match goParamName /\w\+\%(\s*,\s*\w\+\)*\ze\s\+\%(\w\|\.\|\*\|\[\)/ contained nextgroup=goParamType skipwhite skipnl
431 syn match goParamType /\%([^,)]\|\_s\)\+,\?/ contained nextgroup=goParamName skipwhite skipnl
432 \ contains=goVarArgs,goType,goSignedInts,goUnsignedInts,goFloats,goComplexes,goDeclType,goBlock
433 hi def link goReceiverVar goParamName
434 hi def link goParamName Identifier
435 endif
Bram Moolenaard13166e2022-11-18 21:49:57 +0000436 syn match goReceiver /(\s*\%(\w\+\s\+\)\?\*\?\s*\w\+\%(\[\%(\%(\[\]\)\?\w\+\%(,\s*\)\?\)\+\]\)\?\s*)\ze\s*\w/ contained nextgroup=goFunction contains=goReceiverDecl skipwhite skipnl
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200437else
438 syn keyword goDeclaration func
439endif
440hi def link goFunction Function
441
442" Function calls;
443if s:HighlightFunctionCalls()
Bram Moolenaard13166e2022-11-18 21:49:57 +0000444 syn match goFunctionCall /\w\+\ze\%(\[\%(\%(\[]\)\?\w\+\(,\s*\)\?\)\+\]\)\?(/ contains=goBuiltins,goDeclaration
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200445endif
446hi def link goFunctionCall Type
447
448" Fields;
449if s:HighlightFields()
450 " 1. Match a sequence of word characters coming after a '.'
451 " 2. Require the following but dont match it: ( \@= see :h E59)
452 " - The symbols: / - + * % OR
453 " - The symbols: [] {} <> ) OR
454 " - The symbols: \n \r space OR
455 " - The symbols: , : .
456 " 3. Have the start of highlight (hs) be the start of matched
457 " pattern (s) offsetted one to the right (+1) (see :h E401)
458 syn match goField /\.\w\+\
459 \%(\%([\/\-\+*%]\)\|\
460 \%([\[\]{}<\>\)]\)\|\
461 \%([\!=\^|&]\)\|\
462 \%([\n\r\ ]\)\|\
463 \%([,\:.]\)\)\@=/hs=s+1
464endif
465hi def link goField Identifier
466
467" Structs & Interfaces;
468if s:HighlightTypes()
469 syn match goTypeConstructor /\<\w\+{\@=/
470 syn match goTypeDecl /\<type\>/ nextgroup=goTypeName skipwhite skipnl
Bram Moolenaard13166e2022-11-18 21:49:57 +0000471 syn match goTypeName /\w\+/ contained nextgroup=goDeclType,goTypeParams skipwhite skipnl
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200472 syn match goDeclType /\<\%(interface\|struct\)\>/ skipwhite skipnl
473 hi def link goReceiverType Type
474else
475 syn keyword goDeclType struct interface
476 syn keyword goDeclaration type
477endif
478hi def link goTypeConstructor Type
479hi def link goTypeName Type
480hi def link goTypeDecl Keyword
481hi def link goDeclType Keyword
482
483" Variable Assignments
484if s:HighlightVariableAssignments()
485 syn match goVarAssign /\v[_.[:alnum:]]+(,\s*[_.[:alnum:]]+)*\ze(\s*([-^+|^\/%&]|\*|\<\<|\>\>|\&\^)?\=[^=])/
486 hi def link goVarAssign Special
487endif
488
489" Variable Declarations
490if s:HighlightVariableDeclarations()
491 syn match goVarDefs /\v\w+(,\s*\w+)*\ze(\s*:\=)/
492 hi def link goVarDefs Special
493endif
494
495" Build Constraints
496if s:HighlightBuildConstraints()
Bram Moolenaar34cc7d82021-09-21 20:09:51 +0200497 syn match goBuildKeyword display contained "+build\|go:build"
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200498 " Highlight the known values of GOOS, GOARCH, and other +build options.
499 syn keyword goBuildDirectives contained
500 \ android darwin dragonfly freebsd linux nacl netbsd openbsd plan9
501 \ solaris windows 386 amd64 amd64p32 arm armbe arm64 arm64be ppc64
502 \ ppc64le mips mipsle mips64 mips64le mips64p32 mips64p32le ppc
503 \ s390 s390x sparc sparc64 cgo ignore race
504
505 " Other words in the build directive are build tags not listed above, so
506 " avoid highlighting them as comments by using a matchgroup just for the
507 " start of the comment.
508 " The rs=s+2 option lets the \s*+build portion be part of the inner region
509 " instead of the matchgroup so it will be highlighted as a goBuildKeyword.
510 syn region goBuildComment matchgroup=goBuildCommentStart
Bram Moolenaard13166e2022-11-18 21:49:57 +0000511 \ start="//\(\s*+build\s\|go:build\)"rs=s+2 end="$"
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200512 \ contains=goBuildKeyword,goBuildDirectives
513 hi def link goBuildCommentStart Comment
514 hi def link goBuildDirectives Type
515 hi def link goBuildKeyword PreProc
516endif
517
518if s:HighlightBuildConstraints() || s:FoldEnable('package_comment')
519 " One or more line comments that are followed immediately by a "package"
520 " declaration are treated like package documentation, so these must be
521 " matched as comments to avoid looking like working build constraints.
522 " The he, me, and re options let the "package" itself be highlighted by
523 " the usual rules.
524 exe 'syn region goPackageComment start=/\v(\/\/.*\n)+\s*package/'
525 \ . ' end=/\v\n\s*package/he=e-7,me=e-7,re=e-7'
526 \ . ' contains=@goCommentGroup,@Spell'
527 \ . (s:FoldEnable('package_comment') ? ' fold' : '')
528 exe 'syn region goPackageComment start=/\v^\s*\/\*.*\n(.*\n)*\s*\*\/\npackage/'
529 \ . ' end=/\v\*\/\n\s*package/he=e-7,me=e-7,re=e-7'
530 \ . ' contains=@goCommentGroup,@Spell'
531 \ . (s:FoldEnable('package_comment') ? ' fold' : '')
532 hi def link goPackageComment Comment
533endif
534
535" :GoCoverage commands
536hi def link goCoverageNormalText Comment
537
Bram Moolenaarfb539272014-08-22 19:21:47 +0200538" Search backwards for a global declaration to start processing the syntax.
539"syn sync match goSync grouphere NONE /^\(const\|var\|type\|func\)\>/
540
541" There's a bug in the implementation of grouphere. For now, use the
542" following as a more expensive/less precise workaround.
543syn sync minlines=500
544
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200545let b:current_syntax = "go"
546
547let &cpo = s:keepcpo
548unlet s:keepcpo
Bram Moolenaarfb539272014-08-22 19:21:47 +0200549
550" vim: sw=2 sts=2 et