4

Is there a plugin for vim, somewhat like Jsbeautify, which automatically generates JavaDoc like comments in the script files.

For example it will take this

function(a , b , c){
}

and return

/**
 * Description.
 *
 * @param a  Description.
 * @param b  Description.
 * @param c  Description.
 */
function(a , b , c){
}
puk
  • 16,318
  • 29
  • 119
  • 199

3 Answers3

6

Here's a little something to get you started - tweak as required!-)

" generate doc comment template
map <LocalLeader>/ :call GenerateDOCComment()<cr>

function! GenerateDOCComment()
  let l    = line('.')
  let i    = indent(l)
  let pre  = repeat(' ',i)
  let text = getline(l)
  let params   = matchstr(text,'([^)]*)')
  let paramPat = '\([$a-zA-Z_0-9]\+\)[, ]*\(.*\)'
  echomsg params
  let vars = []
  let m    = ' '
  let ml = matchlist(params,paramPat)
  while ml!=[]
    let [_,var;rest]= ml
    let vars += [pre.' * @param '.var]
    let ml = matchlist(rest,paramPat,0)
  endwhile
  let comment = [pre.'/**',pre.' * '] + vars + [pre.' */']
  call append(l-1,comment)
  call cursor(l+1,i+3)
endfunction

Assuming the parameter list is on one line, it tries to match out the parameters, builds up a comment string, and appends that comment string to the line before the function header.

Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
claus
  • 737
  • 5
  • 5
  • In case anyone is interested, this can easily be made to cater to Python style comments by changing the stars to quotes. If you have default values for parameters, however, they will be considered as parameters – puk Mar 01 '12 at 23:22
  • Can you explain how does this work, please? (cc. @puk) – Ionică Bizău Feb 12 '14 at 13:35
4

with snipmate you can make a snippet or use a mix of actual js snippets

JuanPablo
  • 23,792
  • 39
  • 118
  • 164
  • It's a place to start, however, it's not what I was looking for. Does such a thing exist even outside of vim? I'm surprised JSBeautify doesn't provide it as an option. – puk Oct 30 '11 at 21:32
2

I was thinking on a plugin mixed with macros, but how many arguments may a function take? Most of the time, it will be 4 at the max.

The solution with snippets can be a viable one.

Jose Elera
  • 938
  • 9
  • 16
  • Yes but you'd think one would already exist. I actually was hoping jsBeautify would take care of this. – puk Jan 06 '12 at 02:12