go.vim 18 KB

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