-
Notifications
You must be signed in to change notification settings - Fork 2
/
goimpl.vim
231 lines (189 loc) · 5.69 KB
/
goimpl.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
let s:save_cpo = &cpo
set cpo&vim
let g:goimpl#gocmd = get(g:, 'goimpl#gocmd', 'go')
let g:goimpl#cmd = get(g:, 'goimpl#cmd', 'impl')
let g:goimpl#godoccmd = get(g:, 'goimpl#godoccmd', 'godoc')
function! s:bin_path()
" check if our global custom path is set, if not check if $GOBIN is set so
" we can use it, otherwise use $GOPATH + '/bin'
if exists("g:go_bin_path")
return g:go_bin_path
elseif !empty($GOBIN)
return $GOBIN
elseif !empty($GOPATH)
return $GOPATH . '/bin'
endif
return ''
endfunction
function! s:check_bin_path(binpath)
let binpath = a:binpath
if executable(binpath)
return binpath
endif
" just get the basename
let basename = fnamemodify(binpath, ":t")
" check if we have an appropriate bin_path
let go_bin_path = s:bin_path()
if empty(go_bin_path)
return ''
endif
let new_binpath = go_bin_path . '/' . basename
if !executable(new_binpath)
return ''
endif
return new_binpath
endfunction
function! s:error(msg)
echohl ErrorMsg | echomsg a:msg | echohl None
endfunction
function! s:has_vimproc()
if !exists('s:exists_vimproc')
try
silent call vimproc#version()
let s:exists_vimproc = 1
catch
let s:exists_vimproc = 0
endtry
endif
return s:exists_vimproc
endfunction
function! s:system(str, ...)
let command = a:str
let input = a:0 >= 1 ? a:1 : ''
if a:0 == 0
let output = s:has_vimproc() ?
\ vimproc#system(command) : system(command)
elseif a:0 == 1
let output = s:has_vimproc() ?
\ vimproc#system(command, input) : system(command, input)
else
" ignores 3rd argument unless you have vimproc.
let output = s:has_vimproc() ?
\ vimproc#system(command, input, a:2) : system(command, input)
endif
return output
endfunction
function! s:shell_error()
return s:has_vimproc() ? vimproc#get_last_status() : v:shell_error
endfunction
function! s:chomp(str)
return a:str[len(a:str)-1] ==# "\n" ? a:str[:len(a:str)-2] : a:str
endfunction
function! s:os_arch()
let os = s:chomp(s:system(g:goimpl#gocmd . ' env GOOS'))
if s:shell_error()
return ''
endif
let arch = s:chomp(s:system(g:goimpl#gocmd . ' env GOARCH'))
if s:shell_error()
return ''
endif
return os . '_' . arch
endfunction
let g:goimpl#os_arch = get(g:, 'goimpl#os_arch', s:os_arch())
function! goimpl#impl(recv, iface)
let binpath = s:check_bin_path(g:goimpl#cmd)
if empty(binpath)
call s:error(g:goimpl#cmd . ' command is not found. Please check g:goimpl#cmd')
return ''
endif
let result = s:system(printf("%s '%s' '%s'", binpath, a:recv, a:iface))
if s:shell_error()
call s:error(binpath . ' command failed: ' . result)
return ''
endif
return result
endfunction
function! goimpl#do(...)
if a:0 < 2
call s:error('GoImpl {receiver} {interface}')
return
endif
let recv = join(a:000[:-2], ' ')
let iface = a:000[-1]
let result = goimpl#impl(recv, iface)
if result ==# ''
return
end
let pos = getpos('.')
put =result
call setpos('.', pos)
endfunction
if exists('*uniq')
function! s:uniq(list)
return uniq(a:list)
endfunction
else
" 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
endif
function! s:root_dirs()
let dirs = []
let root = substitute(s:chomp(s:system(g:goimpl#gocmd . ' env GOROOT')), '\\', '/', 'g')
if s:shell_error()
return []
endif
if root !=# '' && isdirectory(root)
call add(dirs, root)
endif
let path_sep = has('win32') || has('win64') ? ';' : ':'
let paths = map(split(s:chomp(s:system(g:goimpl#gocmd . ' env GOPATH')), path_sep), "substitute(v:val, '\\\\', '/', 'g')")
if s:shell_error()
return []
endif
if !empty(filter(paths, 'isdirectory(v:val)'))
call extend(dirs, paths)
endif
return dirs
endfunction
function! s:go_packages(dirs)
let pkgs = []
for d in a:dirs
let pkg_root = expand(d . '/pkg/' . s:os_arch())
call extend(pkgs, split(globpath(pkg_root, '**/*.a', 1), "\n"))
endfor
return map(pkgs, "fnamemodify(v:val, ':t:r')")
endfunction
function! s:interface_list(pkg)
let contents = split(s:system(g:goimpl#godoccmd . ' ' . a:pkg), "\n")
if s:shell_error()
return []
endif
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
" Complete package and interface for {interface}
function! goimpl#complete(arglead, cmdline, cursorpos)
if !executable(g:goimpl#godoccmd)
return []
endif
let words = split(a:cmdline, '\s\+', 1)
if len(words) <= 3
" TODO
return []
endif
if words[-1] ==# ''
return s:uniq(sort(s:go_packages(s:root_dirs())))
elseif words[-1] =~# '^\h\w*$'
return s:uniq(sort(filter(s:go_packages(s:root_dirs()), 'stridx(v:val, words[-1]) == 0')))
elseif words[-1] =~# '^\h\w*\.\%(\h\w*\)\=$'
let [pkg, interface] = split(words[-1], '\.', 1)
echomsg pkg
return s:uniq(sort(filter(s:interface_list(pkg), 'v:val =~? words[-1]')))
else
return []
endif
endfunction
let &cpo = s:save_cpo
unlet s:save_cpo