diff --git a/README.md b/README.md index 25177c3..1340464 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,9 @@ Support table based unit test auto generate, parse current function/method name | command | Description | | ----------- | ----------- | | GoTestFunc | run test for current func | +| GoTestFunc yourtag | run test for current func with `-tags yourtag` option | +| GoTestFile | run test for current file folder | +| GoTestFile yourtag | run test for current folder with `-tags yourtag` option | | GoAddTest | | | GoAddExpTest | Add tests for exported funcs| | GoAddAllTest | Add tests for all funcs | @@ -197,6 +200,7 @@ e.g: | ----------- | ----------- | | GoDebug | start debug session | | GoDebug test | start debug session for go test file | +| GoDebug nearest | start debug session for nearest go test function | | GoBreakToggle | | | GoBreakCondition | conditional break | | GoDbgStop | Stop debug session| diff --git a/lua/go.lua b/lua/go.lua index b977f7b..09827ed 100644 --- a/lua/go.lua +++ b/lua/go.lua @@ -64,7 +64,11 @@ function go.setup(cfg) -- vim.cmd([[command! GoTestCompile :setl makeprg=go\ build | :GoMake]]) vim.cmd([[command! GoLint :setl makeprg=golangci-lint\ run\ --out-format\ tab | :GoMake]]) - vim.cmd([[command! GoTestFunc lua require('go.gotest').test_fun()]]) + -- e.g. GoTestFunc unit + vim.cmd([[command! -nargs=* GoTestFunc lua require('go.gotest').test_fun()]]) + + -- e.g. GoTestFile unit + vim.cmd([[command! -nargs=* GoTestFile lua require('go.gotest').test_file()]]) vim.cmd([[command! GoAddTest lua require("go.gotests").fun_test()]]) vim.cmd([[command! GoAddExpTest lua require("go.gotests").exported_test()]]) vim.cmd([[command! GoAddAllTest lua require("go.gotests").all_test()]]) diff --git a/lua/go/dap.lua b/lua/go/dap.lua index c392d45..b8ebe05 100644 --- a/lua/go/dap.lua +++ b/lua/go/dap.lua @@ -111,6 +111,18 @@ M.run = function(...) request = "launch", dlvToolPath = vim.fn.exepath("dlv") } + + local row, col = unpack(vim.api.nvim_win_get_cursor(0)) + row, col = row, col + 1 + + local ns = require("go.ts.go").get_func_method_node_at_pos(row, col) + if ns == nil or ns == {} then + log('ts not not found, debug while file') + if mode == 'nearest' then + mode = 'test' + end + end + if mode == 'test' then dap_cfg.name = dap_cfg.name .. ' test' dap_cfg.mode = "test" @@ -119,6 +131,14 @@ M.run = function(...) dap_cfg.program = "./${relativeFileDirname}" dap.configurations.go = {dap_cfg} dap.continue() + elseif mode == 'nearest' then + dap_cfg.name = dap_cfg.name .. ' test_nearest' + dap_cfg.mode = "test" + dap_cfg.program = "./${relativeFileDirname}" + dap_cfg.args = {'-test.run', '^' .. ns.name} + log(dap_cfg) + dap.configurations.go = {dap_cfg} + dap.continue() else dap_cfg.program = "${file}" dap_cfg.args = args diff --git a/lua/go/gotest.lua b/lua/go/gotest.lua index cd839ad..d16331e 100644 --- a/lua/go/gotest.lua +++ b/lua/go/gotest.lua @@ -11,11 +11,33 @@ M.test_fun = function(args) return end + local tag = '' + utils.log(args) + if args ~= nil then + tag = [[-tags\ ]] .. args .. [[\ ]] + end + utils.log("parnode" .. vim.inspect(ns)) - local cmd = [[setl makeprg=go\ test\ -v\ -run\ ^]] .. ns.name .. [[\ ]] .. fpath + local cmd = [[setl makeprg=go\ test\ ]] .. tag .. [[-v\ -run\ ^]] .. ns.name .. [[\ ]] .. fpath .. [[ | lua require"go.asyncmake".make()]] utils.log("test cmd", cmd) vim.cmd(cmd) end +M.test_file = function(args) + local workfolder = vim.lsp.buf.list_workspace_folders()[1] + local tag = '' + utils.log(args) + if args ~= nil then + tag = [[-tags\ ]] .. args .. [[\ ]] + end + local fpath = vim.fn.expand("%:p:h") .. '/..' + -- local fpath = './' .. vim.fn.expand('%:h') .. '/...' + utils.log("fpath" .. fpath) + local cmd = [[setl makeprg=go\ test\ ]] .. tag .. [[-v\ -run\ ]] .. fpath + .. [[| lua require"go.asyncmake".make()]] + utils.log("test cmd", cmd) + vim.cmd(cmd) +end + return M