sponsor Vim development Vim logo Vim Book Ad

EditJava : lookup source file for classname under cursor

 script karma  Rating 14/5, Downloaded by 1481    Comments, bugs, improvements  Vim wiki

created by
richard emberson
script type
Place cursor over a java classname and enter the scripts map key sequence (I  map it to ",g"). The base java file name is <cword> . ".java"

First if the cursor is over a package classname like 'java.util.Map', then if the cursor is over 'Map', the java/util/Map.java file is opened else if the cursor is over, say, 'util', then the java/util directory is opened.

Otherwise, the script looks for a file with the filename in local directory and  if it exists, opens it.

If not, it looks for an explicit import file with the filename, if it can be found  in the java source path, then it opens it. If not found, use 'locate' command to look for filename plus import package directories prepended. If found then open it.

If not, it looks for a file in "$JAVA_HOME/src/java/lang" and if found opens it.

If not it then looks for a match using any of the implicit import statements, those ending in * and in the java source path. If found open it. Otherwise use 'locate' command to search for filename plus import package path prepended and if found open it.

Lastly, if not found by any of the above methods it then attempts to find a file using the package path. The current file might be in a parallel junit directory or in some parallel directory structure. If this is found in the java source path then it is opened otherwise it uses "locate' command to search for the file and if found opens it.

EditJava plus the bufexplorer.vim script,
the "edit cwd" Tip #2:
map ,d :e <C-R>=expand("%:p:h")<CR><CR>  
and the mapping allowing one to toggle between files
map gg :e#^M
with the buffer edit commands (where N is an integer)
provide one with a collection of tools to view and navigate Java sources.
install details
Place script in your vim script directory:

Add the following to your .vimrc file:

" Set my Leader character
let mapleader = ","

" Source javae.vim
source $HOME/vimscripts/javae.vim

" Create a java source path in .vimrc
" (I currently have 17 source paths in my .vimrc):
  \",$HOME/java/xerces/xerces/src" .
  \",$HOME/java/xerces/xalan/src" .
  \",$HOME/java/jboss/jboss" .

" Creating a mapping to call the script in .vimrc:
" goto
map <Leader>g :call EditJava('e',$JAVASOURCEPATH)<CR>
" open horizontal
map <Leader>h :call EditJava('sp',$JAVASOURCEPATH)<CR>
" open vertical
map <Leader>v :call EditJava('vsp',$JAVASOURCEPATH)<CR>

Define behavior
Set g:javae_use_locate_cmd  to 0 if you do not wish to use a 'locate" command. The default is to use the 'locate' command:
let g:javae_use_locate_cmd = 1

Set the name of the 'locate' command. The default value for g:javae_locate_cmd is "slocate". If you wish to use a different command put the folloing in your .vimrc:
let g:javae_locate_cmd = <command_name>

If using a 'locate' command, then one can force javae to allways prompt even when there is only one match by setting g:javae_locateopen_alwaysprompt to 1.
The default is to only prompt when there is more than one file:
let g:javae_locateopen_alwaysprompt = 0

Also, one can set whether or not to get an error message if in response to a prompt an incorrect value is entered. To not see any error messages set g:javae_locateopen_showerror to 0. The default is to see error messages
let g:javae_locateopen_showerror = 1

When switching to another buffer the current buffer may need to be saved. The g:javae_automatic_update flag allows one to control this behavior. Setting it to 0:
let g:javae_syntax_based = 0
will not automatically update the current buffer if it needs to be saved allowing you to automatically see the new buffer, rather you must first manually save the current buffer and re-run the search.
The default is to allow for automatic updates:
let g:javae_syntax_based = 1.

Import statements do not occur after the first class or interface declaration. While searching the current file for all import statements one can either read each line of the current file or stop after the first class or interface declaration. In addition, what looks like an import statement might be in a comment. If the flag g:javae_syntax_based is set to 0:
let g:javae_syntax_based=0
then each line in the file is read.
On the other hand if it is set to 1:
let g:javae_syntax_based = 1
the default value, then the search for import statements skips comments and stops after the first class or interface declaration. The reason one might not wish to use the g:javae_syntax_based flag is that under the covers java syntax values are used to determine when one is in a comment or declaration and if the user has modified the default java syntax values found in java.vim, then the syntax base code may not work.


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
javae.vim 1.4 2005-01-25 6.0 richard emberson Now looks a the package path to see if the file being searched for is in a parallel directory structure. Whether or not to automatically save the current buffer, if needed, is controled by a flag. Lastly, java syntax can be used to identify when the first class/interface declaration has occurred and the search for import statements should stop.
javae.vim 1.3 2005-01-17 6.0 richard emberson Added ability to use the system "locate" command to
find the java file if it can not be located in the java path
explicitly passed in as a paramenter. Also, fixed a couple
of bugs.
javae.vim 1.2 2005-01-03 6.0 richard emberson The entry point function, 'EditJava', now takes a command
argument which is executed if a matching file or directory is
found. This allows the user to parameterize the call with their
choice of opening a new window for the source file, e.g., the
command can be:
e, vsplit, new, split, vertical, sview, vnew , etc.

Also, the 'EditJava' method was refactored.
javae.vim 1.1 2004-12-27 6.0 richard emberson Now if there is a package path classname such as java.util.Map
in ones code, then with cursor over Map, the Map.java file
is opened and if cursor is over for example 'util' then the
java/util directory is opened. All of the other means of opening
the java file under the cursor are still there.
javae.vim 1.0 2004-12-23 6.0 richard emberson Initial upload
ip used for rating:

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.
SourceForge.net Logo