Skip to content

Commit

Permalink
handle :GoDef from modified buffer
Browse files Browse the repository at this point in the history
Handle jumping to type definitions from a modified buffer. When the
definition is in the same file, skip trying to open the file that
contains the definition - just set the cursor position. When the
definition is in a different file and the current buffer has unsaved
modifications, use :hide edit instead of :edit.

Fixes #1125
  • Loading branch information
bhcleek committed Nov 27, 2016
1 parent c8cf792 commit 5172db2
Showing 1 changed file with 37 additions and 19 deletions.
56 changes: 37 additions & 19 deletions autoload/go/def.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function! go#def#Jump(mode)
let $GOPATH = go#path#Detect()

let fname = fnamemodify(expand("%"), ':p:gs?\\?/?')
let fnameoriginal = fname
if &modified
" Write current unsaved buffer to a temp file and use the modified content
let l:tmpname = tempname()
Expand Down Expand Up @@ -44,8 +45,8 @@ function! go#def#Jump(mode)
let flags = printf(" -tags %s", tags)
endif

let fname = shellescape(fname.':#'.go#util#OffsetCursor())
let command = printf("%s %s definition %s", bin_path, flags, fname)
let target = shellescape(fname.':#'.go#util#OffsetCursor())
let command = printf("%s %s definition %s", bin_path, flags, target)
let out = go#util#System(command)
else
call go#util#EchoError('go_def_mode value: '. bin_name .' is not valid. Valid values are: [godef, guru]')
Expand All @@ -61,11 +62,17 @@ function! go#def#Jump(mode)
return
endif

call s:jump_to_declaration(out, a:mode)
" make sure the jump location doesn't refer to the temp file that was used
" when the current buffer is &modified.
if &modified && out =~ "^" . fname . ":"
let out = substitute(out, fname, fnameoriginal, '')
endif

call s:jump_to_declaration(fnameoriginal, out, a:mode)
let $GOPATH = old_gopath
endfunction

function! s:jump_to_declaration(out, mode)
function! s:jump_to_declaration(from, out, mode)
" strip line ending
let out = split(a:out, go#util#LineEnding())[0]
if go#util#IsWin()
Expand Down Expand Up @@ -98,24 +105,35 @@ function! s:jump_to_declaration(out, mode)
" 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
if a:from != filename
" 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
" jump to existing buffer if it exists
execute bufwinnr(filename) . 'wincmd w'
else
if &modified
let cmd = 'hide edit '
else
let cmd = 'edit '
endif

if a:mode == "tab"
let &switchbuf = "usetab"
if bufloaded(filename) == 0
tab split
endif
elseif a:mode == "split"
split
elseif a:mode == "vsplit"
vsplit
endif

" open the file and jump to line and column
exec cmd . filename
endif
elseif a:mode == "split"
split
elseif a:mode == "vsplit"
vsplit
endif

" open the file and jump to line and column
exec 'edit '.filename
call cursor(line, col)

" also align the line to middle of the view
Expand Down

0 comments on commit 5172db2

Please sign in to comment.