|
| 1 | +" don't spam the user when Vim is started in Vi compatibility mode |
| 2 | +let s:cpo_save = &cpo |
| 3 | +set cpo&vim |
| 4 | + |
| 5 | +function! s:code(group, attr) abort |
| 6 | + let code = synIDattr(synIDtrans(hlID(a:group)), a:attr, "cterm") |
| 7 | + if code =~ '^[0-9]\+$' |
| 8 | + return code |
| 9 | + endif |
| 10 | +endfunction |
| 11 | + |
| 12 | +let s:temp_output = [] |
| 13 | + |
| 14 | +function! s:sink(str) abort |
| 15 | + if len(a:str) < 2 |
| 16 | + return |
| 17 | + endif |
| 18 | + try |
| 19 | + " we jump to the file directory so we can get the fullpath via fnamemodify |
| 20 | + " below |
| 21 | + let l:dir = go#util#Chdir(s:current_dir) |
| 22 | + |
| 23 | + let vals = matchlist(a:str[1], '|\(.\{-}\):\(\d\+\):\(\d\+\)\s*\(.*\)|') |
| 24 | + |
| 25 | + " i.e: main.go |
| 26 | + let filename = vals[1] |
| 27 | + let line = vals[2] |
| 28 | + let col = vals[3] |
| 29 | + |
| 30 | + " i.e: /Users/fatih/vim-go/main.go |
| 31 | + let filepath = fnamemodify(filename, ":p") |
| 32 | + |
| 33 | + let cmd = get({'ctrl-x': 'split', |
| 34 | + \ 'ctrl-v': 'vertical split', |
| 35 | + \ 'ctrl-t': 'tabe'}, a:str[0], 'e') |
| 36 | + execute cmd fnameescape(filepath) |
| 37 | + call cursor(line, col) |
| 38 | + silent! norm! zvzz |
| 39 | + finally |
| 40 | + "jump back to old dir |
| 41 | + call go#util#Chdir(l:dir) |
| 42 | + endtry |
| 43 | +endfunction |
| 44 | + |
| 45 | +function! s:source(output) abort |
| 46 | + let s:current_dir = expand('%:p:h') |
| 47 | + let ret_decls = [] |
| 48 | + |
| 49 | + for line in a:output |
| 50 | + let vals = matchlist(line, '\(.\{-}\):\(\d\+\):\(\d\+\)\s*\(.*\)') |
| 51 | + let filename = fnamemodify(expand(vals[1]), ":~:.") |
| 52 | + let pos = printf("|%s:%s:%s|", |
| 53 | + \ l:filename, |
| 54 | + \ l:vals[2], |
| 55 | + \ l:vals[3] |
| 56 | + \) |
| 57 | + call add(ret_decls, printf("%s\t%s\t%s\t%s", |
| 58 | + \ l:filename, |
| 59 | + \ l:vals[2], |
| 60 | + \ l:vals[4], |
| 61 | + \ pos |
| 62 | + \)) |
| 63 | + endfor |
| 64 | + return ret_decls |
| 65 | +endfunc |
| 66 | + |
| 67 | +function! s:parse_output(exit_val, output, title) abort |
| 68 | + if a:exit_val |
| 69 | + call go#util#EchoError(a:output) |
| 70 | + return |
| 71 | + endif |
| 72 | + |
| 73 | + let normal_fg = s:code("Normal", "fg") |
| 74 | + let normal_bg = s:code("Normal", "bg") |
| 75 | + let cursor_fg = s:code("CursorLine", "fg") |
| 76 | + let cursor_bg = s:code("CursorLine", "bg") |
| 77 | + let colors = printf(" --color %s%s%s%s%s", |
| 78 | + \ &background, |
| 79 | + \ empty(normal_fg) ? "" : (",fg:".normal_fg), |
| 80 | + \ empty(normal_bg) ? "" : (",bg:".normal_bg), |
| 81 | + \ empty(cursor_fg) ? "" : (",fg+:".cursor_fg), |
| 82 | + \ empty(cursor_bg) ? "" : (",bg+:".cursor_bg), |
| 83 | + \) |
| 84 | + call fzf#run(fzf#wrap('GoImplements', { |
| 85 | + \ 'source': call('<sid>source', [a:output]), |
| 86 | + \ 'options': printf('-n 1 --with-nth 1 --delimiter=$''\t'' --preview "bat {1} -r {2}: --color=always" --preview-window "right,nohidden" --ansi --prompt "GoImplements> " --expect=ctrl-t,ctrl-v,ctrl-x%s', colors), |
| 87 | + \ 'sink*': function('s:sink') |
| 88 | + \ })) |
| 89 | +endfunc |
| 90 | + |
| 91 | +function! fzf#implements#cmd(...) abort |
| 92 | + let [l:line, l:col] = getpos('.')[1:2] |
| 93 | + let [l:line, l:col] = go#lsp#lsp#Position(l:line, l:col) |
| 94 | + let l:fname = expand('%:p') |
| 95 | + call go#lsp#Implements(l:fname, l:line, l:col, funcref('s:parse_output')) |
| 96 | + |
| 97 | + " test output |
| 98 | + "let test_output = ['~/src/kerma/vim-go-implements/interface.go:5:15 func (i *impl) Implements() error {', |
| 99 | + " \ '~/src/kerma/vim-go-implements/other_interface.go:6:18 func (i *second) Implements() error {'] |
| 100 | + "call s:parse_output(0, l:test_output, "title") |
| 101 | +endfunction |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | +" restore Vi compatibility settings |
| 106 | +let &cpo = s:cpo_save |
| 107 | +unlet s:cpo_save |
| 108 | + |
| 109 | +" vim: sw=2 ts=2 et |
0 commit comments