[prev in list] [next in list] [prev in thread] [next in thread] 

List:       vim
Subject:    Vim Java script
From:       Scott LaBounty <slabounty () netaphor ! com>
Date:       2001-05-31 14:24:26
[Download RAW message or body]

I wrote the following script and offer it up in hopes that it might be
useful to others. Additionally since this is my first script (although I've
been using vi/vim for a number of years) I hope that a few of the folks on
this list might look it over and offer suggestions. Finally, my biggest
problem is where to place it and how to source it (i.e. from where). The
file allows that user to add a variable and corresponding get and set
methods for it (the simplest case). For example if the user types

:GS int x

they will get in the java file the following (plus some javadoc code):

int x;

public int getX()
{
    return x;
}

public void setX(int X)
{
    this.x = x;
}



Thanks for any help,

Scott LaBounty
Netaphor Software, Inc.

" Vim syntax file
" Language:	Java
" Maintainer:	Scott LaBounty
" URL:		None
" Last Change:  2001 May 27

" This file is used in support of java.vim. It allows the use of the 
" GS function. It can be added to the java.vim file as follows:
"
"if filereadable(expand("<sfile>:p:h")."/gs.vim")
"  source <sfile>:p:h/gs.vim
"endif
"
" assuming of course that the file is in the same file as the
" java.vim syntax file.
"

" This is the GS autogroup. It defines the GS command which allows 
" the following:
" :GS int x
" This performs the following call where the f-args list takes the 
" arguments and puts the quotes around them. It would be nice is the 
" -nargs could be told to expect only two arguments but ... anyway here
" is the actual call that is made:
" call GS("int", "x")
" The command is removed when a java buffer is left.
augroup GS
  autocmd!
  autocmd BufEnter *.java command! -nargs=* GS call GS(<f-args>)
  autocmd BufLeave *.java delcommand GS
augroup END


" Causes a new variable declaration with corresponding get and 
" set methods to be created. 
fun! GS(vType, vName)

  let width = 80
  if exists("&tw")
    let width = &tw
  endif

  " Get the variable name with the first letter capitalized. This
  " will be used for the names of the get and set methods. In other
  " words give "portNumber" this will set capVName to PortNumber.
  let capVName = substitute(a:vName, ".", "\\U\\0", "")

  " Mark get. This will be used later for moving back to 
  " where we started after adding the variable declaration.
  normal mg

  " Move to the beginning of the class and add the
  " declaration. Be careful this will probably *not*
  " work on inner classes. It would be nice also if
  " it could be added and the end of the list of other
  " declarations but, that would require a marker of 
  " some sort. So ...
  normal [[
  execute "normal " .  "o" . "\t" . a:vType . " " . a:vName . ";" . "\<Esc>"


  " Move back to where we started.
  normal 'g

  " Open a blank line.
  execute "normal " .  "O" . "\<Esc>"

  " Mark get. This will be used later for reformatting using the
  " = command.
  normal mg

  " Add in the javadoc comment with the return.
  execute "normal " .  "o" . "/**" . "\<Esc>"
  execute "normal " .  "o" . "* "  . "\<Esc>"
  execute "normal " .  "o" . "* @return " . "\<Esc>"
  execute "normal " .  "o" . "*/" "\<Esc>"

  " Add the get function. Use the vType for the return type and the
  " capVName with get prepended for the method name.
  execute "normal " .  "o" . "public " .  a:vType . " get" . capVName . "()"
. "\<Esc>"
  execute "normal " .  "o" . "{" . "\<Esc>"
  execute "normal " .  "o" . "return " . a:vName . ";" . "\<Esc>"
  execute "normal " .  "o" . "}" . "\<Esc>"

  " Add a blank line
  execute "normal " .  "o" . "\<Esc>"

  " Add in the javadoc comment with the param .
  execute "normal " .  "o" . "/**" . "\<Esc>"
  execute "normal " .  "o" . "* " . "\<Esc>"
  execute "normal " .  "o" . "* @param " . a:vName . "\<Esc>"
  execute "normal " .  "o" . "*/" "\<Esc>"

  " 
  " Add the set method. Use the capVName prepended with set for the 
  " method name. Use the vType and vName for the parameter. Use
  " this.vName for the variable to set.
  execute "normal " .  "o" . "public void set" . capVName . "(" . a:vType .
" " . a:vName . ")" "\<Esc>"
  execute "normal " .  "o" . "{" . "\<Esc>"
  execute "normal " .  "o" . "this." . a:vName . " = " . a:vName . ";" .
"\<Esc>"
  execute "normal " .  "o" . "}" . "\<Esc>"

  " Reformat using the g mark made earlier.
  normal ='g

endfun

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic