2014-10-31 17:30:24 -04:00
|
|
|
" Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
" Use of this source code is governed by a BSD-style
|
|
|
|
" license that can be found in the LICENSE file.
|
|
|
|
"
|
|
|
|
" This file provides a utility function that performs auto-completion of
|
|
|
|
" package names, for use by other commands.
|
|
|
|
|
2018-12-17 06:28:27 -05:00
|
|
|
" don't spam the user when Vim is started in Vi compatibility mode
|
|
|
|
let s:cpo_save = &cpo
|
|
|
|
set cpo&vim
|
|
|
|
|
2014-10-31 17:30:24 -04:00
|
|
|
let s:goos = $GOOS
|
|
|
|
let s:goarch = $GOARCH
|
|
|
|
|
|
|
|
if len(s:goos) == 0
|
2016-06-26 07:12:36 -04:00
|
|
|
if exists('g:golang_goos')
|
|
|
|
let s:goos = g:golang_goos
|
|
|
|
elseif has('win32') || has('win64')
|
|
|
|
let s:goos = 'windows'
|
|
|
|
elseif has('macunix')
|
|
|
|
let s:goos = 'darwin'
|
|
|
|
else
|
|
|
|
let s:goos = '*'
|
|
|
|
endif
|
2014-10-31 17:30:24 -04:00
|
|
|
endif
|
|
|
|
|
|
|
|
if len(s:goarch) == 0
|
2016-06-26 07:12:36 -04:00
|
|
|
if exists('g:golang_goarch')
|
|
|
|
let s:goarch = g:golang_goarch
|
|
|
|
else
|
|
|
|
let s:goarch = '*'
|
|
|
|
endif
|
2014-10-31 17:30:24 -04:00
|
|
|
endif
|
|
|
|
|
2019-05-17 10:09:13 -04:00
|
|
|
function! s:paths() abort
|
2016-06-26 07:12:36 -04:00
|
|
|
let dirs = []
|
|
|
|
|
|
|
|
if !exists("s:goroot")
|
|
|
|
if executable('go')
|
2017-07-06 08:57:35 -04:00
|
|
|
let s:goroot = go#util#env("goroot")
|
2016-06-26 07:12:36 -04:00
|
|
|
if go#util#ShellError() != 0
|
2019-11-16 10:28:42 -05:00
|
|
|
call go#util#EchoError('`go env GOROOT` failed')
|
2016-06-26 07:12:36 -04:00
|
|
|
endif
|
|
|
|
else
|
|
|
|
let s:goroot = $GOROOT
|
2014-10-31 17:30:24 -04:00
|
|
|
endif
|
2016-06-26 07:12:36 -04:00
|
|
|
endif
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
if len(s:goroot) != 0 && isdirectory(s:goroot)
|
|
|
|
let dirs += [s:goroot]
|
|
|
|
endif
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2017-12-13 09:05:24 -05:00
|
|
|
let workspaces = split(go#path#Default(), go#util#PathListSep())
|
2016-06-26 07:12:36 -04:00
|
|
|
if workspaces != []
|
|
|
|
let dirs += workspaces
|
|
|
|
endif
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
return dirs
|
2014-10-31 17:30:24 -04:00
|
|
|
endfunction
|
|
|
|
|
2019-05-17 10:09:13 -04:00
|
|
|
function! s:module() abort
|
|
|
|
let [l:out, l:err] = go#util#ExecInDir(['go', 'list', '-m', '-f', '{{.Dir}}'])
|
|
|
|
if l:err != 0
|
|
|
|
return {}
|
|
|
|
endif
|
|
|
|
let l:dir = split(l:out, '\n')[0]
|
|
|
|
|
|
|
|
let [l:out, l:err] = go#util#ExecInDir(['go', 'list', '-m', '-f', '{{.Path}}'])
|
|
|
|
if l:err != 0
|
|
|
|
return {}
|
|
|
|
endif
|
|
|
|
let l:path = split(l:out, '\n')[0]
|
|
|
|
|
|
|
|
return {'dir': l:dir, 'path': l:path}
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:vendordirs() abort
|
|
|
|
let l:vendorsuffix = go#util#PathSep() . 'vendor'
|
|
|
|
let l:module = s:module()
|
|
|
|
if empty(l:module)
|
|
|
|
let [l:root, l:err] = go#util#ExecInDir(['go', 'list', '-f', '{{.Root}}'])
|
|
|
|
if l:err != 0
|
|
|
|
return []
|
|
|
|
endif
|
2019-08-22 11:36:17 -04:00
|
|
|
if empty(l:root)
|
|
|
|
return []
|
|
|
|
endif
|
|
|
|
|
2019-05-17 10:09:13 -04:00
|
|
|
let l:root = split(l:root, '\n')[0] . go#util#PathSep() . 'src'
|
|
|
|
|
|
|
|
let [l:dir, l:err] = go#util#ExecInDir(['go', 'list', '-f', '{{.Dir}}'])
|
|
|
|
if l:err != 0
|
|
|
|
return []
|
|
|
|
endif
|
|
|
|
let l:dir = split(l:dir, '\n')[0]
|
|
|
|
|
|
|
|
let l:vendordirs = []
|
|
|
|
while l:dir != l:root
|
|
|
|
let l:vendordir = l:dir . l:vendorsuffix
|
|
|
|
if isdirectory(l:vendordir)
|
|
|
|
let l:vendordirs = add(l:vendordirs, l:vendordir)
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:dir = fnamemodify(l:dir, ':h')
|
|
|
|
endwhile
|
|
|
|
|
|
|
|
return l:vendordirs
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:vendordir = l:module.dir . l:vendorsuffix
|
|
|
|
if !isdirectory(l:vendordir)
|
|
|
|
return []
|
|
|
|
endif
|
|
|
|
return [l:vendordir]
|
|
|
|
endfunction
|
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
let s:import_paths = {}
|
2019-08-22 11:36:17 -04:00
|
|
|
" ImportPath returns the import path of the package for current buffer. It
|
|
|
|
" returns -1 if the import path cannot be determined.
|
2017-07-06 08:57:35 -04:00
|
|
|
function! go#package#ImportPath() abort
|
2019-08-22 11:36:17 -04:00
|
|
|
let l:dir = expand("%:p:h")
|
2018-03-31 10:56:26 -04:00
|
|
|
if has_key(s:import_paths, dir)
|
2019-08-22 11:36:17 -04:00
|
|
|
return s:import_paths[l:dir]
|
2017-07-06 08:57:35 -04:00
|
|
|
endif
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2019-08-22 11:36:17 -04:00
|
|
|
let l:importpath = go#package#FromPath(l:dir)
|
|
|
|
if type(l:importpath) == type(0)
|
2016-06-26 07:12:36 -04:00
|
|
|
return -1
|
|
|
|
endif
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2019-08-22 11:36:17 -04:00
|
|
|
let s:import_paths[l:dir] = l:importpath
|
2018-03-31 10:56:26 -04:00
|
|
|
|
2018-12-17 06:28:27 -05:00
|
|
|
return l:importpath
|
2014-10-31 17:30:24 -04:00
|
|
|
endfunction
|
|
|
|
|
2019-05-17 10:09:13 -04:00
|
|
|
" go#package#FromPath returns the import path of arg. -1 is returned when arg
|
|
|
|
" does not specify a package. -2 is returned when arg is a relative path
|
2019-08-22 11:36:17 -04:00
|
|
|
" outside of GOPATH, not in a module, and not below the current working
|
|
|
|
" directory. A relative path is returned when in a null module at or below the
|
|
|
|
" current working directory..
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#package#FromPath(arg) abort
|
2018-12-17 06:28:27 -05:00
|
|
|
let l:cd = exists('*haslocaldir') && haslocaldir() ? 'lcd' : 'cd'
|
|
|
|
let l:dir = getcwd()
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2019-08-22 11:36:17 -04:00
|
|
|
let l:path = fnamemodify(a:arg, ':p')
|
2018-12-17 06:28:27 -05:00
|
|
|
if !isdirectory(l:path)
|
|
|
|
let l:path = fnamemodify(l:path, ':h')
|
|
|
|
endif
|
2016-06-26 07:12:36 -04:00
|
|
|
|
2018-12-17 06:28:27 -05:00
|
|
|
execute l:cd fnameescape(l:path)
|
2019-08-22 11:36:17 -04:00
|
|
|
try
|
|
|
|
if glob("*.go") == ""
|
|
|
|
" There's no Go code in this directory. We might be in a module directory
|
|
|
|
" which doesn't have any code at this level. To avoid `go list` making a
|
|
|
|
" bunch of HTTP requests to fetch dependencies, short-circuit `go list`
|
|
|
|
" and return -1 immediately.
|
|
|
|
if !empty(s:module())
|
|
|
|
return -1
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
let [l:out, l:err] = go#util#Exec(['go', 'list'])
|
|
|
|
if l:err != 0
|
|
|
|
return -1
|
|
|
|
endif
|
2016-06-26 07:12:36 -04:00
|
|
|
|
2019-08-22 11:36:17 -04:00
|
|
|
let l:importpath = split(l:out, '\n')[0]
|
|
|
|
finally
|
|
|
|
execute l:cd fnameescape(l:dir)
|
|
|
|
endtry
|
2018-12-17 06:28:27 -05:00
|
|
|
|
2019-08-22 11:36:17 -04:00
|
|
|
" go list returns '_CURRENTDIRECTORY' if the directory is in a null module
|
|
|
|
" (i.e. neither in GOPATH nor in a module). Return a relative import path
|
|
|
|
" if possible or an error if that is the case.
|
2018-12-17 06:28:27 -05:00
|
|
|
if l:importpath[0] ==# '_'
|
2019-08-22 11:36:17 -04:00
|
|
|
let l:relativeimportpath = fnamemodify(l:importpath[1:], ':.')
|
|
|
|
if go#util#IsWin()
|
|
|
|
let l:relativeimportpath = substitute(l:relativeimportpath, '\\', '/', 'g')
|
|
|
|
endif
|
|
|
|
|
|
|
|
if l:relativeimportpath == l:importpath[1:]
|
|
|
|
return '.'
|
|
|
|
endif
|
|
|
|
|
|
|
|
if l:relativeimportpath[0] == '/'
|
|
|
|
return -2
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:importpath= printf('./%s', l:relativeimportpath)
|
2016-06-26 07:12:36 -04:00
|
|
|
endif
|
2018-12-17 06:28:27 -05:00
|
|
|
|
|
|
|
return l:importpath
|
2014-10-31 17:30:24 -04:00
|
|
|
endfunction
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#package#CompleteMembers(package, member) abort
|
2018-12-17 06:28:27 -05:00
|
|
|
let [l:content, l:err] = go#util#Exec(['go', 'doc', a:package])
|
2018-06-14 06:31:12 -04:00
|
|
|
if l:err || !len(content)
|
2016-06-26 07:12:36 -04:00
|
|
|
return []
|
|
|
|
endif
|
2018-06-14 06:31:12 -04:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
let lines = filter(split(content, "\n"),"v:val !~ '^\\s\\+$'")
|
|
|
|
try
|
|
|
|
let mx1 = '^\s\+\(\S+\)\s\+=\s\+.*'
|
|
|
|
let mx2 = '^\%(const\|var\|type\|func\) \([A-Z][^ (]\+\).*'
|
|
|
|
let candidates = map(filter(copy(lines), 'v:val =~ mx1'),
|
|
|
|
\ 'substitute(v:val, mx1, "\\1", "")')
|
|
|
|
\ + map(filter(copy(lines), 'v:val =~ mx2'),
|
|
|
|
\ 'substitute(v:val, mx2, "\\1", "")')
|
|
|
|
return filter(candidates, '!stridx(v:val, a:member)')
|
|
|
|
catch
|
|
|
|
return []
|
|
|
|
endtry
|
2014-10-31 17:30:24 -04:00
|
|
|
endfunction
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
function! go#package#Complete(ArgLead, CmdLine, CursorPos) abort
|
2016-06-26 07:12:36 -04:00
|
|
|
let words = split(a:CmdLine, '\s\+', 1)
|
2015-07-13 06:22:46 -04:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
" do not complete package members for these commands
|
|
|
|
let neglect_commands = ["GoImportAs", "GoGuruScope"]
|
2015-07-13 06:22:46 -04:00
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
if len(words) > 2 && index(neglect_commands, words[0]) == -1
|
|
|
|
" Complete package members
|
|
|
|
return go#package#CompleteMembers(words[1], words[2])
|
|
|
|
endif
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2019-05-17 10:09:13 -04:00
|
|
|
let dirs = s:paths()
|
|
|
|
let module = s:module()
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2019-05-17 10:09:13 -04:00
|
|
|
if len(dirs) == 0 && empty(module)
|
2018-12-17 06:28:27 -05:00
|
|
|
" should not happen
|
|
|
|
return []
|
|
|
|
endif
|
2014-10-31 17:30:24 -04:00
|
|
|
|
2019-05-17 10:09:13 -04:00
|
|
|
let vendordirs = s:vendordirs()
|
|
|
|
|
2018-12-17 06:28:27 -05:00
|
|
|
let ret = {}
|
|
|
|
for dir in dirs
|
|
|
|
" this may expand to multiple lines
|
|
|
|
let root = split(expand(dir . '/pkg/' . s:goos . '_' . s:goarch), "\n")
|
2019-05-17 10:09:13 -04:00
|
|
|
let root = add(root, expand(dir . '/src'), )
|
|
|
|
let root = extend(root, vendordirs)
|
|
|
|
let root = add(root, module)
|
|
|
|
for item in root
|
|
|
|
" item may be a dictionary when operating in a module.
|
|
|
|
if type(item) == type({})
|
|
|
|
if empty(item)
|
2018-12-17 06:28:27 -05:00
|
|
|
continue
|
|
|
|
endif
|
2019-05-17 10:09:13 -04:00
|
|
|
let dir = item.dir
|
|
|
|
let path = item.path
|
|
|
|
else
|
|
|
|
let dir = item
|
|
|
|
let path = item
|
|
|
|
endif
|
|
|
|
|
|
|
|
if !empty(module) && dir ==# module.dir
|
|
|
|
if stridx(a:ArgLead, module.path) == 0
|
|
|
|
if len(a:ArgLead) != len(module.path)
|
|
|
|
let glob = globpath(module.dir, substitute(a:ArgLead, module.path . '/\?', '', '').'*')
|
|
|
|
else
|
|
|
|
let glob = module.dir
|
|
|
|
endif
|
|
|
|
elseif stridx(module.path, a:ArgLead) == 0 && stridx(module.path, '/', len(a:ArgLead)) < 0
|
|
|
|
" use the module directory when a:ArgLead is contained in
|
|
|
|
" module.path and module.path does not have any path segments after
|
|
|
|
" a:ArgLead.
|
|
|
|
let glob = module.dir
|
|
|
|
else
|
|
|
|
continue
|
|
|
|
endif
|
|
|
|
else
|
|
|
|
let glob = globpath(dir, a:ArgLead.'*')
|
|
|
|
endif
|
|
|
|
for candidate in split(glob)
|
|
|
|
if isdirectory(candidate)
|
|
|
|
" TODO(bc): use wildignore instead of filtering out vendor
|
|
|
|
" directories manually?
|
|
|
|
if fnamemodify(candidate, ':t') == 'vendor'
|
|
|
|
continue
|
|
|
|
endif
|
|
|
|
let candidate .= '/'
|
|
|
|
elseif candidate !~ '\.a$'
|
|
|
|
continue
|
|
|
|
endif
|
|
|
|
|
|
|
|
if dir !=# path
|
|
|
|
let candidate = substitute(candidate, '^' . dir, path, 'g')
|
|
|
|
else
|
|
|
|
let candidate = candidate[len(dir)+1:]
|
|
|
|
endif
|
|
|
|
" replace a backslash with a forward slash and drop .a suffixes
|
|
|
|
let candidate = substitute(substitute(candidate, '[\\]', '/', 'g'),
|
2018-12-17 06:28:27 -05:00
|
|
|
\ '\.a$', '', 'g')
|
|
|
|
|
|
|
|
" without this the result can have duplicates in form of
|
|
|
|
" 'encoding/json' and '/encoding/json/'
|
2019-05-17 10:09:13 -04:00
|
|
|
let candidate = go#util#StripPathSep(candidate)
|
2018-12-17 06:28:27 -05:00
|
|
|
|
2019-05-17 10:09:13 -04:00
|
|
|
let ret[candidate] = candidate
|
2018-12-17 06:28:27 -05:00
|
|
|
endfor
|
2014-10-31 17:30:24 -04:00
|
|
|
endfor
|
2018-12-17 06:28:27 -05:00
|
|
|
endfor
|
|
|
|
return sort(keys(ret))
|
2014-10-31 17:30:24 -04:00
|
|
|
endfunction
|
|
|
|
|
2018-12-17 06:28:27 -05:00
|
|
|
" restore Vi compatibility settings
|
|
|
|
let &cpo = s:cpo_save
|
|
|
|
unlet s:cpo_save
|
|
|
|
|
2016-06-26 07:12:36 -04:00
|
|
|
" vim: sw=2 ts=2 et
|