Vim plugin to generate Javascript Documentation comments

做~自己de王妃 提交于 2019-12-03 08:24:50
claus

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!