From 5172db20d36a7b87b507b1f71c51d0903f3e7503 Mon Sep 17 00:00:00 2001 From: Billie Cleek Date: Sat, 26 Nov 2016 22:11:33 -0800 Subject: [PATCH] handle :GoDef from modified buffer 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 --- autoload/go/def.vim | 56 ++++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/autoload/go/def.vim b/autoload/go/def.vim index bea951178f..244df0060c 100644 --- a/autoload/go/def.vim +++ b/autoload/go/def.vim @@ -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() @@ -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]') @@ -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() @@ -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