doushun9875 2014-11-04 10:48
浏览 102
已采纳

vim-go:如何打开GoFiles返回的文件

Vim-go plugin has a :GoFile function to show source files that depends on the current package. The output looks like this:

['/home/tretkow/tut/main.go', '/home/tretkow/tut/test.go']

How to open files from the list?

  • 写回答

1条回答 默认 最新

  • dongyun8891 2014-11-04 11:23
    关注

    :GoFiles only echoes the output of go#tool#Files().

    From the look of your snippet, it should be possible to extract a filename with something like:

    :e <C-r>=go#tool#Files()[0]<CR>
    

    or put that list in a scratch buffer:

    :vnew<CR>
    :0put=join(go#tool#files(), '')<CR>
    
    /home/tretkow/tut/main.go
    /home/tretkow/tut/test.go
    

    and use gf to jump to the file under the cursor.


    Here is a more sophisticated solution: the :GoFile command lets you choose what file from the :GoFiles command to edit via custom tab-completion.

    " the command
    command! -nargs=1 -complete=customlist,GoFilesComplete GoFile call GoFile(<f-args>)
    
    " the completion function
    function! GoFilesComplete(ArgLead, CmdLine, CursorPos)
        return filter(go#tool#Files(), 'v:val =~ a:ArgLead')
    endfunction
    
    " the :edit wrapper
    function GoFile(file)
        execute "edit " . a:file
    endfunction
    

    Usage:

    :GoFile <Tab>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?