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