1
0
Fork 0
mirror of synced 2024-11-04 16:38:59 -05:00
ultimate-vim/sources_non_forked/vim-go/autoload/go/def.vim

359 lines
9.5 KiB
VimL
Raw Normal View History

2018-12-17 06:28:27 -05:00
" don't spam the user when Vim is started in Vi compatibility mode
let s:cpo_save = &cpo
set cpo&vim
2016-05-14 07:57:54 -04:00
let s:go_stack = []
let s:go_stack_level = 0
2014-10-31 17:30:24 -04:00
2019-03-27 11:08:56 -04:00
function! go#def#Jump(mode, type) abort
2019-08-22 11:36:17 -04:00
let l:fname = fnamemodify(expand("%"), ':p:gs?\\?/?')
2016-06-26 07:12:36 -04:00
" so guru right now is slow for some people. previously we were using
" godef which also has it's own quirks. But this issue come up so many
" times I've decided to support both. By default we still use guru as it
" covers all edge cases, but now anyone can switch to godef if they wish
2018-06-14 06:31:12 -04:00
let bin_name = go#config#DefMode()
2016-06-26 07:12:36 -04:00
if bin_name == 'godef'
2018-12-17 06:28:27 -05:00
let l:cmd = ['godef',
2018-06-14 06:31:12 -04:00
\ '-f=' . l:fname,
\ '-o=' . go#util#OffsetCursor(),
2018-12-17 06:28:27 -05:00
\ '-t']
2016-06-26 07:12:36 -04:00
2018-12-17 06:28:27 -05:00
if &modified
let l:stdin_content = join(go#util#GetLines(), "\n")
call add(l:cmd, "-i")
2019-03-08 06:04:56 -05:00
let [l:out, l:err] = go#util#ExecInDir(l:cmd, l:stdin_content)
2018-12-17 06:28:27 -05:00
else
2019-03-08 06:04:56 -05:00
let [l:out, l:err] = go#util#ExecInDir(l:cmd)
2018-12-17 06:28:27 -05:00
endif
2018-06-14 06:31:12 -04:00
elseif bin_name == 'guru'
2018-07-19 08:52:53 -04:00
let cmd = [go#path#CheckBinPath(bin_name)]
let buildtags = go#config#BuildTags()
if buildtags isnot ''
let cmd += ['-tags', buildtags]
endif
2016-12-27 09:46:49 -05:00
let stdin_content = ""
if &modified
2018-12-17 06:28:27 -05:00
let content = join(go#util#GetLines(), "\n")
2016-12-27 09:46:49 -05:00
let stdin_content = fname . "\n" . strlen(content) . "\n" . content
call add(cmd, "-modified")
endif
2018-06-14 06:31:12 -04:00
call extend(cmd, ["definition", fname . ':#' . go#util#OffsetCursor()])
2016-12-27 09:46:49 -05:00
2018-09-24 20:40:17 -04:00
if go#util#has_job()
let l:state = {}
2016-12-27 09:46:49 -05:00
let l:spawn_args = {
\ 'cmd': cmd,
2018-09-24 20:40:17 -04:00
\ 'complete': function('s:jump_to_declaration_cb', [a:mode, bin_name], l:state),
2018-08-25 12:13:42 -04:00
\ 'for': '_',
2018-09-24 20:40:17 -04:00
\ 'statustype': 'searching declaration',
2016-12-27 09:46:49 -05:00
\ }
if &modified
let l:spawn_args.input = stdin_content
endif
2018-09-24 20:40:17 -04:00
call s:def_job(spawn_args, l:state)
2016-12-27 09:46:49 -05:00
return
endif
2016-08-02 08:48:32 -04:00
if &modified
2019-03-08 06:04:56 -05:00
let [l:out, l:err] = go#util#ExecInDir(l:cmd, l:stdin_content)
2016-08-02 08:48:32 -04:00
else
2019-03-08 06:04:56 -05:00
let [l:out, l:err] = go#util#ExecInDir(l:cmd)
2016-08-02 08:48:32 -04:00
endif
2019-03-27 11:08:56 -04:00
elseif bin_name == 'gopls'
2019-08-22 11:36:17 -04:00
let [l:line, l:col] = go#lsp#lsp#Position()
2019-03-27 11:08:56 -04:00
" delegate to gopls, with an empty job object and an exit status of 0
" (they're irrelevant for gopls).
if a:type
call go#lsp#TypeDef(l:fname, l:line, l:col, function('s:jump_to_declaration_cb', [a:mode, 'gopls', {}, 0]))
else
call go#lsp#Definition(l:fname, l:line, l:col, function('s:jump_to_declaration_cb', [a:mode, 'gopls', {}, 0]))
endif
return
2016-06-26 07:12:36 -04:00
else
2019-03-27 11:08:56 -04:00
call go#util#EchoError('go_def_mode value: '. bin_name .' is not valid. Valid values are: [godef, guru, gopls]')
2016-06-26 07:12:36 -04:00
return
endif
2018-06-14 06:31:12 -04:00
if l:err
2016-06-26 07:12:36 -04:00
call go#util#EchoError(out)
return
endif
2017-02-11 08:01:38 -05:00
call go#def#jump_to_declaration(out, a:mode, bin_name)
2014-10-31 17:30:24 -04:00
endfunction
2018-09-24 20:40:17 -04:00
function! s:jump_to_declaration_cb(mode, bin_name, job, exit_status, data) abort dict
2016-12-27 09:46:49 -05:00
if a:exit_status != 0
return
endif
2017-02-11 08:01:38 -05:00
call go#def#jump_to_declaration(a:data[0], a:mode, a:bin_name)
2018-09-24 20:40:17 -04:00
2019-03-27 11:08:56 -04:00
" capture the active window so that callbacks for jobs, exit_cb and
" close_cb, and callbacks for gopls can return to it when a:mode caused a
" split.
2018-09-24 20:40:17 -04:00
let self.winid = win_getid(winnr())
2016-12-27 09:46:49 -05:00
endfunction
2019-03-27 11:08:56 -04:00
" go#def#jump_to_declaration parses out (expected to be
" 'filename:line:col: message').
2017-02-11 08:01:38 -05:00
function! go#def#jump_to_declaration(out, mode, bin_name) abort
2016-12-27 09:46:49 -05:00
let final_out = a:out
if a:bin_name == "godef"
2019-03-27 11:08:56 -04:00
" append the type information to the same line so it will be parsed
" correctly using guru's output format.
2016-12-27 09:46:49 -05:00
" This makes it compatible with guru output.
let final_out = join(split(a:out, '\n'), ':')
endif
2016-06-26 07:12:36 -04:00
" strip line ending
2016-12-27 09:46:49 -05:00
let out = split(final_out, go#util#LineEnding())[0]
2016-06-26 07:12:36 -04:00
if go#util#IsWin()
let parts = split(out, '\(^[a-zA-Z]\)\@<!:')
else
let parts = split(out, ':')
endif
2019-03-08 06:04:56 -05:00
if len(parts) == 0
call go#util#EchoError('go jump_to_declaration '. a:bin_name .' output is not valid.')
return
endif
let line = 1
let col = 1
let ident = 0
2016-06-26 07:12:36 -04:00
let filename = parts[0]
2019-03-08 06:04:56 -05:00
if len(parts) > 1
let line = parts[1]
endif
if len(parts) > 2
let col = parts[2]
endif
if len(parts) > 3
let ident = parts[3]
endif
2016-06-26 07:12:36 -04:00
" Remove anything newer than the current position, just like basic
" vim tag support
if s:go_stack_level == 0
let s:go_stack = []
else
let s:go_stack = s:go_stack[0:s:go_stack_level-1]
endif
" increment the stack counter
let s:go_stack_level += 1
" push it on to the jumpstack
let stack_entry = {'line': line("."), 'col': col("."), 'file': expand('%:p'), 'ident': ident}
call add(s:go_stack, stack_entry)
" needed for restoring back user setting this is because there are two
" modes of switchbuf which we need based on the split mode
let old_switchbuf = &switchbuf
2016-10-02 07:37:21 -04:00
normal! m'
if filename != fnamemodify(expand("%"), ':p:gs?\\?/?')
2016-08-02 08:48:32 -04:00
" jump to existing buffer if, 1. we have enabled it, 2. the buffer is loaded
" and 3. there is buffer window number we switch to
2019-11-16 10:28:42 -05:00
if go#config#DefReuseBuffer() && bufwinnr(filename) != -1
" jump to existing buffer if it exists
call win_gotoid(bufwinnr(filename))
2016-12-27 09:46:49 -05:00
else
if &modified
let cmd = 'hide edit'
else
let cmd = 'edit'
endif
if a:mode == "tab"
2017-09-02 06:43:18 -04:00
let &switchbuf = "useopen,usetab,newtab"
2016-12-27 09:46:49 -05:00
if bufloaded(filename) == 0
tab split
2017-09-02 06:43:18 -04:00
else
let cmd = 'sbuf'
2016-12-27 09:46:49 -05:00
endif
elseif a:mode == "split"
split
elseif a:mode == "vsplit"
vsplit
2016-08-02 08:48:32 -04:00
endif
2016-06-26 07:12:36 -04:00
2016-12-27 09:46:49 -05:00
" open the file and jump to line and column
2019-11-16 10:28:42 -05:00
try
exec cmd fnameescape(fnamemodify(filename, ':.'))
catch
if stridx(v:exception, ':E325:') < 0
call go#util#EchoError(v:exception)
endif
endtry
2016-12-27 09:46:49 -05:00
endif
2016-08-02 08:48:32 -04:00
endif
2016-06-26 07:12:36 -04:00
call cursor(line, col)
" also align the line to middle of the view
normal! zz
let &switchbuf = old_switchbuf
2014-10-31 17:30:24 -04:00
endfunction
2016-12-27 09:46:49 -05:00
function! go#def#SelectStackEntry() abort
2016-06-26 07:12:36 -04:00
let target_window = go#ui#GetReturnWindow()
if empty(target_window)
let target_window = winnr()
endif
let highlighted_stack_entry = matchstr(getline("."), '^..\zs\(\d\+\)')
if !empty(highlighted_stack_entry)
execute target_window . "wincmd w"
call go#def#Stack(str2nr(highlighted_stack_entry))
endif
call go#ui#CloseWindow()
2014-10-31 17:30:24 -04:00
endfunction
2016-12-27 09:46:49 -05:00
function! go#def#StackUI() abort
2016-06-26 07:12:36 -04:00
if len(s:go_stack) == 0
call go#util#EchoError("godef stack empty")
return
endif
2016-06-11 09:56:50 -04:00
2016-06-26 07:12:36 -04:00
let stackOut = ['" <Up>,<Down>:navigate <Enter>:jump <Esc>,q:exit']
2016-06-11 09:56:50 -04:00
2016-06-26 07:12:36 -04:00
let i = 0
while i < len(s:go_stack)
let entry = s:go_stack[i]
let prefix = ""
2016-06-11 09:56:50 -04:00
2016-06-26 07:12:36 -04:00
if i == s:go_stack_level
let prefix = ">"
else
let prefix = " "
endif
2016-06-11 09:56:50 -04:00
2017-03-07 12:04:28 -05:00
call add(stackOut, printf("%s %d %s|%d col %d|%s",
2016-06-26 07:12:36 -04:00
\ prefix, i+1, entry["file"], entry["line"], entry["col"], entry["ident"]))
let i += 1
endwhile
2016-06-11 09:56:50 -04:00
2016-06-26 07:12:36 -04:00
if s:go_stack_level == i
call add(stackOut, "> ")
endif
2016-06-11 09:56:50 -04:00
2016-06-26 07:12:36 -04:00
call go#ui#OpenWindow("GoDef Stack", stackOut, "godefstack")
2016-06-11 09:56:50 -04:00
2016-06-26 07:12:36 -04:00
noremap <buffer> <silent> <CR> :<C-U>call go#def#SelectStackEntry()<CR>
noremap <buffer> <silent> <Esc> :<C-U>call go#ui#CloseWindow()<CR>
noremap <buffer> <silent> q :<C-U>call go#ui#CloseWindow()<CR>
2016-04-12 04:31:09 -04:00
endfunction
2016-12-27 09:46:49 -05:00
function! go#def#StackClear(...) abort
2016-06-26 07:12:36 -04:00
let s:go_stack = []
let s:go_stack_level = 0
2016-04-12 04:31:09 -04:00
endfunction
2016-12-27 09:46:49 -05:00
function! go#def#StackPop(...) abort
2016-06-26 07:12:36 -04:00
if len(s:go_stack) == 0
call go#util#EchoError("godef stack empty")
return
endif
if s:go_stack_level == 0
call go#util#EchoError("at bottom of the godef stack")
return
endif
if !len(a:000)
let numPop = 1
else
let numPop = a:1
endif
let newLevel = str2nr(s:go_stack_level) - str2nr(numPop)
call go#def#Stack(newLevel + 1)
2016-04-12 04:31:09 -04:00
endfunction
2016-12-27 09:46:49 -05:00
function! go#def#Stack(...) abort
2016-06-26 07:12:36 -04:00
if len(s:go_stack) == 0
call go#util#EchoError("godef stack empty")
return
endif
if !len(a:000)
" Display interactive stack
call go#def#StackUI()
return
else
let jumpTarget = a:1
endif
if jumpTarget !~ '^\d\+$'
if jumpTarget !~ '^\s*$'
call go#util#EchoError("location must be a number")
endif
return
endif
let jumpTarget = str2nr(jumpTarget) - 1
if jumpTarget >= 0 && jumpTarget < len(s:go_stack)
let s:go_stack_level = jumpTarget
let target = s:go_stack[s:go_stack_level]
" jump
2016-12-27 09:46:49 -05:00
if expand('%:p') != target["file"]
if &modified
exec 'hide edit' target["file"]
else
exec 'edit' target["file"]
endif
endif
2016-06-26 07:12:36 -04:00
call cursor(target["line"], target["col"])
normal! zz
else
call go#util#EchoError("invalid location. Try :GoDefStack to see the list of valid entries")
endif
2014-10-31 17:30:24 -04:00
endfunction
2016-05-14 07:57:54 -04:00
2018-09-24 20:40:17 -04:00
function s:def_job(args, state) abort
2018-07-19 08:52:53 -04:00
let l:start_options = go#job#Options(a:args)
2016-12-27 09:46:49 -05:00
2018-09-24 20:40:17 -04:00
let l:state = a:state
function! s:exit_cb(next, job, exitval) dict
call call(a:next, [a:job, a:exitval])
if has_key(self, 'winid')
call win_gotoid(self.winid)
endif
endfunction
let l:start_options.exit_cb = funcref('s:exit_cb', [l:start_options.exit_cb], l:state)
function! s:close_cb(next, ch) dict
call call(a:next, [a:ch])
if has_key(self, 'winid')
call win_gotoid(self.winid)
endif
endfunction
let l:start_options.close_cb = funcref('s:close_cb', [l:start_options.close_cb], l:state)
2016-12-27 09:46:49 -05:00
if &modified
let l:tmpname = tempname()
call writefile(split(a:args.input, "\n"), l:tmpname, "b")
let l:start_options.in_io = "file"
let l:start_options.in_name = l:tmpname
endif
2018-09-24 20:40:17 -04:00
call go#job#Start(a:args.cmd, l:start_options)
2016-12-27 09:46:49 -05:00
endfunction
2018-12-17 06:28:27 -05:00
" restore Vi compatibility settings
let &cpo = s:cpo_save
unlet s:cpo_save
2016-06-26 07:12:36 -04:00
" vim: sw=2 ts=2 et