-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgo.vim
103 lines (86 loc) · 2.85 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
" 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:out, l:err] = gopher#system#run(['go', 'list', '-m'])
if l:err
return -1
endif
return l:out
endfun
" Get the go.mod file location, or -1 if there is none.
fun! gopher#go#gomod() abort
let l:dir = expand('%:p:h')
while 1
" len() check is for Windows; not sure how that's represented.
if l:dir is# '/' || len(l:dir) <= 4
return -1
endif
if filereadable(l:dir + '/go.mod')
return l:dir + '/go.mod'
endif
let l:dir = fnamemodify(l:dir, ':h')
endwhile
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 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: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], "\"'"), ' ') + g:gopher_build_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(g:gopher_build_tags, ' ') . '"']
\ + a:flag_list[l:last_flag+1:]
endfun