Vim插件生成Javascript文档注释

时间:2011-10-30 01:19:19

标签: javascript vim comments vim-plugin

是否有一个vim插件,有点像Jsbeautify,它会自动生成JavaDoc,就像脚本文件中的注释一样。

例如,它将采用此

function(a , b , c){
}

并返回

/**
 * Description.
 *
 * @param a  Description.
 * @param b  Description.
 * @param c  Description.
 */
function(a , b , c){
}

3 个答案:

答案 0 :(得分:5)

这里有一些让你入门的东西 - 根据需要进行调整! - )

" 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

假设参数列表在一行上,它会尝试匹配参数,建立注释字符串,并将该注释字符串附加到函数头之前的行。

答案 1 :(得分:4)

使用snipmate,您可以制作代码段或使用实际js snippets

的组合

答案 2 :(得分:2)

我在考虑一个混有宏的插件,但是函数可以使用多少个参数?大多数情况下,最大值为4。

带有片段的解决方案可能是一个可行的解决方案。

相关问题