diff --git a/runtime/doc/syntax.txt b/runtime/doc/syntax.txt index afc230e112..3870b8d5f6 100644 --- a/runtime/doc/syntax.txt +++ b/runtime/doc/syntax.txt @@ -1542,7 +1542,7 @@ Check the javaid.vim file for a list of all the packages that are supported. Function names are not highlighted, as the way to find functions depends on how you write Java code. The syntax file knows two possible ways to highlight -functions: +headers of function declarations: If you write function declarations that are consistently indented by either a tab, or a space . . . or eight space character(s), you may want to set > @@ -1560,10 +1560,14 @@ However, if you follow the Java guidelines about how functions and classes are supposed to be named (with respect to upper- and lowercase) and there is any amount of indentation, you may want to set > :let java_highlight_functions="style" -If neither setting does work for you, but you would still want function -declarations to be highlighted, create your own definitions by changing the -definitions in java.vim or by creating your own java.vim that includes the -original one and then adds the code to highlight functions. +If neither setting does work for you, but you would still want headers of +function declarations to be highlighted, modify the current syntax definitions +or compose new ones. + +Higher-order function types can be hard to parse by eye, so uniformly toning +down some of their components may be of value. Provided that such type names +conform to the Java naming guidelines, you may arrange it with > + :let java_highlight_generics=1 In Java 1.1 the functions System.out.println() and System.err.println() should only be used for debugging. Therefore it is possible to highlight debugging diff --git a/runtime/syntax/java.vim b/runtime/syntax/java.vim index c2cba1a5f9..b612ccaaad 100644 --- a/runtime/syntax/java.vim +++ b/runtime/syntax/java.vim @@ -146,6 +146,30 @@ if s:module_info_cur_buf syn keyword javaModuleExternal to with endif +" Fancy parameterised types (JLS-17, §4.5). +" +" Note that false positives may elsewhere occur whenever an identifier +" is butted against a less-than operator. Cf. (X\.\)*\<' . s:ff.UpperCase('[$_[:upper:]]', '[^a-z0-9]') . '\k*\>\)\%(\[\]\)*"' + + for s:ctx in [{'dsg': 'javaDimExpr', 'gsg': 'javaGenerics', 'ghg': 'javaGenericsC1', 'csg': 'javaGenericsX', 'c': ''}, + \ {'dsg': 'javaDimExprX', 'gsg': 'javaGenericsX', 'ghg': 'javaGenericsC2', 'csg': 'javaGenerics', 'c': ' contained'}] + " Consider array creation expressions of reifiable types. + exec 'syn region ' . s:ctx.dsg . ' contained transparent matchgroup=' . s:ctx.ghg . ' start="\[" end="\]" nextgroup=' . s:ctx.dsg . ' skipwhite skipnl' + exec 'syn region ' . s:ctx.gsg . s:ctx.c . ' transparent matchgroup=' . s:ctx.ghg . ' start=/' . s:ff.Engine('\%#=2', '') . '\%(\<\K\k*\>\.\)*\<' . s:ff.UpperCase('[$_[:upper:]]', '[^a-z0-9]') . '\k*\><\%([[:space:]\n]*\%([?@]\|\<\%(b\%(oolean\|yte\)\|char\|short\|int\|long\|float\|double\)\[\]\|\%(\<\K\k*\>\.\)*\<' . s:ff.UpperCase('[$_[:upper:]]', '[^a-z0-9]') . '\k*\>\)\)\@=/ end=/>/ contains=' . s:ctx.csg . ',javaAnnotation,javaTypeArgument,javaWildcardBound,javaType,@javaClasses nextgroup=' . s:ctx.dsg . ' skipwhite skipnl' + endfor + + unlet s:ctx + hi def link javaWildcardBound Question + hi def link javaGenericsC1 javaFuncDef + hi def link javaGenericsC2 javaType +endif + if exists("java_highlight_java_lang_ids") let java_highlight_all=1 endif @@ -175,9 +199,15 @@ if exists("java_highlight_all") || exists("java_highlight_java") || exists("ja exec 'syn match javaC_JavaLang "\%(\"' exec 'syn match javaC_JavaLang "\%(\"' exec 'syn match javaC_JavaLang "\%(\"' - exec 'syn match javaC_JavaLang "\%(\"' - 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 keyword javaC_JavaLang Boolean Character ClassLoader Compiler Double Float Integer Long Math Number Object Process Runtime SecurityManager String StringBuffer Thread ThreadGroup Byte Short Void Package RuntimePermission StrictMath StackTraceElement ProcessBuilder StringBuilder Module ModuleLayer StackWalker Record syn match javaC_JavaLang "\" " See javaDebug. + + if !exists("java_highlight_generics") + " The non-interface parameterised names of java.lang members. + exec 'syn match javaC_JavaLang "\%(\"' + syn keyword javaC_JavaLang Class InheritableThreadLocal ThreadLocal Enum ClassValue + endif + " As of JDK 21, java.lang.Compiler is no more (deprecated in JDK 9). syn keyword javaLangDeprecated Compiler syn cluster javaClasses add=javaC_JavaLang @@ -217,7 +247,7 @@ if exists("java_space_errors") endif exec 'syn match javaUserLabel "^\s*\<\K\k*\>\%(\\)\@' . s:ff.Peek('7', '') . '" matchgroup=NONE end=":\|->" contains=javaLabelCastType,javaLabelNumber,javaCharacter,javaString,javaConstant,@javaClasses,javaLabelDefault,javaLabelVarType,javaLabelWhenClause +syn region javaLabelRegion transparent matchgroup=javaLabel start="\" matchgroup=NONE end=":\|->" contains=javaLabelCastType,javaLabelNumber,javaCharacter,javaString,javaConstant,@javaClasses,javaGenerics,javaLabelDefault,javaLabelVarType,javaLabelWhenClause syn region javaLabelRegion transparent matchgroup=javaLabel start="\\%(\s*\%(:\|->\)\)\@=" matchgroup=NONE end=":\|->" oneline " Consider grouped _default_ _case_ labels, i.e. " case null, default -> @@ -332,7 +362,7 @@ syn match javaSpecial "\\u\x\x\x\x" " 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 + syn cluster javaFuncParams contains=javaAnnotation,@javaClasses,javaGenerics,javaType,javaVarArg,javaComment,javaLineComment if java_highlight_functions =~# '^indent[1-8]\=$' let s:last = java_highlight_functions[-1 :] @@ -443,7 +473,7 @@ if exists("java_highlight_functions") " 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', '') . '\%((\_.\{-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' + exec 'syn match javaLambdaDef "\k\@' . s:ff.Peek('4', '') . '\%((\_.\{-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,javaGenerics,javaVarArg' " Match: () -> " | (a[, b, ...]) -> exec 'syn match javaLambdaDef "\k\@' . s:ff.Peek('4', '') . '\%(,[[:space:]\n]*\)\=\)*)[[:space:]\n]*->"'