2016-12-27 09:46:49 -05:00
|
|
|
function! go#impl#Impl(...) abort
|
2016-06-26 07:12:36 -04:00
|
|
|
let recv = ""
|
|
|
|
let iface = ""
|
2017-11-24 08:54:40 -05:00
|
|
|
let interactive = 0
|
|
|
|
|
|
|
|
let pos = getpos('.')
|
2016-05-14 07:57:54 -04:00
|
|
|
|
2017-12-13 09:05:24 -05:00
|
|
|
if a:0 is 0
|
2017-11-24 08:54:40 -05:00
|
|
|
" Interactive mode if user didn't pass any arguments.
|
|
|
|
let recv = s:getReceiver()
|
2016-06-26 07:12:36 -04:00
|
|
|
let iface = input("vim-go: generating method stubs for interface: ")
|
|
|
|
redraw!
|
|
|
|
if empty(iface)
|
|
|
|
call go#util#EchoError('usage: interface type is not provided')
|
|
|
|
return
|
2016-05-14 07:57:54 -04:00
|
|
|
endif
|
2017-12-13 09:05:24 -05:00
|
|
|
elseif a:0 is 1
|
2017-03-07 12:04:28 -05:00
|
|
|
" we assume the user only passed the interface type,
|
2016-06-26 07:12:36 -04:00
|
|
|
" i.e: ':GoImpl io.Writer'
|
2017-11-24 08:54:40 -05:00
|
|
|
let recv = s:getReceiver()
|
2016-06-26 07:12:36 -04:00
|
|
|
let iface = a:1
|
|
|
|
elseif a:0 > 2
|
|
|
|
" user passed receiver and interface type both,
|
|
|
|
" i.e: 'GoImpl f *Foo io.Writer'
|
|
|
|
let recv = join(a:000[:-2], ' ')
|
|
|
|
let iface = a:000[-1]
|
|
|
|
else
|
|
|
|
call go#util#EchoError('usage: GoImpl {receiver} {interface}')
|
|
|
|
return
|
|
|
|
endif
|
2016-05-14 07:57:54 -04:00
|
|
|
|
2017-11-24 08:54:40 -05:00
|
|
|
" Make sure we put the generated code *after* the struct.
|
|
|
|
if getline(".") =~ "struct "
|
|
|
|
normal! $%
|
2016-06-26 07:12:36 -04:00
|
|
|
endif
|
2016-05-14 07:57:54 -04:00
|
|
|
|
2017-11-24 08:54:40 -05:00
|
|
|
try
|
|
|
|
let dirname = fnameescape(expand('%:p:h'))
|
2017-12-13 09:05:24 -05:00
|
|
|
let [result, err] = go#util#Exec(['impl', '-dir', dirname, recv, iface])
|
2017-11-24 08:54:40 -05:00
|
|
|
let result = substitute(result, "\n*$", "", "")
|
2017-12-13 09:05:24 -05:00
|
|
|
if err
|
2017-11-24 08:54:40 -05:00
|
|
|
call go#util#EchoError(result)
|
|
|
|
return
|
|
|
|
endif
|
2016-05-14 07:57:54 -04:00
|
|
|
|
2017-12-13 09:05:24 -05:00
|
|
|
if result is# ''
|
2017-11-24 08:54:40 -05:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
put =''
|
2017-12-13 09:05:24 -05:00
|
|
|
silent put =result
|
2017-11-24 08:54:40 -05:00
|
|
|
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)
|
2016-05-14 07:57:54 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
if exists('*uniq')
|
2016-06-26 07:12:36 -04:00
|
|
|
function! s:uniq(list)
|
|
|
|
return uniq(a:list)
|
|
|
|
endfunction
|
2016-05-14 07:57:54 -04:00
|
|
|
else
|
2016-06-26 07:12:36 -04:00
|
|
|
" Note: Believe that the list is sorted
|
|
|
|
function! s:uniq(list)
|
|
|
|
let i = len(a:list) - 1
|
|
|
|
while 0 < i
|
|
|
|
if a:list[i-1] ==# a:list[i]
|
|
|
|
call remove(a:list, i)
|
|
|
|
let i -= 2
|
|
|
|
else
|
|
|
|
let i -= 1
|
|
|
|
endif
|
|
|
|
endwhile
|
|
|
|
return a:list
|
|
|
|
endfunction
|
2016-05-14 07:57:54 -04:00
|
|
|
endif
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
function! s:root_dirs() abort
|
2016-06-26 07:12:36 -04:00
|
|
|
let dirs = []
|
2017-07-06 08:57:35 -04:00
|
|
|
let root = go#util#env("goroot")
|
2016-06-26 07:12:36 -04:00
|
|
|
if root !=# '' && isdirectory(root)
|
|
|
|
call add(dirs, root)
|
|
|
|
endif
|
2016-05-14 07:57:54 -04:00
|
|
|
|
2017-07-06 08:57:35 -04:00
|
|
|
let paths = map(split(go#util#env("gopath"), go#util#PathListSep()), "substitute(v:val, '\\\\', '/', 'g')")
|
2016-06-26 07:12:36 -04:00
|
|
|
if !empty(filter(paths, 'isdirectory(v:val)'))
|
|
|
|
call extend(dirs, paths)
|
|
|
|
endif
|
2016-05-14 07:57:54 -04:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
return dirs
|
2016-05-14 07:57:54 -04:00
|
|
|
endfunction
|
|
|
|
|
2018-07-19 08:52:53 -04:00
|
|
|
function! s:go_packages(dirs, arglead) abort
|
2016-06-26 07:12:36 -04:00
|
|
|
let pkgs = []
|
2018-07-19 08:52:53 -04:00
|
|
|
for dir in a:dirs
|
|
|
|
" this may expand to multiple lines
|
|
|
|
let scr_root = expand(dir . '/src/')
|
|
|
|
for pkg in split(globpath(scr_root, a:arglead.'*'), "\n")
|
|
|
|
if isdirectory(pkg)
|
|
|
|
let pkg .= '/'
|
|
|
|
elseif pkg !~ '\.a$'
|
|
|
|
continue
|
|
|
|
endif
|
|
|
|
|
|
|
|
" without this the result can have duplicates in form of
|
|
|
|
" 'encoding/json' and '/encoding/json/'
|
|
|
|
let pkg = go#util#StripPathSep(pkg)
|
|
|
|
|
|
|
|
" remove the scr root and keep the package in tact
|
|
|
|
let pkg = substitute(pkg, scr_root, "", "")
|
|
|
|
call add(pkgs, pkg)
|
|
|
|
endfor
|
2016-06-26 07:12:36 -04:00
|
|
|
endfor
|
2018-07-19 08:52:53 -04:00
|
|
|
|
|
|
|
return pkgs
|
2016-05-14 07:57:54 -04:00
|
|
|
endfunction
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
function! s:interface_list(pkg) abort
|
2017-12-13 09:05:24 -05:00
|
|
|
let [contents, err] = go#util#Exec(['go', 'doc', a:pkg])
|
|
|
|
if err
|
2016-06-26 07:12:36 -04:00
|
|
|
return []
|
|
|
|
endif
|
2016-05-14 07:57:54 -04:00
|
|
|
|
2017-12-13 09:05:24 -05:00
|
|
|
let contents = split(contents, "\n")
|
2016-06-26 07:12:36 -04:00
|
|
|
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'')')
|
2016-05-14 07:57:54 -04:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Complete package and interface for {interface}
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#impl#Complete(arglead, cmdline, cursorpos) abort
|
2016-06-26 07:12:36 -04:00
|
|
|
let words = split(a:cmdline, '\s\+', 1)
|
2018-07-19 08:52:53 -04:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
if words[-1] ==# ''
|
2018-07-19 08:52:53 -04:00
|
|
|
" if no words are given, just start completing the first package we found
|
|
|
|
return s:uniq(sort(s:go_packages(s:root_dirs(), a:arglead)))
|
|
|
|
elseif words[-1] =~# '^\(\h\w.*\.\%(\h\w*\)\=$\)\@!\S*$'
|
|
|
|
" start matching go packages. It's negate match of the below match
|
|
|
|
return s:uniq(sort(s:go_packages(s:root_dirs(), a:arglead)))
|
|
|
|
elseif words[-1] =~# '^\h\w.*\.\%(\h\w*\)\=$'
|
|
|
|
" match the following, anything that could indicate an interface candidate
|
|
|
|
"
|
|
|
|
" io.
|
|
|
|
" io.Wr
|
|
|
|
" github.com/fatih/color.
|
|
|
|
" github.com/fatih/color.U
|
|
|
|
" github.com/fatih/color.Un
|
|
|
|
let splitted = split(words[-1], '\.', 1)
|
|
|
|
let pkg = join(splitted[:-2], '.')
|
|
|
|
let interface = splitted[-1]
|
2016-06-26 07:12:36 -04:00
|
|
|
return s:uniq(sort(filter(s:interface_list(pkg), 'v:val =~? words[-1]')))
|
|
|
|
else
|
|
|
|
return []
|
|
|
|
endif
|
2016-05-14 07:57:54 -04:00
|
|
|
endfunction
|
2016-06-26 07:12:36 -04:00
|
|
|
|
|
|
|
" vim: sw=2 ts=2 et
|