標籤:ldoc vim lua vimscript
LDoc是一個Lua的文檔產生工具,詳細介紹見,LDoc的Github首頁,但是在vim中手寫注釋較為複雜,然後看了下vim script,自己寫了一款vim外掛程式,用於自動產生模組注釋,類型注釋和函數注釋,方便開發。將此檔案命名為ldoc.vim放在~/.vim/plugin目錄下使用:將游標移動到需要注釋的當前行,然後在命令模式下,輸入LdocM,LdocT,LdocF分別進行模組注釋,類型注釋和函數注釋,也可通過map自行映射快速鍵
- 關於vim script的參考文檔 http://vimdoc.sourceforge.net/htmldoc/usr_41.html
- 關於ldoc的介紹 http://keplerproject.github.io/luadoc/manual.html
" http://vimdoc.sourceforge.net/htmldoc/usr_41.html"""""""""""""""""""""""""""""" 工具函數"""""""""""""""""""""""""""""function! s:warnMsg(msg)echohl WarningMsgecho a:msgechohl Noneendfunction" 外掛程式重複性載入的衝突檢測if exists("loaded_ldoc_ddc")call s:warnMsg("Ldoc Already Loaded!")finishendiflet loaded_ldoc_ddc = 1"""""""""""""""""""""""""""""" 全域注釋變數"""""""""""""""""""""""""""""if !exists("g:ldoc_startBeginCommentTag")let g:ldoc_startBeginCommentTag = "----------------------------------------"endifif !exists("g:ldoc_startEndCommentTag")let g:ldoc_startEndCommentTag = "----------------------------------------"endifif !exists("g:ldoc_startNoteCommentTag")let g:ldoc_startNoteCommentTag = "--- "endifif !exists("g:ldoc_startFlagCommentTag")let g:ldoc_startFlagCommentTag = "-- "endif"""""""""""""""""""""""""""""" 全域標記狀態變數"""""""""""""""""""""""""""""if !exists("g:ldoc_flagAuthor")let g:ldoc_flagAuthor = "@author "endifif !exists("g:ldoc_flagType")let g:ldoc_flagType = "@type "endifif !exists("g:ldoc_flagParam")let g:ldoc_flagParam = "@param "endifif !exists("g:ldoc_flagReturn")let g:ldoc_flagReturn = "@return "endif"""""""""""""""""""""""""""""" 寫入函數" 詳細參見append函數,參數2可直接傳入列表"""""""""""""""""""""""""""""function! s:writeToNextLine(str)call append(line("."), a:str)endfunctionfunction! s:writeToPrevLine(str)call append(line(".")-1, a:str)endfunction"""""""""""""""""""""""""""""" 模組的注釋"""""""""""""""""""""""""""""function! <SID>ldoc_moduleComment()if !exists("g:ldoc_authorName")let g:ldoc_authorName = input("輸入作者名:")endiflet l:moduleDesc = input("輸入模組的簡單說明(可直接斷行符號,稍後填寫):")mark llet l:writeText = [g:ldoc_startBeginCommentTag]let l:str = g:ldoc_startNoteCommentTagif(strlen(l:moduleDesc) == 0)mark melselet l:str = l:str . l:moduleDescendifcall add(l:writeText, l:str)call add(l:writeText, g:ldoc_startFlagCommentTag . g:ldoc_flagAuthor . g:ldoc_authorName)call add(l:writeText, g:ldoc_startEndCommentTag)call s:writeToPrevLine(l:writeText)if(strlen(l:moduleDesc) == 0)exec "normal 'm"elseexec "normal 'l"endifendfunction"""""""""""""""""""""""""""""" 類型的注釋"""""""""""""""""""""""""""""function! <SID>ldoc_typeComment()let l:curLineStr = getline(line("."))let l:typeNameList = matchlist(l:curLineStr, 'local[ \t]\+\([a-zA-Z0-9_]\+\)[ \t]\+')if(len(l:typeNameList) < 2)call s:warnMsg("擷取type失敗,call [email protected]")returnendiflet l:typeName = l:typeNameList[1]let l:typeDesc = input("輸入類型的簡單說明(可直接斷行符號,稍後填寫):")mark llet l:writeText = []let l:str = g:ldoc_startNoteCommentTagif(strlen(l:typeDesc) == 0)mark melselet l:str = l:str . l:typeDescendifcall add(l:writeText, l:str)call add(l:writeText, g:ldoc_startFlagCommentTag . g:ldoc_flagType . l:typeName)call s:writeToPrevLine(l:writeText)if(strlen(l:typeDesc) == 0)exec "normal 'm"elseexec "normal 'l"endifendfunction"""""""""""""""""""""""""""""" 函數的注釋"""""""""""""""""""""""""""""function! <SID>ldoc_functionComment()let l:curLineStr = getline(line("."))let l:paramList = matchlist(l:curLineStr, 'function[ \t]\+\([a-zA-Z0-9_.:]\+\)[ \t]*(\([a-zA-Z0-9_, \t\.]*\))')if(len(l:paramList) >= 2)elselet l:paramList = matchlist(l:curLineStr, '\([a-zA-Z0-9_]\+\)[ \t]*=[ \t]*function[ \t]*(\([a-zA-Z0-9_, \t\.]*\))')if(len(l:paramList) < 2)call s:warnMsg("擷取函數失敗,call [email protected]")returnendifendiflet l:funcName = l:paramList[1]if(len(l:paramList) > 3)let l:paramList = split(l:paramList[2], '[ \t]*,[ \t]*')let l:paramList2 = []for l:ele in l:paramListcall add(l:paramList2, substitute(l:ele, '[ \t]+', "", ""))endforendifmark llet l:funcDesc = input("輸入函數[" . l:funcName . "]的簡單說明(可直接斷行符號,稍後填寫):")let l:writeText = []let l:str = g:ldoc_startNoteCommentTagif(strlen(l:funcDesc) == 0)mark melselet l:str = l:str . l:funcDescendifcall add(l:writeText, l:str)for l:ele in l:paramList2let l:str = g:ldoc_startFlagCommentTag . g:ldoc_flagParam . l:elelet l:paramDesc = input("輸入參數[" . l:ele . "]的簡單說明:")if(strlen(l:paramDesc) > 0)let l:str = l:str . "\t" . l:paramDescendifcall add(l:writeText, l:str)endforcall s:writeToPrevLine(l:writeText)if(strlen(l:funcDesc) == 0)exec "normal 'm"elseexec "normal 'l"endifendfunction"""""""""""""""""""""""""""""" 快速鍵映射"""""""""""""""""""""""""""""command! -nargs=0 LdocM :call <SID>ldoc_moduleComment()command! -nargs=0 LdocT :call <SID>ldoc_typeComment()command! -nargs=0 LdocF :call <SID>ldoc_functionComment()
自寫vim外掛程式ldoc.vim,提供智能的lua注釋代碼補全