-
-
Notifications
You must be signed in to change notification settings - Fork 126
/
commands.lua
503 lines (461 loc) · 14.1 KB
/
commands.lua
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
local vfn = vim.fn
local utils = require('go.utils')
local create_cmd = function(cmd, func, opt)
opt = vim.tbl_extend('force', { desc = 'go.nvim ' .. cmd }, opt or {})
vim.api.nvim_create_user_command(cmd, func, opt)
end
local dap_config = function()
create_cmd('BreakCondition', function(_)
local dap = utils.load_plugin('nvim-dap', 'dap')
if not dap then
return
end
dap.set_breakpoint(vfn.input('Breakpoint condition: '))
end)
create_cmd('LogPoint', function(_)
local dap = utils.load_plugin('nvim-dap', 'dap')
if not dap then
return
end
dap.set_breakpoint(nil, nil, vfn.input('Log message: '))
end)
create_cmd('ReplRun', function(_)
local dap = utils.load_plugin('nvim-dap', 'dap')
if not dap then
return
end
dap.repl.run_last()
end)
create_cmd('ReplToggle', function(_)
local dap = utils.load_plugin('nvim-dap', 'dap')
if not dap then
return
end
dap.repl.toggle()
end)
create_cmd('ReplOpen', function(_)
local dap = utils.load_plugin('nvim-dap', 'dap')
if not dap then
return
end
dap.repl.open()
vim.cmd('split')
end)
create_cmd('DapRerun', function(_)
local dap = utils.load_plugin('nvim-dap', 'dap')
if not dap then
return
end
dap.disconnect()
dap.close()
dap.run_last()
end)
local gdap = require('go.dap')
create_cmd('GoDebug', function(opts)
local dap = utils.load_plugin('nvim-dap', 'dap')
if not dap then
return
end
gdap.run(unpack(opts.fargs))
end, {
complete = function(a, l)
return package.loaded.go.dbg_complete(a, l)
end,
nargs = '*',
})
create_cmd('GoCreateLaunch', function(_)
require('go.launch').config()
end)
create_cmd('GoBreakSave', function(_)
gdap.save_brks()
end)
create_cmd('GoBreakLoad', function(_)
local dap = utils.load_plugin('nvim-dap', 'dap')
if not dap then
return
end
gdap.load_brks()
end)
create_cmd('DapStop', function(_)
require('go.dap').stop()
end)
create_cmd('GoDbgConfig', function(_)
require('go.launch').config()
end)
create_cmd('GoBreakToggle', function(_)
local dap = utils.load_plugin('nvim-dap', 'dap')
if not dap then
return
end
gdap.breakpt()
end)
create_cmd('GoDbgKeys', function(_)
gdap.debug_keys()
end)
create_cmd('DapUiFloat', function(_)
require('dapui').float_element()
end)
create_cmd('DapUiToggle', function(_)
require('dapui').toggle()
end)
create_cmd('GoDbgStop', function(_)
gdap.stop(true)
end)
create_cmd('GoDbgContinue', function(_)
local dap = utils.load_plugin('nvim-dap', 'dap')
if not dap then
return
end
dap.continue()
end)
end
return {
add_cmds = function()
vim.cmd([[
augroup go.filetype
autocmd!
autocmd FileType go setlocal omnifunc=v:lua.vim.lsp.omnifunc
autocmd FileType go au QuickFixCmdPost [^l]* nested cwindow
autocmd FileType go au QuickFixCmdPost l* nested lwindow
augroup end
]])
create_cmd('GoMake', function(_)
require('go.asyncmake').make()
end)
create_cmd('GoFmt', function(opts)
require('go.format').gofmt(unpack(opts.fargs))
end, { nargs = '*' })
create_cmd('GoImport', function(opts)
vim.notify('GoImport is deprecated, use GoImports')
require('go.format').goimports(unpack(opts.fargs))
end, {})
create_cmd('GoImports', function(opts)
require('go.format').goimports(unpack(opts.fargs))
end, {
complete = function(a, l)
return package.loaded.go.doc_complete(a, l)
end,
nargs = '*',
})
create_cmd('GoGet', function(opts)
require('go.goget').run(opts.fargs)
end, { nargs = '*' })
local gobin = _GO_NVIM_CFG.go
local cmd = string.format(
[[command! -nargs=* GoGenerate :setl makeprg=%s\ generate | lua require'go.asyncmake'.make(<f-args>)]],
gobin
)
vim.cmd(cmd)
cmd = string.format(
[[command! -nargs=* -complete=customlist,v:lua.package.loaded.go.package_complete GoBuild :setl makeprg=%s\ build | lua require'go.asyncmake'.make(<f-args>)]],
gobin
)
vim.cmd(cmd)
cmd = string.format(
[[command! -nargs=* -complete=customlist,v:lua.package.loaded.go.package_complete GoVet :setl makeprg=%s\ vet | lua require'go.asyncmake'.make(<f-args>)]],
gobin
)
vim.cmd(cmd)
local pcmdstr = ''
local preludes = _GO_NVIM_CFG.preludes
local gorun_preludes = preludes.GoRun or preludes.default
if gorun_preludes ~= nil then
local pcmd = gorun_preludes()
if #pcmd > 0 then
pcmdstr = table.concat(pcmd, '\\ ') .. '\\ '
end
end
cmd = string.format(
[[command! -nargs=* -complete=customlist,v:lua.package.loaded.go.package_complete GoRun :setl makeprg=%s%s\ run | lua require'go.asyncmake'.make(<f-args>)]],
pcmdstr,
gobin
)
utils.log(cmd)
vim.cmd(cmd)
create_cmd('GoStop', function(opts)
require('go.asyncmake').stopjob(unpack(opts.fargs))
end, { nargs = '*' })
-- if you want to output to quickfix
-- example to running test in split buffer
-- vim.cmd(
-- [[command! -nargs=* -complete=custom,v:lua.package.loaded.go.package_complete GoTest :setl makeprg=go\ test\ -v\ | lua require'go.runner'.make(<f-args>)]])
create_cmd('GoTest', function(opts)
require('go.gotest').test(unpack(opts.fargs))
end, {
complete = function(a, l)
return package.loaded.go.package_complete(a, l)
end,
nargs = '*',
})
create_cmd('GoTestSum', function(opts)
if opts.fargs[1] == '-w' then
return require('go.gotestsum').watch()
end
require('go.gotestsum').run(unpack(opts.fargs))
end, {
complete = function(a, l)
return package.loaded.go.package_complete(a, l)
end,
nargs = '*',
})
create_cmd('GoCoverage', function(opts)
require('go.coverage').run(unpack(opts.fargs))
end, {
complete = function(a, l)
return package.loaded.go.package_complete(a, l)
end,
nargs = '*',
})
create_cmd('GoPkgOutline', function(opts)
require('go.package').outline(unpack(opts.fargs))
end, {
complete = function(a, l)
return package.loaded.go.package_complete(a, l)
end,
nargs = '*',
})
vim.cmd(
[[command! GoLint :setl makeprg=golangci-lint\ run\ --print-issued-lines=false\ --exclude-use-default=false\ --out-format=line-number | :GoMake]]
)
create_cmd('GoProject', function(opts)
require('go.project').setup()
end)
create_cmd('GoCheat', function(opts)
require('go.chtsh').run(unpack(opts.fargs))
end, { nargs = '*' })
-- e.g. GoTestFunc unit
create_cmd('GoTestFunc', function(opts)
require('go.gotest').test_func(unpack(opts.fargs))
end, { nargs = '*' })
create_cmd('GoTestSubCase', function(opts)
require('go.gotest').test_tblcase(unpack(opts.fargs))
end, { nargs = '*' })
-- e.g. GoTestFile unit
-- command! -nargs=* -complete=custom,v:lua.package.loaded.go.package_complete GoTestFile lua require('go.gotest').test_file(<f-args>)
create_cmd('GoTestFile', function(opts)
require('go.gotest').test_file(unpack(opts.fargs))
end, {
complete = function(a, l)
return package.loaded.go.package_complete(a, l)
end,
nargs = '*',
})
create_cmd('GoTestPkg', function(opts)
require('go.gotest').test_package(unpack(opts.fargs))
end, {
complete = function(a, l)
return package.loaded.go.package_complete(a, l)
end,
nargs = '*',
})
create_cmd('GoAddTest', function(opts)
require('go.gotests').fun_test(unpack(opts.fargs))
end)
create_cmd('GoAddExpTest', function(opts)
require('go.gotests').exported_test(unpack(opts.fargs))
end, { nargs = '*' })
create_cmd('GoAddAllTest', function(opts)
require('go.gotests').all_test(unpack(opts.fargs))
end, { nargs = '*' })
create_cmd('GoModVendor', function(opts)
require('go.mod').run('vendor', unpack(opts.fargs))
end, { nargs = '*' })
create_cmd('GoModInit', function(opts)
require('go.mod').run('init', unpack(opts.fargs))
end, { nargs = '*' })
create_cmd('GoEnv', function(opts)
require('go.env').load_env(unpack(opts.fargs))
end, { nargs = '*' })
create_cmd('GoCodeLenAct', function(_)
require('go.codelens').run_action()
end)
create_cmd('GoCodeAction', function(t)
require('go.codeaction').run_code_action(t)
end, { range = true })
create_cmd('GoModifyTag', function(opts)
require('go.tags').modify(unpack(opts.fargs))
end, {
complete = function(a, l)
return package.loaded.go.modify_tags_complete(a, l)
end,
nargs = '*',
})
create_cmd('GoAddTag', function(opts)
require('go.tags').add(unpack(opts.fargs))
end, {
complete = function(a, l)
return package.loaded.go.add_tags_complete(a, l)
end,
nargs = '*',
})
create_cmd('GoRmTag', function(opts)
require('go.tags').rm(unpack(opts.fargs))
end, { nargs = '*' })
create_cmd('GoImpl', function(opts)
require('go.impl').run(unpack(opts.fargs))
end, {
complete = function(a, l)
return package.loaded.go.impl_complete(a, l)
end,
nargs = '*',
})
create_cmd('GoImplements', function(opts)
vim.lsp.buf.implementation()
end, {})
create_cmd('GoDoc', function(opts)
require('go.godoc').run(opts.fargs)
end, {
complete = function(a, l)
return package.loaded.go.doc_complete(a, l)
end,
nargs = '*',
})
create_cmd('GoInstallBinary', function(opts)
require('go.install').install(unpack(opts.fargs))
end, {
complete = function(a, l)
return package.loaded.go.tools_complete(a, l)
end,
nargs = '*',
})
create_cmd('GoUpdateBinary', function(opts)
require('go.install').update(unpack(opts.fargs))
end, {
complete = function(a, l)
return package.loaded.go.tools_complete(a, l)
end,
nargs = '*',
})
create_cmd('GoInstallBinaries', function(_)
require('go.install').install_all()
end)
create_cmd('GoUpdateBinaries', function(_)
require('go.install').update_all()
end)
create_cmd('GoClearTag', function(_)
require('go.tags').clear()
end)
create_cmd('GoCmt', function(_)
require('go.comment').gen()
end)
create_cmd('GoRename', function(_)
require('go.rename').lsprename()
end)
create_cmd('GoIfErr', function(_)
require('go.iferr').run()
end)
create_cmd('GoFillStruct', function(_)
require('go.reftool').fillstruct()
end)
create_cmd('GoFillSwitch', function(_)
require('go.reftool').fillswitch()
end)
create_cmd('GoFixPlurals', function(_)
require('go.fixplurals').fixplurals()
end)
create_cmd('GoAlt', function(opts)
require('go.alternate').switch(opts.bang, '')
end, { bang = true })
create_cmd('GoAltV', function(opts)
require('go.alternate').switch(opts.bang, 'vsplit')
end, { bang = true })
create_cmd('GoAltS', function(opts)
require('go.alternate').switch(opts.bang, 'split')
end, { bang = true })
create_cmd('GoWork', function(opts)
require('go.work').update(unpack(opts.fargs))
end, {
nargs = '*',
complete = function(_, _, _)
return { 'run', 'use' }
end,
})
create_cmd('GoModTidy', function(_)
require('go.gopls').tidy()
end)
create_cmd('GoListImports', function(_)
local lines = require('go.gopls').list_imports().PackageImports or {}
local close_events = { 'CursorMoved', 'CursorMovedI', 'BufHidden', 'InsertCharPre' }
local config = {
close_events = close_events,
focusable = true,
border = 'single',
width = 80,
zindex = 100,
height = #lines,
}
vim.lsp.util.open_floating_preview(lines, 'go', config)
end)
if _GO_NVIM_CFG.dap_debug then
dap_config()
end
create_cmd('GoMockGen', require('go.mockgen').run, {
nargs = '*',
-- bang = true,
complete = function(_, _, _)
-- return completion candidates as a list-like table
return { '-p', '-d', '-i', '-s' }
end,
})
create_cmd('GoEnv', function(opts)
require('go.env').load_env(unpack(opts.fargs))
end, { nargs = '*' })
create_cmd('GoGenReturn', function()
require('go.lsp').hover_returns()
end)
create_cmd('GoJson2Struct', function(opts)
require('go.json2struct').run(opts)
end, {
nargs = '*',
bang = true,
register = true,
-- complete = function(ArgLead, CmdLine, CursorPos)
complete = function(_, _, _)
return { 'myStruct' }
end,
range = true,
})
create_cmd('Gomvp', function(opts)
require('go.gomvp').run(opts.fargs)
end, {
complete = function(a, l)
return package.loaded.go.package_complete(a, l)
end,
nargs = '*',
})
create_cmd('Govulnckeck', function(opts)
require('go.govulncheck').run(opts.fargs)
end, { nargs = '*' })
create_cmd('GoEnum', function(opts)
require('go.enum').run(unpack(opts.fargs))
end, { nargs = '*' })
create_cmd('GoNew', function(opts)
require('go.template.gonew').new(opts.fargs)
end, {
nargs = '*',
complete = function(_, _, _)
-- return completion candidates as a list-like table
return require('go.template.gonew').complete
end,
})
create_cmd('Ginkgo', function(opts)
require('go.ginkgo').run(opts.fargs)
end, {
nargs = '*',
complete = function(_, _, _)
-- return completion candidates as a list-like table
return { 'generate', 'bootstrap', 'build', 'labels', 'run', 'watch' }
end,
})
create_cmd('GinkgoFunc', function(opts)
require('go.ginkgo').test_func(opts.fargs)
end, {
nargs = '*',
})
create_cmd('GinkgoFile', function(opts)
require('go.ginkgo').test_file(opts.fargs)
end, {
nargs = '*',
})
end,
}