-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgo.vim
301 lines (258 loc) · 7.81 KB
/
go.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
" go.vim: Utilities for working with Go files.
" Report if the current buffer is a Go test file.
fun! gopher#go#is_test() abort
return expand('%')[-8:] is# '_test.go'
endfun
" Report if the current buffer is inside GOPATH.
fun! gopher#go#in_gopath() abort
let [l:out, l:err] = gopher#system#run(['go', 'env', 'GOPATH'])
if l:err
return gopher#error(l:out)
endif
let l:path = expand('%:p')
for l:gopath in split(l:out, gopher#system#pathsep())
if gopher#str#has_prefix(l:path, l:out)
return 1
endif
endfor
return 0
endfun
" Get the Go module name, or -1 if there is none.
fun! gopher#go#module() abort
let l:wd = getcwd()
try
call chdir(expand('%:h'))
let [l:out, l:err] = gopher#system#run(['go', 'list', '-m', '-f',
\ "{{.Path}}\x01{{.Dir}}"])
if l:err
return [-1, -1]
endif
let l:s = split(l:out, "\x01")
if len(l:s) isnot# 2
return [-1, -1]
endif
return l:s
finally
call chdir(l:wd)
endtry
endfun
" Strip version (/v2, /v3 etc.) from a package name.
"
" example.com/pkg/v3 → example.com/pkg
" example.com/pkg → example.com/pkg
fun! gopher#go#remove_version(pkg) abort
return substitute(a:pkg, '/v\d\+$', '', '')
endfun
" Get the package path for the file in the current buffer.
fun! gopher#go#package() abort
let [l:out, l:err] = gopher#system#run(['go', 'list', './' . expand('%:h')])
if l:err || l:out[0] is# '_'
if l:out[0] is# '_' || gopher#str#has_suffix(l:out, 'cannot import absolute path')
let l:out = 'cannot determine module path (outside GOPATH, no go.mod)'
endif
call gopher#error(l:out)
return ''
endif
return l:out
endfun
" Get path to file in current buffer as package/path/file.go
fun! gopher#go#packagepath() abort
return gopher#go#package() . '/' . expand('%:t')
endfun
let s:go_commands = ['go', 'bug', 'build', 'clean', 'doc', 'env', 'fix', 'fmt',
\ 'generate', 'get', 'install', 'list', 'mod', 'run', 'test',
\ 'tool', 'version', 'vet']
" Add b:gopher_build_tags or g:gopher_build_tags to the flag_list; will be
" merged with existing tags (if any).
fun! gopher#go#add_build_tags(flag_list) abort
if get(g:, 'gopher_build_tags', []) == []
return a:flag_list
endif
if type(a:flag_list) isnot v:t_list
call gopher#error('add_build_tags: not a list: %s', a:flag_list)
return a:flag_list
endif
let l:tags = gopher#bufsetting('gopher_build_tags', [])
let l:last_flag = 0
for l:i in range(len(a:flag_list))
if a:flag_list[l:i][0] is# '-' || index(s:go_commands, a:flag_list[l:i]) > -1
let l:last_flag = l:i
endif
if a:flag_list[l:i] is# '-tags'
let l:tags = uniq(split(trim(a:flag_list[l:i+1], "\"'"), ',') + l:tags)
return a:flag_list[:l:i]
\ + ['"' . join(l:tags, ' ') . '"']
\ + a:flag_list[l:i+2:]
endif
endfor
return a:flag_list[:l:last_flag]
\ + ['-tags', '"' . join(l:tags, ',') . '"']
\ + a:flag_list[l:last_flag+1:]
endfun
" Find the build tags for the current buffer; returns a list (or empty list if
" there are none).
fun! gopher#go#find_build_tags() abort
" https://golang.org/pkg/go/build/#hdr-Build_Constraints
for l:i in range(1, line('$'))
let l:line = getline(l:i)
if l:line =~# '^// +build '
return uniq(sort(gopher#list#flatten(map(split(l:line[10:], ' '), {_, v -> split(v, ',')}))))
endif
if l:line =~# '^package \f'
return []
endif
endfor
return []
endfun
" Set b:gopher_build_package to ./cmd/[module-name] if it exists.
fun! gopher#go#set_build_package() abort
if &buftype isnot# '' || &filetype isnot# 'go'
return
endif
if gopher#bufsetting('gopher_build_package', '') isnot ''
return
endif
" TODO: maybe cache this a bit? Don't need to do it for every buffer in the
" same directory.
let [l:module, l:modpath] = gopher#go#module()
if l:module is# -1
return
endif
let l:name = fnamemodify(gopher#go#remove_version(l:module), ':t')
let l:pkg = l:module . '/cmd/' . l:name
let l:path = l:modpath . '/cmd/' . l:name
" We're already in (possible a different) ./cmd/<name> subpackage: use this
" one instead of clobbering ./cmd/other with ./cmd/main
if gopher#str#has_prefix(bufname(''), 'cmd/')
let b:gopher_build_package = l:module . '/' . fnamemodify(bufname(''), ':h')
compiler go
return
endif
if isdirectory(l:path) && get(b:, 'gopher_build_package', '') isnot# l:pkg
let b:gopher_build_package = l:pkg
compiler go
endif
endfun
" Set b:gopher_build_tags to the build tags in the current buffer.
fun! gopher#go#set_build_tags() abort
if &buftype isnot# '' || &filetype isnot# 'go'
return
endif
" TODO: be even smarter about this: merge the g: and b: vars, and allow
" setting a special '%BUFFER%' so you can both set tags from vimrc and merge
" from file.
if len(gopher#bufsetting('gopher_build_tags', '')) > 0
return
endif
let l:tags = gopher#go#find_build_tags()
if l:tags != get(b:, 'gopher_build_tags', [])
let b:gopher_build_tags = l:tags
compiler go
endif
endfun
" Get the function name the cursor is in; the return value is a list where the
" first item is the full name and the second one is the full signature minus
" 'func':
"
" func foo() → ['foo', 'foo()']
" func foo(x int) int → ['foo', 'foo(x int) int']
" func (t T) foo(x int) int → ['T.foo', '(t T) foo(x int) int']
"
" Returns ['', ''] if there is no function.
fun! gopher#go#current_function() abort
if getline('.') =~# '^func'
return s:line_to_fun()
endif
let l:save = winsaveview()
try
" Ensure we're in a function.
" TODO: this can be faster by checking if the closing brace after [[ is
" after the cursor line before the jump.
let [l:fname, l:tmp] = gopher#system#tmpmod()
let l:cmd = ['motion', '-mode', 'enclosing',
\ '-file', l:fname,
\ '-offset', gopher#buf#cursor(),
\ '-format', 'vim',
\ ]
let [l:out, l:err] = gopher#system#tool(l:cmd)
if l:err
return ['', '']
endif
try
let l:loc = json_decode(l:out)
catch
return gopher#error(l:out)
endtry
if !has_key(l:loc, 'comment')
return ['', '']
endif
" Jump to function declaration.
call gopher#motion#jump('n', 'prev')
if getline('.') !~# '^func'
return ['', '']
endif
return s:line_to_fun()
finally
call winrestview(l:save)
endtry
endfun
fun! s:line_to_fun() abort
let l:sig = trim(getline('.')[4:-2])
let l:name = l:sig
if l:name[0] is# '('
let l:name = l:name[stridx(l:name, ')') + 2:]
endif
let l:name = l:name[:stridx(l:name, '(') - 1]
return [l:name, l:sig]
endfun
" Get the current test name as ['TestFoo'), or an empty list if the cursor isn't
" inside a test function.
"
" If a:run is non-0 it will return the name as ['-run', '^TestFoo$'], for
" passing to a 'go test' command.
fun! gopher#go#current_test(run) abort
if !gopher#go#is_test()
return []
endif
let l:f = gopher#go#current_function()[0]
if l:f is# '' || l:f !~# '^Test'
return []
endif
if a:run
return ['-run', printf('^%s$', l:f)]
endif
return l:f
endfun
" Run the go compiler.
fun! gopher#go#run_install() abort
silent! wa
compiler go
echo &l:makeprg
silent make!
redraw!
endfun
" Run go test for the current package.
fun! gopher#go#run_test() abort
silent! wa
compiler gotest
echo &l:makeprg
silent make!
redraw!
endfun
" Run go test for the current function.
fun! gopher#go#run_test_current() abort
silent! wa
compiler gotest
let l:t = gopher#system#join(gopher#go#current_test(1))
echo &l:makeprg . ' ' . l:t
exe 'silent make! ' . l:t
redraw!
endfun
" Run lint tool for the current package.
fun! gopher#go#run_lint() abort
silent! wa
compiler golint
echo &l:makeprg
silent make!
redraw!
endfun