英文:
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 <C-r>=go#tool#Files()[0]<CR>
or put that list in a scratch buffer:
:vnew<CR>
:0put=join(go#tool#files(), '\r')<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论