diff --git a/autoload/go/impl.vim b/autoload/go/impl.vim index 940376bfb2..57c81c4de5 100644 --- a/autoload/go/impl.vim +++ b/autoload/go/impl.vim @@ -6,11 +6,13 @@ function! go#impl#Impl(...) abort let recv = "" let iface = "" + let interactive = 0 + + let pos = getpos('.') if a:0 == 0 - " user didn't passed anything, just called ':GoImpl' - let receiveType = expand("") - let recv = printf("%s *%s", tolower(receiveType)[0], receiveType) + " Interactive mode if user didn't pass any arguments. + let recv = s:getReceiver() let iface = input("vim-go: generating method stubs for interface: ") redraw! if empty(iface) @@ -20,8 +22,7 @@ function! go#impl#Impl(...) abort elseif a:0 == 1 " we assume the user only passed the interface type, " i.e: ':GoImpl io.Writer' - let receiveType = expand("") - let recv = printf("%s *%s", tolower(receiveType)[0], receiveType) + let recv = s:getReceiver() let iface = a:1 elseif a:0 > 2 " user passed receiver and interface type both, @@ -33,20 +34,40 @@ function! go#impl#Impl(...) abort return endif - let result = go#util#System(join(go#util#Shelllist([binpath, recv, iface], ' '))) - if go#util#ShellError() != 0 - call go#util#EchoError(result) - return + " Make sure we put the generated code *after* the struct. + if getline(".") =~ "struct " + normal! $% endif - if result ==# '' - return - end + try + let result = go#util#System(join(go#util#Shelllist([binpath, recv, iface], ' '))) + let result = substitute(result, "\n*$", "", "") + if go#util#ShellError() != 0 + call go#util#EchoError(result) + return + endif - let pos = getpos('.') - put ='' - put =result - call setpos('.', pos) + if result ==# '' + return + end + + put ='' + put =result + finally + call setpos('.', pos) + endtry +endfunction + +function! s:getReceiver() + let receiveType = expand("") + if receiveType == "type" + normal! w + let receiveType = expand("") + elseif receiveType == "struct" + normal! ge + let receiveType = expand("") + endif + return printf("%s *%s", tolower(receiveType)[0], receiveType) endfunction if exists('*uniq') diff --git a/doc/vim-go.txt b/doc/vim-go.txt index 1b0e575ec9..d5d12d045b 100644 --- a/doc/vim-go.txt +++ b/doc/vim-go.txt @@ -676,7 +676,7 @@ CTRL-t receiver and the interface needs to be specified. Example usages: > :GoImpl f *Foo io.Writer - :GoImpl T io.ReadWriteCloser + :GoImpl t Type io.ReadWriteCloser < *:GoAddTags* :[range]GoAddTags [key],[option] [key1],[option] ...