blob: e78f8cf27c48fb51628d57aefb52bbb3283ecc4e [file] [log] [blame]
Bram Moolenaarfb539272014-08-22 19:21:47 +02001" 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
27if exists('b:current_syntax')
28 finish
29endif
30
31if !exists('g:go_highlight_array_whitespace_error')
32 let g:go_highlight_array_whitespace_error = 1
33endif
34if !exists('g:go_highlight_chan_whitespace_error')
35 let g:go_highlight_chan_whitespace_error = 1
36endif
37if !exists('g:go_highlight_extra_types')
38 let g:go_highlight_extra_types = 1
39endif
40if !exists('g:go_highlight_space_tab_error')
41 let g:go_highlight_space_tab_error = 1
42endif
43if !exists('g:go_highlight_trailing_whitespace_error')
44 let g:go_highlight_trailing_whitespace_error = 1
45endif
46
47syn case match
48
49syn keyword goDirective package import
50syn keyword goDeclaration var const type
51syn keyword goDeclType struct interface
52
53hi def link goDirective Statement
54hi def link goDeclaration Keyword
55hi def link goDeclType Keyword
56
57" Keywords within functions
58syn keyword goStatement defer go goto return break continue fallthrough
59syn keyword goConditional if else switch select
60syn keyword goLabel case default
61syn keyword goRepeat for range
62
63hi def link goStatement Statement
64hi def link goConditional Conditional
65hi def link goLabel Label
66hi def link goRepeat Repeat
67
68" Predefined types
69syn keyword goType chan map bool string error
70syn keyword goSignedInts int int8 int16 int32 int64 rune
71syn keyword goUnsignedInts byte uint uint8 uint16 uint32 uint64 uintptr
72syn keyword goFloats float32 float64
73syn keyword goComplexes complex64 complex128
74
75hi def link goType Type
76hi def link goSignedInts Type
77hi def link goUnsignedInts Type
78hi def link goFloats Type
79hi 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.
83syn match goType /\<func\>/
84syn match goDeclaration /^func\>/
85
86" Predefined functions and values
87syn keyword goBuiltins append cap close complex copy delete imag len
88syn keyword goBuiltins make new panic print println real recover
89syn keyword goConstants iota true false nil
90
91hi def link goBuiltins Keyword
92hi def link goConstants Keyword
93
94" Comments; their contents
95syn keyword goTodo contained TODO FIXME XXX BUG
96syn cluster goCommentGroup contains=goTodo
97syn region goComment start="/\*" end="\*/" contains=@goCommentGroup,@Spell
98syn region goComment start="//" end="$" contains=@goCommentGroup,@Spell
99
100hi def link goComment Comment
101hi def link goTodo Todo
102
103" Go escapes
104syn match goEscapeOctal display contained "\\[0-7]\{3}"
105syn match goEscapeC display contained +\\[abfnrtv\\'"]+
106syn match goEscapeX display contained "\\x\x\{2}"
107syn match goEscapeU display contained "\\u\x\{4}"
108syn match goEscapeBigU display contained "\\U\x\{8}"
109syn match goEscapeError display contained +\\[^0-7xuUabfnrtv\\'"]+
110
111hi def link goEscapeOctal goSpecialString
112hi def link goEscapeC goSpecialString
113hi def link goEscapeX goSpecialString
114hi def link goEscapeU goSpecialString
115hi def link goEscapeBigU goSpecialString
116hi def link goSpecialString Special
117hi def link goEscapeError Error
118
119" Strings and their contents
120syn cluster goStringGroup contains=goEscapeOctal,goEscapeC,goEscapeX,goEscapeU,goEscapeBigU,goEscapeError
121syn region goString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=@goStringGroup
122syn region goRawString start=+`+ end=+`+
123
124hi def link goString String
125hi def link goRawString String
126
127" Characters; their contents
128syn cluster goCharacterGroup contains=goEscapeOctal,goEscapeC,goEscapeX,goEscapeU,goEscapeBigU
129syn region goCharacter start=+'+ skip=+\\\\\|\\'+ end=+'+ contains=@goCharacterGroup
130
131hi def link goCharacter Character
132
133" Regions
134syn region goBlock start="{" end="}" transparent fold
135syn region goParen start='(' end=')' transparent
136
137" Integers
138syn match goDecimalInt "\<\d\+\([Ee]\d\+\)\?\>"
139syn match goHexadecimalInt "\<0x\x\+\>"
140syn match goOctalInt "\<0\o\+\>"
141syn match goOctalError "\<0\o*[89]\d*\>"
142
143hi def link goDecimalInt Integer
144hi def link goHexadecimalInt Integer
145hi def link goOctalInt Integer
146hi def link Integer Number
147
148" Floating point
149syn match goFloat "\<\d\+\.\d*\([Ee][-+]\d\+\)\?\>"
150syn match goFloat "\<\.\d\+\([Ee][-+]\d\+\)\?\>"
151syn match goFloat "\<\d\+[Ee][-+]\d\+\>"
152
153hi def link goFloat Float
154
155" Imaginary literals
156syn match goImaginary "\<\d\+i\>"
157syn match goImaginary "\<\d\+\.\d*\([Ee][-+]\d\+\)\?i\>"
158syn match goImaginary "\<\.\d\+\([Ee][-+]\d\+\)\?i\>"
159syn match goImaginary "\<\d\+[Ee][-+]\d\+i\>"
160
161hi def link goImaginary Number
162
163" Spaces after "[]"
164if go_highlight_array_whitespace_error != 0
165 syn match goSpaceError display "\(\[\]\)\@<=\s\+"
166endif
167
168" Spacing errors around the 'chan' keyword
169if 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\+"
176endif
177
178" Extra types commonly seen
179if 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\>/
184endif
185
186" Space-tab error
187if go_highlight_space_tab_error != 0
188 syn match goSpaceError display " \+\t"me=e-1
189endif
190
191" Trailing white space error
192if go_highlight_trailing_whitespace_error != 0
193 syn match goSpaceError display excludenl "\s\+$"
194endif
195
196hi def link goExtraType Type
197hi 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.
204syn sync minlines=500
205
206let b:current_syntax = 'go'
207
208" vim: sw=2 sts=2 et