我目前正在嘗試編寫一個腳本,允許調用Vim命令,該命令依次調用外部命令。我想在具有特定擴展名的文件列表中啓用自動完成功能(請參閱下面的代碼)。Vim腳本命令完成:按Tab重新加載列表
完成實際上遍歷了具有給定擴展名的文件列表,但是當我開始輸入內容並按Tab時,完成是循環從列表的開始處重複,無論輸入什麼內容(而不是實際上完成輸入中給出的文件名的其餘部分)。代碼如下:
call CreateEditCommand('EComponent', 'ComponentFiles')
function! CreateEditCommand(command, listFunction)
silent execute 'command! -nargs=1 -complete=customlist,'. a:listFunction . ' ' . a:command . ' call EditFile(<f-args>)'
endfunction
function! ComponentFiles(A,L,P)
return Files('component.ts')
endfunction
function! Files(extension)
let paths = split(globpath('.', '**/*.' . a:extension), "\n")
let idx = range(0, len(paths)-1)
let g:global_paths = {}
for i in idx
let g:global_paths[fnamemodify(paths[i], ':t:r')] = paths[i]
endfor
call map(paths, 'fnamemodify(v:val, ":t:r")')
return paths
endfunction
function! EditFile(file)
execute 'edit' g:global_paths[a:file]
endfunction
舉例來說,如果我有:app.component.ts和test.component.ts和I型
:EComponent test
和壓片,完整的命令會在下面的
:EComponent app.component.ts
代替:
:EComponent test.component.ts
在此先感謝您的幫助。
那個「命令工廠命令」的目的是什麼? – romainl