blob: feed9646fba8db834a291d6c078f88717b2585f5 [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>
Linda_pp122d0682024-04-14 00:56:17 +09008" Latest Revision: 2024-04-13
Matthew Hughes3d46de72024-03-17 20:05:23 +01009" 2024-03-17: - fix goPackageComment highlight (by Vim Project)
Bram Moolenaar90df4b92021-07-07 20:26:08 +020010" License: BSD-style. See LICENSE file in source repository.
11" Repository: https://github.com/fatih/vim-go
Bram Moolenaarfb539272014-08-22 19:21:47 +020012
13" Quit when a (custom) syntax file was already loaded
Bram Moolenaar90df4b92021-07-07 20:26:08 +020014if exists("b:current_syntax")
Bram Moolenaarfb539272014-08-22 19:21:47 +020015 finish
16endif
17
Bram Moolenaar90df4b92021-07-07 20:26:08 +020018let s:keepcpo = &cpo
19set cpo&vim
20
21function! s:FoldEnable(...) abort
22 if a:0 > 0
23 return index(s:FoldEnable(), a:1) > -1
24 endif
25 return get(g:, 'go_fold_enable', ['block', 'import', 'varconst', 'package_comment'])
26endfunction
27
28function! s:HighlightArrayWhitespaceError() abort
29 return get(g:, 'go_highlight_array_whitespace_error', 0)
30endfunction
31
32function! s:HighlightChanWhitespaceError() abort
33 return get(g:, 'go_highlight_chan_whitespace_error', 0)
34endfunction
35
36function! s:HighlightExtraTypes() abort
37 return get(g:, 'go_highlight_extra_types', 0)
38endfunction
39
40function! s:HighlightSpaceTabError() abort
41 return get(g:, 'go_highlight_space_tab_error', 0)
42endfunction
43
44function! s:HighlightTrailingWhitespaceError() abort
45 return get(g:, 'go_highlight_trailing_whitespace_error', 0)
46endfunction
47
48function! s:HighlightOperators() abort
49 return get(g:, 'go_highlight_operators', 0)
50endfunction
51
52function! s:HighlightFunctions() abort
53 return get(g:, 'go_highlight_functions', 0)
54endfunction
55
56function! s:HighlightFunctionParameters() abort
57 return get(g:, 'go_highlight_function_parameters', 0)
58endfunction
59
60function! s:HighlightFunctionCalls() abort
61 return get(g:, 'go_highlight_function_calls', 0)
62endfunction
63
64function! s:HighlightFields() abort
65 return get(g:, 'go_highlight_fields', 0)
66endfunction
67
68function! s:HighlightTypes() abort
69 return get(g:, 'go_highlight_types', 0)
70endfunction
71
72function! s:HighlightBuildConstraints() abort
73 return get(g:, 'go_highlight_build_constraints', 0)
74endfunction
75
76function! s:HighlightStringSpellcheck() abort
77 return get(g:, 'go_highlight_string_spellcheck', 1)
78endfunction
79
80function! s:HighlightFormatStrings() abort
81 return get(g:, 'go_highlight_format_strings', 1)
82endfunction
83
84function! s:HighlightGenerateTags() abort
85 return get(g:, 'go_highlight_generate_tags', 0)
86endfunction
87
88function! s:HighlightVariableAssignments() abort
89 return get(g:, 'go_highlight_variable_assignments', 0)
90endfunction
91
92function! s:HighlightVariableDeclarations() abort
93 return get(g:, 'go_highlight_variable_declarations', 0)
94endfunction
Bram Moolenaarfb539272014-08-22 19:21:47 +020095
96syn case match
97
Bram Moolenaar90df4b92021-07-07 20:26:08 +020098syn keyword goPackage package
99syn keyword goImport import contained
100syn keyword goVar var contained
101syn keyword goConst const contained
Bram Moolenaarfb539272014-08-22 19:21:47 +0200102
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200103hi def link goPackage Statement
104hi def link goImport Statement
105hi def link goVar Keyword
106hi def link goConst Keyword
Bram Moolenaarfb539272014-08-22 19:21:47 +0200107hi def link goDeclaration Keyword
Bram Moolenaarfb539272014-08-22 19:21:47 +0200108
109" Keywords within functions
110syn keyword goStatement defer go goto return break continue fallthrough
111syn keyword goConditional if else switch select
112syn keyword goLabel case default
113syn keyword goRepeat for range
114
115hi def link goStatement Statement
116hi def link goConditional Conditional
117hi def link goLabel Label
118hi def link goRepeat Repeat
119
120" Predefined types
Bram Moolenaard13166e2022-11-18 21:49:57 +0000121syn keyword goType chan map bool string error any comparable
Bram Moolenaarfb539272014-08-22 19:21:47 +0200122syn keyword goSignedInts int int8 int16 int32 int64 rune
123syn keyword goUnsignedInts byte uint uint8 uint16 uint32 uint64 uintptr
124syn keyword goFloats float32 float64
125syn keyword goComplexes complex64 complex128
126
127hi def link goType Type
128hi def link goSignedInts Type
129hi def link goUnsignedInts Type
130hi def link goFloats Type
131hi def link goComplexes Type
132
Bram Moolenaarfb539272014-08-22 19:21:47 +0200133" Predefined functions and values
José-Paul Db0d584d2023-08-22 20:29:19 +0200134syn keyword goBuiltins append cap clear close complex copy delete imag len
135syn keyword goBuiltins make max min new panic print println real recover
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200136syn keyword goBoolean true false
137syn keyword goPredefinedIdentifiers nil iota
Bram Moolenaarfb539272014-08-22 19:21:47 +0200138
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200139hi def link goBuiltins Identifier
Bram Moolenaar938ae282023-02-20 20:44:55 +0000140hi def link goPredefinedIdentifiers Constant
141" Boolean links to Constant by default by vim: goBoolean and goPredefinedIdentifiers
142" will be highlighted the same, but having the separate groups allows users to
143" have separate highlighting for them if they desire.
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200144hi def link goBoolean Boolean
Bram Moolenaarfb539272014-08-22 19:21:47 +0200145
146" Comments; their contents
147syn keyword goTodo contained TODO FIXME XXX BUG
148syn cluster goCommentGroup contains=goTodo
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200149
150syn region goComment start="//" end="$" contains=goGenerate,@goCommentGroup,@Spell
151if s:FoldEnable('comment')
152 syn region goComment start="/\*" end="\*/" contains=@goCommentGroup,@Spell fold
153 syn match goComment "\v(^\s*//.*\n)+" contains=goGenerate,@goCommentGroup,@Spell fold
154else
155 syn region goComment start="/\*" end="\*/" contains=@goCommentGroup,@Spell
156endif
Bram Moolenaarfb539272014-08-22 19:21:47 +0200157
158hi def link goComment Comment
159hi def link goTodo Todo
160
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200161if s:HighlightGenerateTags()
162 syn match goGenerateVariables contained /\%(\$GOARCH\|\$GOOS\|\$GOFILE\|\$GOLINE\|\$GOPACKAGE\|\$DOLLAR\)\>/
163 syn region goGenerate start="^\s*//go:generate" end="$" contains=goGenerateVariables
164 hi def link goGenerate PreProc
165 hi def link goGenerateVariables Special
166endif
167
Bram Moolenaarfb539272014-08-22 19:21:47 +0200168" Go escapes
169syn match goEscapeOctal display contained "\\[0-7]\{3}"
170syn match goEscapeC display contained +\\[abfnrtv\\'"]+
171syn match goEscapeX display contained "\\x\x\{2}"
172syn match goEscapeU display contained "\\u\x\{4}"
173syn match goEscapeBigU display contained "\\U\x\{8}"
174syn match goEscapeError display contained +\\[^0-7xuUabfnrtv\\'"]+
175
176hi def link goEscapeOctal goSpecialString
177hi def link goEscapeC goSpecialString
178hi def link goEscapeX goSpecialString
179hi def link goEscapeU goSpecialString
180hi def link goEscapeBigU goSpecialString
181hi def link goSpecialString Special
182hi def link goEscapeError Error
183
184" Strings and their contents
185syn cluster goStringGroup contains=goEscapeOctal,goEscapeC,goEscapeX,goEscapeU,goEscapeBigU,goEscapeError
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200186if s:HighlightStringSpellcheck()
187 syn region goString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@goStringGroup,@Spell
188 syn region goRawString start=+`+ end=+`+ contains=@Spell
189else
190 syn region goString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@goStringGroup
191 syn region goRawString start=+`+ end=+`+
192endif
193
Linda_pp122d0682024-04-14 00:56:17 +0900194syn match goImportString /^\%(\s\+\|import \)\(\h\w* \)\?\zs"[^"]\+"/ contained containedin=goImport
Bram Moolenaard13166e2022-11-18 21:49:57 +0000195
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200196if s:HighlightFormatStrings()
197 " [n] notation is valid for specifying explicit argument indexes
198 " 1. Match a literal % not preceded by a %.
199 " 2. Match any number of -, #, 0, space, or +
200 " 3. Match * or [n]* or any number or nothing before a .
201 " 4. Match * or [n]* or any number or nothing after a .
202 " 5. Match [n] or nothing before a verb
203 " 6. Match a formatting verb
204 syn match goFormatSpecifier /\
205 \%([^%]\%(%%\)*\)\
206 \@<=%[-#0 +]*\
207 \%(\%(\%(\[\d\+\]\)\=\*\)\|\d\+\)\=\
208 \%(\.\%(\%(\%(\[\d\+\]\)\=\*\)\|\d\+\)\=\)\=\
209 \%(\[\d\+\]\)\=[vTtbcdoqxXUeEfFgGspw]/ contained containedin=goString,goRawString
210 hi def link goFormatSpecifier goSpecialString
211endif
Bram Moolenaarfb539272014-08-22 19:21:47 +0200212
Bram Moolenaard13166e2022-11-18 21:49:57 +0000213hi def link goImportString String
Bram Moolenaarfb539272014-08-22 19:21:47 +0200214hi def link goString String
215hi def link goRawString String
216
217" Characters; their contents
218syn cluster goCharacterGroup contains=goEscapeOctal,goEscapeC,goEscapeX,goEscapeU,goEscapeBigU
219syn region goCharacter start=+'+ skip=+\\\\\|\\'+ end=+'+ contains=@goCharacterGroup
220
221hi def link goCharacter Character
222
223" Regions
Bram Moolenaarfb539272014-08-22 19:21:47 +0200224syn region goParen start='(' end=')' transparent
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200225if s:FoldEnable('block')
226 syn region goBlock start="{" end="}" transparent fold
227else
228 syn region goBlock start="{" end="}" transparent
229endif
230
231" import
232if s:FoldEnable('import')
Bram Moolenaard13166e2022-11-18 21:49:57 +0000233 syn region goImport start='import (' end=')' transparent fold contains=goImport,goImportString,goComment
Billie Cleekea9a93e2024-01-22 11:08:44 -0800234 syn match goImport /^import ()/ transparent fold contains=goImport
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200235else
Bram Moolenaard13166e2022-11-18 21:49:57 +0000236 syn region goImport start='import (' end=')' transparent contains=goImport,goImportString,goComment
Billie Cleekea9a93e2024-01-22 11:08:44 -0800237 syn match goImport /^import ()/ transparent contains=goImport
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200238endif
239
240" var, const
241if s:FoldEnable('varconst')
242 syn region goVar start='var (' end='^\s*)$' transparent fold
Billie Cleekea9a93e2024-01-22 11:08:44 -0800243 \ contains=ALLBUT,goParen,goBlock,goFunction,goTypeName,goReceiverType,goReceiverVar,goParamName,goParamType,goSimpleParams,goPointerOperator
244 syn match goVar /var ()/ transparent fold
245 \ contains=goVar
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200246 syn region goConst start='const (' end='^\s*)$' transparent fold
Billie Cleekea9a93e2024-01-22 11:08:44 -0800247 \ contains=ALLBUT,goParen,goBlock,goFunction,goTypeName,goReceiverType,goReceiverVar,goParamName,goParamType,goSimpleParams,goPointerOperator
248 syn match goConst /const ()/ transparent fold
249 \ contains=goConst
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200250else
251 syn region goVar start='var (' end='^\s*)$' transparent
Billie Cleekea9a93e2024-01-22 11:08:44 -0800252 \ contains=ALLBUT,goParen,goBlock,goFunction,goTypeName,goReceiverType,goReceiverVar,goParamName,goParamType,goSimpleParams,goPointerOperator
253 syn match goVar /var ()/ transparent
254 \ contains=goVar
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200255 syn region goConst start='const (' end='^\s*)$' transparent
Billie Cleekea9a93e2024-01-22 11:08:44 -0800256 \ contains=ALLBUT,goParen,goBlock,goFunction,goTypeName,goReceiverType,goReceiverVar,goParamName,goParamType,goSimpleParams,goPointerOperator
257 syn match goConst /const ()/ transparent
258 \ contains=goConst
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200259endif
260
261" Single-line var, const, and import.
262syn match goSingleDecl /\%(import\|var\|const\) [^(]\@=/ contains=goImport,goVar,goConst
Bram Moolenaarfb539272014-08-22 19:21:47 +0200263
264" Integers
Bram Moolenaard13166e2022-11-18 21:49:57 +0000265syn match goDecimalInt "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)\>"
266syn match goHexadecimalInt "\<-\=0[xX]_\?\%(\x\|\x_\x\)\+\>"
267syn match goOctalInt "\<-\=0[oO]\?_\?\%(\o\|\o_\o\)\+\>"
268syn match goBinaryInt "\<-\=0[bB]_\?\%([01]\|[01]_[01]\)\+\>"
Bram Moolenaarfb539272014-08-22 19:21:47 +0200269
270hi def link goDecimalInt Integer
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200271hi def link goDecimalError Error
Bram Moolenaarfb539272014-08-22 19:21:47 +0200272hi def link goHexadecimalInt Integer
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200273hi def link goHexadecimalError Error
Bram Moolenaarfb539272014-08-22 19:21:47 +0200274hi def link goOctalInt Integer
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200275hi def link goOctalError Error
276hi def link goBinaryInt Integer
277hi def link goBinaryError Error
Bram Moolenaarfb539272014-08-22 19:21:47 +0200278hi def link Integer Number
279
280" Floating point
Bram Moolenaard13166e2022-11-18 21:49:57 +0000281"float_lit = decimal_float_lit | hex_float_lit .
282"
283"decimal_float_lit = decimal_digits "." [ decimal_digits ] [ decimal_exponent ] |
284" decimal_digits decimal_exponent |
285" "." decimal_digits [ decimal_exponent ] .
286"decimal_exponent = ( "e" | "E" ) [ "+" | "-" ] decimal_digits .
287"
288"hex_float_lit = "0" ( "x" | "X" ) hex_mantissa hex_exponent .
289"hex_mantissa = [ "_" ] hex_digits "." [ hex_digits ] |
290" [ "_" ] hex_digits |
291" "." hex_digits .
292"hex_exponent = ( "p" | "P" ) [ "+" | "-" ] decimal_digits .
293" decimal floats with a decimal point
294syn match goFloat "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)\.\%(\%(\%(\d\|\d_\d\)\+\)\=\%([Ee][-+]\=\%(\d\|\d_\d\)\+\)\=\>\)\="
295syn match goFloat "\s\zs-\=\.\%(\d\|\d_\d\)\+\%(\%([Ee][-+]\=\%(\d\|\d_\d\)\+\)\>\)\="
296" decimal floats without a decimal point
297syn match goFloat "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)[Ee][-+]\=\%(\d\|\d_\d\)\+\>"
298" hexadecimal floats with a decimal point
299syn match goHexadecimalFloat "\<-\=0[xX]\%(_\x\|\x\)\+\.\%(\%(\x\|\x_\x\)\+\)\=\%([Pp][-+]\=\%(\d\|\d_\d\)\+\)\=\>"
300syn match goHexadecimalFloat "\<-\=0[xX]\.\%(\x\|\x_\x\)\+\%([Pp][-+]\=\%(\d\|\d_\d\)\+\)\=\>"
301" hexadecimal floats without a decimal point
302syn match goHexadecimalFloat "\<-\=0[xX]\%(_\x\|\x\)\+[Pp][-+]\=\%(\d\|\d_\d\)\+\>"
Bram Moolenaarfb539272014-08-22 19:21:47 +0200303
304hi def link goFloat Float
Bram Moolenaard13166e2022-11-18 21:49:57 +0000305hi def link goHexadecimalFloat Float
Bram Moolenaarfb539272014-08-22 19:21:47 +0200306
307" Imaginary literals
Bram Moolenaard13166e2022-11-18 21:49:57 +0000308syn match goImaginaryDecimal "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)i\>"
309syn match goImaginaryHexadecimal "\<-\=0[xX]_\?\%(\x\|\x_\x\)\+i\>"
310syn match goImaginaryOctal "\<-\=0[oO]\?_\?\%(\o\|\o_\o\)\+i\>"
311syn match goImaginaryBinary "\<-\=0[bB]_\?\%([01]\|[01]_[01]\)\+i\>"
Bram Moolenaarfb539272014-08-22 19:21:47 +0200312
Bram Moolenaard13166e2022-11-18 21:49:57 +0000313" imaginary decimal floats with a decimal point
314syn match goImaginaryFloat "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)\.\%(\%(\%(\d\|\d_\d\)\+\)\=\%([Ee][-+]\=\%(\d\|\d_\d\)\+\)\=\)\=i\>"
315syn match goImaginaryFloat "\s\zs-\=\.\%(\d\|\d_\d\)\+\%([Ee][-+]\=\%(\d\|\d_\d\)\+\)\=i\>"
316" imaginary decimal floats without a decimal point
317syn match goImaginaryFloat "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)[Ee][-+]\=\%(\d\|\d_\d\)\+i\>"
318" imaginary hexadecimal floats with a decimal point
319syn match goImaginaryHexadecimalFloat "\<-\=0[xX]\%(_\x\|\x\)\+\.\%(\%(\x\|\x_\x\)\+\)\=\%([Pp][-+]\=\%(\d\|\d_\d\)\+\)\=i\>"
320syn match goImaginaryHexadecimalFloat "\<-\=0[xX]\.\%(\x\|\x_\x\)\+\%([Pp][-+]\=\%(\d\|\d_\d\)\+\)\=i\>"
321" imaginary hexadecimal floats without a decimal point
322syn match goImaginaryHexadecimalFloat "\<-\=0[xX]\%(_\x\|\x\)\+[Pp][-+]\=\%(\d\|\d_\d\)\+i\>"
323
324hi def link goImaginaryDecimal Number
325hi def link goImaginaryHexadecimal Number
326hi def link goImaginaryOctal Number
327hi def link goImaginaryBinary Number
328hi def link goImaginaryFloat Float
329hi def link goImaginaryHexadecimalFloat Float
Bram Moolenaarfb539272014-08-22 19:21:47 +0200330
331" Spaces after "[]"
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200332if s:HighlightArrayWhitespaceError()
333 syn match goSpaceError display "\%(\[\]\)\@<=\s\+"
Bram Moolenaarfb539272014-08-22 19:21:47 +0200334endif
335
336" Spacing errors around the 'chan' keyword
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200337if s:HighlightChanWhitespaceError()
Bram Moolenaarfb539272014-08-22 19:21:47 +0200338 " receive-only annotation on chan type
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200339 "
340 " \(\<chan\>\)\@<!<- (only pick arrow when it doesn't come after a chan)
341 " this prevents picking up 'chan<- chan<-' but not '<- chan'
342 syn match goSpaceError display "\%(\%(\<chan\>\)\@<!<-\)\@<=\s\+\%(\<chan\>\)\@="
343
Bram Moolenaarfb539272014-08-22 19:21:47 +0200344 " send-only annotation on chan type
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200345 "
346 " \(<-\)\@<!\<chan\> (only pick chan when it doesn't come after an arrow)
347 " this prevents picking up '<-chan <-chan' but not 'chan <-'
348 syn match goSpaceError display "\%(\%(<-\)\@<!\<chan\>\)\@<=\s\+\%(<-\)\@="
349
Bram Moolenaarfb539272014-08-22 19:21:47 +0200350 " value-ignoring receives in a few contexts
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200351 syn match goSpaceError display "\%(\%(^\|[={(,;]\)\s*<-\)\@<=\s\+"
Bram Moolenaarfb539272014-08-22 19:21:47 +0200352endif
353
354" Extra types commonly seen
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200355if s:HighlightExtraTypes()
356 syn match goExtraType /\<bytes\.\%(Buffer\)\>/
357 syn match goExtraType /\<context\.\%(Context\)\>/
358 syn match goExtraType /\<io\.\%(Reader\|ReadSeeker\|ReadWriter\|ReadCloser\|ReadWriteCloser\|Writer\|WriteCloser\|Seeker\)\>/
359 syn match goExtraType /\<reflect\.\%(Kind\|Type\|Value\)\>/
Bram Moolenaarfb539272014-08-22 19:21:47 +0200360 syn match goExtraType /\<unsafe\.Pointer\>/
361endif
362
363" Space-tab error
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200364if s:HighlightSpaceTabError()
Bram Moolenaarfb539272014-08-22 19:21:47 +0200365 syn match goSpaceError display " \+\t"me=e-1
366endif
367
368" Trailing white space error
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200369if s:HighlightTrailingWhitespaceError()
Bram Moolenaarfb539272014-08-22 19:21:47 +0200370 syn match goSpaceError display excludenl "\s\+$"
371endif
372
373hi def link goExtraType Type
374hi def link goSpaceError Error
375
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200376
377
378" included from: https://github.com/athom/more-colorful.vim/blob/master/after/syntax/go.vim
379"
380" Comments; their contents
381syn keyword goTodo contained NOTE
382hi def link goTodo Todo
383
384syn match goVarArgs /\.\.\./
385
386" Operators;
387if s:HighlightOperators()
388 " match single-char operators: - + % < > ! & | ^ * =
389 " and corresponding two-char operators: -= += %= <= >= != &= |= ^= *= ==
390 syn match goOperator /[-+%<>!&|^*=]=\?/
391 " match / and /=
392 syn match goOperator /\/\%(=\|\ze[^/*]\)/
393 " match two-char operators: << >> &^
394 " and corresponding three-char operators: <<= >>= &^=
395 syn match goOperator /\%(<<\|>>\|&^\)=\?/
396 " match remaining two-char operators: := && || <- ++ --
397 syn match goOperator /:=\|||\|<-\|++\|--/
Bram Moolenaard13166e2022-11-18 21:49:57 +0000398 " match ~
399 syn match goOperator /\~/
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200400 " match ...
401
402 hi def link goPointerOperator goOperator
403 hi def link goVarArgs goOperator
404endif
405hi def link goOperator Operator
406
Bram Moolenaard13166e2022-11-18 21:49:57 +0000407" -> type constraint opening bracket
408" |-> start non-counting group
409" || -> any word character
410" || | -> at least one, as many as possible
411" || | | -> start non-counting group
412" || | | | -> match ~
413" || | | | | -> at most once
414" || | | | | | -> allow a slice type
415" || | | | | | | -> any word character
416" || | | | | | | | -> start a non-counting group
417" || | | | | | | | | -> that matches word characters and |
418" || | | | | | | | | | -> close the non-counting group
419" || | | | | | | | | | | -> close the non-counting group
420" || | | | | | | | | | | |-> any number of matches
421" || | | | | | | | | | | || -> start a non-counting group
422" || | | | | | | | | | | || | -> a comma and whitespace
423" || | | | | | | | | | | || | | -> at most once
424" || | | | | | | | | | | || | | | -> close the non-counting group
425" || | | | | | | | | | | || | | | | -> at least one of those non-counting groups, as many as possible
426" || | | | | | -------- | | | | || | | | | | -> type constraint closing bracket
427" || | | | | || | | | | | || | | | | | |
428syn match goTypeParams /\[\%(\w\+\s\+\%(\~\?\%(\[]\)\?\w\%(\w\||\)\)*\%(,\s*\)\?\)\+\]/ nextgroup=goSimpleParams,goDeclType contained
429
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200430" Functions;
431if s:HighlightFunctions() || s:HighlightFunctionParameters()
432 syn match goDeclaration /\<func\>/ nextgroup=goReceiver,goFunction,goSimpleParams skipwhite skipnl
Bram Moolenaard13166e2022-11-18 21:49:57 +0000433 syn match goReceiverDecl /(\s*\zs\%(\%(\w\+\s\+\)\?\*\?\w\+\%(\[\%(\%(\[\]\)\?\w\+\%(,\s*\)\?\)\+\]\)\?\)\ze\s*)/ contained contains=goReceiverVar,goReceiverType,goPointerOperator
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200434 syn match goReceiverVar /\w\+\ze\s\+\%(\w\|\*\)/ nextgroup=goPointerOperator,goReceiverType skipwhite skipnl contained
435 syn match goPointerOperator /\*/ nextgroup=goReceiverType contained skipwhite skipnl
Bram Moolenaard13166e2022-11-18 21:49:57 +0000436 syn match goFunction /\w\+/ nextgroup=goSimpleParams,goTypeParams contained skipwhite skipnl
437 syn match goReceiverType /\w\+\%(\[\%(\%(\[\]\)\?\w\+\%(,\s*\)\?\)\+\]\)\?\ze\s*)/ contained
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200438 if s:HighlightFunctionParameters()
439 syn match goSimpleParams /(\%(\w\|\_s\|[*\.\[\],\{\}<>-]\)*)/ contained contains=goParamName,goType nextgroup=goFunctionReturn skipwhite skipnl
440 syn match goFunctionReturn /(\%(\w\|\_s\|[*\.\[\],\{\}<>-]\)*)/ contained contains=goParamName,goType skipwhite skipnl
441 syn match goParamName /\w\+\%(\s*,\s*\w\+\)*\ze\s\+\%(\w\|\.\|\*\|\[\)/ contained nextgroup=goParamType skipwhite skipnl
442 syn match goParamType /\%([^,)]\|\_s\)\+,\?/ contained nextgroup=goParamName skipwhite skipnl
443 \ contains=goVarArgs,goType,goSignedInts,goUnsignedInts,goFloats,goComplexes,goDeclType,goBlock
444 hi def link goReceiverVar goParamName
445 hi def link goParamName Identifier
446 endif
Bram Moolenaard13166e2022-11-18 21:49:57 +0000447 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 +0200448else
449 syn keyword goDeclaration func
450endif
451hi def link goFunction Function
452
453" Function calls;
454if s:HighlightFunctionCalls()
Bram Moolenaard13166e2022-11-18 21:49:57 +0000455 syn match goFunctionCall /\w\+\ze\%(\[\%(\%(\[]\)\?\w\+\(,\s*\)\?\)\+\]\)\?(/ contains=goBuiltins,goDeclaration
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200456endif
457hi def link goFunctionCall Type
458
459" Fields;
460if s:HighlightFields()
461 " 1. Match a sequence of word characters coming after a '.'
462 " 2. Require the following but dont match it: ( \@= see :h E59)
463 " - The symbols: / - + * % OR
464 " - The symbols: [] {} <> ) OR
465 " - The symbols: \n \r space OR
466 " - The symbols: , : .
467 " 3. Have the start of highlight (hs) be the start of matched
468 " pattern (s) offsetted one to the right (+1) (see :h E401)
469 syn match goField /\.\w\+\
470 \%(\%([\/\-\+*%]\)\|\
471 \%([\[\]{}<\>\)]\)\|\
472 \%([\!=\^|&]\)\|\
473 \%([\n\r\ ]\)\|\
474 \%([,\:.]\)\)\@=/hs=s+1
475endif
476hi def link goField Identifier
477
478" Structs & Interfaces;
479if s:HighlightTypes()
480 syn match goTypeConstructor /\<\w\+{\@=/
481 syn match goTypeDecl /\<type\>/ nextgroup=goTypeName skipwhite skipnl
Bram Moolenaard13166e2022-11-18 21:49:57 +0000482 syn match goTypeName /\w\+/ contained nextgroup=goDeclType,goTypeParams skipwhite skipnl
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200483 syn match goDeclType /\<\%(interface\|struct\)\>/ skipwhite skipnl
484 hi def link goReceiverType Type
485else
486 syn keyword goDeclType struct interface
487 syn keyword goDeclaration type
488endif
489hi def link goTypeConstructor Type
490hi def link goTypeName Type
491hi def link goTypeDecl Keyword
492hi def link goDeclType Keyword
493
494" Variable Assignments
495if s:HighlightVariableAssignments()
496 syn match goVarAssign /\v[_.[:alnum:]]+(,\s*[_.[:alnum:]]+)*\ze(\s*([-^+|^\/%&]|\*|\<\<|\>\>|\&\^)?\=[^=])/
497 hi def link goVarAssign Special
498endif
499
500" Variable Declarations
501if s:HighlightVariableDeclarations()
502 syn match goVarDefs /\v\w+(,\s*\w+)*\ze(\s*:\=)/
503 hi def link goVarDefs Special
504endif
505
506" Build Constraints
507if s:HighlightBuildConstraints()
Bram Moolenaar34cc7d82021-09-21 20:09:51 +0200508 syn match goBuildKeyword display contained "+build\|go:build"
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200509 " Highlight the known values of GOOS, GOARCH, and other +build options.
510 syn keyword goBuildDirectives contained
511 \ android darwin dragonfly freebsd linux nacl netbsd openbsd plan9
512 \ solaris windows 386 amd64 amd64p32 arm armbe arm64 arm64be ppc64
513 \ ppc64le mips mipsle mips64 mips64le mips64p32 mips64p32le ppc
514 \ s390 s390x sparc sparc64 cgo ignore race
515
516 " Other words in the build directive are build tags not listed above, so
517 " avoid highlighting them as comments by using a matchgroup just for the
518 " start of the comment.
519 " The rs=s+2 option lets the \s*+build portion be part of the inner region
520 " instead of the matchgroup so it will be highlighted as a goBuildKeyword.
521 syn region goBuildComment matchgroup=goBuildCommentStart
Bram Moolenaard13166e2022-11-18 21:49:57 +0000522 \ start="//\(\s*+build\s\|go:build\)"rs=s+2 end="$"
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200523 \ contains=goBuildKeyword,goBuildDirectives
524 hi def link goBuildCommentStart Comment
525 hi def link goBuildDirectives Type
526 hi def link goBuildKeyword PreProc
527endif
528
529if s:HighlightBuildConstraints() || s:FoldEnable('package_comment')
530 " One or more line comments that are followed immediately by a "package"
531 " declaration are treated like package documentation, so these must be
532 " matched as comments to avoid looking like working build constraints.
533 " The he, me, and re options let the "package" itself be highlighted by
534 " the usual rules.
Matthew Hughes3d46de72024-03-17 20:05:23 +0100535 exe 'syn region goPackageComment start=/\v(\/\/.*\n)+\s*package\s/'
536 \ . ' end=/\v\n\s*package\s/he=e-8,me=e-8,re=e-8'
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200537 \ . ' contains=@goCommentGroup,@Spell'
538 \ . (s:FoldEnable('package_comment') ? ' fold' : '')
Matthew Hughes3d46de72024-03-17 20:05:23 +0100539 exe 'syn region goPackageComment start=/\v^\s*\/\*.*\n(.*\n)*\s*\*\/\npackage\s/'
540 \ . ' end=/\v\*\/\n\s*package\s/he=e-8,me=e-8,re=e-8'
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200541 \ . ' contains=@goCommentGroup,@Spell'
542 \ . (s:FoldEnable('package_comment') ? ' fold' : '')
543 hi def link goPackageComment Comment
544endif
545
546" :GoCoverage commands
547hi def link goCoverageNormalText Comment
548
Bram Moolenaarfb539272014-08-22 19:21:47 +0200549" Search backwards for a global declaration to start processing the syntax.
550"syn sync match goSync grouphere NONE /^\(const\|var\|type\|func\)\>/
551
552" There's a bug in the implementation of grouphere. For now, use the
553" following as a more expensive/less precise workaround.
554syn sync minlines=500
555
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200556let b:current_syntax = "go"
557
558let &cpo = s:keepcpo
559unlet s:keepcpo
Bram Moolenaarfb539272014-08-22 19:21:47 +0200560
561" vim: sw=2 sts=2 et