go.vim 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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: 2021-09-18
  9. " License: BSD-style. See LICENSE file in source repository.
  10. " Repository: https://github.com/fatih/vim-go
  11. " Quit when a (custom) syntax file was already loaded
  12. if exists("b:current_syntax")
  13. finish
  14. endif
  15. let s:keepcpo = &cpo
  16. set cpo&vim
  17. function! s:FoldEnable(...) abort
  18. if a:0 > 0
  19. return index(s:FoldEnable(), a:1) > -1
  20. endif
  21. return get(g:, 'go_fold_enable', ['block', 'import', 'varconst', 'package_comment'])
  22. endfunction
  23. function! s:HighlightArrayWhitespaceError() abort
  24. return get(g:, 'go_highlight_array_whitespace_error', 0)
  25. endfunction
  26. function! s:HighlightChanWhitespaceError() abort
  27. return get(g:, 'go_highlight_chan_whitespace_error', 0)
  28. endfunction
  29. function! s:HighlightExtraTypes() abort
  30. return get(g:, 'go_highlight_extra_types', 0)
  31. endfunction
  32. function! s:HighlightSpaceTabError() abort
  33. return get(g:, 'go_highlight_space_tab_error', 0)
  34. endfunction
  35. function! s:HighlightTrailingWhitespaceError() abort
  36. return get(g:, 'go_highlight_trailing_whitespace_error', 0)
  37. endfunction
  38. function! s:HighlightOperators() abort
  39. return get(g:, 'go_highlight_operators', 0)
  40. endfunction
  41. function! s:HighlightFunctions() abort
  42. return get(g:, 'go_highlight_functions', 0)
  43. endfunction
  44. function! s:HighlightFunctionParameters() abort
  45. return get(g:, 'go_highlight_function_parameters', 0)
  46. endfunction
  47. function! s:HighlightFunctionCalls() abort
  48. return get(g:, 'go_highlight_function_calls', 0)
  49. endfunction
  50. function! s:HighlightFields() abort
  51. return get(g:, 'go_highlight_fields', 0)
  52. endfunction
  53. function! s:HighlightTypes() abort
  54. return get(g:, 'go_highlight_types', 0)
  55. endfunction
  56. function! s:HighlightBuildConstraints() abort
  57. return get(g:, 'go_highlight_build_constraints', 0)
  58. endfunction
  59. function! s:HighlightStringSpellcheck() abort
  60. return get(g:, 'go_highlight_string_spellcheck', 1)
  61. endfunction
  62. function! s:HighlightFormatStrings() abort
  63. return get(g:, 'go_highlight_format_strings', 1)
  64. endfunction
  65. function! s:HighlightGenerateTags() abort
  66. return get(g:, 'go_highlight_generate_tags', 0)
  67. endfunction
  68. function! s:HighlightVariableAssignments() abort
  69. return get(g:, 'go_highlight_variable_assignments', 0)
  70. endfunction
  71. function! s:HighlightVariableDeclarations() abort
  72. return get(g:, 'go_highlight_variable_declarations', 0)
  73. endfunction
  74. syn case match
  75. syn keyword goPackage package
  76. syn keyword goImport import contained
  77. syn keyword goVar var contained
  78. syn keyword goConst const contained
  79. hi def link goPackage Statement
  80. hi def link goImport Statement
  81. hi def link goVar Keyword
  82. hi def link goConst Keyword
  83. hi def link goDeclaration Keyword
  84. " Keywords within functions
  85. syn keyword goStatement defer go goto return break continue fallthrough
  86. syn keyword goConditional if else switch select
  87. syn keyword goLabel case default
  88. syn keyword goRepeat for range
  89. hi def link goStatement Statement
  90. hi def link goConditional Conditional
  91. hi def link goLabel Label
  92. hi def link goRepeat Repeat
  93. " Predefined types
  94. syn keyword goType chan map bool string error
  95. syn keyword goSignedInts int int8 int16 int32 int64 rune
  96. syn keyword goUnsignedInts byte uint uint8 uint16 uint32 uint64 uintptr
  97. syn keyword goFloats float32 float64
  98. syn keyword goComplexes complex64 complex128
  99. hi def link goType Type
  100. hi def link goSignedInts Type
  101. hi def link goUnsignedInts Type
  102. hi def link goFloats Type
  103. hi def link goComplexes Type
  104. " Predefined functions and values
  105. syn keyword goBuiltins append cap close complex copy delete imag len
  106. syn keyword goBuiltins make new panic print println real recover
  107. syn keyword goBoolean true false
  108. syn keyword goPredefinedIdentifiers nil iota
  109. hi def link goBuiltins Identifier
  110. hi def link goBoolean Boolean
  111. hi def link goPredefinedIdentifiers goBoolean
  112. " Comments; their contents
  113. syn keyword goTodo contained TODO FIXME XXX BUG
  114. syn cluster goCommentGroup contains=goTodo
  115. syn region goComment start="//" end="$" contains=goGenerate,@goCommentGroup,@Spell
  116. if s:FoldEnable('comment')
  117. syn region goComment start="/\*" end="\*/" contains=@goCommentGroup,@Spell fold
  118. syn match goComment "\v(^\s*//.*\n)+" contains=goGenerate,@goCommentGroup,@Spell fold
  119. else
  120. syn region goComment start="/\*" end="\*/" contains=@goCommentGroup,@Spell
  121. endif
  122. hi def link goComment Comment
  123. hi def link goTodo Todo
  124. if s:HighlightGenerateTags()
  125. syn match goGenerateVariables contained /\%(\$GOARCH\|\$GOOS\|\$GOFILE\|\$GOLINE\|\$GOPACKAGE\|\$DOLLAR\)\>/
  126. syn region goGenerate start="^\s*//go:generate" end="$" contains=goGenerateVariables
  127. hi def link goGenerate PreProc
  128. hi def link goGenerateVariables Special
  129. endif
  130. " Go escapes
  131. syn match goEscapeOctal display contained "\\[0-7]\{3}"
  132. syn match goEscapeC display contained +\\[abfnrtv\\'"]+
  133. syn match goEscapeX display contained "\\x\x\{2}"
  134. syn match goEscapeU display contained "\\u\x\{4}"
  135. syn match goEscapeBigU display contained "\\U\x\{8}"
  136. syn match goEscapeError display contained +\\[^0-7xuUabfnrtv\\'"]+
  137. hi def link goEscapeOctal goSpecialString
  138. hi def link goEscapeC goSpecialString
  139. hi def link goEscapeX goSpecialString
  140. hi def link goEscapeU goSpecialString
  141. hi def link goEscapeBigU goSpecialString
  142. hi def link goSpecialString Special
  143. hi def link goEscapeError Error
  144. " Strings and their contents
  145. syn cluster goStringGroup contains=goEscapeOctal,goEscapeC,goEscapeX,goEscapeU,goEscapeBigU,goEscapeError
  146. if s:HighlightStringSpellcheck()
  147. syn region goString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@goStringGroup,@Spell
  148. syn region goRawString start=+`+ end=+`+ contains=@Spell
  149. else
  150. syn region goString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@goStringGroup
  151. syn region goRawString start=+`+ end=+`+
  152. endif
  153. if s:HighlightFormatStrings()
  154. " [n] notation is valid for specifying explicit argument indexes
  155. " 1. Match a literal % not preceded by a %.
  156. " 2. Match any number of -, #, 0, space, or +
  157. " 3. Match * or [n]* or any number or nothing before a .
  158. " 4. Match * or [n]* or any number or nothing after a .
  159. " 5. Match [n] or nothing before a verb
  160. " 6. Match a formatting verb
  161. syn match goFormatSpecifier /\
  162. \%([^%]\%(%%\)*\)\
  163. \@<=%[-#0 +]*\
  164. \%(\%(\%(\[\d\+\]\)\=\*\)\|\d\+\)\=\
  165. \%(\.\%(\%(\%(\[\d\+\]\)\=\*\)\|\d\+\)\=\)\=\
  166. \%(\[\d\+\]\)\=[vTtbcdoqxXUeEfFgGspw]/ contained containedin=goString,goRawString
  167. hi def link goFormatSpecifier goSpecialString
  168. endif
  169. hi def link goString String
  170. hi def link goRawString String
  171. " Characters; their contents
  172. syn cluster goCharacterGroup contains=goEscapeOctal,goEscapeC,goEscapeX,goEscapeU,goEscapeBigU
  173. syn region goCharacter start=+'+ skip=+\\\\\|\\'+ end=+'+ contains=@goCharacterGroup
  174. hi def link goCharacter Character
  175. " Regions
  176. syn region goParen start='(' end=')' transparent
  177. if s:FoldEnable('block')
  178. syn region goBlock start="{" end="}" transparent fold
  179. else
  180. syn region goBlock start="{" end="}" transparent
  181. endif
  182. " import
  183. if s:FoldEnable('import')
  184. syn region goImport start='import (' end=')' transparent fold contains=goImport,goString,goComment
  185. else
  186. syn region goImport start='import (' end=')' transparent contains=goImport,goString,goComment
  187. endif
  188. " var, const
  189. if s:FoldEnable('varconst')
  190. syn region goVar start='var (' end='^\s*)$' transparent fold
  191. \ contains=ALLBUT,goParen,goBlock,goFunction,goTypeName,goReceiverType,goReceiverVar,goParamName,goParamType,goSimpleParams,goPointerOperator
  192. syn region goConst start='const (' end='^\s*)$' transparent fold
  193. \ contains=ALLBUT,goParen,goBlock,goFunction,goTypeName,goReceiverType,goReceiverVar,goParamName,goParamType,goSimpleParams,goPointerOperator
  194. else
  195. syn region goVar start='var (' end='^\s*)$' transparent
  196. \ contains=ALLBUT,goParen,goBlock,goFunction,goTypeName,goReceiverType,goReceiverVar,goParamName,goParamType,goSimpleParams,goPointerOperator
  197. syn region goConst start='const (' end='^\s*)$' transparent
  198. \ contains=ALLBUT,goParen,goBlock,goFunction,goTypeName,goReceiverType,goReceiverVar,goParamName,goParamType,goSimpleParams,goPointerOperator
  199. endif
  200. " Single-line var, const, and import.
  201. syn match goSingleDecl /\%(import\|var\|const\) [^(]\@=/ contains=goImport,goVar,goConst
  202. " Integers
  203. syn match goDecimalInt "\<-\=\(0\|[1-9]_\?\(\d\|\d\+_\?\d\+\)*\)\%([Ee][-+]\=\d\+\)\=\>"
  204. syn match goDecimalError "\<-\=\(_\(\d\+_*\)\+\|\([1-9]\d*_*\)\+__\(\d\+_*\)\+\|\([1-9]\d*_*\)\+_\+\)\%([Ee][-+]\=\d\+\)\=\>"
  205. syn match goHexadecimalInt "\<-\=0[xX]_\?\(\x\+_\?\)\+\>"
  206. syn match goHexadecimalError "\<-\=0[xX]_\?\(\x\+_\?\)*\(\([^ \t0-9A-Fa-f_)]\|__\)\S*\|_\)\>"
  207. syn match goOctalInt "\<-\=0[oO]\?_\?\(\o\+_\?\)\+\>"
  208. syn match goOctalError "\<-\=0[0-7oO_]*\(\([^ \t0-7oOxX_/)\]\}\:;]\|[oO]\{2,\}\|__\)\S*\|_\|[oOxX]\)\>"
  209. syn match goBinaryInt "\<-\=0[bB]_\?\([01]\+_\?\)\+\>"
  210. syn match goBinaryError "\<-\=0[bB]_\?[01_]*\([^ \t01_)]\S*\|__\S*\|_\)\>"
  211. hi def link goDecimalInt Integer
  212. hi def link goDecimalError Error
  213. hi def link goHexadecimalInt Integer
  214. hi def link goHexadecimalError Error
  215. hi def link goOctalInt Integer
  216. hi def link goOctalError Error
  217. hi def link goBinaryInt Integer
  218. hi def link goBinaryError Error
  219. hi def link Integer Number
  220. " Floating point
  221. syn match goFloat "\<-\=\d\+\.\d*\%([Ee][-+]\=\d\+\)\=\>"
  222. syn match goFloat "\<-\=\.\d\+\%([Ee][-+]\=\d\+\)\=\>"
  223. hi def link goFloat Float
  224. " Imaginary literals
  225. syn match goImaginary "\<-\=\d\+i\>"
  226. syn match goImaginary "\<-\=\d\+[Ee][-+]\=\d\+i\>"
  227. syn match goImaginaryFloat "\<-\=\d\+\.\d*\%([Ee][-+]\=\d\+\)\=i\>"
  228. syn match goImaginaryFloat "\<-\=\.\d\+\%([Ee][-+]\=\d\+\)\=i\>"
  229. hi def link goImaginary Number
  230. hi def link goImaginaryFloat Float
  231. " Spaces after "[]"
  232. if s:HighlightArrayWhitespaceError()
  233. syn match goSpaceError display "\%(\[\]\)\@<=\s\+"
  234. endif
  235. " Spacing errors around the 'chan' keyword
  236. if s:HighlightChanWhitespaceError()
  237. " receive-only annotation on chan type
  238. "
  239. " \(\<chan\>\)\@<!<- (only pick arrow when it doesn't come after a chan)
  240. " this prevents picking up 'chan<- chan<-' but not '<- chan'
  241. syn match goSpaceError display "\%(\%(\<chan\>\)\@<!<-\)\@<=\s\+\%(\<chan\>\)\@="
  242. " send-only annotation on chan type
  243. "
  244. " \(<-\)\@<!\<chan\> (only pick chan when it doesn't come after an arrow)
  245. " this prevents picking up '<-chan <-chan' but not 'chan <-'
  246. syn match goSpaceError display "\%(\%(<-\)\@<!\<chan\>\)\@<=\s\+\%(<-\)\@="
  247. " value-ignoring receives in a few contexts
  248. syn match goSpaceError display "\%(\%(^\|[={(,;]\)\s*<-\)\@<=\s\+"
  249. endif
  250. " Extra types commonly seen
  251. if s:HighlightExtraTypes()
  252. syn match goExtraType /\<bytes\.\%(Buffer\)\>/
  253. syn match goExtraType /\<context\.\%(Context\)\>/
  254. syn match goExtraType /\<io\.\%(Reader\|ReadSeeker\|ReadWriter\|ReadCloser\|ReadWriteCloser\|Writer\|WriteCloser\|Seeker\)\>/
  255. syn match goExtraType /\<reflect\.\%(Kind\|Type\|Value\)\>/
  256. syn match goExtraType /\<unsafe\.Pointer\>/
  257. endif
  258. " Space-tab error
  259. if s:HighlightSpaceTabError()
  260. syn match goSpaceError display " \+\t"me=e-1
  261. endif
  262. " Trailing white space error
  263. if s:HighlightTrailingWhitespaceError()
  264. syn match goSpaceError display excludenl "\s\+$"
  265. endif
  266. hi def link goExtraType Type
  267. hi def link goSpaceError Error
  268. " included from: https://github.com/athom/more-colorful.vim/blob/master/after/syntax/go.vim
  269. "
  270. " Comments; their contents
  271. syn keyword goTodo contained NOTE
  272. hi def link goTodo Todo
  273. syn match goVarArgs /\.\.\./
  274. " Operators;
  275. if s:HighlightOperators()
  276. " match single-char operators: - + % < > ! & | ^ * =
  277. " and corresponding two-char operators: -= += %= <= >= != &= |= ^= *= ==
  278. syn match goOperator /[-+%<>!&|^*=]=\?/
  279. " match / and /=
  280. syn match goOperator /\/\%(=\|\ze[^/*]\)/
  281. " match two-char operators: << >> &^
  282. " and corresponding three-char operators: <<= >>= &^=
  283. syn match goOperator /\%(<<\|>>\|&^\)=\?/
  284. " match remaining two-char operators: := && || <- ++ --
  285. syn match goOperator /:=\|||\|<-\|++\|--/
  286. " match ...
  287. hi def link goPointerOperator goOperator
  288. hi def link goVarArgs goOperator
  289. endif
  290. hi def link goOperator Operator
  291. " Functions;
  292. if s:HighlightFunctions() || s:HighlightFunctionParameters()
  293. syn match goDeclaration /\<func\>/ nextgroup=goReceiver,goFunction,goSimpleParams skipwhite skipnl
  294. syn match goReceiverVar /\w\+\ze\s\+\%(\w\|\*\)/ nextgroup=goPointerOperator,goReceiverType skipwhite skipnl contained
  295. syn match goPointerOperator /\*/ nextgroup=goReceiverType contained skipwhite skipnl
  296. syn match goFunction /\w\+/ nextgroup=goSimpleParams contained skipwhite skipnl
  297. syn match goReceiverType /\w\+/ contained
  298. if s:HighlightFunctionParameters()
  299. syn match goSimpleParams /(\%(\w\|\_s\|[*\.\[\],\{\}<>-]\)*)/ contained contains=goParamName,goType nextgroup=goFunctionReturn skipwhite skipnl
  300. syn match goFunctionReturn /(\%(\w\|\_s\|[*\.\[\],\{\}<>-]\)*)/ contained contains=goParamName,goType skipwhite skipnl
  301. syn match goParamName /\w\+\%(\s*,\s*\w\+\)*\ze\s\+\%(\w\|\.\|\*\|\[\)/ contained nextgroup=goParamType skipwhite skipnl
  302. syn match goParamType /\%([^,)]\|\_s\)\+,\?/ contained nextgroup=goParamName skipwhite skipnl
  303. \ contains=goVarArgs,goType,goSignedInts,goUnsignedInts,goFloats,goComplexes,goDeclType,goBlock
  304. hi def link goReceiverVar goParamName
  305. hi def link goParamName Identifier
  306. endif
  307. syn match goReceiver /(\s*\w\+\%(\s\+\*\?\s*\w\+\)\?\s*)\ze\s*\w/ contained nextgroup=goFunction contains=goReceiverVar skipwhite skipnl
  308. else
  309. syn keyword goDeclaration func
  310. endif
  311. hi def link goFunction Function
  312. " Function calls;
  313. if s:HighlightFunctionCalls()
  314. syn match goFunctionCall /\w\+\ze(/ contains=goBuiltins,goDeclaration
  315. endif
  316. hi def link goFunctionCall Type
  317. " Fields;
  318. if s:HighlightFields()
  319. " 1. Match a sequence of word characters coming after a '.'
  320. " 2. Require the following but dont match it: ( \@= see :h E59)
  321. " - The symbols: / - + * % OR
  322. " - The symbols: [] {} <> ) OR
  323. " - The symbols: \n \r space OR
  324. " - The symbols: , : .
  325. " 3. Have the start of highlight (hs) be the start of matched
  326. " pattern (s) offsetted one to the right (+1) (see :h E401)
  327. syn match goField /\.\w\+\
  328. \%(\%([\/\-\+*%]\)\|\
  329. \%([\[\]{}<\>\)]\)\|\
  330. \%([\!=\^|&]\)\|\
  331. \%([\n\r\ ]\)\|\
  332. \%([,\:.]\)\)\@=/hs=s+1
  333. endif
  334. hi def link goField Identifier
  335. " Structs & Interfaces;
  336. if s:HighlightTypes()
  337. syn match goTypeConstructor /\<\w\+{\@=/
  338. syn match goTypeDecl /\<type\>/ nextgroup=goTypeName skipwhite skipnl
  339. syn match goTypeName /\w\+/ contained nextgroup=goDeclType skipwhite skipnl
  340. syn match goDeclType /\<\%(interface\|struct\)\>/ skipwhite skipnl
  341. hi def link goReceiverType Type
  342. else
  343. syn keyword goDeclType struct interface
  344. syn keyword goDeclaration type
  345. endif
  346. hi def link goTypeConstructor Type
  347. hi def link goTypeName Type
  348. hi def link goTypeDecl Keyword
  349. hi def link goDeclType Keyword
  350. " Variable Assignments
  351. if s:HighlightVariableAssignments()
  352. syn match goVarAssign /\v[_.[:alnum:]]+(,\s*[_.[:alnum:]]+)*\ze(\s*([-^+|^\/%&]|\*|\<\<|\>\>|\&\^)?\=[^=])/
  353. hi def link goVarAssign Special
  354. endif
  355. " Variable Declarations
  356. if s:HighlightVariableDeclarations()
  357. syn match goVarDefs /\v\w+(,\s*\w+)*\ze(\s*:\=)/
  358. hi def link goVarDefs Special
  359. endif
  360. " Build Constraints
  361. if s:HighlightBuildConstraints()
  362. syn match goBuildKeyword display contained "+build\|go:build"
  363. " Highlight the known values of GOOS, GOARCH, and other +build options.
  364. syn keyword goBuildDirectives contained
  365. \ android darwin dragonfly freebsd linux nacl netbsd openbsd plan9
  366. \ solaris windows 386 amd64 amd64p32 arm armbe arm64 arm64be ppc64
  367. \ ppc64le mips mipsle mips64 mips64le mips64p32 mips64p32le ppc
  368. \ s390 s390x sparc sparc64 cgo ignore race
  369. " Other words in the build directive are build tags not listed above, so
  370. " avoid highlighting them as comments by using a matchgroup just for the
  371. " start of the comment.
  372. " The rs=s+2 option lets the \s*+build portion be part of the inner region
  373. " instead of the matchgroup so it will be highlighted as a goBuildKeyword.
  374. syn region goBuildComment matchgroup=goBuildCommentStart
  375. \ start="//\s*+build\s"rs=s+2 end="$"
  376. \ contains=goBuildKeyword,goBuildDirectives
  377. hi def link goBuildCommentStart Comment
  378. hi def link goBuildDirectives Type
  379. hi def link goBuildKeyword PreProc
  380. endif
  381. if s:HighlightBuildConstraints() || s:FoldEnable('package_comment')
  382. " One or more line comments that are followed immediately by a "package"
  383. " declaration are treated like package documentation, so these must be
  384. " matched as comments to avoid looking like working build constraints.
  385. " The he, me, and re options let the "package" itself be highlighted by
  386. " the usual rules.
  387. exe 'syn region goPackageComment start=/\v(\/\/.*\n)+\s*package/'
  388. \ . ' end=/\v\n\s*package/he=e-7,me=e-7,re=e-7'
  389. \ . ' contains=@goCommentGroup,@Spell'
  390. \ . (s:FoldEnable('package_comment') ? ' fold' : '')
  391. exe 'syn region goPackageComment start=/\v^\s*\/\*.*\n(.*\n)*\s*\*\/\npackage/'
  392. \ . ' end=/\v\*\/\n\s*package/he=e-7,me=e-7,re=e-7'
  393. \ . ' contains=@goCommentGroup,@Spell'
  394. \ . (s:FoldEnable('package_comment') ? ' fold' : '')
  395. hi def link goPackageComment Comment
  396. endif
  397. " :GoCoverage commands
  398. hi def link goCoverageNormalText Comment
  399. " Search backwards for a global declaration to start processing the syntax.
  400. "syn sync match goSync grouphere NONE /^\(const\|var\|type\|func\)\>/
  401. " There's a bug in the implementation of grouphere. For now, use the
  402. " following as a more expensive/less precise workaround.
  403. syn sync minlines=500
  404. let b:current_syntax = "go"
  405. let &cpo = s:keepcpo
  406. unlet s:keepcpo
  407. " vim: sw=2 sts=2 et