LoginSignup
9
10

More than 5 years have passed since last update.

JSDoc形式のコメントを追加するVim Script

Posted at

Qiita初めての投稿です。テスト的な意味も込めて、Gistで公開しているヤツを転載です。
関数定義をしている行で実行すると、その前にコメントを挿入します。
ftpluginのjavascript.vimとかに入れてやると便利かと思います。

jdsoc.vim
" JSDoc形式のコメントを追加(functionの行で実行する)
" hogeFunc: function() の形式と function hogeFunc() に対応
" 関数定義でない場合は、コメントだけ出力する
function! AddJSDoc()
    let l:jsDocregex = '\s*\([a-zA-Z]*\)\s*[:=]\s*function\s*(\s*\(.*\)\s*).*'
    let l:jsDocregex2 = '\s*function \([a-zA-Z]*\)\s*(\s*\(.*\)\s*).*'

    let l:line = getline('.')
    let l:indent = indent('.')
    let l:space = repeat(" ", l:indent)

    if l:line =~ l:jsDocregex
        let l:flag = 1
        let l:regex = l:jsDocregex
    elseif l:line =~ l:jsDocregex2
        let l:flag = 1
        let l:regex = l:jsDocregex2
    else
        let l:flag = 0
    endif

    let l:lines = []
    let l:desc = input('Description :')
    call add(l:lines, l:space. '/**')
    call add(l:lines, l:space . ' * ' . l:desc)
    if l:flag
        let l:funcName = substitute(l:line, l:regex, '\1', "g")
        let l:arg = substitute(l:line, l:regex, '\2', "g")
        let l:args = split(l:arg, '\s*,\s*')
        call add(l:lines, l:space . ' * @name ' . l:funcName)
        call add(l:lines, l:space . ' * @function')
        for l:arg in l:args
            call add(l:lines, l:space . ' * @param ' . l:arg)
        endfor
    endif
    call add(l:lines, l:space . ' */')
    call append(line('.')-1, l:lines)
endfunction

" JSDocのキーバインド
nmap ,d :<C-u>call AddJSDoc()<CR>
9
10
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
10