2016-08-05 32 views
0

我目前正在嘗試編寫一個腳本,允許調用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 

在此先感謝您的幫助。

+0

那個「命令工廠命令」的目的是什麼? – romainl

回答

0

ComponentFiles()應該過濾基於光標(ArgLead)之前的字符的文件列表,但你總是返回相同的列表中,這樣功能沒有任何用處。

下完成自定義函數返回僅在ls字符光標之前相匹配的項目:

function! MyComp(ArgLead, CmdLine, CursorPos) 
    return filter(systemlist("ls"), 'v:val =~ a:ArgLead') 
endfunction 

function! MyFunc(arg) 
    execute "edit " . a:arg 
endfunction 

command! -nargs=1 -complete=customlist,MyComp MyEdit call MyFunc(<f-args>) 

:help :command-completion-customlist

+0

謝謝,你的答案解決了我的問題。我錯過了使用ArgLead參數的過濾。我設法完成了這項工作。 –