@@ -28,43 +28,75 @@ if len(s:goarch) == 0
2828 endif
2929endif
3030
31+ function ! go#complete#PackageMembers (package, member)
32+ silent ! let content = system (' godoc ' . a: package )
33+ if v: shell_error || ! len (content)
34+ return []
35+ endif
36+ let lines = filter (split (content, " \n " )," v:val !~ '^\\ s\\ +$'" )
37+ try
38+ let mx1 = ' ^\s\+\(\S+\)\s\+=\s\+.*'
39+ let mx2 = ' ^\%(const\|var\|type\|func\) \([A-Z][^ (]\+\).*'
40+ let candidates =
41+ \ map (filter (copy (lines ), ' v:val =~ mx1' ), ' substitute(v:val, mx1, "\\1", "")' )
42+ \ + map (filter (copy (lines ), ' v:val =~ mx2' ), ' substitute(v:val, mx2, "\\1", "")' )
43+ return filter (candidates, ' !stridx(v:val, a:member)' )
44+ catch
45+ return []
46+ endtry
47+ endfunction
48+
3149function ! go#complete#Package (ArgLead, CmdLine, CursorPos)
3250 let dirs = []
3351
52+ let words = split (a: CmdLine , ' \s\+' , 1 )
53+ if len (words) > 2
54+ " Complete package members
55+ return go#complete#PackageMembers (words[1 ], words[2 ])
56+ endif
57+
3458 if executable (' go' )
35- let goroot = substitute (system (' go env GOROOT' ), ' \n' , ' ' , ' g' )
36- if v: shell_error
37- echo ' \' go env GOROOT\' failed'
38- endif
59+ let goroot = substitute (system (' go env GOROOT' ), ' \n' , ' ' , ' g' )
60+ if v: shell_error
61+ echomsg ' \' go env GOROOT\' failed'
62+ endif
3963 else
40- let goroot = $GOROOT
64+ let goroot = $GOROOT
4165 endif
4266
4367 if len (goroot) != 0 && isdirectory (goroot)
44- let dirs += [ goroot ]
68+ let dirs += [goroot]
4569 endif
4670
47- let workspaces = split ($GOPATH , ' :' )
71+ let pathsep = ' :'
72+ if s: goos == ' windows'
73+ let pathsep = ' ;'
74+ endif
75+ let workspaces = split ($GOPATH , pathsep)
4876 if workspaces != []
49- let dirs += workspaces
77+ let dirs += workspaces
5078 endif
5179
5280 if len (dirs) == 0
53- " should not happen
54- return []
81+ " should not happen
82+ return []
5583 endif
5684
5785 let ret = {}
5886 for dir in dirs
59- let root = expand (dir . ' /pkg/' . s: goos . ' _' . s: goarch )
60- for i in split (globpath (root, a: ArgLead .' *' ), " \n " )
61- if isdirectory (i )
62- let i .= ' /'
63- elseif i !~ ' \.a$'
64- continue
65- endif
66- let i = substitute (substitute (i [len (root)+ 1 :], ' [\\]' , ' /' , ' g' ), ' \.a$' , ' ' , ' g' )
67- let ret [i ] = i
87+ " this may expand to multiple lines
88+ let root = split (expand (dir . ' /pkg/' . s: goos . ' _' . s: goarch ), " \n " )
89+ call add (root, expand (dir . ' /src' ))
90+ for r in root
91+ for i in split (globpath (r , a: ArgLead .' *' ), " \n " )
92+ if isdirectory (i )
93+ let i .= ' /'
94+ elseif i !~ ' \.a$'
95+ continue
96+ endif
97+ let i = substitute (substitute (i [len (r )+ 1 :], ' [\\]' , ' /' , ' g' ), ' \.a$' , ' ' , ' g' )
98+ let ret [i ] = i
99+ endfor
68100 endfor
69101 endfor
70102 return sort (keys (ret ))
0 commit comments