diff --git a/lua/go/commands.lua b/lua/go/commands.lua index 7e95297..6e154d0 100644 --- a/lua/go/commands.lua +++ b/lua/go/commands.lua @@ -1,29 +1,148 @@ 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 {}) + 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() - local dap = require("dap") - create_cmd("BreakCondition", function(_) - dap.set_breakpoint(vfn.input("Breakpoint condition: ")) + 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(_) + 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(_) + 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(_) + create_cmd('ReplOpen', function(_) + local dap = utils.load_plugin('nvim-dap', 'dap') + if not dap then + return + end dap.repl.open() - vim.cmd("split") + vim.cmd('split') 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.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 = 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 return { @@ -37,20 +156,20 @@ return { augroup end ]]) - create_cmd("GoMake", function(_) - require("go.asyncmake").make() + create_cmd('GoMake', function(_) + require('go.asyncmake').make() end) - create_cmd("GoFmt", function(opts) - require("go.format").gofmt(unpack(opts.fargs), { nargs = "*" }) + create_cmd('GoFmt', function(opts) + require('go.format').gofmt(unpack(opts.fargs), { nargs = '*' }) end) - create_cmd("GoImport", function(opts) - require("go.format").goimport(unpack(opts.fargs)) - end, { complete = package.loaded.go.doc_complete, nargs = "*" }) + create_cmd('GoImport', function(opts) + require('go.format').goimport(unpack(opts.fargs)) + end, { complete = package.loaded.go.doc_complete, nargs = '*' }) - create_cmd("GoGet", function(opts) - require("go.goget").run(unpack(opts.fargs), { nargs = "*" }) + create_cmd('GoGet', function(opts) + require('go.goget').run(unpack(opts.fargs), { nargs = '*' }) end) local gobin = _GO_NVIM_CFG.go local cmd = string.format([[command! GoGenerate :setl makeprg=%s\ generate | :GoMake]], gobin) @@ -71,8 +190,8 @@ return { ) vim.cmd(cmd) - create_cmd("GoStop", function(opts) - require("go.asyncmake").stopjob(unpack(opts.fargs), { nargs = "*" }) + create_cmd('GoStop', function(opts) + require('go.asyncmake').stopjob(unpack(opts.fargs), { nargs = '*' }) end) -- if you want to output to quickfix @@ -80,17 +199,17 @@ return { -- vim.cmd( -- [[command! -nargs=* -complete=custom,v:lua.package.loaded.go.package_complete GoTest :setl makeprg=go\ test\ -v\ | lua require'go.runner'.make()]]) - create_cmd("GoTest", function(opts) - require("go.gotest").test(unpack(opts.fargs)) - end, { complete = package.loaded.go.package_complete, nargs = "*" }) + create_cmd('GoTest', function(opts) + require('go.gotest').test(unpack(opts.fargs)) + end, { complete = package.loaded.go.package_complete, nargs = '*' }) - create_cmd("GoCoverage", function(opts) - require("go.coverage").run(unpack(opts.fargs)) - end, { complete = package.loaded.go.package_complete, nargs = "*" }) + create_cmd('GoCoverage', function(opts) + require('go.coverage').run(unpack(opts.fargs)) + end, { complete = package.loaded.go.package_complete, nargs = '*' }) - create_cmd("GoPkgOutline", function(opts) - require("go.package").outline(unpack(opts.fargs)) - end, { complete = package.loaded.go.package_complete, nargs = "*" }) + create_cmd('GoPkgOutline', function(opts) + require('go.package').outline(unpack(opts.fargs)) + end, { complete = package.loaded.go.package_complete, nargs = '*' }) -- vim.cmd([[command! GoTestCompile :setl makeprg=go\ build | :GoMake]]) --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]] ) - create_cmd("GoProject", function(opts) - require("go.project").setup() + create_cmd('GoProject', function(opts) + require('go.project').setup() end) - create_cmd("GoCheat", function(opts) - require("go.chtsh").run(unpack(opts.fargs), { nargs = "*" }) + create_cmd('GoCheat', function(opts) + require('go.chtsh').run(unpack(opts.fargs), { nargs = '*' }) end) -- e.g. GoTestFunc unit - create_cmd("GoTestFunc", function(opts) - require("go.gotest").test_func(unpack(opts.fargs), { nargs = "*" }) + create_cmd('GoTestFunc', function(opts) + require('go.gotest').test_func(unpack(opts.fargs), { nargs = '*' }) end) -- e.g. GoTestFile unit -- command! -nargs=* -complete=custom,v:lua.package.loaded.go.package_complete GoTestFile lua require('go.gotest').test_file() - create_cmd("GoTestFile", function(opts) - require("go.gotest").test_file(unpack(opts.fargs)) - end, { complete = package.loaded.go.package_complete, nargs = "*" }) - create_cmd("GoTestPkg", function(opts) - require("go.gotest").test_package(unpack(opts.fargs)) - end, { complete = package.loaded.go.package_complete, nargs = "*" }) - create_cmd("GoAddTest", function(opts) - require("go.gotests").fun_test(unpack(opts.fargs)) + create_cmd('GoTestFile', function(opts) + require('go.gotest').test_file(unpack(opts.fargs)) + end, { complete = package.loaded.go.package_complete, nargs = '*' }) + create_cmd('GoTestPkg', function(opts) + require('go.gotest').test_package(unpack(opts.fargs)) + end, { complete = package.loaded.go.package_complete, 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), { nargs = "*" }) + create_cmd('GoAddExpTest', function(opts) + require('go.gotests').exported_test(unpack(opts.fargs), { nargs = '*' }) end) - create_cmd("GoAddAllTest", function(opts) - require("go.gotests").all_test(unpack(opts.fargs), { nargs = "*" }) + create_cmd('GoAddAllTest', function(opts) + require('go.gotests').all_test(unpack(opts.fargs), { nargs = '*' }) end) - create_cmd("GoModVendor", function(opts) - require("go.mod").run(unpack(opts.fargs), { nargs = "*" }) + create_cmd('GoModVendor', function(opts) + require('go.mod').run(unpack(opts.fargs), { nargs = '*' }) end) - create_cmd("GoModInit", function(opts) - require("go.mod").run(unpack(opts.fargs), { nargs = "*" }) + create_cmd('GoModInit', function(opts) + require('go.mod').run(unpack(opts.fargs), { nargs = '*' }) end) - create_cmd("GoEnv", function(opts) - require("go.env").load_env(unpack(opts.fargs), { nargs = "*" }) + create_cmd('GoEnv', function(opts) + require('go.env').load_env(unpack(opts.fargs), { nargs = '*' }) end) - create_cmd("GoCodeLenAct", function(_) - require("go.codelens").run_action() + create_cmd('GoCodeLenAct', function(_) + require('go.codelens').run_action() end) - create_cmd("GoCodeAction", function(_) - require("go.codeaction").run_action() + create_cmd('GoCodeAction', function(_) + require('go.codeaction').run_action() end) - create_cmd("GoModifyTag", function(opts) - require("go.tags").modify(unpack(opts.fargs)) - end, { complete = package.loaded.go.modify_tags_complete, nargs = "*" }) - create_cmd("GoAddTag", function(opts) - require("go.tags").add(unpack(opts.fargs)) - end, { complete = package.loaded.go.add_tags_complete, nargs = "*" }) - create_cmd("GoRmTag", function(opts) - require("go.tags").rm(unpack(opts.fargs), { nargs = "*" }) + create_cmd('GoModifyTag', function(opts) + require('go.tags').modify(unpack(opts.fargs)) + end, { complete = package.loaded.go.modify_tags_complete, nargs = '*' }) + create_cmd('GoAddTag', function(opts) + require('go.tags').add(unpack(opts.fargs)) + end, { complete = package.loaded.go.add_tags_complete, nargs = '*' }) + create_cmd('GoRmTag', function(opts) + require('go.tags').rm(unpack(opts.fargs), { nargs = '*' }) end) - create_cmd("GoImpl", function(opts) - require("go.impl").run(unpack(opts.fargs)) - end, { complete = package.loaded.go.impl_complete, nargs = "*" }) + create_cmd('GoImpl', function(opts) + require('go.impl').run(unpack(opts.fargs)) + end, { complete = package.loaded.go.impl_complete, nargs = '*' }) - create_cmd("GoDoc", function(opts) - require("go.godoc").run(unpack(opts.fargs)) - end, { complete = package.loaded.go.doc_complete, nargs = "*" }) + create_cmd('GoDoc', function(opts) + require('go.godoc').run(unpack(opts.fargs)) + end, { complete = package.loaded.go.doc_complete, nargs = '*' }) - create_cmd("GoInstallBinary", function(opts) - require("go.install").install(unpack(opts.fargs)) - end, { complete = package.loaded.go.tools_complete, nargs = "*" }) + create_cmd('GoInstallBinary', function(opts) + require('go.install').install(unpack(opts.fargs)) + end, { complete = package.loaded.go.tools_complete, nargs = '*' }) - create_cmd("GoUpdateBinary", function(opts) - require("go.install").update(unpack(opts.fargs)) - end, { complete = package.loaded.go.tools_complete, nargs = "*" }) + create_cmd('GoUpdateBinary', function(opts) + require('go.install').update(unpack(opts.fargs)) + end, { complete = package.loaded.go.tools_complete, nargs = '*' }) - create_cmd("GoInstallBinaries", function(_) - require("go.install").install_all() + create_cmd('GoInstallBinaries', function(_) + require('go.install').install_all() end) - create_cmd("GoUpdateBinaries", function(_) - require("go.install").update_all() + create_cmd('GoUpdateBinaries', function(_) + require('go.install').update_all() end) - create_cmd("GoClearTag", function(_) - require("go.tags").clear() + create_cmd('GoClearTag', function(_) + require('go.tags').clear() end) - create_cmd("GoCmt", function(_) - require("go.comment").gen() + create_cmd('GoCmt', function(_) + require('go.comment').gen() end) - create_cmd("GoRename", function(_) - require("go.rename").lsprename() + create_cmd('GoRename', function(_) + require('go.rename').lsprename() end) - create_cmd("GoIfErr", function(_) - require("go.iferr").run() + create_cmd('GoIfErr', function(_) + require('go.iferr').run() end) - create_cmd("GoFillStruct", function(_) - require("go.reftool").fillstruct() + create_cmd('GoFillStruct', function(_) + require('go.reftool').fillstruct() end) - create_cmd("GoFillSwitch", function(_) - require("go.reftool").fillswitch() + create_cmd('GoFillSwitch', function(_) + require('go.reftool').fillswitch() end) - create_cmd("GoFixPlurals", function(_) - require("go.fixplurals").fixplurals() + create_cmd('GoFixPlurals', function(_) + require('go.fixplurals').fixplurals() end) - create_cmd("GoAlt", function(opts) - require("go.alternate").switch(opts.bang, "") + 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") + 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") + create_cmd('GoAltS', function(opts) + require('go.alternate').switch(opts.bang, 'split') end, { bang = true }) - create_cmd("GoModTidy", function(_) - require("go.gopls").tidy() + create_cmd('GoModTidy', function(_) + require('go.gopls').tidy() end) - create_cmd("GoListImports", function(_) - print(vim.inspect(require("go.gopls").list_imports())) + create_cmd('GoListImports', function(_) + print(vim.inspect(require('go.gopls').list_imports())) end) - create_cmd("GoCallstack", function(_) - require("go.guru").callstack(-1) + create_cmd('GoCallstack', function(_) + require('go.guru').callstack(-1) end) - create_cmd("GoChannel", function(_) - require("go.guru").channel_peers(-1) + create_cmd('GoChannel', function(_) + require('go.guru').channel_peers(-1) end) if _GO_NVIM_CFG.dap_debug then 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 - create_cmd("GoMockGen", require("go.mockgen").run, { - nargs = "*", + create_cmd('GoMockGen', require('go.mockgen').run, { + nargs = '*', -- bang = true, complete = function(ArgLead, CmdLine, CursorPos) -- return completion candidates as a list-like table - return { "-p", "-d", "-i", "-s" } + return { '-p', '-d', '-i', '-s' } end, }) end,