vim-patch:c4d0c8c81245

runtime(java): Improve the recognition of the "indent" method declarations (vim/vim#14659)

There is a flaw in the current implementation that has been
exacerbated around v5.2.  It lies in the recognition of all
three indentation styles simultaneously: a tab, two space,
and eight space character(s).  With it, it is not uncommon
to misidentify various constructs as method declarations
when they belong to two-space indented members and other
blocks of a type and are offset at eight space characters or
a tab from the start of the line.

For example,

------------------------------------------------------------
class Test
{
  static String hello() { return "hello"; }

  public static void main(String[] args)
  {
    try {
      if (args.length > 0) {
        // FIXME: eight spaces.
        System.out.println(args[0]);
      } else {
        // FIXME: a tab.
	System.out.println(hello());
      }
    } catch (Exception e) {
      throw new Error(e);
    }
  }
}
------------------------------------------------------------

------------------------------------------------------------
:let g:java_highlight_functions = 'indent'
:doautocmd Syntax
------------------------------------------------------------

A better approach is to pick an only indentation style out
of all supported styles (so either two spaces _or_ eight
spaces _or_ a tab).  Note that tabs and spaces can still be
mixed, only the leading tab or the leading run of spaces
matters for the recognition.  And there is no reason to not
complement the set of valid styles with any number of spaces
from 1 to 8, inclusively.

Please proceed with the necessary change as follows:

- rename from "indent" to "indent2" for a 2-space run;
- rename from "indent" to "indent8" for an 8-space run;
- continue to have "indent" for a tab run;
- define an "indent" variable with a suffix number denoting
  the preferred amount of indentation for any other run of
  spaces [1-8].

As before, this alternative style of recognition of method
declarations still does not prescribe naming conventions and
still cannot recognise method declarations in nested types
that are conventionally indented.

The proposed changes also follow suit of "style" in stopping
the claiming of constructor and enum constant declarations.

c4d0c8c812

Co-authored-by: Aliaksei Budavei <32549825+zzzyxwvut@users.noreply.github.com>
This commit is contained in:
Christian Clason
2024-04-29 23:44:53 +02:00
parent 672138245f
commit b7e5769132
2 changed files with 33 additions and 10 deletions

View File

@ -1542,15 +1542,25 @@ 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:
If you write function declarations that are always indented by either
a tab, 8 spaces or 2 spaces you may want to set >
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 >
:let java_highlight_functions="indent"
:let java_highlight_functions="indent1"
:let java_highlight_functions="indent2"
:let java_highlight_functions="indent3"
:let java_highlight_functions="indent4"
:let java_highlight_functions="indent5"
:let java_highlight_functions="indent6"
:let java_highlight_functions="indent7"
:let java_highlight_functions="indent8"
Note that in terms of 'shiftwidth', this is the leftmost step of indentation.
However, if you follow the Java guidelines about how functions and classes are
supposed to be named (with respect to upper and lowercase), use >
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 both options do not 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 which includes the
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.
In Java 1.1 the functions System.out.println() and System.err.println() should

View File

@ -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 Apr 28
" Please check :help java.vim for comments on some of the options available.
@ -292,10 +292,23 @@ syn cluster javaTop add=javaString,javaStrTempl,javaCharacter,javaNumber,javaSpe
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\)\@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