sponsor Vim development Vim logo Vim Book Ad

javacomplete : Omni Completion for JAVA

 script karma  Rating 729/462, Downloaded by 29755  Comments, bugs, improvements  Vim wiki

created by
fang cheng
 
script type
ftplugin
 
description
This is javacomplete, an omni-completion script of JAVA language for vim 7.

It includes javacomplete.vim, java_parser.vim, Reflection.java, and
javacomplete.txt

Features:
- List members of a class, including (static) fields, (static) methods and ctors.
- List classes or subpackages of a package.
- Provide parameters information of a method, list all overload methods.
- Complete an incomplete word.
- Provide a complete JAVA parser written in Vim script language.
- Use the JVM to obtain most information.
- Use the embedded parser to obtain the class information from source files.
- Tags generated by ctags can also be used.
- JSP is supported, Builtin objects such as request, session can be recognized.
  The classes and jar files in the WEB-INF will be appended automatically to classpath.


Requirements:
It works on all the platforms where
- Vim version 7.0 and above
- JDK version 1.1 and above
existed

Input context:
It recognize nearly all kinds of Primary Expressions (see langspec-3.0)
except for "Primary.new Indentifier". Casting conversion is also supported.
Samples of input contexts are as following: ('|' indicates cursor)
    (1). after '.', list members of a class or a package
    - package.|         subpackages and classes of a package
    - Type.|                static members of the 'Type' class and "class"
    - var.| or field.|     members of a variable or a field
    - method().|         members of result of method()
    - this.|                   members of the current class
    - ClassName.this.|  members of the qualified class
    - super.|               members of the super class
    - array.|                members of an array object
    - array[i].|             array access, return members of the element of array
    - "String".|            String literal, return members of java.lang.String
    - int.| or void.|       primitive type or pseudo-type, return "class"
    - int[].|                   array type, return members of a array type and "class"
    - java.lang.String[].|
    - new int[].|           members of the new array instance
    - new java.lang.String[i=1][].|
    - new Type().|      members of the new class instance
    - Type.class.|      class literal, return members of java.lang.Class
    - void.class.| or int.class.|
    - ((Type)var).|         cast var as Type, return members of Type.
    - (var.method()).|   same with "var.|"
    - (new Class()).|    same with "new Class().|"

   (2). after '(', list matching methods with parameters information.
    - method(|)                 methods matched
    - var.method(|)           methods matched
    - new ClassName(|)  constructors matched
    - this(|)                        constructors of current class matched
    - super(|)                     constructors of super class matched
    Any place between '(' and ')' will be supported soon.
    Help information of javadoc is not supported yet.

   (3). after an incomplete word, list all the matched beginning with it.
    - var.ab|          subset of members of var beginning with `ab`
    - ab|                list of all maybes

   (4). import statement
    - " import         java.util.|"
    - " import         java.ut|"
    - " import         ja|"
    - " import         java.lang.Character.|"        e.g. "Subset"
    - " import static java.lang.Math.|"        e.g. "PI, abs"

   (5). package declaration
    - " package         com.|"

   The above are in simple expression.
   (6). after compound expression:
    - PrimaryExpr.var.|
    - PrimaryExpr.method().|
    - PrimaryExpr.method(|)
    - PrimaryExpr.var.ab|
    e.g.
    - "java.lang        . System.in .|"
    - "java.lang.System.getenv().|"
    - "int.class.toString().|"
    - "list.toArray().|"
    - "new ZipFile(path).|"
    - "new ZipFile(path).entries().|"

   (7). Nested expression:
    - "System.out.println( str.| )"
    - "System.out.println(str.charAt(| )"
    - "for (int i = 0; i < str.|; i++)"
    - "for ( Object o : a.getCollect| )"


Limitations:
The embedded parser works a bit slower than expected.

TODO:
-  Improve performance of the embedded parser. Incremental parser.
-  Add quick information using balloonexpr, ballooneval, balloondelay.
-  Add javadoc
-  Give a hint for class name conflict in different packages.
-  Support parameter information for template
-  Make it faster and more robust.

Screenshots:
members of a package, http://blog.chinaunix.net/photo/44758_070917101010.jpg
members of a type, http://blog.chinaunix.net/photo/44758_070917101048.jpg
local variable, http://blog.chinaunix.net/photo/44758_070917101134.jpg
special reference super, http://blog.chinaunix.net/photo/44758_070917101158.jpg
object of method result,  http://blog.chinaunix.net/photo/44758_070917101236.jpg

FeedBack:
Any problem, bug or suggest are welcome to send to fangread@yahoo.com.cn

BTW, If you want to get more functions on writting java program besides code completion,
you can try VJDE. http://www.vim.org/scripts/script.php?script_id=1213
 
install details
1. Unzip javacomplete.zip to a directory of 'runtimepath', e.g.
$HOME/.vim (unix/linux), $VIM/vimfiles (windows).
> unzip javacomplete.zip -d ~/.vim

    To update Vim help tags, run vim and run command:
:helptags $HOME/.vim/doc
    or
:helptags $VIM/vimfiles/doc

NOTE: javacomplete.vim, java_parser.vim and Reflection.java should be in one
autoload directory of 'runtimepath'.
javacomplete.txt should be in one doc directory of 'runtimepath'.

2. Set 'omnifunc' option. e.g.
        :setlocal omnifunc=javacomplete#Complete
  Or, use autocmd:
        :" Only do this part when compiled with support for autocommands.
        :if has("autocmd")
        :  autocmd Filetype java setlocal omnifunc=javacomplete#Complete
        :endif
  You can add this command to your .vimrc or _vimrc.

