sponsor Vim development Vim logo Vim Book Ad

basic Tip #822: add java import statements easily (assume using ctags)

 tip karma   Rating 18/6, Viewed by 1909 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   November 17, 2004 12:18      complexity:   basic
author:   Richard Faber      as of Vim:   5.7


I like the build (make) process to be fast... Eclipse/Netbeans users are excited about "auto-import" features...
Vim can do it just fine....  Well, I just got this working today....and it may need some tweaking.
If It doesn't work for you.... take some java.... that compiles... break one of the imports (assume you don't using foo.*; syntax much).
Then comment lines below starting from bottom up....  watch editing happen.... try to do edits slow to reproduce whats here.
I generally hit F1 key to build with (jikes) (tip # 3).... 90% of build failures are missing import statements....
If you already have a ctags system (tip #804, 94, etc)... the information can be found!
I hit F1 (:make), then hit F9 (add import for keword found under cursor)...then F1 (make/build)....and so on.  (FAST)

(Add Import)  
:noremap <F9> <esc>
                  \<C-W>}o//TEMP MARKER<esc>
                  \<C-W>P1G/public class<cr><esc>yy<C-W>pG?import<cr><esc>p<esc>
          \<C-W>P1G/package<cr><esc>yy<C-W>pG?import<cr><esc>p<esc>
          \$xa.<esc>0jwwi<cr><esc>kdd<esc>
                  \wDx<esc>kJxx<esc>$a;<esc>
          \0cwimport<esc>
          \:update<cr><esc>
                  \/TEMP MARKER<cr>dd<esc>
(Make)    
:noremap <F1> :update<cr>:make<cr><C-W>j<esc>:cw 3<cr><C-W>p  


 rate this tip  Life Changing Helpful Unfulfilling 

<< Simplest buffer explorer ever | Add your note files to vim's help system. >>

Additional Notes

Anonymous, November 18, 2004 9:37
the JavaImp plugin (https://www.vim8.org/scripts/script.php?script_id=325) is also a very good solution to this problem.

it has some other nice goodies as well (i use a slightly modified version of it for auto import, smart import sorting, and tab completion of class names).
Anonymous, November 19, 2004 7:54
This change handles Interfaces too...(Assumes public starts at col 0.  Note:  public final class not working)
        \<C-W>P1G/^public <cr><esc>yy<C-W>pG?import<cr><esc>p<esc>
    (PS.  Thanks for reminder about vimscript #325.  Currently, its too complex for me... but good script ideas)
    (My favorite, really, is to let perl do the filtering.... It has nice regex)
Richard Faber
Anonymous, November 23, 2004 14:25
A slightly more robust implementation, but essentially the same:


noremap <F5> :call JavaInsertImport()<CR>
function! JavaInsertImport()

   exe "normal mz"

   let cur_class = expand("<cword>")

   try
      exe "normal \<C-W>}"
      exe "normal \<C-W>P"

      1
      if search('^public.* \%(class\|interface\) ' . cur_class) > 0
         1
         if search('^package ') > 0
            yank y
         else
            throw "Package definition not found!"
         endif
      else
         throw cur_class . ": class not found!"
      endif

      exe "normal \<C-W>pG"

      " insert after last import or in first line
      if search('^import ', 'b') > 0
         put y
      else
         1
         put! y
      endif

      exe "normal cwimport\<Esc>$R." . cur_class . ";\<Esc>"

   catch /.*/
      echoerr v:exception

   finally
      exe "normal \<C-W>z"
      exe "normal `z"

   endtry

endfunction
Anonymous, November 24, 2004 7:04
Very cool.....  Thanks for the above post....  works much better than my hack. (Richard Faber)
Anonymous, November 24, 2004 15:01
Thanks!

I have a couple of small improvements. Do not add import statement if one already exists and update regexps to cope better with whitespaces.  

noremap <F5> :call JavaInsertImport()<CR>
function! JavaInsertImport()

   exe "normal mz"
   let cur_class = expand("<cword>")

   try
      if search('^\s*import\s.*\.' . cur_class . '\s*;') > 0
         throw getline('.') . ": import already exist!"
      endif

      exe "normal \<C-W>}"
      exe "normal \<C-W>P"

      1
      if search('^\s*public.*\s\%(class\|interface\)\s\+' . cur_class) > 0
         1
         if search('^\s*package\s') > 0
            yank y
         else
            throw "Package definition not found!"
         endif
      else
         throw cur_class . ": class not found!"
      endif

      exe "normal \<C-W>pG"

      " insert after last import or in first line
      if search('^\s*import\s', 'b') > 0
         put y
      else
         1
         put! y
      endif

      exe "normal d0cwimport\<Esc>"
      substitute/\s\+/ /g
      exe "normal 2ER." . cur_class . ";\<Esc>lD"

   catch /.*/
      echoerr v:exception

   finally
      exe "normal \<C-W>z"
      exe "normal `z"

   endtry

endfunction
Thrysoee, November 24, 2004 15:20
By the way Richard, I'm not trying to be rude or the likes by posting as Anonymous. I do have an account on vim.org and I actually thought that the posts were signed with my user name when I was logged in, but apparantly not!

..Jess Thrysoee
Thrysoee, December 2, 2004 14:23
Revised version which doesn't clutter the buffer list and uses wincmd

noremap <F5> :call JavaInsertImport()<CR>
function! JavaInsertImport()

   exe "normal mz"
   let cur_class = expand("<cword>")

   try
      if search('^\s*import\s.*\.' . cur_class . '\s*;') > 0
         throw getline('.') . ": import already exist!"
      endif

      wincmd }
      wincmd P

      1
      if search('^\s*public.*\s\%(class\|interface\)\s\+' . cur_class) > 0
         1
         if search('^\s*package\s') > 0
            yank y
         else
            throw "Package definition not found!"
         endif
      else
         throw cur_class . ": class not found!"
      endif

      wincmd p
      normal G

      " insert after last import or in first line
      if search('^\s*import\s', 'b') > 0
         put y
      else
         1
         put! y
      endif

      substitute/^\s*package/import/g
      substitute/\s\+/ /g
      exe "normal 2ER." . cur_class . ";\<Esc>lD"

   catch /.*/
      echoerr v:exception

   finally
      " wipe preview window (from buffer list)
      silent! wincmd P
      if &previewwindow;
         bwipeout
      endif
      exe "normal `z"

   endtry
  
endfunction
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.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo