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