Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC for fzf wrap for GoImplements #3320

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 1 addition & 32 deletions autoload/fzf/decls.vim
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,6 @@ function! s:color(str, group) abort
return printf("\x1b[%sm%s\x1b[m", color, a:str)
endfunction

function! s:sink(str) abort
if len(a:str) < 2
return
endif
try
" we jump to the file directory so we can get the fullpath via fnamemodify
" below
let l:dir = go#util#Chdir(s:current_dir)

let vals = matchlist(a:str[1], '|\(.\{-}\):\(\d\+\):\(\d\+\)\s*\(.*\)|')

" i.e: main.go
let filename = vals[1]
let line = vals[2]
let col = vals[3]

" i.e: /Users/fatih/vim-go/main.go
let filepath = fnamemodify(filename, ":p")

let cmd = get({'ctrl-x': 'split',
\ 'ctrl-v': 'vertical split',
\ 'ctrl-t': 'tabe'}, a:str[0], 'e')
execute cmd fnameescape(filepath)
call cursor(line, col)
silent! norm! zvzz
finally
"jump back to old dir
call go#util#Chdir(l:dir)
endtry
endfunction

function! s:source(mode,...) abort
let s:current_dir = expand('%:p:h')
let ret_decls = []
Expand Down Expand Up @@ -144,7 +113,7 @@ function! fzf#decls#cmd(...) abort
call fzf#run(fzf#wrap('GoDecls', {
\ 'source': call('<sid>source', a:000),
\ 'options': printf('-n 1 --with-nth 1,2 --delimiter=$''\t'' --preview "echo {3}" --preview-window "wrap" --ansi --prompt "GoDecls> " --expect=ctrl-t,ctrl-v,ctrl-x%s', colors),
\ 'sink*': function('s:sink')
\ 'sink*': function('fzf#fzf#sink')
\ }))
endfunction

Expand Down
43 changes: 43 additions & 0 deletions autoload/fzf/fzf.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
" don't spam the user when Vim is started in Vi compatibility mode
let s:cpo_save = &cpo
set cpo&vim

function! fzf#fzf#sink(str) abort
if len(a:str) < 2
return
endif

let s:current_dir = expand('%:p:h')

try
" we jump to the file directory so we can get the fullpath via fnamemodify
" below
let l:dir = go#util#Chdir(s:current_dir)

let vals = matchlist(a:str[1], '|\(.\{-}\):\(\d\+\):\(\d\+\)\s*\(.*\)|')

" i.e: main.go
let filename = vals[1]
let line = vals[2]
let col = vals[3]

" i.e: /Users/fatih/vim-go/main.go
let filepath = fnamemodify(filename, ":p")

let cmd = get({'ctrl-x': 'split',
\ 'ctrl-v': 'vertical split',
\ 'ctrl-t': 'tabe'}, a:str[0], 'e')
execute cmd fnameescape(filepath)
call cursor(line, col)
silent! norm! zvzz
finally
"jump back to old dir
call go#util#Chdir(l:dir)
endtry
endfunction

" restore Vi compatibility settings
let &cpo = s:cpo_save
unlet s:cpo_save

" vim: sw=2 ts=2 et
62 changes: 62 additions & 0 deletions autoload/fzf/implements.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
" don't spam the user when Vim is started in Vi compatibility mode
let s:cpo_save = &cpo
set cpo&vim

function! s:source(...) abort
let ret_files = []

let l:bin = "gopls"
let l:fname = expand('%:p')
let [l:line, l:col] = getpos('.')[1:2]
let pos = printf("%s:%s:%s",
\ l:fname,
\ l:line,
\ l:col
\)

let bin_path = go#path#CheckBinPath(l:bin)
if empty(bin_path)
return
endif

call go#cmd#autowrite()

let [l:out, l:err] = go#util#Exec([l:bin, 'implementation', l:pos])
if l:err
call go#util#EchoError(l:out)
return
endif

for line in split(out, '\n')
let vals = matchlist(line, '\(.\{-}\):\(\d\+\):\(\d\+\)-\(\d\+\)')
let filename = fnamemodify(expand(vals[1]), ":~:.")
let pos = printf("|%s:%s:%s|",
\ l:filename,
\ l:vals[2],
\ l:vals[3]
\)
call add(ret_files, printf("%s:%s\t%s",
\ l:filename,
\ l:vals[2],
\ pos
\))
endfor
return sort(ret_files)
endfunc


function! fzf#implements#cmd(...) abort
let l:spec = {
\ 'source': call('<sid>source', [a:000]),
\ 'sink*': function('fzf#fzf#sink'),
\ 'options': printf('-n 1 --with-nth 1 --delimiter=$''\t'' --ansi --prompt "GoImplements> " --expect=ctrl-t,ctrl-v,ctrl-x')
\ }
call fzf#run(fzf#wrap("GoImplements", fzf#vim#with_preview(l:spec, 'right:50%:nohidden')))
endfunc


" restore Vi compatibility settings
let &cpo = s:cpo_save
unlet s:cpo_save

" vim: sw=2 ts=2 et
5 changes: 5 additions & 0 deletions autoload/go/implements.vim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ function! go#implements#Implements(selected) abort
if !go#config#GoplsEnabled()
call go#util#EchoError("go_implements_mode is 'gopls', but gopls is disabled")
endif
let l:listtype = go#list#Type("GoImplements")
if l:listtype == "fzf"
call fzf#implements#cmd()
return
endif
let [l:line, l:col] = getpos('.')[1:2]
let [l:line, l:col] = go#lsp#lsp#Position(l:line, l:col)
let l:fname = expand('%:p')
Expand Down