You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
7.7 KiB

  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. "
  7. " Options:
  8. " There are some options for customizing the highlighting; the recommended
  9. " settings are the default values, but you can write:
  10. " let OPTION_NAME = 0
  11. " in your ~/.vimrc file to disable particular options. You can also write:
  12. " let OPTION_NAME = 1
  13. " to enable particular options. At present, all options default to on.
  14. "
  15. " - go_highlight_array_whitespace_error
  16. " Highlights white space after "[]".
  17. " - go_highlight_chan_whitespace_error
  18. " Highlights white space around the communications operator that don't follow
  19. " the standard style.
  20. " - go_highlight_extra_types
  21. " Highlights commonly used library types (io.Reader, etc.).
  22. " - go_highlight_space_tab_error
  23. " Highlights instances of tabs following spaces.
  24. " - go_highlight_trailing_whitespace_error
  25. " Highlights trailing white space.
  26. " Quit when a (custom) syntax file was already loaded
  27. if exists("b:current_syntax")
  28. finish
  29. endif
  30. if !exists("go_highlight_array_whitespace_error")
  31. let go_highlight_array_whitespace_error = 1
  32. endif
  33. if !exists("go_highlight_chan_whitespace_error")
  34. let go_highlight_chan_whitespace_error = 1
  35. endif
  36. if !exists("go_highlight_extra_types")
  37. let go_highlight_extra_types = 1
  38. endif
  39. if !exists("go_highlight_space_tab_error")
  40. let go_highlight_space_tab_error = 1
  41. endif
  42. if !exists("go_highlight_trailing_whitespace_error")
  43. let go_highlight_trailing_whitespace_error = 1
  44. endif
  45. syn case match
  46. syn keyword goDirective package import
  47. syn keyword goDeclaration var const type
  48. syn keyword goDeclType struct interface
  49. hi def link goDirective Statement
  50. hi def link goDeclaration Keyword
  51. hi def link goDeclType Keyword
  52. " Keywords within functions
  53. syn keyword goStatement defer go goto return break continue fallthrough
  54. syn keyword goConditional if else switch select
  55. syn keyword goLabel case default
  56. syn keyword goRepeat for range
  57. hi def link goStatement Statement
  58. hi def link goConditional Conditional
  59. hi def link goLabel Label
  60. hi def link goRepeat Repeat
  61. " Predefined types
  62. syn keyword goType chan map bool string error
  63. syn keyword goSignedInts int int8 int16 int32 int64 rune
  64. syn keyword goUnsignedInts byte uint uint8 uint16 uint32 uint64 uintptr
  65. syn keyword goFloats float32 float64
  66. syn keyword goComplexes complex64 complex128
  67. hi def link goType Type
  68. hi def link goSignedInts Type
  69. hi def link goUnsignedInts Type
  70. hi def link goFloats Type
  71. hi def link goComplexes Type
  72. " Treat func specially: it's a declaration at the start of a line, but a type
  73. " elsewhere. Order matters here.
  74. syn match goType /\<func\>/
  75. syn match goDeclaration /^func\>/
  76. " Predefined functions and values
  77. syn keyword goBuiltins append cap close complex copy delete imag len
  78. syn keyword goBuiltins make new panic print println real recover
  79. syn keyword goConstants iota true false nil
  80. hi def link goBuiltins Keyword
  81. hi def link goConstants Keyword
  82. " Comments; their contents
  83. syn keyword goTodo contained TODO FIXME XXX BUG
  84. syn cluster goCommentGroup contains=goTodo
  85. syn region goComment start="/\*" end="\*/" contains=@goCommentGroup,@Spell
  86. syn region goComment start="//" end="$" contains=@goCommentGroup,@Spell
  87. hi def link goComment Comment
  88. hi def link goTodo Todo
  89. " Go escapes
  90. syn match goEscapeOctal display contained "\\[0-7]\{3}"
  91. syn match goEscapeC display contained +\\[abfnrtv\\'"]+
  92. syn match goEscapeX display contained "\\x\x\{2}"
  93. syn match goEscapeU display contained "\\u\x\{4}"
  94. syn match goEscapeBigU display contained "\\U\x\{8}"
  95. syn match goEscapeError display contained +\\[^0-7xuUabfnrtv\\'"]+
  96. hi def link goEscapeOctal goSpecialString
  97. hi def link goEscapeC goSpecialString
  98. hi def link goEscapeX goSpecialString
  99. hi def link goEscapeU goSpecialString
  100. hi def link goEscapeBigU goSpecialString
  101. hi def link goSpecialString Special
  102. hi def link goEscapeError Error
  103. " Strings and their contents
  104. syn cluster goStringGroup contains=goEscapeOctal,goEscapeC,goEscapeX,goEscapeU,goEscapeBigU,goEscapeError
  105. syn region goString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@goStringGroup
  106. syn region goRawString start=+`+ end=+`+
  107. hi def link goString String
  108. hi def link goRawString String
  109. " Characters; their contents
  110. syn cluster goCharacterGroup contains=goEscapeOctal,goEscapeC,goEscapeX,goEscapeU,goEscapeBigU
  111. syn region goCharacter start=+'+ skip=+\\\\\|\\'+ end=+'+ contains=@goCharacterGroup
  112. hi def link goCharacter Character
  113. " Regions
  114. syn region goBlock start="{" end="}" transparent fold
  115. syn region goParen start='(' end=')' transparent
  116. " Integers
  117. syn match goDecimalInt "\<\d\+\([Ee]\d\+\)\?\>"
  118. syn match goHexadecimalInt "\<0x\x\+\>"
  119. syn match goOctalInt "\<0\o\+\>"
  120. syn match goOctalError "\<0\o*[89]\d*\>"
  121. hi def link goDecimalInt Integer
  122. hi def link goHexadecimalInt Integer
  123. hi def link goOctalInt Integer
  124. hi def link Integer Number
  125. " Floating point
  126. syn match goFloat "\<\d\+\.\d*\([Ee][-+]\d\+\)\?\>"
  127. syn match goFloat "\<\.\d\+\([Ee][-+]\d\+\)\?\>"
  128. syn match goFloat "\<\d\+[Ee][-+]\d\+\>"
  129. hi def link goFloat Float
  130. " Imaginary literals
  131. syn match goImaginary "\<\d\+i\>"
  132. syn match goImaginary "\<\d\+\.\d*\([Ee][-+]\d\+\)\?i\>"
  133. syn match goImaginary "\<\.\d\+\([Ee][-+]\d\+\)\?i\>"
  134. syn match goImaginary "\<\d\+[Ee][-+]\d\+i\>"
  135. hi def link goImaginary Number
  136. " Spaces after "[]"
  137. if go_highlight_array_whitespace_error != 0
  138. syn match goSpaceError display "\(\[\]\)\@<=\s\+"
  139. endif
  140. " Spacing errors around the 'chan' keyword
  141. if go_highlight_chan_whitespace_error != 0
  142. " receive-only annotation on chan type
  143. syn match goSpaceError display "\(<-\)\@<=\s\+\(chan\>\)\@="
  144. " send-only annotation on chan type
  145. syn match goSpaceError display "\(\<chan\)\@<=\s\+\(<-\)\@="
  146. " value-ignoring receives in a few contexts
  147. syn match goSpaceError display "\(\(^\|[={(,;]\)\s*<-\)\@<=\s\+"
  148. endif
  149. " Extra types commonly seen
  150. if go_highlight_extra_types != 0
  151. syn match goExtraType /\<bytes\.\(Buffer\)\>/
  152. syn match goExtraType /\<io\.\(Reader\|Writer\|ReadWriter\|ReadWriteCloser\)\>/
  153. syn match goExtraType /\<reflect\.\(Kind\|Type\|Value\)\>/
  154. syn match goExtraType /\<unsafe\.Pointer\>/
  155. endif
  156. " Space-tab error
  157. if go_highlight_space_tab_error != 0
  158. syn match goSpaceError display " \+\t"me=e-1
  159. endif
  160. " Trailing white space error
  161. if go_highlight_trailing_whitespace_error != 0
  162. syn match goSpaceError display excludenl "\s\+$"
  163. endif
  164. hi def link goExtraType Type
  165. hi def link goSpaceError Error
  166. " Search backwards for a global declaration to start processing the syntax.
  167. "syn sync match goSync grouphere NONE /^\(const\|var\|type\|func\)\>/
  168. " There's a bug in the implementation of grouphere. For now, use the
  169. " following as a more expensive/less precise workaround.
  170. syn sync minlines=500
  171. let b:current_syntax = "go"