allow dap to be lazyload

This commit is contained in:
ray-x 2022-08-01 10:53:37 +10:00
parent b7d6288bbd
commit f4d8a0b691

View File

@ -1,29 +1,148 @@
local vfn = vim.fn local vfn = vim.fn
local utils = require('go.utils')
local create_cmd = function(cmd, func, opt) local create_cmd = function(cmd, func, opt)
opt = vim.tbl_extend("force", { desc = "go.nvim " .. cmd }, opt or {}) opt = vim.tbl_extend('force', { desc = 'go.nvim ' .. cmd }, opt or {})
vim.api.nvim_create_user_command(cmd, func, opt) vim.api.nvim_create_user_command(cmd, func, opt)
end end
local dap_config = function() local dap_config = function()
local dap = require("dap") create_cmd('BreakCondition', function(_)
create_cmd("BreakCondition", function(_) local dap = utils.load_plugin('nvim-dap', 'dap')
dap.set_breakpoint(vfn.input("Breakpoint condition: ")) if not dap then
return
end
dap.set_breakpoint(vfn.input('Breakpoint condition: '))
end) end)
create_cmd("ReplRun", function(_) create_cmd('ReplRun', function(_)
local dap = utils.load_plugin('nvim-dap', 'dap')
if not dap then
return
end
dap.repl.run_last() dap.repl.run_last()
end) end)
create_cmd("ReplToggle", function(_) create_cmd('ReplToggle', function(_)
local dap = utils.load_plugin('nvim-dap', 'dap')
if not dap then
return
end
dap.repl.toggle() dap.repl.toggle()
end) end)
create_cmd("ReplOpen", function(_) create_cmd('ReplOpen', function(_)
local dap = utils.load_plugin('nvim-dap', 'dap')
if not dap then
return
end
dap.repl.open() dap.repl.open()
vim.cmd("split") vim.cmd('split')
end) end)
create_cmd("DapRerun", function(_) create_cmd('DapRerun', function(_)
local dap = utils.load_plugin('nvim-dap', 'dap')
if not dap then
return
end
dap.disconnect() dap.disconnect()
dap.close() dap.close()
dap.run_last() dap.run_last()
end) 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 = package.loaded.go.dbg_complete, nargs = '*' })
create_cmd('GoCreateLaunch', function(_)
require('go.launch').config()
end)
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('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('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.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)
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 end
return { return {
@ -37,20 +156,20 @@ return {
augroup end augroup end
]]) ]])
create_cmd("GoMake", function(_) create_cmd('GoMake', function(_)
require("go.asyncmake").make() require('go.asyncmake').make()
end) end)
create_cmd("GoFmt", function(opts) create_cmd('GoFmt', function(opts)
require("go.format").gofmt(unpack(opts.fargs), { nargs = "*" }) require('go.format').gofmt(unpack(opts.fargs), { nargs = '*' })
end) end)
create_cmd("GoImport", function(opts) create_cmd('GoImport', function(opts)
require("go.format").goimport(unpack(opts.fargs)) require('go.format').goimport(unpack(opts.fargs))
end, { complete = package.loaded.go.doc_complete, nargs = "*" }) end, { complete = package.loaded.go.doc_complete, nargs = '*' })
create_cmd("GoGet", function(opts) create_cmd('GoGet', function(opts)
require("go.goget").run(unpack(opts.fargs), { nargs = "*" }) require('go.goget').run(unpack(opts.fargs), { nargs = '*' })
end) end)
local gobin = _GO_NVIM_CFG.go local gobin = _GO_NVIM_CFG.go
local cmd = string.format([[command! GoGenerate :setl makeprg=%s\ generate | :GoMake]], gobin) local cmd = string.format([[command! GoGenerate :setl makeprg=%s\ generate | :GoMake]], gobin)
@ -71,8 +190,8 @@ return {
) )
vim.cmd(cmd) vim.cmd(cmd)
create_cmd("GoStop", function(opts) create_cmd('GoStop', function(opts)
require("go.asyncmake").stopjob(unpack(opts.fargs), { nargs = "*" }) require('go.asyncmake').stopjob(unpack(opts.fargs), { nargs = '*' })
end) end)
-- if you want to output to quickfix -- if you want to output to quickfix
@ -80,17 +199,17 @@ return {
-- vim.cmd( -- 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>)]]) -- [[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) create_cmd('GoTest', function(opts)
require("go.gotest").test(unpack(opts.fargs)) require('go.gotest').test(unpack(opts.fargs))
end, { complete = package.loaded.go.package_complete, nargs = "*" }) end, { complete = package.loaded.go.package_complete, nargs = '*' })
create_cmd("GoCoverage", function(opts) create_cmd('GoCoverage', function(opts)
require("go.coverage").run(unpack(opts.fargs)) require('go.coverage').run(unpack(opts.fargs))
end, { complete = package.loaded.go.package_complete, nargs = "*" }) end, { complete = package.loaded.go.package_complete, nargs = '*' })
create_cmd("GoPkgOutline", function(opts) create_cmd('GoPkgOutline', function(opts)
require("go.package").outline(unpack(opts.fargs)) require('go.package').outline(unpack(opts.fargs))
end, { complete = package.loaded.go.package_complete, nargs = "*" }) end, { complete = package.loaded.go.package_complete, nargs = '*' })
-- vim.cmd([[command! GoTestCompile :setl makeprg=go\ build | :GoMake]]) -- vim.cmd([[command! GoTestCompile :setl makeprg=go\ build | :GoMake]])
--print-issued-lines=false --print-issued-lines=false
@ -99,201 +218,139 @@ return {
[[command! GoLint :setl makeprg=golangci-lint\ run\ --print-issued-lines=false\ --exclude-use-default=false | :GoMake]] [[command! GoLint :setl makeprg=golangci-lint\ run\ --print-issued-lines=false\ --exclude-use-default=false | :GoMake]]
) )
create_cmd("GoProject", function(opts) create_cmd('GoProject', function(opts)
require("go.project").setup() require('go.project').setup()
end) end)
create_cmd("GoCheat", function(opts) create_cmd('GoCheat', function(opts)
require("go.chtsh").run(unpack(opts.fargs), { nargs = "*" }) require('go.chtsh').run(unpack(opts.fargs), { nargs = '*' })
end) end)
-- e.g. GoTestFunc unit -- e.g. GoTestFunc unit
create_cmd("GoTestFunc", function(opts) create_cmd('GoTestFunc', function(opts)
require("go.gotest").test_func(unpack(opts.fargs), { nargs = "*" }) require('go.gotest').test_func(unpack(opts.fargs), { nargs = '*' })
end) end)
-- e.g. GoTestFile unit -- e.g. GoTestFile unit
-- command! -nargs=* -complete=custom,v:lua.package.loaded.go.package_complete GoTestFile lua require('go.gotest').test_file(<f-args>) -- 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) create_cmd('GoTestFile', function(opts)
require("go.gotest").test_file(unpack(opts.fargs)) require('go.gotest').test_file(unpack(opts.fargs))
end, { complete = package.loaded.go.package_complete, nargs = "*" }) end, { complete = package.loaded.go.package_complete, nargs = '*' })
create_cmd("GoTestPkg", function(opts) create_cmd('GoTestPkg', function(opts)
require("go.gotest").test_package(unpack(opts.fargs)) require('go.gotest').test_package(unpack(opts.fargs))
end, { complete = package.loaded.go.package_complete, nargs = "*" }) end, { complete = package.loaded.go.package_complete, nargs = '*' })
create_cmd("GoAddTest", function(opts) create_cmd('GoAddTest', function(opts)
require("go.gotests").fun_test(unpack(opts.fargs)) require('go.gotests').fun_test(unpack(opts.fargs))
end) end)
create_cmd("GoAddExpTest", function(opts) create_cmd('GoAddExpTest', function(opts)
require("go.gotests").exported_test(unpack(opts.fargs), { nargs = "*" }) require('go.gotests').exported_test(unpack(opts.fargs), { nargs = '*' })
end) end)
create_cmd("GoAddAllTest", function(opts) create_cmd('GoAddAllTest', function(opts)
require("go.gotests").all_test(unpack(opts.fargs), { nargs = "*" }) require('go.gotests').all_test(unpack(opts.fargs), { nargs = '*' })
end) end)
create_cmd("GoModVendor", function(opts) create_cmd('GoModVendor', function(opts)
require("go.mod").run(unpack(opts.fargs), { nargs = "*" }) require('go.mod').run(unpack(opts.fargs), { nargs = '*' })
end) end)
create_cmd("GoModInit", function(opts) create_cmd('GoModInit', function(opts)
require("go.mod").run(unpack(opts.fargs), { nargs = "*" }) require('go.mod').run(unpack(opts.fargs), { nargs = '*' })
end) end)
create_cmd("GoEnv", function(opts) create_cmd('GoEnv', function(opts)
require("go.env").load_env(unpack(opts.fargs), { nargs = "*" }) require('go.env').load_env(unpack(opts.fargs), { nargs = '*' })
end) end)
create_cmd("GoCodeLenAct", function(_) create_cmd('GoCodeLenAct', function(_)
require("go.codelens").run_action() require('go.codelens').run_action()
end) end)
create_cmd("GoCodeAction", function(_) create_cmd('GoCodeAction', function(_)
require("go.codeaction").run_action() require('go.codeaction').run_action()
end) end)
create_cmd("GoModifyTag", function(opts) create_cmd('GoModifyTag', function(opts)
require("go.tags").modify(unpack(opts.fargs)) require('go.tags').modify(unpack(opts.fargs))
end, { complete = package.loaded.go.modify_tags_complete, nargs = "*" }) end, { complete = package.loaded.go.modify_tags_complete, nargs = '*' })
create_cmd("GoAddTag", function(opts) create_cmd('GoAddTag', function(opts)
require("go.tags").add(unpack(opts.fargs)) require('go.tags').add(unpack(opts.fargs))
end, { complete = package.loaded.go.add_tags_complete, nargs = "*" }) end, { complete = package.loaded.go.add_tags_complete, nargs = '*' })
create_cmd("GoRmTag", function(opts) create_cmd('GoRmTag', function(opts)
require("go.tags").rm(unpack(opts.fargs), { nargs = "*" }) require('go.tags').rm(unpack(opts.fargs), { nargs = '*' })
end) end)
create_cmd("GoImpl", function(opts) create_cmd('GoImpl', function(opts)
require("go.impl").run(unpack(opts.fargs)) require('go.impl').run(unpack(opts.fargs))
end, { complete = package.loaded.go.impl_complete, nargs = "*" }) end, { complete = package.loaded.go.impl_complete, nargs = '*' })
create_cmd("GoDoc", function(opts) create_cmd('GoDoc', function(opts)
require("go.godoc").run(unpack(opts.fargs)) require('go.godoc').run(unpack(opts.fargs))
end, { complete = package.loaded.go.doc_complete, nargs = "*" }) end, { complete = package.loaded.go.doc_complete, nargs = '*' })
create_cmd("GoInstallBinary", function(opts) create_cmd('GoInstallBinary', function(opts)
require("go.install").install(unpack(opts.fargs)) require('go.install').install(unpack(opts.fargs))
end, { complete = package.loaded.go.tools_complete, nargs = "*" }) end, { complete = package.loaded.go.tools_complete, nargs = '*' })
create_cmd("GoUpdateBinary", function(opts) create_cmd('GoUpdateBinary', function(opts)
require("go.install").update(unpack(opts.fargs)) require('go.install').update(unpack(opts.fargs))
end, { complete = package.loaded.go.tools_complete, nargs = "*" }) end, { complete = package.loaded.go.tools_complete, nargs = '*' })
create_cmd("GoInstallBinaries", function(_) create_cmd('GoInstallBinaries', function(_)
require("go.install").install_all() require('go.install').install_all()
end) end)
create_cmd("GoUpdateBinaries", function(_) create_cmd('GoUpdateBinaries', function(_)
require("go.install").update_all() require('go.install').update_all()
end) end)
create_cmd("GoClearTag", function(_) create_cmd('GoClearTag', function(_)
require("go.tags").clear() require('go.tags').clear()
end) end)
create_cmd("GoCmt", function(_) create_cmd('GoCmt', function(_)
require("go.comment").gen() require('go.comment').gen()
end) end)
create_cmd("GoRename", function(_) create_cmd('GoRename', function(_)
require("go.rename").lsprename() require('go.rename').lsprename()
end) end)
create_cmd("GoIfErr", function(_) create_cmd('GoIfErr', function(_)
require("go.iferr").run() require('go.iferr').run()
end) end)
create_cmd("GoFillStruct", function(_) create_cmd('GoFillStruct', function(_)
require("go.reftool").fillstruct() require('go.reftool').fillstruct()
end) end)
create_cmd("GoFillSwitch", function(_) create_cmd('GoFillSwitch', function(_)
require("go.reftool").fillswitch() require('go.reftool').fillswitch()
end) end)
create_cmd("GoFixPlurals", function(_) create_cmd('GoFixPlurals', function(_)
require("go.fixplurals").fixplurals() require('go.fixplurals').fixplurals()
end) end)
create_cmd("GoAlt", function(opts) create_cmd('GoAlt', function(opts)
require("go.alternate").switch(opts.bang, "") require('go.alternate').switch(opts.bang, '')
end, { bang = true }) end, { bang = true })
create_cmd("GoAltV", function(opts) create_cmd('GoAltV', function(opts)
require("go.alternate").switch(opts.bang, "vsplit") require('go.alternate').switch(opts.bang, 'vsplit')
end, { bang = true }) end, { bang = true })
create_cmd("GoAltS", function(opts) create_cmd('GoAltS', function(opts)
require("go.alternate").switch(opts.bang, "split") require('go.alternate').switch(opts.bang, 'split')
end, { bang = true }) end, { bang = true })
create_cmd("GoModTidy", function(_) create_cmd('GoModTidy', function(_)
require("go.gopls").tidy() require('go.gopls').tidy()
end) end)
create_cmd("GoListImports", function(_) create_cmd('GoListImports', function(_)
print(vim.inspect(require("go.gopls").list_imports())) print(vim.inspect(require('go.gopls').list_imports()))
end) end)
create_cmd("GoCallstack", function(_) create_cmd('GoCallstack', function(_)
require("go.guru").callstack(-1) require('go.guru').callstack(-1)
end) end)
create_cmd("GoChannel", function(_) create_cmd('GoChannel', function(_)
require("go.guru").channel_peers(-1) require('go.guru').channel_peers(-1)
end) end)
if _GO_NVIM_CFG.dap_debug then if _GO_NVIM_CFG.dap_debug then
dap_config() dap_config()
local dap = require("dap")
local gdap = require("go.dap")
create_cmd("GoDebug", function(opts)
gdap.run(unpack(opts.fargs))
end, { complete = package.loaded.go.dbg_complete, nargs = "*" })
create_cmd("GoCreateLaunch", function(_)
require("go.launch").config()
end)
create_cmd("GoCreateLaunch", function(_)
require("go.launch").config()
end)
create_cmd("GoBreakSave", function(_)
gdap.save_brks()
end)
create_cmd("GoBreakLoad", function(_)
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(_)
gdap.breakpt()
end)
create_cmd("GoDbgKeys", function(_)
gdap.debug_keys()
end)
create_cmd("BreakCondition", function(_)
dap.set_breakpoint(vfn.input("Breakpoint condition: "))
end)
create_cmd("ReplRun", function(_)
dap.repl.run_last()
end)
create_cmd("ReplToggle", function(_)
dap.repl.toggle()
end)
create_cmd("ReplOpen", function(_)
dap.open()
vim.cmd("split")
end)
create_cmd("DapRerun", function(_)
dap.disconnect()
dap.close()
dap.run_last()
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(_)
dap.continue()
end)
end end
create_cmd("GoMockGen", require("go.mockgen").run, { create_cmd('GoMockGen', require('go.mockgen').run, {
nargs = "*", nargs = '*',
-- bang = true, -- bang = true,
complete = function(ArgLead, CmdLine, CursorPos) complete = function(ArgLead, CmdLine, CursorPos)
-- return completion candidates as a list-like table -- return completion candidates as a list-like table
return { "-p", "-d", "-i", "-s" } return { '-p', '-d', '-i', '-s' }
end, end,
}) })
end, end,