blob: 904c8ad7f2fcf5cb279686d2ea72e24bddcbb896 [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>
Bram Moolenaard13166e2022-11-18 21:49:57 +00008" Latest Revision: 2022-11-17
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
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200133syn keyword goBuiltins append cap close complex copy delete imag len
134syn keyword goBuiltins make new panic print println real recover
135syn 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
139hi def link goBoolean Boolean
140hi def link goPredefinedIdentifiers goBoolean
Bram Moolenaarfb539272014-08-22 19:21:47 +0200141
142" Comments; their contents
143syn keyword goTodo contained TODO FIXME XXX BUG
144syn cluster goCommentGroup contains=goTodo
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200145
146syn region goComment start="//" end="$" contains=goGenerate,@goCommentGroup,@Spell
147if s:FoldEnable('comment')
148 syn region goComment start="/\*" end="\*/" contains=@goCommentGroup,@Spell fold
149 syn match goComment "\v(^\s*//.*\n)+" contains=goGenerate,@goCommentGroup,@Spell fold
150else
151 syn region goComment start="/\*" end="\*/" contains=@goCommentGroup,@Spell
152endif
Bram Moolenaarfb539272014-08-22 19:21:47 +0200153
154hi def link goComment Comment
155hi def link goTodo Todo
156
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200157if s:HighlightGenerateTags()
158 syn match goGenerateVariables contained /\%(\$GOARCH\|\$GOOS\|\$GOFILE\|\$GOLINE\|\$GOPACKAGE\|\$DOLLAR\)\>/
159 syn region goGenerate start="^\s*//go:generate" end="$" contains=goGenerateVariables
160 hi def link goGenerate PreProc
161 hi def link goGenerateVariables Special
162endif
163
Bram Moolenaarfb539272014-08-22 19:21:47 +0200164" Go escapes
165syn match goEscapeOctal display contained "\\[0-7]\{3}"
166syn match goEscapeC display contained +\\[abfnrtv\\'"]+
167syn match goEscapeX display contained "\\x\x\{2}"
168syn match goEscapeU display contained "\\u\x\{4}"
169syn match goEscapeBigU display contained "\\U\x\{8}"
170syn match goEscapeError display contained +\\[^0-7xuUabfnrtv\\'"]+
171
172hi def link goEscapeOctal goSpecialString
173hi def link goEscapeC goSpecialString
174hi def link goEscapeX goSpecialString
175hi def link goEscapeU goSpecialString
176hi def link goEscapeBigU goSpecialString
177hi def link goSpecialString Special
178hi def link goEscapeError Error
179
180" Strings and their contents
181syn cluster goStringGroup contains=goEscapeOctal,goEscapeC,goEscapeX,goEscapeU,goEscapeBigU,goEscapeError
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200182if s:HighlightStringSpellcheck()
183 syn region goString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@goStringGroup,@Spell
184 syn region goRawString start=+`+ end=+`+ contains=@Spell
185else
186 syn region goString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@goStringGroup
187 syn region goRawString start=+`+ end=+`+
188endif
189
Bram Moolenaard13166e2022-11-18 21:49:57 +0000190syn match goImportString /^\%(\s\+\|import \)\(\h\w* \)\?\zs"[^"]\+"$/ contained containedin=goImport
191
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200192if s:HighlightFormatStrings()
193 " [n] notation is valid for specifying explicit argument indexes
194 " 1. Match a literal % not preceded by a %.
195 " 2. Match any number of -, #, 0, space, or +
196 " 3. Match * or [n]* or any number or nothing before a .
197 " 4. Match * or [n]* or any number or nothing after a .
198 " 5. Match [n] or nothing before a verb
199 " 6. Match a formatting verb
200 syn match goFormatSpecifier /\
201 \%([^%]\%(%%\)*\)\
202 \@<=%[-#0 +]*\
203 \%(\%(\%(\[\d\+\]\)\=\*\)\|\d\+\)\=\
204 \%(\.\%(\%(\%(\[\d\+\]\)\=\*\)\|\d\+\)\=\)\=\
205 \%(\[\d\+\]\)\=[vTtbcdoqxXUeEfFgGspw]/ contained containedin=goString,goRawString
206 hi def link goFormatSpecifier goSpecialString
207endif
Bram Moolenaarfb539272014-08-22 19:21:47 +0200208
Bram Moolenaard13166e2022-11-18 21:49:57 +0000209hi def link goImportString String
Bram Moolenaarfb539272014-08-22 19:21:47 +0200210hi def link goString String
211hi def link goRawString String
212
213" Characters; their contents
214syn cluster goCharacterGroup contains=goEscapeOctal,goEscapeC,goEscapeX,goEscapeU,goEscapeBigU
215syn region goCharacter start=+'+ skip=+\\\\\|\\'+ end=+'+ contains=@goCharacterGroup
216
217hi def link goCharacter Character
218
219" Regions
Bram Moolenaarfb539272014-08-22 19:21:47 +0200220syn region goParen start='(' end=')' transparent
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200221if s:FoldEnable('block')
222 syn region goBlock start="{" end="}" transparent fold
223else
224 syn region goBlock start="{" end="}" transparent
225endif
226
227" import
228if s:FoldEnable('import')
Bram Moolenaard13166e2022-11-18 21:49:57 +0000229 syn region goImport start='import (' end=')' transparent fold contains=goImport,goImportString,goComment
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200230else
Bram Moolenaard13166e2022-11-18 21:49:57 +0000231 syn region goImport start='import (' end=')' transparent contains=goImport,goImportString,goComment
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200232endif
233
234" var, const
235if s:FoldEnable('varconst')
236 syn region goVar start='var (' end='^\s*)$' transparent fold
237 \ contains=ALLBUT,goParen,goBlock,goFunction,goTypeName,goReceiverType,goReceiverVar,goParamName,goParamType,goSimpleParams,goPointerOperator
238 syn region goConst start='const (' end='^\s*)$' transparent fold
239 \ contains=ALLBUT,goParen,goBlock,goFunction,goTypeName,goReceiverType,goReceiverVar,goParamName,goParamType,goSimpleParams,goPointerOperator
240else
241 syn region goVar start='var (' end='^\s*)$' transparent
242 \ contains=ALLBUT,goParen,goBlock,goFunction,goTypeName,goReceiverType,goReceiverVar,goParamName,goParamType,goSimpleParams,goPointerOperator
243 syn region goConst start='const (' end='^\s*)$' transparent
244 \ contains=ALLBUT,goParen,goBlock,goFunction,goTypeName,goReceiverType,goReceiverVar,goParamName,goParamType,goSimpleParams,goPointerOperator
245endif
246
247" Single-line var, const, and import.
248syn match goSingleDecl /\%(import\|var\|const\) [^(]\@=/ contains=goImport,goVar,goConst
Bram Moolenaarfb539272014-08-22 19:21:47 +0200249
250" Integers
Bram Moolenaard13166e2022-11-18 21:49:57 +0000251syn match goDecimalInt "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)\>"
252syn match goHexadecimalInt "\<-\=0[xX]_\?\%(\x\|\x_\x\)\+\>"
253syn match goOctalInt "\<-\=0[oO]\?_\?\%(\o\|\o_\o\)\+\>"
254syn match goBinaryInt "\<-\=0[bB]_\?\%([01]\|[01]_[01]\)\+\>"
Bram Moolenaarfb539272014-08-22 19:21:47 +0200255
256hi def link goDecimalInt Integer
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200257hi def link goDecimalError Error
Bram Moolenaarfb539272014-08-22 19:21:47 +0200258hi def link goHexadecimalInt Integer
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200259hi def link goHexadecimalError Error
Bram Moolenaarfb539272014-08-22 19:21:47 +0200260hi def link goOctalInt Integer
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200261hi def link goOctalError Error
262hi def link goBinaryInt Integer
263hi def link goBinaryError Error
Bram Moolenaarfb539272014-08-22 19:21:47 +0200264hi def link Integer Number
265
266" Floating point
Bram Moolenaard13166e2022-11-18 21:49:57 +0000267"float_lit = decimal_float_lit | hex_float_lit .
268"
269"decimal_float_lit = decimal_digits "." [ decimal_digits ] [ decimal_exponent ] |
270" decimal_digits decimal_exponent |
271" "." decimal_digits [ decimal_exponent ] .
272"decimal_exponent = ( "e" | "E" ) [ "+" | "-" ] decimal_digits .
273"
274"hex_float_lit = "0" ( "x" | "X" ) hex_mantissa hex_exponent .
275"hex_mantissa = [ "_" ] hex_digits "." [ hex_digits ] |
276" [ "_" ] hex_digits |
277" "." hex_digits .
278"hex_exponent = ( "p" | "P" ) [ "+" | "-" ] decimal_digits .
279" decimal floats with a decimal point
280syn match goFloat "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)\.\%(\%(\%(\d\|\d_\d\)\+\)\=\%([Ee][-+]\=\%(\d\|\d_\d\)\+\)\=\>\)\="
281syn match goFloat "\s\zs-\=\.\%(\d\|\d_\d\)\+\%(\%([Ee][-+]\=\%(\d\|\d_\d\)\+\)\>\)\="
282" decimal floats without a decimal point
283syn match goFloat "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)[Ee][-+]\=\%(\d\|\d_\d\)\+\>"
284" hexadecimal floats with a decimal point
285syn match goHexadecimalFloat "\<-\=0[xX]\%(_\x\|\x\)\+\.\%(\%(\x\|\x_\x\)\+\)\=\%([Pp][-+]\=\%(\d\|\d_\d\)\+\)\=\>"
286syn match goHexadecimalFloat "\<-\=0[xX]\.\%(\x\|\x_\x\)\+\%([Pp][-+]\=\%(\d\|\d_\d\)\+\)\=\>"
287" hexadecimal floats without a decimal point
288syn match goHexadecimalFloat "\<-\=0[xX]\%(_\x\|\x\)\+[Pp][-+]\=\%(\d\|\d_\d\)\+\>"
Bram Moolenaarfb539272014-08-22 19:21:47 +0200289
290hi def link goFloat Float
Bram Moolenaard13166e2022-11-18 21:49:57 +0000291hi def link goHexadecimalFloat Float
Bram Moolenaarfb539272014-08-22 19:21:47 +0200292
293" Imaginary literals
Bram Moolenaard13166e2022-11-18 21:49:57 +0000294syn match goImaginaryDecimal "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)i\>"
295syn match goImaginaryHexadecimal "\<-\=0[xX]_\?\%(\x\|\x_\x\)\+i\>"
296syn match goImaginaryOctal "\<-\=0[oO]\?_\?\%(\o\|\o_\o\)\+i\>"
297syn match goImaginaryBinary "\<-\=0[bB]_\?\%([01]\|[01]_[01]\)\+i\>"
Bram Moolenaarfb539272014-08-22 19:21:47 +0200298
Bram Moolenaard13166e2022-11-18 21:49:57 +0000299" imaginary decimal floats with a decimal point
300syn match goImaginaryFloat "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)\.\%(\%(\%(\d\|\d_\d\)\+\)\=\%([Ee][-+]\=\%(\d\|\d_\d\)\+\)\=\)\=i\>"
301syn match goImaginaryFloat "\s\zs-\=\.\%(\d\|\d_\d\)\+\%([Ee][-+]\=\%(\d\|\d_\d\)\+\)\=i\>"
302" imaginary decimal floats without a decimal point
303syn match goImaginaryFloat "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)[Ee][-+]\=\%(\d\|\d_\d\)\+i\>"
304" imaginary hexadecimal floats with a decimal point
305syn match goImaginaryHexadecimalFloat "\<-\=0[xX]\%(_\x\|\x\)\+\.\%(\%(\x\|\x_\x\)\+\)\=\%([Pp][-+]\=\%(\d\|\d_\d\)\+\)\=i\>"
306syn match goImaginaryHexadecimalFloat "\<-\=0[xX]\.\%(\x\|\x_\x\)\+\%([Pp][-+]\=\%(\d\|\d_\d\)\+\)\=i\>"
307" imaginary hexadecimal floats without a decimal point
308syn match goImaginaryHexadecimalFloat "\<-\=0[xX]\%(_\x\|\x\)\+[Pp][-+]\=\%(\d\|\d_\d\)\+i\>"
309
310hi def link goImaginaryDecimal Number
311hi def link goImaginaryHexadecimal Number
312hi def link goImaginaryOctal Number
313hi def link goImaginaryBinary Number
314hi def link goImaginaryFloat Float
315hi def link goImaginaryHexadecimalFloat Float
Bram Moolenaarfb539272014-08-22 19:21:47 +0200316
317" Spaces after "[]"
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200318if s:HighlightArrayWhitespaceError()
319 syn match goSpaceError display "\%(\[\]\)\@<=\s\+"
Bram Moolenaarfb539272014-08-22 19:21:47 +0200320endif
321
322" Spacing errors around the 'chan' keyword
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200323if s:HighlightChanWhitespaceError()
Bram Moolenaarfb539272014-08-22 19:21:47 +0200324 " receive-only annotation on chan type
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200325 "
326 " \(\<chan\>\)\@<!<- (only pick arrow when it doesn't come after a chan)
327 " this prevents picking up 'chan<- chan<-' but not '<- chan'
328 syn match goSpaceError display "\%(\%(\<chan\>\)\@<!<-\)\@<=\s\+\%(\<chan\>\)\@="
329
Bram Moolenaarfb539272014-08-22 19:21:47 +0200330 " send-only annotation on chan type
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200331 "
332 " \(<-\)\@<!\<chan\> (only pick chan when it doesn't come after an arrow)
333 " this prevents picking up '<-chan <-chan' but not 'chan <-'
334 syn match goSpaceError display "\%(\%(<-\)\@<!\<chan\>\)\@<=\s\+\%(<-\)\@="
335
Bram Moolenaarfb539272014-08-22 19:21:47 +0200336 " value-ignoring receives in a few contexts
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200337 syn match goSpaceError display "\%(\%(^\|[={(,;]\)\s*<-\)\@<=\s\+"
Bram Moolenaarfb539272014-08-22 19:21:47 +0200338endif
339
340" Extra types commonly seen
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200341if s:HighlightExtraTypes()
342 syn match goExtraType /\<bytes\.\%(Buffer\)\>/
343 syn match goExtraType /\<context\.\%(Context\)\>/
344 syn match goExtraType /\<io\.\%(Reader\|ReadSeeker\|ReadWriter\|ReadCloser\|ReadWriteCloser\|Writer\|WriteCloser\|Seeker\)\>/
345 syn match goExtraType /\<reflect\.\%(Kind\|Type\|Value\)\>/
Bram Moolenaarfb539272014-08-22 19:21:47 +0200346 syn match goExtraType /\<unsafe\.Pointer\>/
347endif
348
349" Space-tab error
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200350if s:HighlightSpaceTabError()
Bram Moolenaarfb539272014-08-22 19:21:47 +0200351 syn match goSpaceError display " \+\t"me=e-1
352endif
353
354" Trailing white space error
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200355if s:HighlightTrailingWhitespaceError()
Bram Moolenaarfb539272014-08-22 19:21:47 +0200356 syn match goSpaceError display excludenl "\s\+$"
357endif
358
359hi def link goExtraType Type
360hi def link goSpaceError Error
361
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200362
363
364" included from: https://github.com/athom/more-colorful.vim/blob/master/after/syntax/go.vim
365"
366" Comments; their contents
367syn keyword goTodo contained NOTE
368hi def link goTodo Todo
369
370syn match goVarArgs /\.\.\./
371
372" Operators;
373if s:HighlightOperators()
374 " match single-char operators: - + % < > ! & | ^ * =
375 " and corresponding two-char operators: -= += %= <= >= != &= |= ^= *= ==
376 syn match goOperator /[-+%<>!&|^*=]=\?/
377 " match / and /=
378 syn match goOperator /\/\%(=\|\ze[^/*]\)/
379 " match two-char operators: << >> &^
380 " and corresponding three-char operators: <<= >>= &^=
381 syn match goOperator /\%(<<\|>>\|&^\)=\?/
382 " match remaining two-char operators: := && || <- ++ --
383 syn match goOperator /:=\|||\|<-\|++\|--/
Bram Moolenaard13166e2022-11-18 21:49:57 +0000384 " match ~
385 syn match goOperator /\~/
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200386 " match ...
387
388 hi def link goPointerOperator goOperator
389 hi def link goVarArgs goOperator
390endif
391hi def link goOperator Operator
392
Bram Moolenaard13166e2022-11-18 21:49:57 +0000393" -> type constraint opening bracket
394" |-> start non-counting group
395" || -> any word character
396" || | -> at least one, as many as possible
397" || | | -> start non-counting group
398" || | | | -> match ~
399" || | | | | -> at most once
400" || | | | | | -> allow a slice type
401" || | | | | | | -> any word character
402" || | | | | | | | -> start a non-counting group
403" || | | | | | | | | -> that matches word characters and |
404" || | | | | | | | | | -> close the non-counting group
405" || | | | | | | | | | | -> close the non-counting group
406" || | | | | | | | | | | |-> any number of matches
407" || | | | | | | | | | | || -> start a non-counting group
408" || | | | | | | | | | | || | -> a comma and whitespace
409" || | | | | | | | | | | || | | -> at most once
410" || | | | | | | | | | | || | | | -> close the non-counting group
411" || | | | | | | | | | | || | | | | -> at least one of those non-counting groups, as many as possible
412" || | | | | | -------- | | | | || | | | | | -> type constraint closing bracket
413" || | | | | || | | | | | || | | | | | |
414syn match goTypeParams /\[\%(\w\+\s\+\%(\~\?\%(\[]\)\?\w\%(\w\||\)\)*\%(,\s*\)\?\)\+\]/ nextgroup=goSimpleParams,goDeclType contained
415
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200416" Functions;
417if s:HighlightFunctions() || s:HighlightFunctionParameters()
418 syn match goDeclaration /\<func\>/ nextgroup=goReceiver,goFunction,goSimpleParams skipwhite skipnl
Bram Moolenaard13166e2022-11-18 21:49:57 +0000419 syn match goReceiverDecl /(\s*\zs\%(\%(\w\+\s\+\)\?\*\?\w\+\%(\[\%(\%(\[\]\)\?\w\+\%(,\s*\)\?\)\+\]\)\?\)\ze\s*)/ contained contains=goReceiverVar,goReceiverType,goPointerOperator
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200420 syn match goReceiverVar /\w\+\ze\s\+\%(\w\|\*\)/ nextgroup=goPointerOperator,goReceiverType skipwhite skipnl contained
421 syn match goPointerOperator /\*/ nextgroup=goReceiverType contained skipwhite skipnl
Bram Moolenaard13166e2022-11-18 21:49:57 +0000422 syn match goFunction /\w\+/ nextgroup=goSimpleParams,goTypeParams contained skipwhite skipnl
423 syn match goReceiverType /\w\+\%(\[\%(\%(\[\]\)\?\w\+\%(,\s*\)\?\)\+\]\)\?\ze\s*)/ contained
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200424 if s:HighlightFunctionParameters()
425 syn match goSimpleParams /(\%(\w\|\_s\|[*\.\[\],\{\}<>-]\)*)/ contained contains=goParamName,goType nextgroup=goFunctionReturn skipwhite skipnl
426 syn match goFunctionReturn /(\%(\w\|\_s\|[*\.\[\],\{\}<>-]\)*)/ contained contains=goParamName,goType skipwhite skipnl
427 syn match goParamName /\w\+\%(\s*,\s*\w\+\)*\ze\s\+\%(\w\|\.\|\*\|\[\)/ contained nextgroup=goParamType skipwhite skipnl
428 syn match goParamType /\%([^,)]\|\_s\)\+,\?/ contained nextgroup=goParamName skipwhite skipnl
429 \ contains=goVarArgs,goType,goSignedInts,goUnsignedInts,goFloats,goComplexes,goDeclType,goBlock
430 hi def link goReceiverVar goParamName
431 hi def link goParamName Identifier
432 endif
Bram Moolenaard13166e2022-11-18 21:49:57 +0000433 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 +0200434else
435 syn keyword goDeclaration func
436endif
437hi def link goFunction Function
438
439" Function calls;
440if s:HighlightFunctionCalls()
Bram Moolenaard13166e2022-11-18 21:49:57 +0000441 syn match goFunctionCall /\w\+\ze\%(\[\%(\%(\[]\)\?\w\+\(,\s*\)\?\)\+\]\)\?(/ contains=goBuiltins,goDeclaration
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200442endif
443hi def link goFunctionCall Type
444
445" Fields;
446if s:HighlightFields()
447 " 1. Match a sequence of word characters coming after a '.'
448 " 2. Require the following but dont match it: ( \@= see :h E59)
449 " - The symbols: / - + * % OR
450 " - The symbols: [] {} <> ) OR
451 " - The symbols: \n \r space OR
452 " - The symbols: , : .
453 " 3. Have the start of highlight (hs) be the start of matched
454 " pattern (s) offsetted one to the right (+1) (see :h E401)
455 syn match goField /\.\w\+\
456 \%(\%([\/\-\+*%]\)\|\
457 \%([\[\]{}<\>\)]\)\|\
458 \%([\!=\^|&]\)\|\
459 \%([\n\r\ ]\)\|\
460 \%([,\:.]\)\)\@=/hs=s+1
461endif
462hi def link goField Identifier
463
464" Structs & Interfaces;
465if s:HighlightTypes()
466 syn match goTypeConstructor /\<\w\+{\@=/
467 syn match goTypeDecl /\<type\>/ nextgroup=goTypeName skipwhite skipnl
Bram Moolenaard13166e2022-11-18 21:49:57 +0000468 syn match goTypeName /\w\+/ contained nextgroup=goDeclType,goTypeParams skipwhite skipnl
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200469 syn match goDeclType /\<\%(interface\|struct\)\>/ skipwhite skipnl
470 hi def link goReceiverType Type
471else
472 syn keyword goDeclType struct interface
473 syn keyword goDeclaration type
474endif
475hi def link goTypeConstructor Type
476hi def link goTypeName Type
477hi def link goTypeDecl Keyword
478hi def link goDeclType Keyword
479
480" Variable Assignments
481if s:HighlightVariableAssignments()
482 syn match goVarAssign /\v[_.[:alnum:]]+(,\s*[_.[:alnum:]]+)*\ze(\s*([-^+|^\/%&]|\*|\<\<|\>\>|\&\^)?\=[^=])/
483 hi def link goVarAssign Special
484endif
485
486" Variable Declarations
487if s:HighlightVariableDeclarations()
488 syn match goVarDefs /\v\w+(,\s*\w+)*\ze(\s*:\=)/
489 hi def link goVarDefs Special
490endif
491
492" Build Constraints
493if s:HighlightBuildConstraints()
Bram Moolenaar34cc7d82021-09-21 20:09:51 +0200494 syn match goBuildKeyword display contained "+build\|go:build"
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200495 " Highlight the known values of GOOS, GOARCH, and other +build options.
496 syn keyword goBuildDirectives contained
497 \ android darwin dragonfly freebsd linux nacl netbsd openbsd plan9
498 \ solaris windows 386 amd64 amd64p32 arm armbe arm64 arm64be ppc64
499 \ ppc64le mips mipsle mips64 mips64le mips64p32 mips64p32le ppc
500 \ s390 s390x sparc sparc64 cgo ignore race
501
502 " Other words in the build directive are build tags not listed above, so
503 " avoid highlighting them as comments by using a matchgroup just for the
504 " start of the comment.
505 " The rs=s+2 option lets the \s*+build portion be part of the inner region
506 " instead of the matchgroup so it will be highlighted as a goBuildKeyword.
507 syn region goBuildComment matchgroup=goBuildCommentStart
Bram Moolenaard13166e2022-11-18 21:49:57 +0000508 \ start="//\(\s*+build\s\|go:build\)"rs=s+2 end="$"
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200509 \ contains=goBuildKeyword,goBuildDirectives
510 hi def link goBuildCommentStart Comment
511 hi def link goBuildDirectives Type
512 hi def link goBuildKeyword PreProc
513endif
514
515if s:HighlightBuildConstraints() || s:FoldEnable('package_comment')
516 " One or more line comments that are followed immediately by a "package"
517 " declaration are treated like package documentation, so these must be
518 " matched as comments to avoid looking like working build constraints.
519 " The he, me, and re options let the "package" itself be highlighted by
520 " the usual rules.
521 exe 'syn region goPackageComment start=/\v(\/\/.*\n)+\s*package/'
522 \ . ' end=/\v\n\s*package/he=e-7,me=e-7,re=e-7'
523 \ . ' contains=@goCommentGroup,@Spell'
524 \ . (s:FoldEnable('package_comment') ? ' fold' : '')
525 exe 'syn region goPackageComment start=/\v^\s*\/\*.*\n(.*\n)*\s*\*\/\npackage/'
526 \ . ' end=/\v\*\/\n\s*package/he=e-7,me=e-7,re=e-7'
527 \ . ' contains=@goCommentGroup,@Spell'
528 \ . (s:FoldEnable('package_comment') ? ' fold' : '')
529 hi def link goPackageComment Comment
530endif
531
532" :GoCoverage commands
533hi def link goCoverageNormalText Comment
534
Bram Moolenaarfb539272014-08-22 19:21:47 +0200535" Search backwards for a global declaration to start processing the syntax.
536"syn sync match goSync grouphere NONE /^\(const\|var\|type\|func\)\>/
537
538" There's a bug in the implementation of grouphere. For now, use the
539" following as a more expensive/less precise workaround.
540syn sync minlines=500
541
Bram Moolenaar90df4b92021-07-07 20:26:08 +0200542let b:current_syntax = "go"
543
544let &cpo = s:keepcpo
545unlet s:keepcpo
Bram Moolenaarfb539272014-08-22 19:21:47 +0200546
547" vim: sw=2 sts=2 et