3 " Maintainer: Claudio Fleiner <claudio@fleiner.com>
4 " URL: http://www.fleiner.com/vim/syntax/java.vim
5 " Last Change: 2007 Dec 21
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'
20 " don't use standard HiLink, it will not work with included syntax files
22 command! -nargs=+ JavaHiLink hi link <args>
24 command! -nargs=+ JavaHiLink hi def link <args>
27 " some characters that cannot be in a java program (outside a string)
28 syn match javaError "[\\@`]"
29 syn match javaError "<<<\|\.\.\|=>\|<>\|||=\|&&=\|[^-]->\|\*\/"
30 syn match javaOK "\.\.\."
32 " use separate name so that it can be deleted in javacc.vim
33 syn match javaError2 "#\|=<"
34 JavaHiLink javaError2 javaError
39 syn keyword javaExternal native package
40 syn match javaExternal "\<import\>\(\s\+static\>\)\?"
41 syn keyword javaError goto const
42 syn keyword javaConditional if else switch
43 syn keyword javaRepeat while for do
44 syn keyword javaBoolean true false
45 syn keyword javaConstant null
46 syn keyword javaTypedef this super
47 syn keyword javaOperator new instanceof
48 syn keyword javaType boolean char byte short int long float double
49 syn keyword javaType void
50 syn keyword javaStatement return
51 syn keyword javaStorageClass static synchronized transient volatile final strictfp serializable
52 syn keyword javaExceptions throw try catch finally
53 syn keyword javaAssert assert
54 syn keyword javaMethodDecl synchronized throws
55 syn keyword javaClassDecl extends implements interface
56 " to differentiate the keyword class from MyClass.class we use a match here
57 syn match javaTypedef "\.\s*\<class\>"ms=s+1
58 syn keyword javaClassDecl enum
59 syn match javaClassDecl "^class\>"
60 syn match javaClassDecl "[^.]\s*\<class\>"ms=s+1
61 syn match javaAnnotation "@[_$a-zA-Z][_$a-zA-Z0-9_]*\>"
62 syn match javaClassDecl "@interface\>"
63 syn keyword javaBranch break continue nextgroup=javaUserLabelRef skipwhite
64 syn match javaUserLabelRef "\k\+" contained
65 syn match javaVarArg "\.\.\."
66 syn keyword javaScopeDecl public protected private abstract
68 if exists("java_highlight_java_lang_ids")
69 let java_highlight_all=1
71 if exists("java_highlight_all") || exists("java_highlight_java") || exists("java_highlight_java_lang")
73 syn match javaLangClass "\<System\>"
74 syn keyword javaR_JavaLang NegativeArraySizeException ArrayStoreException IllegalStateException RuntimeException IndexOutOfBoundsException UnsupportedOperationException ArrayIndexOutOfBoundsException ArithmeticException ClassCastException EnumConstantNotPresentException StringIndexOutOfBoundsException IllegalArgumentException IllegalMonitorStateException IllegalThreadStateException NumberFormatException NullPointerException TypeNotPresentException SecurityException
75 syn cluster javaTop add=javaR_JavaLang
76 syn cluster javaClasses add=javaR_JavaLang
77 JavaHiLink javaR_JavaLang javaR_Java
78 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
79 syn cluster javaTop add=javaC_JavaLang
80 syn cluster javaClasses add=javaC_JavaLang
81 JavaHiLink javaC_JavaLang javaC_Java
82 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
83 syn cluster javaTop add=javaE_JavaLang
84 syn cluster javaClasses add=javaE_JavaLang
85 JavaHiLink javaE_JavaLang javaE_Java
86 syn keyword javaX_JavaLang CloneNotSupportedException Exception NoSuchMethodException IllegalAccessException NoSuchFieldException Throwable InterruptedException ClassNotFoundException InstantiationException
87 syn cluster javaTop add=javaX_JavaLang
88 syn cluster javaClasses add=javaX_JavaLang
89 JavaHiLink javaX_JavaLang javaX_Java
91 JavaHiLink javaR_Java javaR_
92 JavaHiLink javaC_Java javaC_
93 JavaHiLink javaE_Java javaE_
94 JavaHiLink javaX_Java javaX_
95 JavaHiLink javaX_ javaExceptions
96 JavaHiLink javaR_ javaExceptions
97 JavaHiLink javaE_ javaExceptions
98 JavaHiLink javaC_ javaConstant
100 syn keyword javaLangObject clone equals finalize getClass hashCode
101 syn keyword javaLangObject notify notifyAll toString wait
102 JavaHiLink javaLangObject javaConstant
103 syn cluster javaTop add=javaLangObject
106 if filereadable(expand("<sfile>:p:h")."/javaid.vim")
107 source <sfile>:p:h/javaid.vim
110 if exists("java_space_errors")
111 if !exists("java_no_trail_space_error")
112 syn match javaSpaceError "\s\+$"
114 if !exists("java_no_tab_space_error")
115 syn match javaSpaceError " \+\t"me=e-1
119 syn region javaLabelRegion transparent matchgroup=javaLabel start="\<case\>" matchgroup=NONE end=":" contains=javaNumber,javaCharacter
120 syn match javaUserLabel "^\s*[_$a-zA-Z][_$a-zA-Z0-9_]*\s*:"he=e-1 contains=javaLabel
121 syn keyword javaLabel default
123 if !exists("java_allow_cpp_keywords")
124 " The default used to be to highlight C++ keywords. But several people
125 " don't like that, so default to not highlighting these.
126 let java_allow_cpp_keywords = 1
128 if !java_allow_cpp_keywords
129 syn keyword javaError auto delete extern friend inline redeclared
130 syn keyword javaError register signed sizeof struct template typedef union
131 syn keyword javaError unsigned operator
134 " The following cluster contains all java groups except the contained ones
135 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
139 syn keyword javaTodo contained TODO FIXME XXX
140 if exists("java_comment_strings")
141 syn region javaCommentString contained start=+"+ end=+"+ end=+$+ end=+\*/+me=s-1,he=s-1 contains=javaSpecial,javaCommentStar,javaSpecialChar,@Spell
142 syn region javaComment2String contained start=+"+ end=+$\|"+ contains=javaSpecial,javaSpecialChar,@Spell
143 syn match javaCommentCharacter contained "'\\[^']\{1,6\}'" contains=javaSpecialChar
144 syn match javaCommentCharacter contained "'\\''" contains=javaSpecialChar
145 syn match javaCommentCharacter contained "'[^\\]'"
146 syn cluster javaCommentSpecial add=javaCommentString,javaCommentCharacter,javaNumber
147 syn cluster javaCommentSpecial2 add=javaComment2String,javaCommentCharacter,javaNumber
149 syn region javaComment start="/\*" end="\*/" contains=@javaCommentSpecial,javaTodo,@Spell
150 syn match javaCommentStar contained "^\s*\*[^/]"me=e-1
151 syn match javaCommentStar contained "^\s*\*$"
152 syn match javaLineComment "//.*" contains=@javaCommentSpecial2,javaTodo,@Spell
153 JavaHiLink javaCommentString javaString
154 JavaHiLink javaComment2String javaString
155 JavaHiLink javaCommentCharacter javaCharacter
157 syn cluster javaTop add=javaComment,javaLineComment
159 if !exists("java_ignore_javadoc") && main_syntax != 'jsp'
161 " syntax coloring for javadoc comments (HTML)
162 syntax include @javaHtml <sfile>:p:h/html.vim
163 unlet b:current_syntax
164 syn region javaDocComment start="/\*\*" end="\*/" keepend contains=javaCommentTitle,@javaHtml,javaDocTags,javaDocSeeTag,javaTodo,@Spell
165 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
167 syn region javaDocTags contained start="{@\(link\|linkplain\|inherit[Dd]oc\|doc[rR]oot\|value\)" end="}"
168 syn match javaDocTags contained "@\(param\|exception\|throws\|since\)\s\+\S\+" contains=javaDocParam
169 syn match javaDocParam contained "\s\S\+"
170 syn match javaDocTags contained "@\(version\|author\|return\|deprecated\|serial\|serialField\|serialData\)\>"
171 syn region javaDocSeeTag contained matchgroup=javaDocTags start="@see\s\+" matchgroup=NONE end="\_."re=e-1 contains=javaDocSeeTagParam
172 syn match javaDocSeeTagParam contained @"\_[^"]\+"\|<a\s\+\_.\{-}</a>\|\(\k\|\.\)*\(#\k\+\((\_[^)]\+)\)\=\)\=@ extend
176 " match the special comment /**/
177 syn match javaComment "/\*\*/"
179 " Strings and constants
180 syn match javaSpecialError contained "\\."
181 syn match javaSpecialCharError contained "[^']"
182 syn match javaSpecialChar contained "\\\([4-9]\d\|[0-3]\d\d\|[\"\\'ntbrf]\|u\x\{4\}\)"
183 syn region javaString start=+"+ end=+"+ end=+$+ contains=javaSpecialChar,javaSpecialError,@Spell
184 " next line disabled, it can cause a crash for a long line
185 "syn match javaStringError +"\([^"\\]\|\\.\)*$+
186 syn match javaCharacter "'[^']*'" contains=javaSpecialChar,javaSpecialCharError
187 syn match javaCharacter "'\\''" contains=javaSpecialChar
188 syn match javaCharacter "'[^\\]'"
189 syn match javaNumber "\<\(0[0-7]*\|0[xX]\x\+\|\d\+\)[lL]\=\>"
190 syn match javaNumber "\(\<\d\+\.\d*\|\.\d\+\)\([eE][-+]\=\d\+\)\=[fFdD]\="
191 syn match javaNumber "\<\d\+[eE][-+]\=\d\+[fFdD]\=\>"
192 syn match javaNumber "\<\d\+\([eE][-+]\=\d\+\)\=[fFdD]\>"
195 syn match javaSpecial "\\u\d\{4\}"
197 syn cluster javaTop add=javaString,javaCharacter,javaNumber,javaSpecial,javaStringError
199 if exists("java_highlight_functions")
200 if java_highlight_functions == "indent"
201 syn match javaFuncDef "^\(\t\| \{8\}\)[_$a-zA-Z][_$a-zA-Z0-9_. \[\]]*([^-+*/()]*)" contains=javaScopeDecl,javaType,javaStorageClass,@javaClasses
202 syn region javaFuncDef start=+^\(\t\| \{8\}\)[$_a-zA-Z][$_a-zA-Z0-9_. \[\]]*([^-+*/()]*,\s*+ end=+)+ contains=javaScopeDecl,javaType,javaStorageClass,@javaClasses
203 syn match javaFuncDef "^ [$_a-zA-Z][$_a-zA-Z0-9_. \[\]]*([^-+*/()]*)" contains=javaScopeDecl,javaType,javaStorageClass,@javaClasses
204 syn region javaFuncDef start=+^ [$_a-zA-Z][$_a-zA-Z0-9_. \[\]]*([^-+*/()]*,\s*+ end=+)+ contains=javaScopeDecl,javaType,javaStorageClass,@javaClasses
206 " This line catches method declarations at any indentation>0, but it assumes
208 " 1. class names are always capitalized (ie: Button)
209 " 2. method names are never capitalized (except constructors, of course)
210 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
212 syn match javaBraces "[{}]"
213 syn cluster javaTop add=javaFuncDef,javaBraces
216 if exists("java_highlight_debug")
218 " Strings and constants
219 syn match javaDebugSpecial contained "\\\d\d\d\|\\."
220 syn region javaDebugString contained start=+"+ end=+"+ contains=javaDebugSpecial
221 syn match javaDebugStringError +"\([^"\\]\|\\.\)*$+
222 syn match javaDebugCharacter contained "'[^\\]'"
223 syn match javaDebugSpecialCharacter contained "'\\.'"
224 syn match javaDebugSpecialCharacter contained "'\\''"
225 syn match javaDebugNumber contained "\<\(0[0-7]*\|0[xX]\x\+\|\d\+\)[lL]\=\>"
226 syn match javaDebugNumber contained "\(\<\d\+\.\d*\|\.\d\+\)\([eE][-+]\=\d\+\)\=[fFdD]\="
227 syn match javaDebugNumber contained "\<\d\+[eE][-+]\=\d\+[fFdD]\=\>"
228 syn match javaDebugNumber contained "\<\d\+\([eE][-+]\=\d\+\)\=[fFdD]\>"
229 syn keyword javaDebugBoolean contained true false
230 syn keyword javaDebugType contained null this super
231 syn region javaDebugParen start=+(+ end=+)+ contained contains=javaDebug.*,javaDebugParen
233 " to make this work you must define the highlighting for these groups
234 syn match javaDebug "\<System\.\(out\|err\)\.print\(ln\)*\s*("me=e-1 contains=javaDebug.* nextgroup=javaDebugParen
235 syn match javaDebug "\<p\s*("me=e-1 contains=javaDebug.* nextgroup=javaDebugParen
236 syn match javaDebug "[A-Za-z][a-zA-Z0-9_]*\.printStackTrace\s*("me=e-1 contains=javaDebug.* nextgroup=javaDebugParen
237 syn match javaDebug "\<trace[SL]\=\s*("me=e-1 contains=javaDebug.* nextgroup=javaDebugParen
239 syn cluster javaTop add=javaDebug
241 if version >= 508 || !exists("did_c_syn_inits")
242 JavaHiLink javaDebug Debug
243 JavaHiLink javaDebugString DebugString
244 JavaHiLink javaDebugStringError javaError
245 JavaHiLink javaDebugType DebugType
246 JavaHiLink javaDebugBoolean DebugBoolean
247 JavaHiLink javaDebugNumber Debug
248 JavaHiLink javaDebugSpecial DebugSpecial
249 JavaHiLink javaDebugSpecialCharacter DebugSpecial
250 JavaHiLink javaDebugCharacter DebugString
251 JavaHiLink javaDebugParen Debug
253 JavaHiLink DebugString String
254 JavaHiLink DebugSpecial Special
255 JavaHiLink DebugBoolean Boolean
256 JavaHiLink DebugType Type
260 if exists("java_mark_braces_in_parens_as_errors")
261 syn match javaInParen contained "[{}]"
262 JavaHiLink javaInParen javaError
263 syn cluster javaTop add=javaInParen
266 " catch errors caused by wrong parenthesis
267 syn region javaParenT transparent matchgroup=javaParen start="(" end=")" contains=@javaTop,javaParenT1
268 syn region javaParenT1 transparent matchgroup=javaParen1 start="(" end=")" contains=@javaTop,javaParenT2 contained
269 syn region javaParenT2 transparent matchgroup=javaParen2 start="(" end=")" contains=@javaTop,javaParenT contained
270 syn match javaParenError ")"
271 " catch errors caused by wrong square parenthesis
272 syn region javaParenT transparent matchgroup=javaParen start="\[" end="\]" contains=@javaTop,javaParenT1
273 syn region javaParenT1 transparent matchgroup=javaParen1 start="\[" end="\]" contains=@javaTop,javaParenT2 contained
274 syn region javaParenT2 transparent matchgroup=javaParen2 start="\[" end="\]" contains=@javaTop,javaParenT contained
275 syn match javaParenError "\]"
277 JavaHiLink javaParenError javaError
279 if !exists("java_minlines")
280 let java_minlines = 10
282 exec "syn sync ccomment javaComment minlines=" . java_minlines
284 " The default highlighting.
285 if version >= 508 || !exists("did_java_syn_inits")
287 let did_java_syn_inits = 1
289 JavaHiLink javaFuncDef Function
290 JavaHiLink javaVarArg Function
291 JavaHiLink javaBraces Function
292 JavaHiLink javaBranch Conditional
293 JavaHiLink javaUserLabelRef javaUserLabel
294 JavaHiLink javaLabel Label
295 JavaHiLink javaUserLabel Label
296 JavaHiLink javaConditional Conditional
297 JavaHiLink javaRepeat Repeat
298 JavaHiLink javaExceptions Exception
299 JavaHiLink javaAssert Statement
300 JavaHiLink javaStorageClass StorageClass
301 JavaHiLink javaMethodDecl javaStorageClass
302 JavaHiLink javaClassDecl javaStorageClass
303 JavaHiLink javaScopeDecl javaStorageClass
304 JavaHiLink javaBoolean Boolean
305 JavaHiLink javaSpecial Special
306 JavaHiLink javaSpecialError Error
307 JavaHiLink javaSpecialCharError Error
308 JavaHiLink javaString String
309 JavaHiLink javaCharacter Character
310 JavaHiLink javaSpecialChar SpecialChar
311 JavaHiLink javaNumber Number
312 JavaHiLink javaError Error
313 JavaHiLink javaStringError Error
314 JavaHiLink javaStatement Statement
315 JavaHiLink javaOperator Operator
316 JavaHiLink javaComment Comment
317 JavaHiLink javaDocComment Comment
318 JavaHiLink javaLineComment Comment
319 JavaHiLink javaConstant Constant
320 JavaHiLink javaTypedef Typedef
321 JavaHiLink javaTodo Todo
322 JavaHiLink javaAnnotation PreProc
324 JavaHiLink javaCommentTitle SpecialComment
325 JavaHiLink javaDocTags Special
326 JavaHiLink javaDocParam Function
327 JavaHiLink javaDocSeeTagParam Function
328 JavaHiLink javaCommentStar javaComment
330 JavaHiLink javaType Type
331 JavaHiLink javaExternal Include
333 JavaHiLink htmlComment Special
334 JavaHiLink htmlCommentPart Special
335 JavaHiLink javaSpaceError Error
338 delcommand JavaHiLink
340 let b:current_syntax = "java"
342 if main_syntax == 'java'
346 let b:spell_options="contained"