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

Fix :GoImpl completion #1581

Merged
merged 1 commit into from
Nov 25, 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
26 changes: 9 additions & 17 deletions autoload/go/impl.vim
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
function! go#impl#Impl(...) abort
let binpath = go#path#CheckBinPath('impl')
if empty(binpath)
return
endif

let recv = ""
let iface = ""
let interactive = 0

let pos = getpos('.')

if a:0 == 0
if a:0 is 0
" Interactive mode if user didn't pass any arguments.
let recv = s:getReceiver()
let iface = input("vim-go: generating method stubs for interface: ")
Expand All @@ -19,7 +14,7 @@ function! go#impl#Impl(...) abort
call go#util#EchoError('usage: interface type is not provided')
return
endif
elseif a:0 == 1
elseif a:0 is 1
" we assume the user only passed the interface type,
" i.e: ':GoImpl io.Writer'
let recv = s:getReceiver()
Expand All @@ -41,19 +36,19 @@ function! go#impl#Impl(...) abort

try
let dirname = fnameescape(expand('%:p:h'))
let result = go#util#System(join(go#util#Shelllist([binpath, '-dir', dirname, recv, iface], ' ')))
let [result, err] = go#util#Exec(['impl', '-dir', dirname, recv, iface])
let result = substitute(result, "\n*$", "", "")
if go#util#ShellError() != 0
if err
call go#util#EchoError(result)
return
endif

if result ==# ''
if result is# ''
return
end

put =''
put =result
silent put =result
finally
call setpos('.', pos)
endtry
Expand Down Expand Up @@ -99,10 +94,6 @@ function! s:root_dirs() abort
endif

let paths = map(split(go#util#env("gopath"), go#util#PathListSep()), "substitute(v:val, '\\\\', '/', 'g')")
if go#util#ShellError()
return []
endif

if !empty(filter(paths, 'isdirectory(v:val)'))
call extend(dirs, paths)
endif
Expand All @@ -120,11 +111,12 @@ function! s:go_packages(dirs) abort
endfunction

function! s:interface_list(pkg) abort
let contents = split(go#util#System('go doc ' . a:pkg), "\n")
if go#util#ShellError()
let [contents, err] = go#util#Exec(['go', 'doc', a:pkg])
if err
return []
endif

let contents = split(contents, "\n")
call filter(contents, 'v:val =~# ''^type\s\+\h\w*\s\+interface''')
return map(contents, 'a:pkg . "." . matchstr(v:val, ''^type\s\+\zs\h\w*\ze\s\+interface'')')
endfunction
Expand Down
37 changes: 37 additions & 0 deletions autoload/go/impl_test.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
func! Test_impl() abort
try
let l:tmp = gotest#write_file('a/a.go', [
\ 'package a',
\ '',
\ ''])

call go#impl#Impl('r', 'reader', 'io.Reader')
call gotest#assert_buffer(1, [
\ 'func (r reader) Read(p []byte) (n int, err error) {',
\ ' panic("not implemented")',
\ '}'])
finally
call delete(l:tmp, 'rf')
endtry
endfunc

func! Test_impl_get() abort
try
let l:tmp = gotest#write_file('a/a.go', [
\ 'package a',
\ '',
\ 'type reader struct {}'])

call go#impl#Impl('io.Reader')
call gotest#assert_buffer(0, [
\ 'package a',
\ '',
\ 'type reader struct {}',
\ '',
\ 'func (r *reader) Read(p []byte) (n int, err error) {',
\ ' panic("not implemented")',
\ '}'])
finally
call delete(l:tmp, 'rf')
endtry
endfunc
2 changes: 1 addition & 1 deletion autoload/gotest.vim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fun! gotest#write_file(path, contents) abort
call mkdir(fnamemodify(l:full_path, ':h'), 'p')
call writefile(a:contents, l:full_path)
exe 'cd ' . l:dir . '/src'
silent exe 'e ' . a:path
silent exe 'e! ' . a:path

" Set cursor.
let l:lnum = 1
Expand Down
2 changes: 1 addition & 1 deletion ftplugin/go/commands.vim
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ command! -nargs=? -complete=file GoDecls call go#decls#Decls(0, <q-args>)
command! -nargs=? -complete=dir GoDeclsDir call go#decls#Decls(1, <q-args>)

" -- impl
command! -nargs=* -buffer -complete=customlist,go#impl#Complete GoImpl call go#impl#Impl(<f-args>)
command! -nargs=* -complete=customlist,go#impl#Complete GoImpl call go#impl#Impl(<f-args>)

" -- template
command! -nargs=0 GoTemplateAutoCreateToggle call go#template#ToggleAutoCreate()
Expand Down