Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some small :GoImpl improvements #1386

Merged
merged 1 commit into from
Sep 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 37 additions & 16 deletions autoload/go/impl.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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("<cword>")
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)
Expand All @@ -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("<cword>")
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,
Expand All @@ -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("<cword>")
if receiveType == "type"
normal! w
let receiveType = expand("<cword>")
elseif receiveType == "struct"
normal! ge
let receiveType = expand("<cword>")
endif
return printf("%s *%s", tolower(receiveType)[0], receiveType)
endfunction

if exists('*uniq')
Expand Down
2 changes: 1 addition & 1 deletion doc/vim-go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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] ...
Expand Down