Vim logo vim online Vim Book Ad

JavaImp.vim : A utility to generate, insert, and sort Java import statements.

 script karma  Rating 27/9, Downloaded by 355

created by
William Lee
 
script type
utility
 
description
This script can generate a Java class name to package mapping file from multiple source paths.  Once you have create the mapping file,  you can use this file to insert import statements in your .java file using a simple key mapping.  This script can save you time you spent looking up the import package name and typing in the string.  It can also sort your imports too!  Give it a try!
 
install details
Put this file in your ~/.vim/plugin directory.

You need to set two global variables in your .vimrc in order for this to
work:

    let g:JavaImpPaths = "..."
    let g:JavaImpClassList = "..."

The g:JavaImpPaths is a comma separated list of paths that point to the
roots of the 'com' or 'org' etc.

For example: if you have your Java source files in
/project/src/java/com/blah and /project2/javasrc/org/blah...., you'll put
this in your .vimrc file:

    let g:JavaImpPaths = "/project/src/java,/project2/javasrc"

If there are too many paths, you can split them into separate lines:

    let g:JavaImpClassList = "/project/src/java," .
       \ "/project2/javasrc," .
       \ "/project3/javasrc"

Note: Don't forget the ',' to separate the paths.

The g:JavaImpClassList is a file you specify to store the class mappings.

For example, you can put this in your .vimrc:

    let g:JavaImpClassList = "/project/JavaImp.txt"

By default, the sorting algorithm gives preferences to the java.* classes.
You can turn this behavior off by putting this in your .vimrc file.

    let g:JavaImpSortJavaFirst = 0

NOTE: If your JavaImpClassList gets too huge, the vim-only sorting
might choke. If you are on a unix machine or you have the sort binary in
your path, it's recommended that you set:

    let g:JavaImpSortBin = "sort"

But this means that the sorting will use the order of the "sort". If you
have this set it'll ignore the g:JavaImpSortJavaFirst variable.

It's recommended that you set a directory for the caching result for your
jar files.  This is used to store the results from the "jar" command
so you do not need to run it each time if the jar files has not changed.
You can do so by dedicating a directory to store the cached results.  For
example:

    let g:JavaImpJarCache = $HOME . "/.JavaImpCache"

If this directory is set, jar caching will be turned on automatically.
Make sure you create the directory prior to running the script.

Now you are ready for some acions.  You can now do a:

    :JavaImpGenerate or :JIG

This will go through your JavaImpPaths and search for anything that ends
with .java, .class, or .jar.  It'll then write the mappings to the
JavaImp.txt file.

After you've generated your JavaImp file, move your cursor to a class name
in a Java file and do a:

    :JavaImp or :JI

And the magic happens!  You'll realize that you have an extra import
statement inserted after the last import statement in the file.  It'll
also prompts you with duplicate class names and insert what you have
selected.  If the class name is already imported, it'll do nothing.  

Doing a:

    :JavaImpSilent

will do a similar thing with less verbosity.  This is useful to be used in
another script.

You can also sort the import statements in the file by doing:

    :JavaImpSort or :JIS

One extra benefit you get by generating the mapping file is that you can
use it as your dictionary for autocompletion.  For example, you can put
the following in your java.vim ftplugin:

exe "setlocal dict=" . g:JavaImpClassList
setlocal complete-=k
setlocal complete+=k

or put this in your .vimrc

exe "set dict=" . g:JavaImpClassList
set complete-=k
set complete+=k


Enjoy!
 

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
JavaImp.vim 2.0.2 09-Sep-2002 6.0 William Lee Make the script to work with version 6.0.  It was using a 6.1 specific function.
JavaImp.vim 2.0.1 06-Sep-2002 6.0 William Lee Now the script works in Windows.  It's recommend that you get the "sort" binary from cygwin though.  Added the silent option for scripting purposes.  Fixes some bugs.
JavaImp.vim 2.0 05-Sep-2002 6.0 William Lee Added jar support, improved performance by allowing you to use the "sort" binary, new menu for selecting class names if there are multiple matches.
JavaImp.vim 1.0.1 01-Jul-2002 6.0 William Lee Fixed a bug where the current window closes when you have multiple windows opened.
JavaImp.vim 1.0 01-Jul-2002 6.0 William Lee Initial upload

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 [email protected] after searching the archive. Help Bram help Uganda.
SourceForge Logo