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

huangapple go评论60阅读模式
英文:

vim-go: How to open files returned by GoFiles

问题

Vim-go插件有一个:GoFile函数,用于显示依赖于当前包的源文件。输出的格式如下:

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

如何从列表中打开文件?

英文:

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

得分: 1

:GoFiles只会输出go#tool#Files()的结果。

从你的代码片段来看,可以通过以下方式提取文件名:

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

或者将该列表放入一个临时缓冲区:

:vnew<CR>
:0put=join(go#tool#files(), '\r')<CR>

/home/tretkow/tut/main.go
/home/tretkow/tut/test.go

然后使用gf命令跳转到光标下的文件。


这里有一个更复杂的解决方案::GoFile命令允许您通过自定义的Tab补全来选择要从:GoFiles命令中编辑的文件。

" 命令
command! -nargs=1 -complete=customlist,GoFilesComplete GoFile call GoFile(<f-args>)

" 补全函数
function! GoFilesComplete(ArgLead, CmdLine, CursorPos)
    return filter(go#tool#Files(), 'v:val =~ a:ArgLead')
endfunction

" :edit的包装函数
function GoFile(file)
    execute "edit " . a:file
endfunction

用法:

:GoFile <Tab>
英文:

: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 &lt;C-r&gt;=go#tool#Files()[0]&lt;CR&gt;

or put that list in a scratch buffer:

:vnew&lt;CR&gt;
:0put=join(go#tool#files(), &#39;\r&#39;)&lt;CR&gt;

/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.

&quot; the command
command! -nargs=1 -complete=customlist,GoFilesComplete GoFile call GoFile(&lt;f-args&gt;)

&quot; the completion function
function! GoFilesComplete(ArgLead, CmdLine, CursorPos)
    return filter(go#tool#Files(), &#39;v:val =~ a:ArgLead&#39;)
endfunction

&quot; the :edit wrapper
function GoFile(file)
    execute &quot;edit &quot; . a:file
endfunction

Usage:

:GoFile &lt;Tab&gt;

huangapple
  • 本文由 发表于 2014年11月4日 18:48:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/26733223.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定