-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
go.vim
284 lines (233 loc) · 9.2 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
" install necessary Go tools
if exists("g:go_loaded_install")
finish
endif
let g:go_loaded_install = 1
" don't spam the user when Vim is started in Vi compatibility mode
let s:cpo_save = &cpo
set cpo&vim
function! s:checkVersion() abort
let l:unsupported = 0
if go#config#VersionWarning() != 0
if has('nvim')
let l:unsupported = !has('nvim-0.4.0')
else
let l:unsupported = !has('patch-8.1.2269')
endif
if l:unsupported == 1
echohl Error
echom "vim-go requires at least Vim 8.1.2269 or Neovim 0.4.0, but you're using an older version."
echom "Please update your Vim for the best vim-go experience."
echom "If you really want to continue you can set this to make the error go away:"
echom " let g:go_version_warning = 0"
echom "Note that some features may error out or behave incorrectly."
echom "Please do not report bugs unless you're using at least Vim 8.1.2269 or Neovim 0.4.0."
echohl None
" Make sure people see this.
sleep 2
endif
endif
endfunction
call s:checkVersion()
" these packages are used by vim-go and can be automatically installed if
" needed by the user with GoInstallBinaries.
" NOTE(bc): varying the binary name and the tail of the import path does not yet work in module aware mode.
let s:packages = {
\ 'asmfmt': ['github.com/klauspost/asmfmt/cmd/asmfmt@latest'],
\ 'dlv': ['github.com/go-delve/delve/cmd/dlv@latest'],
\ 'errcheck': ['github.com/kisielk/errcheck@latest'],
\ 'fillstruct': ['github.com/davidrjenni/reftools/cmd/fillstruct@master'],
\ 'godef': ['github.com/rogpeppe/godef@latest'],
\ 'goimports': ['golang.org/x/tools/cmd/goimports@master'],
\ 'revive': ['github.com/mgechev/revive@latest'],
\ 'gopls': ['golang.org/x/tools/gopls@latest', {}, {'after': function('go#lsp#Restart', [])}],
\ 'golangci-lint': ['github.com/golangci/golangci-lint/cmd/golangci-lint@latest'],
\ 'staticcheck': ['honnef.co/go/tools/cmd/staticcheck@latest'],
\ 'gomodifytags': ['github.com/fatih/gomodifytags@latest'],
\ 'gorename': ['golang.org/x/tools/cmd/gorename@master'],
\ 'gotags': ['github.com/jstemmer/gotags@master'],
\ 'impl': ['github.com/josharian/impl@main'],
\ 'motion': ['github.com/fatih/motion@latest'],
\ 'iferr': ['github.com/koron/iferr@master'],
\ }
" These commands are available on any filetypes
command! -nargs=* -complete=customlist,s:complete GoInstallBinaries call s:GoInstallBinaries(-1, <f-args>)
command! -nargs=* -complete=customlist,s:complete GoUpdateBinaries call s:GoInstallBinaries(1, <f-args>)
command! -nargs=? -complete=dir GoPath call go#path#GoPath(<f-args>)
fun! s:complete(lead, cmdline, cursor)
return filter(keys(s:packages), 'strpart(v:val, 0, len(a:lead)) == a:lead')
endfun
" GoInstallBinaries downloads and installs binaries defined in s:packages to
" $GOBIN or $GOPATH/bin. GoInstallBinaries will update already installed
" binaries only if updateBinaries = 1. By default, all packages in s:packages
" will be installed, but the set can be limited by passing the desired
" packages in the unnamed arguments.
function! s:GoInstallBinaries(updateBinaries, ...)
let err = s:CheckBinaries()
if err != 0
return
endif
if go#path#Default() == ""
call go#util#EchoError('$GOPATH is not set and `go env GOPATH` returns empty')
return
endif
let go_bin_path = go#path#BinPath()
let [l:goos, l:goarch] = go#util#hostosarch()
let Restore_goos = go#util#SetEnv('GOOS', l:goos)
let Restore_goarch = go#util#SetEnv('GOARCH', l:goarch)
" change $GOBIN so go can automatically install to it
let Restore_gobin = go#util#SetEnv('GOBIN', go_bin_path)
" vim's executable path is looking in PATH so add our go_bin path to it
let Restore_path = go#util#SetEnv('PATH', go_bin_path . go#util#PathListSep() . $PATH)
" when shellslash is set on MS-* systems, shellescape puts single quotes
" around the output string. cmd on Windows does not handle single quotes
" correctly. Unsetting shellslash forces shellescape to use double quotes
" instead.
let resetshellslash = 0
if has('win32') && &shellslash
let resetshellslash = 1
set noshellslash
endif
let l:get_base_cmd = ['go', 'install', '-v', '-mod=mod']
" Filter packages from arguments (if any).
let l:packages = {}
if a:0 > 0
for l:bin in a:000
let l:version = substitute(l:bin, '.*@', '', '')
if l:version == l:bin
let l:version = ''
endif
let l:bin = substitute(l:bin, '@.*', '', '')
let l:pkg = get(s:packages, l:bin, [])
if len(l:pkg) == 0
call go#util#EchoError('unknown binary: ' . l:bin)
return
endif
if l:version isnot ''
let l:pkg[0] = substitute(l:pkg[0], '@\zs.*', l:version, '')
endif
let l:packages[l:bin] = l:pkg
endfor
else
let l:packages = s:packages
endif
let l:platform = ''
if go#util#IsWin()
let l:platform = 'windows'
endif
let l:oldmore = &more
let &more = 0
let Restore_modules = go#util#SetEnv('GO111MODULE', 'on')
for [l:binary, l:pkg] in items(l:packages)
let l:importPath = l:pkg[0]
" TODO(bc): how to support this with modules? Do we have to clone and then
" install manually? Probably not. I suspect that we can just use GOPATH
" mode and then do the legacy method.
let bin_setting_name = "go_" . l:binary . "_bin"
let l:extension = ''
if go#util#IsWin()
let l:extension = '.exe'
endif
let bin = get(g:, bin_setting_name, l:binary . l:extension)
if !executable(bin) || a:updateBinaries == 1
if a:updateBinaries == 1
echo "vim-go: Updating " . l:binary . ". Reinstalling ". importPath . " to folder " . go_bin_path
else
echo "vim-go: ". l:binary ." not found. Installing ". importPath . " to folder " . go_bin_path
endif
let l:tmpdir = go#util#tempdir('vim-go')
try
let l:dir = go#util#Chdir(l:tmpdir)
let l:get_cmd = copy(l:get_base_cmd)
if len(l:pkg) > 1 && get(l:pkg[1], l:platform, []) isnot []
let l:get_cmd += get(l:pkg[1], l:platform, [])
endif
" TODO(bc): how to install the bin to a different name than the binary
" path? go install does not support -o
"let l:get_cmd += ['-o', printf('%s%s%s', go_bin_path, go#util#PathSep(), bin)]
let [l:out, l:err] = go#util#Exec(l:get_cmd + [l:importPath])
if l:err
call go#util#EchoError(printf('Error installing %s: %s', l:importPath, l:out))
endif
finally
call go#util#Chdir(l:dir)
endtry
if len(l:pkg) > 2
call call(get(l:pkg[2], 'after', function('s:noop', [])), [])
endif
endif
endfor
" restore back!
call call(Restore_modules, [])
call call(Restore_path, [])
call call(Restore_gobin, [])
call call(Restore_goarch, [])
call call(Restore_goos, [])
if resetshellslash
set shellslash
endif
if a:updateBinaries == 1
call go#util#EchoInfo('updating finished!')
else
call go#util#EchoInfo('installing finished!')
endif
let &more = l:oldmore
endfunction
" CheckBinaries checks if the necessary binaries to install the Go tool
" commands are available.
function! s:CheckBinaries()
if !executable('go')
call go#util#EchoError('go executable not found.')
return -1
endif
if !executable('git')
call go#util#EchoError('git executable not found.')
return -1
endif
endfunction
" Autocommands
" ============================================================================
"
" We take care to preserve the user's fileencodings and fileformats,
" because those settings are global (not buffer local), yet we want
" to override them for loading Go files, which are defined to be UTF-8.
let s:current_fileformats = ''
let s:current_fileencodings = ''
" define fileencodings to open as utf-8 encoding even if it's ascii.
function! s:gofiletype_pre()
let s:current_fileformats = &g:fileformats
let s:current_fileencodings = &g:fileencodings
set fileencodings=utf-8 fileformats=unix
endfunction
" restore fileencodings as others
function! s:gofiletype_post()
let &g:fileformats = s:current_fileformats
let &g:fileencodings = s:current_fileencodings
endfunction
function! s:register()
if !(&modifiable && expand('<amatch>') ==# 'go')
return
endif
" Resolve any symlinks in <afile> so that the filename will match what Vim
" will ultimately and usually produce.
call go#lsp#DidOpen(resolve(expand('<afile>:p')))
endfunction
function! s:noop(...) abort
endfunction
augroup vim-go
autocmd!
autocmd BufNewFile *.go if &modifiable | setlocal fileencoding=utf-8 fileformat=unix | endif
autocmd BufNewFile *.go call go#auto#template_autocreate()
autocmd BufRead *.go call s:gofiletype_pre()
autocmd BufReadPost *.go call s:gofiletype_post()
autocmd BufNewFile *.s if &modifiable | setlocal fileencoding=utf-8 fileformat=unix | endif
autocmd BufRead *.s call s:gofiletype_pre()
autocmd BufReadPost *.s call s:gofiletype_post()
if go#util#has_job()
autocmd FileType * call s:register()
endif
augroup end
" restore Vi compatibility settings
let &cpo = s:cpo_save
unlet s:cpo_save
" vim: sw=2 ts=2 et