summaryrefslogtreecommitdiffstats
path: root/runtime/syntax/java.vim
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--runtime/syntax/java.vim253
1 files changed, 165 insertions, 88 deletions
diff --git a/runtime/syntax/java.vim b/runtime/syntax/java.vim
index 8ffe9e0..6f64bdc 100644
--- a/runtime/syntax/java.vim
+++ b/runtime/syntax/java.vim
@@ -3,7 +3,7 @@
" Maintainer: Aliaksei Budavei <0x000c70 AT gmail DOT com>
" Former Maintainer: Claudio Fleiner <claudio@fleiner.com>
" Repository: https://github.com/zzzyxwvut/java-vim.git
-" Last Change: 2024 Apr 22
+" Last Change: 2024 Jun 15
" Please check :help java.vim for comments on some of the options available.
@@ -14,14 +14,50 @@ if !exists("main_syntax")
endif
" we define it here so that included files can test for it
let main_syntax='java'
- syn region javaFold start="{" end="}" transparent fold
endif
let s:cpo_save = &cpo
set cpo&vim
+"""" STRIVE TO REMAIN COMPATIBLE FOR AT LEAST VIM 7.0.
+let s:ff = {}
+
+function! s:ff.LeftConstant(x, y) abort
+ return a:x
+endfunction
+
+function! s:ff.RightConstant(x, y) abort
+ return a:y
+endfunction
+
+if !exists("*s:ReportOnce")
+ function s:ReportOnce(message) abort
+ echomsg 'syntax/java.vim: ' . a:message
+ endfunction
+else
+ function! s:ReportOnce(dummy)
+ endfunction
+endif
+
+function! JavaSyntaxFoldTextExpr() abort
+ return getline(v:foldstart) !~ '/\*\+\s*$'
+ \ ? foldtext()
+ \ : printf('+-%s%3d lines: ',
+ \ v:folddashes,
+ \ (v:foldend - v:foldstart + 1)) .
+ \ getline(v:foldstart + 1)
+endfunction
+
+" E120 for "fdt=s:JavaSyntaxFoldTextExpr()" before v8.2.3900.
+setlocal foldtext=JavaSyntaxFoldTextExpr()
+
" Admit the ASCII dollar sign to keyword characters (JLS-17, §3.8):
-execute printf('syntax iskeyword %s,$', &l:iskeyword)
+try
+ exec 'syntax iskeyword ' . &l:iskeyword . ',$'
+catch /\<E410:/
+ call s:ReportOnce(v:exception)
+ setlocal iskeyword+=$
+endtry
" some characters that cannot be in a java program (outside a string)
syn match javaError "[\\@`]"
@@ -42,6 +78,7 @@ syn keyword javaConstant null
syn keyword javaTypedef this super
syn keyword javaOperator new instanceof
syn match javaOperator "\<var\>\%(\s*(\)\@!"
+
" Since the yield statement, which could take a parenthesised operand,
" and _qualified_ yield methods get along within the switch block
" (JLS-17, §3.8), it seems futile to make a region definition for this
@@ -49,7 +86,15 @@ syn match javaOperator "\<var\>\%(\s*(\)\@!"
" backtrack (arbitrarily) 80 bytes, at most, on the matched line and,
" if necessary, on the line before that (h: \@<=), trying to match
" neither a method reference nor a qualified method invocation.
-syn match javaOperator "\%(\%(::\|\.\)[[:space:]\n]*\)\@80<!\<yield\>"
+try
+ syn match javaOperator "\%(\%(::\|\.\)[[:space:]\n]*\)\@80<!\<yield\>"
+ let s:ff.Peek = s:ff.LeftConstant
+catch /\<E59:/
+ call s:ReportOnce(v:exception)
+ syn match javaOperator "\%(\%(::\|\.\)[[:space:]\n]*\)\@<!\<yield\>"
+ let s:ff.Peek = s:ff.RightConstant
+endtry
+
syn keyword javaType boolean char byte short int long float double
syn keyword javaType void
syn keyword javaStatement return
@@ -65,6 +110,7 @@ syn match javaClassDecl "\<record\>\%(\s*(\)\@!"
syn match javaClassDecl "^class\>"
syn match javaClassDecl "[^.]\s*\<class\>"ms=s+1
syn match javaAnnotation "@\%(\K\k*\.\)*\K\k*\>"
+syn region javaAnnotation transparent matchgroup=javaAnnotationStart start=/@\%(\K\k*\.\)*\K\k*(/ end=/)/ skip=/\/\*.\{-}\*\/\|\/\/.*$/ contains=javaAnnotation,javaParenT,javaBlock,javaString,javaBoolean,javaNumber,javaTypedef,javaComment,javaLineComment
syn match javaClassDecl "@interface\>"
syn keyword javaBranch break continue nextgroup=javaUserLabelRef skipwhite
syn match javaUserLabelRef "\k\+" contained
@@ -82,15 +128,22 @@ syn match javaConceptKind "\<default\>\%(\s*\%(:\|->\)\)\@!"
" ".java\=" extension used for a production version and an arbitrary
" extension used for a testing version.
let s:module_info_cur_buf = fnamemodify(bufname("%"), ":t") =~ '^module-info\%(\.class\>\)\@!'
-let s:selectable_regexp_engine = !(v:version < 704)
-lockvar s:selectable_regexp_engine s:module_info_cur_buf
+lockvar s:module_info_cur_buf
+
+if !(v:version < 704)
+ " Request the new regexp engine for [:upper:] and [:lower:].
+ let [s:ff.Engine, s:ff.UpperCase, s:ff.LowerCase] = repeat([s:ff.LeftConstant], 3)
+else
+ " XXX: \C\<[^a-z0-9]\k*\> rejects "type", but matches "τύπος".
+ " XXX: \C\<[^A-Z0-9]\k*\> rejects "Method", but matches "Μέθοδος".
+ let [s:ff.Engine, s:ff.UpperCase, s:ff.LowerCase] = repeat([s:ff.RightConstant], 3)
+endif
" Java modules (since Java 9, for "module-info.java" file).
if s:module_info_cur_buf
syn keyword javaModuleStorageClass module transitive
syn keyword javaModuleStmt open requires exports opens uses provides
syn keyword javaModuleExternal to with
- syn cluster javaTop add=javaModuleStorageClass,javaModuleStmt,javaModuleExternal
endif
if exists("java_highlight_java_lang_ids")
@@ -107,36 +160,32 @@ if exists("java_highlight_all") || exists("java_highlight_java") || exists("ja
" the current keyword placement. The below _match_es follow suit.
syn keyword javaR_JavaLang ArithmeticException ArrayIndexOutOfBoundsException ArrayStoreException ClassCastException IllegalArgumentException IllegalMonitorStateException IllegalThreadStateException IndexOutOfBoundsException NegativeArraySizeException NullPointerException NumberFormatException RuntimeException SecurityException StringIndexOutOfBoundsException IllegalStateException UnsupportedOperationException EnumConstantNotPresentException TypeNotPresentException IllegalCallerException LayerInstantiationException WrongThreadException MatchException
- syn cluster javaTop add=javaR_JavaLang
syn cluster javaClasses add=javaR_JavaLang
hi def link javaR_JavaLang javaR_Java
" Member enumerations:
- syn match javaC_JavaLang "\%(\<Thread\.\)\@<=\<State\>"
- syn match javaC_JavaLang "\%(\<Character\.\)\@<=\<UnicodeScript\>"
- syn match javaC_JavaLang "\%(\<ProcessBuilder\.Redirect\.\)\@<=\<Type\>"
- syn match javaC_JavaLang "\%(\<StackWalker\.\)\@<=\<Option\>"
- syn match javaC_JavaLang "\%(\<System\.Logger\.\)\@<=\<Level\>"
+ exec 'syn match javaC_JavaLang "\%(\<Thread\.\)\@' . s:ff.Peek('7', '') . '<=\<State\>"'
+ exec 'syn match javaC_JavaLang "\%(\<Character\.\)\@' . s:ff.Peek('10', '') . '<=\<UnicodeScript\>"'
+ exec 'syn match javaC_JavaLang "\%(\<ProcessBuilder\.Redirect\.\)\@' . s:ff.Peek('24', '') . '<=\<Type\>"'
+ exec 'syn match javaC_JavaLang "\%(\<StackWalker\.\)\@' . s:ff.Peek('12', '') . '<=\<Option\>"'
+ exec 'syn match javaC_JavaLang "\%(\<System\.Logger\.\)\@' . s:ff.Peek('14', '') . '<=\<Level\>"'
" Member classes:
- syn match javaC_JavaLang "\%(\<Character\.\)\@<=\<Subset\>"
- syn match javaC_JavaLang "\%(\<Character\.\)\@<=\<UnicodeBlock\>"
- syn match javaC_JavaLang "\%(\<ProcessBuilder\.\)\@<=\<Redirect\>"
- syn match javaC_JavaLang "\%(\<ModuleLayer\.\)\@<=\<Controller\>"
- syn match javaC_JavaLang "\%(\<Runtime\.\)\@<=\<Version\>"
- syn match javaC_JavaLang "\%(\<System\.\)\@<=\<LoggerFinder\>"
- syn match javaC_JavaLang "\%(\<Enum\.\)\@<=\<EnumDesc\>"
+ exec 'syn match javaC_JavaLang "\%(\<Character\.\)\@' . s:ff.Peek('10', '') . '<=\<Subset\>"'
+ exec 'syn match javaC_JavaLang "\%(\<Character\.\)\@' . s:ff.Peek('10', '') . '<=\<UnicodeBlock\>"'
+ exec 'syn match javaC_JavaLang "\%(\<ProcessBuilder\.\)\@' . s:ff.Peek('15', '') . '<=\<Redirect\>"'
+ exec 'syn match javaC_JavaLang "\%(\<ModuleLayer\.\)\@' . s:ff.Peek('12', '') . '<=\<Controller\>"'
+ exec 'syn match javaC_JavaLang "\%(\<Runtime\.\)\@' . s:ff.Peek('8', '') . '<=\<Version\>"'
+ exec 'syn match javaC_JavaLang "\%(\<System\.\)\@' . s:ff.Peek('7', '') . '<=\<LoggerFinder\>"'
+ exec 'syn match javaC_JavaLang "\%(\<Enum\.\)\@' . s:ff.Peek('5', '') . '<=\<EnumDesc\>"'
syn keyword javaC_JavaLang Boolean Character Class ClassLoader Compiler Double Float Integer Long Math Number Object Process Runtime SecurityManager String StringBuffer Thread ThreadGroup Byte Short Void InheritableThreadLocal Package RuntimePermission ThreadLocal StrictMath StackTraceElement Enum ProcessBuilder StringBuilder ClassValue Module ModuleLayer StackWalker Record
syn match javaC_JavaLang "\<System\>" " See javaDebug.
" As of JDK 21, java.lang.Compiler is no more (deprecated in JDK 9).
syn keyword javaLangDeprecated Compiler
- syn cluster javaTop add=javaC_JavaLang
syn cluster javaClasses add=javaC_JavaLang
hi def link javaC_JavaLang javaC_Java
syn keyword javaE_JavaLang AbstractMethodError ClassCircularityError ClassFormatError Error IllegalAccessError IncompatibleClassChangeError InstantiationError InternalError LinkageError NoClassDefFoundError NoSuchFieldError NoSuchMethodError OutOfMemoryError StackOverflowError ThreadDeath UnknownError UnsatisfiedLinkError VerifyError VirtualMachineError ExceptionInInitializerError UnsupportedClassVersionError AssertionError BootstrapMethodError
- syn cluster javaTop add=javaE_JavaLang
syn cluster javaClasses add=javaE_JavaLang
hi def link javaE_JavaLang javaE_Java
syn keyword javaX_JavaLang ClassNotFoundException CloneNotSupportedException Exception IllegalAccessException InstantiationException InterruptedException NoSuchMethodException Throwable NoSuchFieldException ReflectiveOperationException
- syn cluster javaTop add=javaX_JavaLang
syn cluster javaClasses add=javaX_JavaLang
hi def link javaX_JavaLang javaX_Java
@@ -152,10 +201,9 @@ if exists("java_highlight_all") || exists("java_highlight_java") || exists("ja
syn keyword javaLangObject clone equals finalize getClass hashCode
syn keyword javaLangObject notify notifyAll toString wait
hi def link javaLangObject javaConstant
- syn cluster javaTop add=javaLangObject
endif
-if filereadable(expand("<sfile>:p:h")."/javaid.vim")
+if filereadable(expand("<sfile>:p:h") . "/javaid.vim")
source <sfile>:p:h/javaid.vim
endif
@@ -168,7 +216,7 @@ if exists("java_space_errors")
endif
endif
-syn match javaUserLabel "^\s*\<\K\k*\>\%(\<default\>\)\@<!\s*:"he=e-1
+exec 'syn match javaUserLabel "^\s*\<\K\k*\>\%(\<default\>\)\@' . s:ff.Peek('7', '') . '<!\s*:"he=e-1'
syn region javaLabelRegion transparent matchgroup=javaLabel start="\<case\>" matchgroup=NONE end=":\|->" contains=javaLabelCastType,javaLabelNumber,javaCharacter,javaString,javaConstant,@javaClasses,javaLabelDefault,javaLabelVarType,javaLabelWhenClause
syn region javaLabelRegion transparent matchgroup=javaLabel start="\<default\>\%(\s*\%(:\|->\)\)\@=" matchgroup=NONE end=":\|->" oneline
" Consider grouped _default_ _case_ labels, i.e.
@@ -179,7 +227,7 @@ syn keyword javaLabelVarType contained var
syn keyword javaLabelCastType contained char byte short int
" Allow for the contingency of the enclosing region not being able to
" _keep_ its _end_, e.g. case ':':.
-syn region javaLabelWhenClause contained transparent matchgroup=javaLabel start="\<when\>" matchgroup=NONE end=":"me=e-1 end="->"me=e-2 contains=TOP,javaExternal
+syn region javaLabelWhenClause contained transparent matchgroup=javaLabel start="\<when\>" matchgroup=NONE end=":"me=e-1 end="->"me=e-2 contains=TOP,javaExternal,javaLambdaDef
syn match javaLabelNumber contained "\<0\>[lL]\@!"
syn match javaLabelNumber contained "\<\%(0\%([xX]\x\%(_*\x\)*\|_*\o\%(_*\o\)*\|[bB][01]\%(_*[01]\)*\)\|[1-9]\%(_*\d\)*\)\>[lL]\@!"
hi def link javaLabelDefault javaLabel
@@ -187,13 +235,6 @@ hi def link javaLabelVarType javaOperator
hi def link javaLabelNumber javaNumber
hi def link javaLabelCastType javaType
-" highlighting C++ keywords as errors removed, too many people find it
-" annoying. Was: if !exists("java_allow_cpp_keywords")
-
-" The following cluster contains all java groups except the contained ones
-syn cluster javaTop add=javaExternal,javaError,javaBranch,javaLabelRegion,javaConditional,javaRepeat,javaBoolean,javaConstant,javaTypedef,javaOperator,javaType,javaStatement,javaStorageClass,javaAssert,javaExceptions,javaMethodDecl,javaClassDecl,javaScopeDecl,javaConceptKind,javaError2,javaUserLabel,javaLangObject,javaAnnotation,javaVarArg
-
-
" Comments
syn keyword javaTodo contained TODO FIXME XXX
@@ -208,13 +249,13 @@ if exists("java_comment_strings")
syn cluster javaCommentSpecial2 add=javaComment2String,javaCommentCharacter,javaNumber,javaStrTempl
endif
-syn region javaComment matchgroup=javaCommentStart start="/\*" end="\*/" contains=@javaCommentSpecial,javaTodo,javaCommentError,javaSpaceError,@Spell
+syn region javaComment matchgroup=javaCommentStart start="/\*" end="\*/" contains=@javaCommentSpecial,javaTodo,javaCommentError,javaSpaceError,@Spell fold
syn match javaCommentStar contained "^\s*\*[^/]"me=e-1
syn match javaCommentStar contained "^\s*\*$"
syn match javaLineComment "//.*" contains=@javaCommentSpecial2,javaTodo,javaCommentMarkupTag,javaSpaceError,@Spell
syn match javaCommentMarkupTag contained "@\%(end\|highlight\|link\|replace\|start\)\>" nextgroup=javaCommentMarkupTagAttr,javaSpaceError skipwhite
syn match javaCommentMarkupTagAttr contained "\<region\>" nextgroup=javaCommentMarkupTagAttr,javaSpaceError skipwhite
-syn region javaCommentMarkupTagAttr contained transparent matchgroup=htmlArg start=/\<\%(re\%(gex\|gion\|placement\)\|substring\|t\%(arget\|ype\)\)\%(\s*=\)\@=/ matchgroup=htmlString end=/\%(=\s*\)\@<=\%("[^"]\+"\|'[^']\+'\|\%([.-]\|\k\)\+\)/ nextgroup=javaCommentMarkupTagAttr,javaSpaceError skipwhite oneline
+exec 'syn region javaCommentMarkupTagAttr contained transparent matchgroup=htmlArg start=/\<\%(re\%(gex\|gion\|placement\)\|substring\|t\%(arget\|ype\)\)\%(\s*=\)\@=/ matchgroup=htmlString end=/\%(=\s*\)\@' . s:ff.Peek('80', '') . '<=\%("[^"]\+"\|' . "\x27[^\x27]\\+\x27" . '\|\%([.-]\|\k\)\+\)/ nextgroup=javaCommentMarkupTagAttr,javaSpaceError skipwhite oneline'
hi def link javaCommentMarkupTagAttr htmlArg
hi def link javaCommentString javaString
hi def link javaComment2String javaString
@@ -223,20 +264,24 @@ syn match javaCommentError contained "/\*"me=e-1 display
hi def link javaCommentError javaError
hi def link javaCommentStart javaComment
-syn cluster javaTop add=javaComment,javaLineComment
-
if !exists("java_ignore_javadoc") && main_syntax != 'jsp'
syntax case ignore
- " syntax coloring for javadoc comments (HTML)
+
+ " Include HTML syntax coloring for Javadoc comments.
syntax include @javaHtml syntax/html.vim
unlet b:current_syntax
- " HTML enables spell checking for all text that is not in a syntax item. This
- " is wrong for Java (all identifiers would be spell-checked), so it's undone
- " here.
- syntax spell default
- syn region javaDocComment start="/\*\*" end="\*/" keepend contains=javaCommentTitle,@javaHtml,javaDocTags,javaDocSeeTag,javaDocCodeTag,javaDocSnippetTag,javaTodo,javaCommentError,javaSpaceError,@Spell
- syn region javaCommentTitle contained matchgroup=javaDocComment start="/\*\*" matchgroup=javaCommentTitle end="\.$" end="\.[ \t\r]\@=" end="\%(^\s*\**\s*\)\@<=@"me=s-2,he=s-1 end="\*/"me=s-1,he=s-1 contains=@javaHtml,javaCommentStar,javaTodo,javaCommentError,javaSpaceError,@Spell,javaDocTags,javaDocSeeTag,javaDocCodeTag,javaDocSnippetTag
+ " HTML enables spell checking for all text that is not in a syntax
+ " item (:syntax spell toplevel); instead, limit spell checking to
+ " items matchable with syntax groups containing the @Spell cluster.
+ try
+ syntax spell default
+ catch /\<E390:/
+ call s:ReportOnce(v:exception)
+ endtry
+
+ syn region javaDocComment start="/\*\*" end="\*/" keepend contains=javaCommentTitle,@javaHtml,javaDocTags,javaDocSeeTag,javaDocCodeTag,javaDocSnippetTag,javaTodo,javaCommentError,javaSpaceError,@Spell fold
+ exec 'syn region javaCommentTitle contained matchgroup=javaDocComment start="/\*\*" matchgroup=javaCommentTitle end="\.$" end="\.[ \t\r]\@=" end="\%(^\s*\**\s*\)\@' . s:ff.Peek('80', '') . '<=@"me=s-2,he=s-1 end="\*/"me=s-1,he=s-1 contains=@javaHtml,javaCommentStar,javaTodo,javaCommentError,javaSpaceError,@Spell,javaDocTags,javaDocSeeTag,javaDocCodeTag,javaDocSnippetTag'
syn region javaCommentTitle contained matchgroup=javaDocComment start="/\*\*\s*\r\=\n\=\s*\**\s*\%({@return\>\)\@=" matchgroup=javaCommentTitle end="}\%(\s*\.*\)*" contains=@javaHtml,javaCommentStar,javaTodo,javaCommentError,javaSpaceError,@Spell,javaDocTags,javaDocSeeTag,javaDocCodeTag,javaDocSnippetTag
syn region javaDocTags contained start="{@\%(li\%(teral\|nk\%(plain\)\=\)\|inherit[Dd]oc\|doc[rR]oot\|value\)\>" end="}"
syn match javaDocTags contained "@\%(param\|exception\|throws\|since\)\s\+\S\+" contains=javaDocParam
@@ -246,9 +291,10 @@ if !exists("java_ignore_javadoc") && main_syntax != 'jsp'
syn match javaDocSeeTagParam contained @"\_[^"]\+"\|<a\s\+\_.\{-}</a>\|\%(\k\|\.\)*\%(#\k\+\%((\_[^)]*)\)\=\)\=@ contains=@javaHtml extend
syn region javaCodeSkipBlock contained transparent start="{\%(@code\>\)\@!" end="}" contains=javaCodeSkipBlock,javaDocCodeTag
syn region javaDocCodeTag contained start="{@code\>" end="}" contains=javaDocCodeTag,javaCodeSkipBlock
- syn region javaDocSnippetTagAttr contained transparent matchgroup=htmlArg start=/\<\%(class\|file\|id\|lang\|region\)\%(\s*=\)\@=/ matchgroup=htmlString end=/:$/ end=/\%(=\s*\)\@<=\%("[^"]\+"\|'[^']\+'\|\%([.\\/-]\|\k\)\+\)/ nextgroup=javaDocSnippetTagAttr skipwhite skipnl
+ exec 'syn region javaDocSnippetTagAttr contained transparent matchgroup=htmlArg start=/\<\%(class\|file\|id\|lang\|region\)\%(\s*=\)\@=/ matchgroup=htmlString end=/:$/ end=/\%(=\s*\)\@' . s:ff.Peek('80', '') . '<=\%("[^"]\+"\|' . "\x27[^\x27]\\+\x27" . '\|\%([.\\/-]\|\k\)\+\)/ nextgroup=javaDocSnippetTagAttr skipwhite skipnl'
syn region javaSnippetSkipBlock contained transparent start="{\%(@snippet\>\)\@!" end="}" contains=javaSnippetSkipBlock,javaDocSnippetTag,javaCommentMarkupTag
syn region javaDocSnippetTag contained start="{@snippet\>" end="}" contains=javaDocSnippetTag,javaSnippetSkipBlock,javaDocSnippetTagAttr,javaCommentMarkupTag
+
syntax case match
endif
@@ -264,10 +310,8 @@ syn region javaString start=+"+ end=+"+ end=+$+ contains=javaSpecialChar,javaS
syn region javaString start=+"""[ \t\x0c\r]*$+hs=e+1 end=+"""+he=s-1 contains=javaSpecialChar,javaSpecialError,javaTextBlockError,@Spell
syn match javaTextBlockError +"""\s*"""+
syn region javaStrTemplEmbExp contained matchgroup=javaStrTempl start="\\{" end="}" contains=TOP
-syn region javaStrTempl start=+\%(\.[[:space:]\n]*\)\@<="+ end=+"+ contains=javaStrTemplEmbExp,javaSpecialChar,javaSpecialError,@Spell
-syn region javaStrTempl start=+\%(\.[[:space:]\n]*\)\@<="""[ \t\x0c\r]*$+hs=e+1 end=+"""+he=s-1 contains=javaStrTemplEmbExp,javaSpecialChar,javaSpecialError,javaTextBlockError,@Spell
-" The next line is commented out, it can cause a crash for a long line
-"syn match javaStringError +"\%([^"\\]\|\\.\)*$+
+exec 'syn region javaStrTempl start=+\%(\.[[:space:]\n]*\)\@' . s:ff.Peek('80', '') . '<="+ end=+"+ contains=javaStrTemplEmbExp,javaSpecialChar,javaSpecialError,@Spell'
+exec 'syn region javaStrTempl start=+\%(\.[[:space:]\n]*\)\@' . s:ff.Peek('80', '') . '<="""[ \t\x0c\r]*$+hs=e+1 end=+"""+he=s-1 contains=javaStrTemplEmbExp,javaSpecialChar,javaSpecialError,javaTextBlockError,@Spell'
syn match javaCharacter "'[^']*'" contains=javaSpecialChar,javaSpecialCharError
syn match javaCharacter "'\\''" contains=javaSpecialChar
syn match javaCharacter "'[^\\]'"
@@ -286,36 +330,35 @@ syn match javaNumber "\<0[xX]\%(\x\%(_*\x\)*\.\=\|\%(\x\%(_*\x\)*\)\=\.\x\%(
" Unicode characters
syn match javaSpecial "\\u\x\x\x\x"
-syn cluster javaTop add=javaString,javaStrTempl,javaCharacter,javaNumber,javaSpecial,javaStringError,javaTextBlockError
-
" Method declarations (JLS-17, §8.4.3, §8.4.4, §9.4).
if exists("java_highlight_functions")
syn cluster javaFuncParams contains=javaAnnotation,@javaClasses,javaType,javaVarArg,javaComment,javaLineComment
- if java_highlight_functions == "indent"
+ if java_highlight_functions =~# '^indent[1-8]\=$'
+ let s:last = java_highlight_functions[-1 :]
+ let s:indent = s:last != 't' ? repeat("\x20", s:last) : "\t"
syn cluster javaFuncParams add=javaScopeDecl,javaConceptKind,javaStorageClass,javaExternal
- syn match javaFuncDef "^\%(\t\| \%( \{6\}\)\=\)\K\%(\k\|[ .,<>\[\]]\)*([^-+*/]*)" contains=@javaFuncParams
- syn region javaFuncDef start=+^\%(\t\| \%( \{6\}\)\=\)\K\%(\k\|[ .,<>\[\]]\)*([^-+*/]*,\s*+ end=+)+ contains=@javaFuncParams
+ " Try to not match other type members, initialiser blocks, enum
+ " constants (JLS-17, §8.9.1), and constructors (JLS-17, §8.1.7):
+ " at any _conventional_ indentation, skip over all fields with
+ " "[^=]*", all records with "\<record\s", and let the "*Skip*"
+ " definitions take care of constructor declarations and enum
+ " constants (with no support for @Foo(value = "bar")).
+ exec 'syn region javaFuncDef start=+^' . s:indent . '\%(<[^>]\+>\+\s\+\|\%(\%(@\%(\K\k*\.\)*\K\k*\>\)\s\+\)\+\)\=\%(\<\K\k*\>\.\)*\K\k*\>[^=]*\%(\<record\)\@' . s:ff.Peek('6', '') . '<!\s\K\k*\s*(+ end=+)+ contains=@javaFuncParams'
+ " As long as package-private constructors cannot be matched with
+ " javaFuncDef, do not look with javaConstructorSkipDeclarator for
+ " them.
+ exec 'syn match javaConstructorSkipDeclarator transparent +^' . s:indent . '\%(\%(@\%(\K\k*\.\)*\K\k*\>\)\s\+\)*p\%(ublic\|rotected\|rivate\)\s\+\%(<[^>]\+>\+\s\+\)\=\K\k*\s*\ze(+ contains=javaAnnotation,javaScopeDecl'
+ exec 'syn match javaEnumSkipArgumentativeConstant transparent +^' . s:indent . '\%(\%(@\%(\K\k*\.\)*\K\k*\>\)\s\+\)*\K\k*\s*\ze(+ contains=javaAnnotation'
+ unlet s:indent s:last
else
" This is the "style" variant (:help ft-java-syntax).
syn cluster javaFuncParams add=javaScopeDecl,javaConceptKind,javaStorageClass,javaExternal
" Match arbitrarily indented camelCasedName method declarations.
" Match: [@ɐ] [abstract] [<α, β>] Τʬ[<γ>][[][]] μʭʭ(/* ... */);
-
- if s:selectable_regexp_engine
- " Request the new regexp engine for [:upper:] and [:lower:].
- syn region javaFuncDef start=/\%#=2^\s\+\%(\%(@\%(\K\k*\.\)*\K\k*\>\)\s\+\)*\%(p\%(ublic\|rotected\|rivate\)\s\+\)\=\%(\%(abstract\|default\)\s\+\|\%(\%(final\|\%(native\|strictfp\)\|s\%(tatic\|ynchronized\)\)\s\+\)*\)\=\%(<.*[[:space:]-]\@1<!>\s\+\)\=\%(void\|\%(b\%(oolean\|yte\)\|char\|short\|int\|long\|float\|double\|\%(\<\K\k*\>\.\)*\<[$_[:upper:]]\k*\>\%(<[^(){}]*[[:space:]-]\@1<!>\)\=\)\%(\[\]\)*\)\s\+\<[$_[:lower:]]\k*\>\s*(/ end=/)/ skip=/\/\*.\{-}\*\/\|\/\/.*$/ contains=@javaFuncParams
- else
- " XXX: \C\<[^a-z0-9]\k*\> rejects "type", but matches "τύπος".
- " XXX: \C\<[^A-Z0-9]\k*\> rejects "Method", but matches "Μέθοδος".
- syn region javaFuncDef start=/^\s\+\%(\%(@\%(\K\k*\.\)*\K\k*\>\)\s\+\)*\%(p\%(ublic\|rotected\|rivate\)\s\+\)\=\%(\%(abstract\|default\)\s\+\|\%(\%(final\|\%(native\|strictfp\)\|s\%(tatic\|ynchronized\)\)\s\+\)*\)\=\%(<.*[[:space:]-]\@1<!>\s\+\)\=\%(void\|\%(b\%(oolean\|yte\)\|char\|short\|int\|long\|float\|double\|\%(\<\K\k*\>\.\)*\<[^a-z0-9]\k*\>\%(<[^(){}]*[[:space:]-]\@1<!>\)\=\)\%(\[\]\)*\)\s\+\<[^A-Z0-9]\k*\>\s*(/ end=/)/ skip=/\/\*.\{-}\*\/\|\/\/.*$/ contains=@javaFuncParams
- endif
+ exec 'syn region javaFuncDef start=/' . s:ff.Engine('\%#=2', '') . '^\s\+\%(\%(@\%(\K\k*\.\)*\K\k*\>\)\s\+\)*\%(p\%(ublic\|rotected\|rivate\)\s\+\)\=\%(\%(abstract\|default\)\s\+\|\%(\%(final\|\%(native\|strictfp\)\|s\%(tatic\|ynchronized\)\)\s\+\)*\)\=\%(<.*[[:space:]-]\@' . s:ff.Peek('1', '') . '<!>\s\+\)\=\%(void\|\%(b\%(oolean\|yte\)\|char\|short\|int\|long\|float\|double\|\%(\<\K\k*\>\.\)*\<' . s:ff.UpperCase('[$_[:upper:]]', '[^a-z0-9]') . '\k*\>\%(<[^(){}]*[[:space:]-]\@' . s:ff.Peek('1', '') . '<!>\)\=\)\%(\[\]\)*\)\s\+\<' . s:ff.LowerCase('[$_[:lower:]]', '[^A-Z0-9]') . '\k*\>\s*(/ end=/)/ skip=/\/\*.\{-}\*\/\|\/\/.*$/ contains=@javaFuncParams'
endif
-
- syn match javaLambdaDef "\<\K\k*\>\%(\<default\>\)\@<!\s*->"
- syn match javaBraces "[{}]"
- syn cluster javaTop add=javaFuncDef,javaBraces,javaLambdaDef
endif
if exists("java_highlight_debug")
@@ -326,10 +369,8 @@ if exists("java_highlight_debug")
" The highlight groups of java{StrTempl,Debug{,Paren,StrTempl}}\,
" share one colour by default. Do not conflate unrelated parens.
syn region javaDebugStrTemplEmbExp contained matchgroup=javaDebugStrTempl start="\\{" end="}" contains=javaComment,javaLineComment,javaDebug\%(Paren\)\@!.*
- syn region javaDebugStrTempl contained start=+\%(\.[[:space:]\n]*\)\@<="+ end=+"+ contains=javaDebugStrTemplEmbExp,javaDebugSpecial
- syn region javaDebugStrTempl contained start=+\%(\.[[:space:]\n]*\)\@<="""[ \t\x0c\r]*$+hs=e+1 end=+"""+he=s-1 contains=javaDebugStrTemplEmbExp,javaDebugSpecial,javaDebugTextBlockError
- " The next line is commented out, it can cause a crash for a long line
-" syn match javaDebugStringError contained +"\%([^"\\]\|\\.\)*$+
+ exec 'syn region javaDebugStrTempl contained start=+\%(\.[[:space:]\n]*\)\@' . s:ff.Peek('80', '') . '<="+ end=+"+ contains=javaDebugStrTemplEmbExp,javaDebugSpecial'
+ exec 'syn region javaDebugStrTempl contained start=+\%(\.[[:space:]\n]*\)\@' . s:ff.Peek('80', '') . '<="""[ \t\x0c\r]*$+hs=e+1 end=+"""+he=s-1 contains=javaDebugStrTemplEmbExp,javaDebugSpecial,javaDebugTextBlockError'
syn match javaDebugTextBlockError contained +"""\s*"""+
syn match javaDebugCharacter contained "'[^\\]'"
syn match javaDebugSpecialCharacter contained "'\\.'"
@@ -353,13 +394,10 @@ if exists("java_highlight_debug")
" FIXME: What API do "trace*" belong to?
" syn match javaDebug "\<trace[SL]\=\s*("me=e-1 contains=javaDebug.* nextgroup=javaDebugParen
- syn cluster javaTop add=javaDebug
-
hi def link javaDebug Debug
hi def link javaDebugString DebugString
hi def link javaDebugStrTempl Macro
- hi def link javaDebugStringError javaError
- hi def link javaDebugTextBlockError javaDebugStringError
+ hi def link javaDebugTextBlockError Error
hi def link javaDebugType DebugType
hi def link javaDebugBoolean DebugBoolean
hi def link javaDebugNumber Debug
@@ -375,15 +413,18 @@ if exists("java_highlight_debug")
endif
if exists("java_mark_braces_in_parens_as_errors")
- syn match javaInParen contained "[{}]"
- hi def link javaInParen javaError
- syn cluster javaTop add=javaInParen
+ syn match javaInParen contained "[{}]"
+ hi def link javaInParen javaError
endif
+" Try not to fold top-level-type bodies under assumption that there is
+" but one such body.
+exec 'syn region javaBlock transparent start="\%(^\|^\S[^:]\+\)\@' . s:ff.Peek('120', '') . '<!{" end="}" fold'
+
" catch errors caused by wrong parenthesis
-syn region javaParenT transparent matchgroup=javaParen start="(" end=")" contains=@javaTop,javaParenT1
-syn region javaParenT1 transparent matchgroup=javaParen1 start="(" end=")" contains=@javaTop,javaParenT2 contained
-syn region javaParenT2 transparent matchgroup=javaParen2 start="(" end=")" contains=@javaTop,javaParenT contained
+syn region javaParenT transparent matchgroup=javaParen start="(" end=")" contains=@javaTop,javaInParen,javaParenT1
+syn region javaParenT1 transparent matchgroup=javaParen1 start="(" end=")" contains=@javaTop,javaInParen,javaParenT2 contained
+syn region javaParenT2 transparent matchgroup=javaParen2 start="(" end=")" contains=@javaTop,javaInParen,javaParenT contained
syn match javaParenError ")"
" catch errors caused by wrong square parenthesis
syn region javaParenT transparent matchgroup=javaParen start="\[" end="\]" contains=@javaTop,javaParenT1
@@ -393,11 +434,32 @@ syn match javaParenError "\]"
hi def link javaParenError javaError
+" Lambda expressions (JLS-17, §15.27).
if exists("java_highlight_functions")
" Make ()-matching definitions after the parenthesis error catcher.
- syn match javaLambdaDef "\k\@4<!(\%(\k\|[[:space:]<>?\[\]@,.]\)*)\s*->"
+ "
+ " Match: ([@A [@B ...] final] var a[, var b, ...]) ->
+ " | ([@A [@B ...] final] T[<α>][[][]] a[, T b, ...]) ->
+ " There is no recognition of expressions interspersed with comments
+ " or of expressions whose parameterised parameter types are written
+ " across multiple lines.
+ exec 'syn match javaLambdaDef "\k\@' . s:ff.Peek('4', '') . '<!([[:space:]\n]*\%(\%(@\%(\K\k*\.\)*\K\k*\>\%((\_.\{-1,})\)\{-,1}[[:space:]\n]\+\)*\%(final[[:space:]\n]\+\)\=\%(\<\K\k*\>\.\)*\<\K\k*\>\%(<[^(){}]*[[:space:]-]\@' . s:ff.Peek('1', '') . '<!>\)\=\%(\%(\%(\[\]\)\+\|\.\.\.\)\)\=[[:space:]\n]\+\<\K\k*\>\%(\[\]\)*\%(,[[:space:]\n]*\)\=\)\+)[[:space:]\n]*->" contains=javaAnnotation,javaParamModifier,javaLambdaVarType,javaType,@javaClasses,javaVarArg'
+ " Match: () ->
+ " | (a[, b, ...]) ->
+ exec 'syn match javaLambdaDef "\k\@' . s:ff.Peek('4', '') . '<!([[:space:]\n]*\%(\<\K\k*\>\%(,[[:space:]\n]*\)\=\)*)[[:space:]\n]*->"'
+ " Match: a ->
+ exec 'syn match javaLambdaDef "\<\K\k*\>\%(\<default\>\)\@' . s:ff.Peek('7', '') . '<![[:space:]\n]*->"'
+
+ syn keyword javaParamModifier contained final
+ hi def link javaParamModifier javaConceptKind
+ syn keyword javaLambdaVarType contained var
+ hi def link javaLambdaVarType javaOperator
endif
+" The @javaTop cluster comprises non-contained Java syntax groups.
+" Note that the syntax file "aidl.vim" relies on its availability.
+syn cluster javaTop contains=TOP,javaDocComment,javaBlock,javaParenError,javaParenT
+
if !exists("java_minlines")
let java_minlines = 10
endif
@@ -414,7 +476,6 @@ exec "syn sync ccomment javaComment minlines=" . java_minlines
hi def link javaLambdaDef Function
hi def link javaFuncDef Function
hi def link javaVarArg Function
-hi def link javaBraces Function
hi def link javaBranch Conditional
hi def link javaUserLabelRef javaUserLabel
hi def link javaLabel Label
@@ -439,8 +500,7 @@ hi def link javaCharacter Character
hi def link javaSpecialChar SpecialChar
hi def link javaNumber Number
hi def link javaError Error
-hi def link javaStringError Error
-hi def link javaTextBlockError javaStringError
+hi def link javaTextBlockError Error
hi def link javaStatement Statement
hi def link javaOperator Operator
hi def link javaComment Comment
@@ -450,6 +510,7 @@ hi def link javaConstant Constant
hi def link javaTypedef Typedef
hi def link javaTodo Todo
hi def link javaAnnotation PreProc
+hi def link javaAnnotationStart javaAnnotation
hi def link javaCommentTitle SpecialComment
hi def link javaDocTags Special
@@ -482,6 +543,22 @@ endif
let b:spell_options = "contained"
let &cpo = s:cpo_save
-unlet s:selectable_regexp_engine s:module_info_cur_buf s:cpo_save
+unlet s:module_info_cur_buf s:ff s:cpo_save
+
+" See ":help vim9-mix".
+if !has("vim9script")
+ finish
+endif
-" vim: ts=8
+def! s:JavaSyntaxFoldTextExpr(): string
+ return getline(v:foldstart) !~ '/\*\+\s*$'
+ ? foldtext()
+ : printf('+-%s%3d lines: ',
+ v:folddashes,
+ (v:foldend - v:foldstart + 1)) ..
+ getline(v:foldstart + 1)
+enddef
+
+setlocal foldtext=s:JavaSyntaxFoldTextExpr()
+delfunction! g:JavaSyntaxFoldTextExpr
+" vim: sw=2 ts=8 noet sta