1
0
Fork 0
mirror of synced 2024-11-17 22:45:34 -05:00
ultimate-vim/sources_non_forked/vim-go/autoload/go/def.vim

203 lines
4.7 KiB
VimL
Raw Normal View History

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
2016-05-14 07:57:54 -04:00
function! go#def#Jump(mode)
let bin_path = go#path#CheckBinPath("guru")
2015-01-18 07:58:28 -05:00
if empty(bin_path)
return
2014-10-31 17:30:24 -04:00
endif
2015-07-13 06:22:46 -04:00
let old_gopath = $GOPATH
let $GOPATH = go#path#Detect()
2016-05-14 07:57:54 -04:00
let flags = ""
if exists('g:go_guru_tags')
let tags = get(g:, 'go_guru_tags')
let flags = printf(" -tags %s", tags)
endif
2014-10-31 17:30:24 -04:00
2016-05-14 07:57:54 -04:00
let fname = fnamemodify(expand("%"), ':p:gs?\\?/?')
let command = printf("%s %s definition %s:#%s", bin_path, flags, shellescape(fname), go#util#OffsetCursor())
2014-10-31 17:30:24 -04:00
2016-05-14 07:57:54 -04:00
let out = go#util#System(command)
if go#util#ShellError() != 0
call go#util#EchoError(out)
return
endif
2016-04-12 04:31:09 -04:00
2016-05-14 07:57:54 -04:00
call s:jump_to_declaration(out, a:mode)
2015-07-13 06:22:46 -04:00
let $GOPATH = old_gopath
2014-10-31 17:30:24 -04:00
endfunction
2016-05-14 07:57:54 -04:00
function! s:jump_to_declaration(out, mode)
" strip line ending
let out = split(a:out, go#util#LineEnding())[0]
if go#util#IsWin()
let parts = split(out, '\(^[a-zA-Z]\)\@<!:')
else
let parts = split(out, ':')
endif
2014-10-31 17:30:24 -04:00
2016-05-14 07:57:54 -04:00
let filename = parts[0]
let line = parts[1]
let col = parts[2]
let ident = parts[3]
2014-10-31 17:30:24 -04:00
2016-05-14 07:57:54 -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]
2014-10-31 17:30:24 -04:00
endif
2016-05-14 07:57:54 -04:00
" 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
" 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
if get(g:, 'go_def_reuse_buffer', 0) && bufloaded(filename) != 0 && bufwinnr(filename) != -1
" jumpt to existing buffer if it exists
execute bufwinnr(filename) . 'wincmd w'
elseif a:mode == "tab"
let &switchbuf = "usetab"
if bufloaded(filename) == 0
tab split
endif
elseif a:mode == "split"
split
elseif a:mode == "vsplit"
vsplit
endif
2014-10-31 17:30:24 -04:00
2016-05-14 07:57:54 -04:00
" open the file and jump to line and column
exec 'edit '.filename
call cursor(line, col)
2014-10-31 17:30:24 -04:00
2016-05-14 07:57:54 -04:00
" also align the line to middle of the view
normal! zz
2016-04-12 04:31:09 -04:00
2016-05-14 07:57:54 -04:00
let &switchbuf = old_switchbuf
2014-10-31 17:30:24 -04:00
endfunction
2016-05-14 07:57:54 -04:00
function! go#def#SelectStackEntry()
let target_window = go#ui#GetReturnWindow()
if empty(target_window)
let target_window = winnr()
endif
2014-10-31 17:30:24 -04:00
2016-05-14 07:57:54 -04:00
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-05-14 07:57:54 -04:00
function! go#def#StackUI()
if len(s:go_stack) == 0
call go#util#EchoError("godef stack empty")
return
endif
let stackOut = ['" <Up>,<Down>:navigate <Enter>:jump <Esc>,q:exit']
2014-10-31 17:30:24 -04:00
2016-05-14 07:57:54 -04:00
let i = 0
while i < len(s:go_stack)
let entry = s:go_stack[i]
let prefix = ""
if i == s:go_stack_level
let prefix = ">"
else
let prefix = " "
endif
call add(stackOut, printf("%s %d %s|%d col %d|%s",
\ prefix, i+1, entry["file"], entry["line"], entry["col"], entry["ident"]))
let i += 1
endwhile
if s:go_stack_level == i
call add(stackOut, "> ")
endif
call go#ui#OpenWindow("GoDef Stack", stackOut, "godefstack")
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-05-14 07:57:54 -04:00
function! go#def#StackClear(...)
let s:go_stack = []
let s:go_stack_level = 0
2016-04-12 04:31:09 -04:00
endfunction
function! go#def#StackPop(...)
2016-05-14 07:57:54 -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
2016-04-12 04:31:09 -04:00
if !len(a:000)
2016-05-14 07:57:54 -04:00
let numPop = 1
2014-10-31 17:30:24 -04:00
else
2016-05-14 07:57:54 -04:00
let numPop = a:1
2016-04-12 04:31:09 -04:00
endif
2016-05-14 07:57:54 -04:00
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-05-14 07:57:54 -04:00
function! go#def#Stack(...)
if len(s:go_stack) == 0
call go#util#EchoError("godef stack empty")
return
endif
2016-04-12 04:31:09 -04:00
2016-05-14 07:57:54 -04:00
if !len(a:000)
" Display interactive stack
call go#def#StackUI()
return
else
let jumpTarget = a:1
endif
2016-04-12 04:31:09 -04:00
2016-05-14 07:57:54 -04:00
if jumpTarget !~ '^\d\+$'
if jumpTarget !~ '^\s*$'
call go#util#EchoError("location must be a number")
endif
return
endif
2016-04-12 04:31:09 -04:00
2016-05-14 07:57:54 -04:00
let jumpTarget = str2nr(jumpTarget) - 1
2016-04-12 04:31:09 -04:00
2016-05-14 07:57:54 -04:00
if jumpTarget >= 0 && jumpTarget < len(s:go_stack)
let s:go_stack_level = jumpTarget
let target = s:go_stack[s:go_stack_level]
" jump
exec 'edit '.target["file"]
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