Skip to content

Commit

Permalink
POC for fatih#3319 - add fzf wrap for GoImplements
Browse files Browse the repository at this point in the history
  • Loading branch information
kerma committed Nov 7, 2021
1 parent 2831f48 commit c35a926
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 32 deletions.
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
6 changes: 6 additions & 0 deletions autoload/go/implements.vim
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ function! go#implements#Implements(selected) abort
let l:fname = expand('%:p')
call go#lsp#Implements(l:fname, l:line, l:col, funcref('s:parse_output'))
return
elseif l:mode == 'fzf'
if !go#config#GoplsEnabled()
call go#util#EchoError("go_implements_mode is 'fzf', but gopls is disabled")
endif
call fzf#implements#cmd()
return
else
call go#util#EchoWarning('unknown value for g:go_implements_mode')
endif
Expand Down

0 comments on commit c35a926

Please sign in to comment.