go.vim (25525B)
1 " 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. 4 " 5 " go.vim: Vim syntax file for Go. 6 " Language: Go 7 " Maintainer: Billie Cleek <bhcleek@gmail.com> 8 " Latest Revision: 2024-04-13 9 " 2024-03-17: - fix goPackageComment highlight (by Vim Project) 10 " License: BSD-style. See LICENSE file in source repository. 11 " Repository: https://github.com/fatih/vim-go 12 13 " Quit when a (custom) syntax file was already loaded 14 if exists("b:current_syntax") 15 finish 16 endif 17 18 let s:keepcpo = &cpo 19 set cpo&vim 20 21 function! 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']) 26 endfunction 27 28 function! s:HighlightArrayWhitespaceError() abort 29 return get(g:, 'go_highlight_array_whitespace_error', 0) 30 endfunction 31 32 function! s:HighlightChanWhitespaceError() abort 33 return get(g:, 'go_highlight_chan_whitespace_error', 0) 34 endfunction 35 36 function! s:HighlightExtraTypes() abort 37 return get(g:, 'go_highlight_extra_types', 0) 38 endfunction 39 40 function! s:HighlightSpaceTabError() abort 41 return get(g:, 'go_highlight_space_tab_error', 0) 42 endfunction 43 44 function! s:HighlightTrailingWhitespaceError() abort 45 return get(g:, 'go_highlight_trailing_whitespace_error', 0) 46 endfunction 47 48 function! s:HighlightOperators() abort 49 return get(g:, 'go_highlight_operators', 0) 50 endfunction 51 52 function! s:HighlightFunctions() abort 53 return get(g:, 'go_highlight_functions', 0) 54 endfunction 55 56 function! s:HighlightFunctionParameters() abort 57 return get(g:, 'go_highlight_function_parameters', 0) 58 endfunction 59 60 function! s:HighlightFunctionCalls() abort 61 return get(g:, 'go_highlight_function_calls', 0) 62 endfunction 63 64 function! s:HighlightFields() abort 65 return get(g:, 'go_highlight_fields', 0) 66 endfunction 67 68 function! s:HighlightTypes() abort 69 return get(g:, 'go_highlight_types', 0) 70 endfunction 71 72 function! s:HighlightBuildConstraints() abort 73 return get(g:, 'go_highlight_build_constraints', 0) 74 endfunction 75 76 function! s:HighlightStringSpellcheck() abort 77 return get(g:, 'go_highlight_string_spellcheck', 1) 78 endfunction 79 80 function! s:HighlightFormatStrings() abort 81 return get(g:, 'go_highlight_format_strings', 1) 82 endfunction 83 84 function! s:HighlightGenerateTags() abort 85 return get(g:, 'go_highlight_generate_tags', 0) 86 endfunction 87 88 function! s:HighlightVariableAssignments() abort 89 return get(g:, 'go_highlight_variable_assignments', 0) 90 endfunction 91 92 function! s:HighlightVariableDeclarations() abort 93 return get(g:, 'go_highlight_variable_declarations', 0) 94 endfunction 95 96 syn case match 97 98 syn keyword goPackage package 99 syn keyword goImport import contained 100 syn keyword goVar var contained 101 syn keyword goConst const contained 102 103 hi def link goPackage Statement 104 hi def link goImport Statement 105 hi def link goVar Keyword 106 hi def link goConst Keyword 107 hi def link goDeclaration Keyword 108 109 " Keywords within functions 110 syn keyword goStatement defer go goto return break continue fallthrough 111 syn keyword goConditional if else switch select 112 syn keyword goLabel case default 113 syn keyword goRepeat for range 114 115 hi def link goStatement Statement 116 hi def link goConditional Conditional 117 hi def link goLabel Label 118 hi def link goRepeat Repeat 119 120 " Predefined types 121 syn keyword goType chan map bool string error any comparable 122 syn keyword goSignedInts int int8 int16 int32 int64 rune 123 syn keyword goUnsignedInts byte uint uint8 uint16 uint32 uint64 uintptr 124 syn keyword goFloats float32 float64 125 syn keyword goComplexes complex64 complex128 126 127 hi def link goType Type 128 hi def link goSignedInts Type 129 hi def link goUnsignedInts Type 130 hi def link goFloats Type 131 hi def link goComplexes Type 132 133 " Predefined functions and values 134 syn keyword goBuiltins append cap clear close complex copy delete imag len 135 syn keyword goBuiltins make max min new panic print println real recover 136 syn keyword goBoolean true false 137 syn keyword goPredefinedIdentifiers nil iota 138 139 hi def link goBuiltins Identifier 140 hi 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. 144 hi def link goBoolean Boolean 145 146 " Comments; their contents 147 syn keyword goTodo contained TODO FIXME XXX BUG 148 syn cluster goCommentGroup contains=goTodo 149 150 syn region goComment start="//" end="$" contains=goGenerate,@goCommentGroup,@Spell 151 if 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 154 else 155 syn region goComment start="/\*" end="\*/" contains=@goCommentGroup,@Spell 156 endif 157 158 hi def link goComment Comment 159 hi def link goTodo Todo 160 161 if 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 166 endif 167 168 " Go escapes 169 syn match goEscapeOctal display contained "\\[0-7]\{3}" 170 syn match goEscapeC display contained +\\[abfnrtv\\'"]+ 171 syn match goEscapeX display contained "\\x\x\{2}" 172 syn match goEscapeU display contained "\\u\x\{4}" 173 syn match goEscapeBigU display contained "\\U\x\{8}" 174 syn match goEscapeError display contained +\\[^0-7xuUabfnrtv\\'"]+ 175 176 hi def link goEscapeOctal goSpecialString 177 hi def link goEscapeC goSpecialString 178 hi def link goEscapeX goSpecialString 179 hi def link goEscapeU goSpecialString 180 hi def link goEscapeBigU goSpecialString 181 hi def link goSpecialString Special 182 hi def link goEscapeError Error 183 184 " Strings and their contents 185 syn cluster goStringGroup contains=goEscapeOctal,goEscapeC,goEscapeX,goEscapeU,goEscapeBigU,goEscapeError 186 if s:HighlightStringSpellcheck() 187 syn region goString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@goStringGroup,@Spell 188 syn region goRawString start=+`+ end=+`+ contains=@Spell 189 else 190 syn region goString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@goStringGroup 191 syn region goRawString start=+`+ end=+`+ 192 endif 193 194 syn match goImportString /^\%(\s\+\|import \)\(\h\w* \)\?\zs"[^"]\+"/ contained containedin=goImport 195 196 if 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 211 endif 212 213 hi def link goImportString String 214 hi def link goString String 215 hi def link goRawString String 216 217 " Characters; their contents 218 syn cluster goCharacterGroup contains=goEscapeOctal,goEscapeC,goEscapeX,goEscapeU,goEscapeBigU 219 syn region goCharacter start=+'+ skip=+\\\\\|\\'+ end=+'+ contains=@goCharacterGroup 220 221 hi def link goCharacter Character 222 223 " Regions 224 syn region goParen start='(' end=')' transparent 225 if s:FoldEnable('block') 226 syn region goBlock start="{" end="}" transparent fold 227 else 228 syn region goBlock start="{" end="}" transparent 229 endif 230 231 " import 232 if s:FoldEnable('import') 233 syn region goImport start='import (' end=')' transparent fold contains=goImport,goImportString,goComment 234 syn match goImport /^import ()/ transparent fold contains=goImport 235 else 236 syn region goImport start='import (' end=')' transparent contains=goImport,goImportString,goComment 237 syn match goImport /^import ()/ transparent contains=goImport 238 endif 239 240 " var, const 241 if s:FoldEnable('varconst') 242 syn region goVar start='var (' end='^\s*)$' transparent fold 243 \ contains=ALLBUT,goParen,goBlock,goFunction,goTypeName,goReceiverType,goReceiverVar,goParamName,goParamType,goSimpleParams,goPointerOperator 244 syn match goVar /var ()/ transparent fold 245 \ contains=goVar 246 syn region goConst start='const (' end='^\s*)$' transparent fold 247 \ contains=ALLBUT,goParen,goBlock,goFunction,goTypeName,goReceiverType,goReceiverVar,goParamName,goParamType,goSimpleParams,goPointerOperator 248 syn match goConst /const ()/ transparent fold 249 \ contains=goConst 250 else 251 syn region goVar start='var (' end='^\s*)$' transparent 252 \ contains=ALLBUT,goParen,goBlock,goFunction,goTypeName,goReceiverType,goReceiverVar,goParamName,goParamType,goSimpleParams,goPointerOperator 253 syn match goVar /var ()/ transparent 254 \ contains=goVar 255 syn region goConst start='const (' end='^\s*)$' transparent 256 \ contains=ALLBUT,goParen,goBlock,goFunction,goTypeName,goReceiverType,goReceiverVar,goParamName,goParamType,goSimpleParams,goPointerOperator 257 syn match goConst /const ()/ transparent 258 \ contains=goConst 259 endif 260 261 " Single-line var, const, and import. 262 syn match goSingleDecl /\%(import\|var\|const\) [^(]\@=/ contains=goImport,goVar,goConst 263 264 " Integers 265 syn match goDecimalInt "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)\>" 266 syn match goHexadecimalInt "\<-\=0[xX]_\?\%(\x\|\x_\x\)\+\>" 267 syn match goOctalInt "\<-\=0[oO]\?_\?\%(\o\|\o_\o\)\+\>" 268 syn match goBinaryInt "\<-\=0[bB]_\?\%([01]\|[01]_[01]\)\+\>" 269 270 hi def link goDecimalInt Integer 271 hi def link goDecimalError Error 272 hi def link goHexadecimalInt Integer 273 hi def link goHexadecimalError Error 274 hi def link goOctalInt Integer 275 hi def link goOctalError Error 276 hi def link goBinaryInt Integer 277 hi def link goBinaryError Error 278 hi def link Integer Number 279 280 " Floating point 281 "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 294 syn match goFloat "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)\.\%(\%(\%(\d\|\d_\d\)\+\)\=\%([Ee][-+]\=\%(\d\|\d_\d\)\+\)\=\>\)\=" 295 syn match goFloat "\s\zs-\=\.\%(\d\|\d_\d\)\+\%(\%([Ee][-+]\=\%(\d\|\d_\d\)\+\)\>\)\=" 296 " decimal floats without a decimal point 297 syn match goFloat "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)[Ee][-+]\=\%(\d\|\d_\d\)\+\>" 298 " hexadecimal floats with a decimal point 299 syn match goHexadecimalFloat "\<-\=0[xX]\%(_\x\|\x\)\+\.\%(\%(\x\|\x_\x\)\+\)\=\%([Pp][-+]\=\%(\d\|\d_\d\)\+\)\=\>" 300 syn match goHexadecimalFloat "\<-\=0[xX]\.\%(\x\|\x_\x\)\+\%([Pp][-+]\=\%(\d\|\d_\d\)\+\)\=\>" 301 " hexadecimal floats without a decimal point 302 syn match goHexadecimalFloat "\<-\=0[xX]\%(_\x\|\x\)\+[Pp][-+]\=\%(\d\|\d_\d\)\+\>" 303 304 hi def link goFloat Float 305 hi def link goHexadecimalFloat Float 306 307 " Imaginary literals 308 syn match goImaginaryDecimal "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)i\>" 309 syn match goImaginaryHexadecimal "\<-\=0[xX]_\?\%(\x\|\x_\x\)\+i\>" 310 syn match goImaginaryOctal "\<-\=0[oO]\?_\?\%(\o\|\o_\o\)\+i\>" 311 syn match goImaginaryBinary "\<-\=0[bB]_\?\%([01]\|[01]_[01]\)\+i\>" 312 313 " imaginary decimal floats with a decimal point 314 syn match goImaginaryFloat "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)\.\%(\%(\%(\d\|\d_\d\)\+\)\=\%([Ee][-+]\=\%(\d\|\d_\d\)\+\)\=\)\=i\>" 315 syn match goImaginaryFloat "\s\zs-\=\.\%(\d\|\d_\d\)\+\%([Ee][-+]\=\%(\d\|\d_\d\)\+\)\=i\>" 316 " imaginary decimal floats without a decimal point 317 syn match goImaginaryFloat "\<-\=\%(0\|\%(\d\|\d_\d\)\+\)[Ee][-+]\=\%(\d\|\d_\d\)\+i\>" 318 " imaginary hexadecimal floats with a decimal point 319 syn match goImaginaryHexadecimalFloat "\<-\=0[xX]\%(_\x\|\x\)\+\.\%(\%(\x\|\x_\x\)\+\)\=\%([Pp][-+]\=\%(\d\|\d_\d\)\+\)\=i\>" 320 syn match goImaginaryHexadecimalFloat "\<-\=0[xX]\.\%(\x\|\x_\x\)\+\%([Pp][-+]\=\%(\d\|\d_\d\)\+\)\=i\>" 321 " imaginary hexadecimal floats without a decimal point 322 syn match goImaginaryHexadecimalFloat "\<-\=0[xX]\%(_\x\|\x\)\+[Pp][-+]\=\%(\d\|\d_\d\)\+i\>" 323 324 hi def link goImaginaryDecimal Number 325 hi def link goImaginaryHexadecimal Number 326 hi def link goImaginaryOctal Number 327 hi def link goImaginaryBinary Number 328 hi def link goImaginaryFloat Float 329 hi def link goImaginaryHexadecimalFloat Float 330 331 " Spaces after "[]" 332 if s:HighlightArrayWhitespaceError() 333 syn match goSpaceError display "\%(\[\]\)\@<=\s\+" 334 endif 335 336 " Spacing errors around the 'chan' keyword 337 if s:HighlightChanWhitespaceError() 338 " receive-only annotation on chan type 339 " 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 344 " send-only annotation on chan type 345 " 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 350 " value-ignoring receives in a few contexts 351 syn match goSpaceError display "\%(\%(^\|[={(,;]\)\s*<-\)\@<=\s\+" 352 endif 353 354 " Extra types commonly seen 355 if 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\)\>/ 360 syn match goExtraType /\<unsafe\.Pointer\>/ 361 endif 362 363 " Space-tab error 364 if s:HighlightSpaceTabError() 365 syn match goSpaceError display " \+\t"me=e-1 366 endif 367 368 " Trailing white space error 369 if s:HighlightTrailingWhitespaceError() 370 syn match goSpaceError display excludenl "\s\+$" 371 endif 372 373 hi def link goExtraType Type 374 hi def link goSpaceError Error 375 376 377 378 " included from: https://github.com/athom/more-colorful.vim/blob/master/after/syntax/go.vim 379 " 380 " Comments; their contents 381 syn keyword goTodo contained NOTE 382 hi def link goTodo Todo 383 384 syn match goVarArgs /\.\.\./ 385 386 " Operators; 387 if 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 /:=\|||\|<-\|++\|--/ 398 " match ~ 399 syn match goOperator /\~/ 400 " match ... 401 402 hi def link goPointerOperator goOperator 403 hi def link goVarArgs goOperator 404 endif 405 hi def link goOperator Operator 406 407 " -> 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 " || | | | | || | | | | | || | | | | | | 428 syn match goTypeParams /\[\%(\w\+\s\+\%(\~\?\%(\[]\)\?\w\%(\w\||\)\)*\%(,\s*\)\?\)\+\]/ nextgroup=goSimpleParams,goDeclType contained 429 430 " Functions; 431 if s:HighlightFunctions() || s:HighlightFunctionParameters() 432 syn match goDeclaration /\<func\>/ nextgroup=goReceiver,goFunction,goSimpleParams skipwhite skipnl 433 syn match goReceiverDecl /(\s*\zs\%(\%(\w\+\s\+\)\?\*\?\w\+\%(\[\%(\%(\[\]\)\?\w\+\%(,\s*\)\?\)\+\]\)\?\)\ze\s*)/ contained contains=goReceiverVar,goReceiverType,goPointerOperator 434 syn match goReceiverVar /\w\+\ze\s\+\%(\w\|\*\)/ nextgroup=goPointerOperator,goReceiverType skipwhite skipnl contained 435 syn match goPointerOperator /\*/ nextgroup=goReceiverType contained skipwhite skipnl 436 syn match goFunction /\w\+/ nextgroup=goSimpleParams,goTypeParams contained skipwhite skipnl 437 syn match goReceiverType /\w\+\%(\[\%(\%(\[\]\)\?\w\+\%(,\s*\)\?\)\+\]\)\?\ze\s*)/ contained 438 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 447 syn match goReceiver /(\s*\%(\w\+\s\+\)\?\*\?\s*\w\+\%(\[\%(\%(\[\]\)\?\w\+\%(,\s*\)\?\)\+\]\)\?\s*)\ze\s*\w/ contained nextgroup=goFunction contains=goReceiverDecl skipwhite skipnl 448 else 449 syn keyword goDeclaration func 450 endif 451 hi def link goFunction Function 452 453 " Function calls; 454 if s:HighlightFunctionCalls() 455 syn match goFunctionCall /\w\+\ze\%(\[\%(\%(\[]\)\?\w\+\(,\s*\)\?\)\+\]\)\?(/ contains=goBuiltins,goDeclaration 456 endif 457 hi def link goFunctionCall Type 458 459 " Fields; 460 if 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 475 endif 476 hi def link goField Identifier 477 478 " Structs & Interfaces; 479 if s:HighlightTypes() 480 syn match goTypeConstructor /\<\w\+{\@=/ 481 syn match goTypeDecl /\<type\>/ nextgroup=goTypeName skipwhite skipnl 482 syn match goTypeName /\w\+/ contained nextgroup=goDeclType,goTypeParams skipwhite skipnl 483 syn match goDeclType /\<\%(interface\|struct\)\>/ skipwhite skipnl 484 hi def link goReceiverType Type 485 else 486 syn keyword goDeclType struct interface 487 syn keyword goDeclaration type 488 endif 489 hi def link goTypeConstructor Type 490 hi def link goTypeName Type 491 hi def link goTypeDecl Keyword 492 hi def link goDeclType Keyword 493 494 " Variable Assignments 495 if s:HighlightVariableAssignments() 496 syn match goVarAssign /\v[_.[:alnum:]]+(,\s*[_.[:alnum:]]+)*\ze(\s*([-^+|^\/%&]|\*|\<\<|\>\>|\&\^)?\=[^=])/ 497 hi def link goVarAssign Special 498 endif 499 500 " Variable Declarations 501 if s:HighlightVariableDeclarations() 502 syn match goVarDefs /\v\w+(,\s*\w+)*\ze(\s*:\=)/ 503 hi def link goVarDefs Special 504 endif 505 506 " Build Constraints 507 if s:HighlightBuildConstraints() 508 syn match goBuildKeyword display contained "+build\|go:build" 509 " 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 522 \ start="//\(\s*+build\s\|go:build\)"rs=s+2 end="$" 523 \ contains=goBuildKeyword,goBuildDirectives 524 hi def link goBuildCommentStart Comment 525 hi def link goBuildDirectives Type 526 hi def link goBuildKeyword PreProc 527 endif 528 529 if 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. 535 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' 537 \ . ' contains=@goCommentGroup,@Spell' 538 \ . (s:FoldEnable('package_comment') ? ' fold' : '') 539 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' 541 \ . ' contains=@goCommentGroup,@Spell' 542 \ . (s:FoldEnable('package_comment') ? ' fold' : '') 543 hi def link goPackageComment Comment 544 endif 545 546 " :GoCoverage commands 547 hi def link goCoverageNormalText Comment 548 549 " 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. 554 syn sync minlines=500 555 556 let b:current_syntax = "go" 557 558 let &cpo = s:keepcpo 559 unlet s:keepcpo 560 561 " vim: sw=2 sts=2 et