3 " Maintainer: Claudio Fleiner <claudio@fleiner.com>
4 " URL: http://www.fleiner.com/vim/syntax/java.vim
5 " Last Change: 2009 Mar 14
7 " Please check :help java.vim for comments on some of the options available.
9 " Quit when a syntax file was already loaded
10 if !exists("main_syntax")
13 elseif exists("b:current_syntax")
16 " we define it here so that included files can test for it
17 let main_syntax='java'
18 syn region javaFold start="{" end="}" transparent fold
21 " don't use standard HiLink, it will not work with included syntax files
23 command! -nargs=+ JavaHiLink hi link <args>
25 command! -nargs=+ JavaHiLink hi def link <args>
28 " some characters that cannot be in a java program (outside a string)
29 syn match javaError "[\\@`]"
30 syn match javaError "<<<\|\.\.\|=>\|<>\|||=\|&&=\|[^-]->\|\*\/"
31 syn match javaOK "\.\.\."
33 " use separate name so that it can be deleted in javacc.vim
34 syn match javaError2 "#\|=<"
35 JavaHiLink javaError2 javaError
40 syn keyword javaExternal native package
41 syn match javaExternal "\<import\>\(\s\+static\>\)\?"
42 syn keyword javaError goto const
43 syn keyword javaConditional if else switch
44 syn keyword javaRepeat while for do
45 syn keyword javaBoolean true false
46 syn keyword javaConstant null
47 syn keyword javaTypedef this super
48 syn keyword javaOperator new instanceof
49 syn keyword javaType boolean char byte short int long float double
50 syn keyword javaType void
51 syn keyword javaStatement return
52 syn keyword javaStorageClass static synchronized transient volatile final strictfp serializable
53 syn keyword javaExceptions throw try catch finally
54 syn keyword javaAssert assert
55 syn keyword javaMethodDecl synchronized throws
56 syn keyword javaClassDecl extends implements interface
57 " to differentiate the keyword class from MyClass.class we use a match here
58 syn match javaTypedef "\.\s*\<class\>"ms=s+1
59 syn keyword javaClassDecl enum
60 syn match javaClassDecl "^class\>"
61 syn match javaClassDecl "[^.]\s*\<class\>"ms=s+1
62 syn match javaAnnotation "@\([_$a-zA-Z][_$a-zA-Z0-9]*\.\)*[_$a-zA-Z][_$a-zA-Z0-9]*\>"
63 syn match javaClassDecl "@interface\>"
64 syn keyword javaBranch break continue nextgroup=javaUserLabelRef skipwhite
65 syn match javaUserLabelRef "\k\+" contained
66 syn match javaVarArg "\.\.\."
67 syn keyword javaScopeDecl public protected private abstract
69 if exists("java_highlight_java_lang_ids")
70 let java_highlight_all=1
72 if exists("java_highlight_all") || exists("java_highlight_java") || exists("java_highlight_java_lang")
74 syn match javaLangClass "\<System\>"
75 syn keyword javaR_JavaLang NegativeArraySizeException ArrayStoreException IllegalStateException RuntimeException IndexOutOfBoundsException UnsupportedOperationException ArrayIndexOutOfBoundsException ArithmeticException ClassCastException EnumConstantNotPresentException StringIndexOutOfBoundsException IllegalArgumentException IllegalMonitorStateException IllegalThreadStateException NumberFormatException NullPointerException TypeNotPresentException SecurityException
76 syn cluster javaTop add=javaR_JavaLang
77 syn cluster javaClasses add=javaR_JavaLang
78 JavaHiLink javaR_JavaLang javaR_Java
79 syn keyword javaC_JavaLang Process RuntimePermission StringKeySet CharacterData01 Class ThreadLocal ThreadLocalMap CharacterData0E Package Character StringCoding Long ProcessImpl ProcessEnvironment Short AssertionStatusDirectives 1PackageInfoProxy UnicodeBlock InheritableThreadLocal AbstractStringBuilder StringEnvironment ClassLoader ConditionalSpecialCasing CharacterDataPrivateUse StringBuffer StringDecoder Entry StringEntry WrappedHook StringBuilder StrictMath State ThreadGroup Runtime CharacterData02 MethodArray Object CharacterDataUndefined Integer Gate Boolean Enum Variable Subset StringEncoder Void Terminator CharsetSD IntegerCache CharacterCache Byte CharsetSE Thread SystemClassLoaderAction CharacterDataLatin1 StringValues StackTraceElement Shutdown ShortCache String ConverterSD ByteCache Lock EnclosingMethodInfo Math Float Value Double SecurityManager LongCache ProcessBuilder StringEntrySet Compiler Number UNIXProcess ConverterSE ExternalData CaseInsensitiveComparator CharacterData00 NativeLibrary
80 syn cluster javaTop add=javaC_JavaLang
81 syn cluster javaClasses add=javaC_JavaLang
82 JavaHiLink javaC_JavaLang javaC_Java
83 syn keyword javaE_JavaLang IncompatibleClassChangeError InternalError UnknownError ClassCircularityError AssertionError ThreadDeath IllegalAccessError NoClassDefFoundError ClassFormatError UnsupportedClassVersionError NoSuchFieldError VerifyError ExceptionInInitializerError InstantiationError LinkageError NoSuchMethodError Error UnsatisfiedLinkError StackOverflowError AbstractMethodError VirtualMachineError OutOfMemoryError
84 syn cluster javaTop add=javaE_JavaLang
85 syn cluster javaClasses add=javaE_JavaLang
86 JavaHiLink javaE_JavaLang javaE_Java
87 syn keyword javaX_JavaLang CloneNotSupportedException Exception NoSuchMethodException IllegalAccessException NoSuchFieldException Throwable InterruptedException ClassNotFoundException InstantiationException
88 syn cluster javaTop add=javaX_JavaLang
89 syn cluster javaClasses add=javaX_JavaLang
90 JavaHiLink javaX_JavaLang javaX_Java
92 JavaHiLink javaR_Java javaR_
93 JavaHiLink javaC_Java javaC_
94 JavaHiLink javaE_Java javaE_
95 JavaHiLink javaX_Java javaX_
96 JavaHiLink javaX_ javaExceptions
97 JavaHiLink javaR_ javaExceptions
98 JavaHiLink javaE_ javaExceptions
99 JavaHiLink javaC_ javaConstant
101 syn keyword javaLangObject clone equals finalize getClass hashCode
102 syn keyword javaLangObject notify notifyAll toString wait
103 JavaHiLink javaLangObject javaConstant
104 syn cluster javaTop add=javaLangObject
107 if filereadable(expand("<sfile>:p:h")."/javaid.vim")
108 source <sfile>:p:h/javaid.vim
111 if exists("java_space_errors")
112 if !exists("java_no_trail_space_error")
113 syn match javaSpaceError "\s\+$"
115 if !exists("java_no_tab_space_error")
116 syn match javaSpaceError " \+\t"me=e-1
120 syn region javaLabelRegion transparent matchgroup=javaLabel start="\<case\>" matchgroup=NONE end=":" contains=javaNumber,javaCharacter
121 syn match javaUserLabel "^\s*[_$a-zA-Z][_$a-zA-Z0-9_]*\s*:"he=e-1 contains=javaLabel
122 syn keyword javaLabel default
124 if !exists("java_allow_cpp_keywords")
125 syn keyword javaError auto delete extern friend inline redeclared
126 syn keyword javaError register signed sizeof struct template typedef union
127 syn keyword javaError unsigned operator
130 " The following cluster contains all java groups except the contained ones
131 syn cluster javaTop add=javaExternal,javaError,javaError,javaBranch,javaLabelRegion,javaLabel,javaConditional,javaRepeat,javaBoolean,javaConstant,javaTypedef,javaOperator,javaType,javaType,javaStatement,javaStorageClass,javaAssert,javaExceptions,javaMethodDecl,javaClassDecl,javaClassDecl,javaClassDecl,javaScopeDecl,javaError,javaError2,javaUserLabel,javaLangObject,javaAnnotation,javaVarArg
135 syn keyword javaTodo contained TODO FIXME XXX
136 if exists("java_comment_strings")
137 syn region javaCommentString contained start=+"+ end=+"+ end=+$+ end=+\*/+me=s-1,he=s-1 contains=javaSpecial,javaCommentStar,javaSpecialChar,@Spell
138 syn region javaComment2String contained start=+"+ end=+$\|"+ contains=javaSpecial,javaSpecialChar,@Spell
139 syn match javaCommentCharacter contained "'\\[^']\{1,6\}'" contains=javaSpecialChar
140 syn match javaCommentCharacter contained "'\\''" contains=javaSpecialChar
141 syn match javaCommentCharacter contained "'[^\\]'"
142 syn cluster javaCommentSpecial add=javaCommentString,javaCommentCharacter,javaNumber
143 syn cluster javaCommentSpecial2 add=javaComment2String,javaCommentCharacter,javaNumber
145 syn region javaComment start="/\*" end="\*/" contains=@javaCommentSpecial,javaTodo,@Spell
146 syn match javaCommentStar contained "^\s*\*[^/]"me=e-1
147 syn match javaCommentStar contained "^\s*\*$"
148 syn match javaLineComment "//.*" contains=@javaCommentSpecial2,javaTodo,@Spell
149 JavaHiLink javaCommentString javaString
150 JavaHiLink javaComment2String javaString
151 JavaHiLink javaCommentCharacter javaCharacter
153 syn cluster javaTop add=javaComment,javaLineComment
155 if !exists("java_ignore_javadoc") && main_syntax != 'jsp'
157 " syntax coloring for javadoc comments (HTML)
158 syntax include @javaHtml <sfile>:p:h/html.vim
159 unlet b:current_syntax
160 " HTML enables spell checking for all text that is not in a syntax item. This
161 " is wrong for Java (all identifiers would be spell-checked), so it's undone
165 syn region javaDocComment start="/\*\*" end="\*/" keepend contains=javaCommentTitle,@javaHtml,javaDocTags,javaDocSeeTag,javaTodo,@Spell
166 syn region javaCommentTitle contained matchgroup=javaDocComment start="/\*\*" matchgroup=javaCommentTitle keepend end="\.$" end="\.[ \t\r<&]"me=e-1 end="[^{]@"me=s-2,he=s-1 end="\*/"me=s-1,he=s-1 contains=@javaHtml,javaCommentStar,javaTodo,@Spell,javaDocTags,javaDocSeeTag
168 syn region javaDocTags contained start="{@\(link\|linkplain\|inherit[Dd]oc\|doc[rR]oot\|value\)" end="}"
169 syn match javaDocTags contained "@\(param\|exception\|throws\|since\)\s\+\S\+" contains=javaDocParam
170 syn match javaDocParam contained "\s\S\+"
171 syn match javaDocTags contained "@\(version\|author\|return\|deprecated\|serial\|serialField\|serialData\)\>"
172 syn region javaDocSeeTag contained matchgroup=javaDocTags start="@see\s\+" matchgroup=NONE end="\_."re=e-1 contains=javaDocSeeTagParam
173 syn match javaDocSeeTagParam contained @"\_[^"]\+"\|<a\s\+\_.\{-}</a>\|\(\k\|\.\)*\(#\k\+\((\_[^)]\+)\)\=\)\=@ extend
177 " match the special comment /**/
178 syn match javaComment "/\*\*/"
180 " Strings and constants
181 syn match javaSpecialError contained "\\."
182 syn match javaSpecialCharError contained "[^']"
183 syn match javaSpecialChar contained "\\\([4-9]\d\|[0-3]\d\d\|[\"\\'ntbrf]\|u\+\x\{4\}\)"
184 syn region javaString start=+"+ end=+"+ end=+$+ contains=javaSpecialChar,javaSpecialError,@Spell
185 " next line disabled, it can cause a crash for a long line
186 "syn match javaStringError +"\([^"\\]\|\\.\)*$+
187 syn match javaCharacter "'[^']*'" contains=javaSpecialChar,javaSpecialCharError
188 syn match javaCharacter "'\\''" contains=javaSpecialChar
189 syn match javaCharacter "'[^\\]'"
190 syn match javaNumber "\<\(0[0-7]*\|0[xX]\x\+\|\d\+\)[lL]\=\>"
191 syn match javaNumber "\(\<\d\+\.\d*\|\.\d\+\)\([eE][-+]\=\d\+\)\=[fFdD]\="
192 syn match javaNumber "\<\d\+[eE][-+]\=\d\+[fFdD]\=\>"
193 syn match javaNumber "\<\d\+\([eE][-+]\=\d\+\)\=[fFdD]\>"
196 syn match javaSpecial "\\u\+\d\{4\}"
198 syn cluster javaTop add=javaString,javaCharacter,javaNumber,javaSpecial,javaStringError
200 if exists("java_highlight_functions")
201 if java_highlight_functions == "indent"
202 syn match javaFuncDef "^\(\t\| \{8\}\)[_$a-zA-Z][_$a-zA-Z0-9_. \[\]]*([^-+*/()]*)" contains=javaScopeDecl,javaType,javaStorageClass,@javaClasses
203 syn region javaFuncDef start=+^\(\t\| \{8\}\)[$_a-zA-Z][$_a-zA-Z0-9_. \[\]]*([^-+*/()]*,\s*+ end=+)+ contains=javaScopeDecl,javaType,javaStorageClass,@javaClasses
204 syn match javaFuncDef "^ [$_a-zA-Z][$_a-zA-Z0-9_. \[\]]*([^-+*/()]*)" contains=javaScopeDecl,javaType,javaStorageClass,@javaClasses
205 syn region javaFuncDef start=+^ [$_a-zA-Z][$_a-zA-Z0-9_. \[\]]*([^-+*/()]*,\s*+ end=+)+ contains=javaScopeDecl,javaType,javaStorageClass,@javaClasses
207 " This line catches method declarations at any indentation>0, but it assumes
209 " 1. class names are always capitalized (ie: Button)
210 " 2. method names are never capitalized (except constructors, of course)
211 syn region javaFuncDef start=+^\s\+\(\(public\|protected\|private\|static\|abstract\|final\|native\|synchronized\)\s\+\)*\(\(void\|boolean\|char\|byte\|short\|int\|long\|float\|double\|\([A-Za-z_][A-Za-z0-9_$]*\.\)*[A-Z][A-Za-z0-9_$]*\)\(<[^>]*>\)\=\(\[\]\)*\s\+[a-z][A-Za-z0-9_$]*\|[A-Z][A-Za-z0-9_$]*\)\s*([^0-9]+ end=+)+ contains=javaScopeDecl,javaType,javaStorageClass,javaComment,javaLineComment,@javaClasses
213 syn match javaBraces "[{}]"
214 syn cluster javaTop add=javaFuncDef,javaBraces
217 if exists("java_highlight_debug")
219 " Strings and constants
220 syn match javaDebugSpecial contained "\\\d\d\d\|\\."
221 syn region javaDebugString contained start=+"+ end=+"+ contains=javaDebugSpecial
222 syn match javaDebugStringError +"\([^"\\]\|\\.\)*$+
223 syn match javaDebugCharacter contained "'[^\\]'"
224 syn match javaDebugSpecialCharacter contained "'\\.'"
225 syn match javaDebugSpecialCharacter contained "'\\''"
226 syn match javaDebugNumber contained "\<\(0[0-7]*\|0[xX]\x\+\|\d\+\)[lL]\=\>"
227 syn match javaDebugNumber contained "\(\<\d\+\.\d*\|\.\d\+\)\([eE][-+]\=\d\+\)\=[fFdD]\="
228 syn match javaDebugNumber contained "\<\d\+[eE][-+]\=\d\+[fFdD]\=\>"
229 syn match javaDebugNumber contained "\<\d\+\([eE][-+]\=\d\+\)\=[fFdD]\>"
230 syn keyword javaDebugBoolean contained true false
231 syn keyword javaDebugType contained null this super
232 syn region javaDebugParen start=+(+ end=+)+ contained contains=javaDebug.*,javaDebugParen
234 " to make this work you must define the highlighting for these groups
235 syn match javaDebug "\<System\.\(out\|err\)\.print\(ln\)*\s*("me=e-1 contains=javaDebug.* nextgroup=javaDebugParen
236 syn match javaDebug "\<p\s*("me=e-1 contains=javaDebug.* nextgroup=javaDebugParen
237 syn match javaDebug "[A-Za-z][a-zA-Z0-9_]*\.printStackTrace\s*("me=e-1 contains=javaDebug.* nextgroup=javaDebugParen
238 syn match javaDebug "\<trace[SL]\=\s*("me=e-1 contains=javaDebug.* nextgroup=javaDebugParen
240 syn cluster javaTop add=javaDebug
242 if version >= 508 || !exists("did_c_syn_inits")
243 JavaHiLink javaDebug Debug
244 JavaHiLink javaDebugString DebugString
245 JavaHiLink javaDebugStringError javaError
246 JavaHiLink javaDebugType DebugType
247 JavaHiLink javaDebugBoolean DebugBoolean
248 JavaHiLink javaDebugNumber Debug
249 JavaHiLink javaDebugSpecial DebugSpecial
250 JavaHiLink javaDebugSpecialCharacter DebugSpecial
251 JavaHiLink javaDebugCharacter DebugString
252 JavaHiLink javaDebugParen Debug
254 JavaHiLink DebugString String
255 JavaHiLink DebugSpecial Special
256 JavaHiLink DebugBoolean Boolean
257 JavaHiLink DebugType Type
261 if exists("java_mark_braces_in_parens_as_errors")
262 syn match javaInParen contained "[{}]"
263 JavaHiLink javaInParen javaError
264 syn cluster javaTop add=javaInParen
267 " catch errors caused by wrong parenthesis
268 syn region javaParenT transparent matchgroup=javaParen start="(" end=")" contains=@javaTop,javaParenT1
269 syn region javaParenT1 transparent matchgroup=javaParen1 start="(" end=")" contains=@javaTop,javaParenT2 contained
270 syn region javaParenT2 transparent matchgroup=javaParen2 start="(" end=")" contains=@javaTop,javaParenT contained
271 syn match javaParenError ")"
272 " catch errors caused by wrong square parenthesis
273 syn region javaParenT transparent matchgroup=javaParen start="\[" end="\]" contains=@javaTop,javaParenT1
274 syn region javaParenT1 transparent matchgroup=javaParen1 start="\[" end="\]" contains=@javaTop,javaParenT2 contained
275 syn region javaParenT2 transparent matchgroup=javaParen2 start="\[" end="\]" contains=@javaTop,javaParenT contained
276 syn match javaParenError "\]"
278 JavaHiLink javaParenError javaError
280 if !exists("java_minlines")
281 let java_minlines = 10
283 exec "syn sync ccomment javaComment minlines=" . java_minlines
285 " The default highlighting.
286 if version >= 508 || !exists("did_java_syn_inits")
288 let did_java_syn_inits = 1
290 JavaHiLink javaFuncDef Function
291 JavaHiLink javaVarArg Function
292 JavaHiLink javaBraces Function
293 JavaHiLink javaBranch Conditional
294 JavaHiLink javaUserLabelRef javaUserLabel
295 JavaHiLink javaLabel Label
296 JavaHiLink javaUserLabel Label
297 JavaHiLink javaConditional Conditional
298 JavaHiLink javaRepeat Repeat
299 JavaHiLink javaExceptions Exception
300 JavaHiLink javaAssert Statement
301 JavaHiLink javaStorageClass StorageClass
302 JavaHiLink javaMethodDecl javaStorageClass
303 JavaHiLink javaClassDecl javaStorageClass
304 JavaHiLink javaScopeDecl javaStorageClass
305 JavaHiLink javaBoolean Boolean
306 JavaHiLink javaSpecial Special
307 JavaHiLink javaSpecialError Error
308 JavaHiLink javaSpecialCharError Error
309 JavaHiLink javaString String
310 JavaHiLink javaCharacter Character
311 JavaHiLink javaSpecialChar SpecialChar
312 JavaHiLink javaNumber Number
313 JavaHiLink javaError Error
314 JavaHiLink javaStringError Error
315 JavaHiLink javaStatement Statement
316 JavaHiLink javaOperator Operator
317 JavaHiLink javaComment Comment
318 JavaHiLink javaDocComment Comment
319 JavaHiLink javaLineComment Comment
320 JavaHiLink javaConstant Constant
321 JavaHiLink javaTypedef Typedef
322 JavaHiLink javaTodo Todo
323 JavaHiLink javaAnnotation PreProc
325 JavaHiLink javaCommentTitle SpecialComment
326 JavaHiLink javaDocTags Special
327 JavaHiLink javaDocParam Function
328 JavaHiLink javaDocSeeTagParam Function
329 JavaHiLink javaCommentStar javaComment
331 JavaHiLink javaType Type
332 JavaHiLink javaExternal Include
334 JavaHiLink htmlComment Special
335 JavaHiLink htmlCommentPart Special
336 JavaHiLink javaSpaceError Error
339 delcommand JavaHiLink
341 let b:current_syntax = "java"
343 if main_syntax == 'java'
347 let b:spell_options="contained"