3. Set 'completefunc' option to show parameters information IF YOU LIKE. e.g.
:setlocal completefunc=javacomplete#CompleteParamsInfo
You can map as follows for better display:
:inoremap <buffer> <C-X><C-U> <C-X><C-U><C-P>
:inoremap <buffer> <C-S-Space> <C-X><C-U><C-P>

4. Reflection.java will be automatcally compiled and placed to $HOME when you
use first time. Assure that Reflection.java is in the same directory with
javacomplete.vim to be searched in autoload subdirectory of &rtp.
If no Reflection.class is generated, check that you have the write permission
in $HOME directory.
If a previous Reflection.java is not compatible with the new version
javacomplete.vim, please compile Reflection.java manually.
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
javacomplete0.77.1.2.zip 0.77.1.2 2011-01-30 7.0 fang cheng Fixed the bug caused by that globpath() (vim version < 7.2) does not support the 3rd parameter.
Thanks to Sam Lidder's patch.
javacomplete.zip 0.77.1.1 2010-11-12 7.0 fang cheng Fixed to ignore the 'suffixes' and 'wildignore' options.
Reflection.class can not be found if :set wildignore=*.class
javacomplete.zip 0.77.1 2007-09-25 7.0 fang cheng + Supported showing method parameters information in any place between parenthesises.
javacomplete.zip 0.77 2007-09-18 7.0 fang cheng + Supported new contexts "jav|", "var|", just after an incomplete word.
+ Supported accessible static imported members, add new context "abs(|)".
+ Supported accessible inherited members.
- [Reflection.java]Improved getting paths of all system jar files for different JDKs in different platforms.
- Rewrote some core functions.
javacomplete.zip 0.76.8 2007-08-30 6.0 fang cheng - Improved support of JSP: appending automatically jar or classpath under WEB-INF.
- Supported local and anonymous classes.
- Improved searching type or symbol name.
javacomplete.zip 0.76.7 2007-08-28 7.0 fang cheng - Improved process of type arguments of generic type and method parameters.
- Reorganize codes in javacomplete#Complete()
- Fixed case of "new java.util.zip.ZipFile().|"
javacomplete.zip 0.76.6 2007-08-23 7.0 fang cheng - Improved recognizing methods, ctors, and variable declarators declared in most common form.
- Optimized code and fixed bugs.
javacomplete.zip 0.76.5 2007-08-21 7.0 fang cheng - Optimized code: s:modeAndEXPR(), formalParameter(), and others.
- Added quick recognizing fields or methods declared in most common form.
- Fixed bug: "foo().|", "getFoo().foo().|", "for (Enumeration entries = ; entries.|; )".
- Supported input contexts: "((Object)o).|", "((Object)o).getClass().|", "new ZipFile(path).|", "(new String().)|".
javacomplete.zip 0.76.4.1 2007-08-17 7.0 fang cheng - Made a major improvement of Reflection. REMEMBER to DELETE the old Reflection.class.
- Support accessible nested classes from reflection.
- Support import static members and import accessible nested classes.
- Improved input contexts: "int.class.toString().|", "list.toArray().|".
- Fixed recognition of "this(|)", "method1(|)"
- Added the 'kind' letter to distinguish between classes and packages.
- Fixed a bug when Reflection.java is in the path which contains space.
- Better support for JSP.
javacomplete.zip 0.76.3 2007-08-10 7.0 fang cheng - Make an improvement for JSP file.
- Add an option 'searchdecl' set by javacomplete#SetSearchdeclMethod().
- Clear cache when set options affecting classpath.
- Improved DoGetPackageList() and s:GenerateImports().
- Replace codes searching list of string with index().
- Removed the unclear s:tokens and s:modifier_keywords in java_parser.
- Add java_parser#GetSnapshot() and java_parser#Restore().
- Fixed a bug when no top level class defined
javacomplete.zip 0.76.2 2007-08-08 7.0 fang cheng - Add inherited fields and methods to local class.
- Combine members of local packages and loadable packages.
- Add quick recognition of package or import.
- Fix failing to list members of nested class.
- Improved java_parser.
- [Reflection] Return a modifier value as a string because it more than 32bit.
javacomplete.zip 0.76.1 2007-08-05 6.0 fang cheng - Support new language features in JAVA 5 and above.
- The embedded JAVA parser is full-featured now, adding supports for parsing statement, block or expression.
- Java_parser.vim is an autoload script now.
- Support JDK1.1.
- Improve for 'this' or 'super'.
- Support searching toplevel class in current directory, same package or in sourcepath.
- Supports input context: "StringLiteral".|, "int.|", "void.|"
- Add java compiler option, default 'javac'
- Add JVM launcher option, default 'java'
- Automatcally compile Reflection.java and place it to $HOME.
- Easier to install and use.
javacomplete.zip 0.75beta 2007-02-13 7.0 fang cheng " Add java_parser.vim.
" Add b:sourcepath option.
" Improve recognition of classes defined in current buffer or in source path.
" Support generating class information from tags instead of returning list directly.
javacomplete.zip 0.74 2007-02-03 7.0 fang cheng Support jre1.2 (and above).
Support input context like "boolean.class.|"
Handle java primitive types such as 'int'.
javacomplete.zip 0.73 2007-02-01 7.0 fang cheng Fix bug that CLASSPATH not used when b:classpath or g:java_classpath not set.
Fix bug that call filter() without making a copy for incomplete.
Improve recognition of declaration of this class
javacomplete.zip 0.72 2007-01-31 7.0 fang cheng Fix bugs in nested expression:
"System.out.println( str.| )"
"System.out.println(str.charAt(| )"
"for (int i = 0; i < str.|; i++)"
javacomplete.zip 0.71 2007-01-28 7.0 fang cheng Initial upload
ip used for rating: 18.116.43.119

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Vim at Github