Breaking changes for GoDebug, deprecate old argument and using linux style command line arguments

pull/93/head
ray-x 2 years ago
parent 0fe0a9ee33
commit ed3dfb85b2

@ -285,13 +285,15 @@ e.g:
| command | Description |
| ---------------- | ------------------------------------------------ |
| GoDebug | start debug session |
| GoDebug test | start debug session for go test file |
| GoDebug restart | restart debug session for go test file |
| GoDebug nearest | start debug session for nearest go test function |
| GoDebug file | same as GoDebug |
| GoDebug stop | stop debug session and unmap debug keymap |
| GoBreakToggle | |
| GoBreakCondition | conditional break |
| GoDebug -t | start debug session for go test file |
| GoDebug -R | restart debug session for go test file |
| GoDebug -n | start debug session for nearest go test function |
| GoDebug -f | same as GoDebug |
| GoDebug -p | debug package |
| GoDebug -a | attach to remote process |
| GoDebug -s | stop debug session and unmap debug keymap |
| GoBreakToggle | GoDebug -b |
| GoBreakCondition | conditional break |
## Switch between go and test file

@ -246,7 +246,7 @@ COMMANDS *go-nvim-commands*
:GoDebug {options} *:GoDebug*
Start debuger
options: test, restart, nearest, file, stop
options: -t(test), -R(restart), -n(nearest), -f(file), -s(stop), -b(breakpoint)
If no option provided, will
1) check launch.json and launch the valid configuration from
launch.json

@ -160,38 +160,41 @@ function test_arg(arg)
opts, optind, optarg = alt_getopt.get_ordered_opts(arg, "cg:hvo:n:rS:st", long_opts)
print("ordered opts")
vim.inspect(opts)
vim.inspect(optind)
vim.inspect(optarg)
print(vim.inspect(opts))
print(vim.inspect(optind))
print(vim.inspect(optarg))
print("ordered opts end")
for i, v in ipairs(opts) do
if optarg[i] then
print("option " .. v .. ": " .. vim.inspect(optarg[i]))
print("option opts[i] " .. v .. ": " .. vim.inspect(optarg[i]))
else
print("option " .. v)
end
end
print("get_opts")
optarg, optind = alt_getopt.get_opts(arg, "g:hVvo:n:rS:st", long_opts)
print(vim.inspect(opts), vim.inspect(optind))
local fin_options = {}
for k, v in pairs(optarg) do
table.insert(fin_options, "fin-option " .. k .. ": " .. vim.inspect(v) .. "\n")
end
table.sort(fin_options)
-- -- print(table.concat(fin_options))
print(table.concat(fin_options))
--
-- -- for i = optind, #arg do
-- -- print(string.format("ARGV [%s] = %s\n", i, arg[i]))
-- -- end
for i = optind, #arg do
print(string.format("ARGV [%s] = %s\n", i, arg[i]))
end
end
-- test_arg()
--
-- print("test 2")
--
-- test_arg({ "-tr", "-c", "-g", "unit,integration" })
-- test_arg({ "--tags", "unit,integration", "--restart" })
-- test_arg({ "run", "restart" })
-- test_arg({ "--tags", "unit,integration", "-c", "--restart" })
return alt_getopt

@ -3,6 +3,33 @@ local map_cr = bind.map_cr
local utils = require("go.utils")
local log = utils.log
local sep = "." .. utils.sep()
local getopt = require("go.alt_getopt")
local long_opts = {
compile = "c",
run = "r",
test = "t",
restart = "R",
help = "h",
nearest = "n",
file = "f",
breakpoint = "b",
tag = "T",
}
local opts = "tcrRnfsbhT:"
local function help()
return "Usage: GoDebug [OPTION]\n"
.. "Options:\n"
.. " -c, --compile compile and run\n"
.. " -r, --run run\n"
.. " -t, --test run tests\n"
.. " -R, --restart restart\n"
.. " -h, --help display this help and exit\n"
.. " -n, --nearest display nearest file\n"
.. " -f, --file display file\n"
.. " -b, --breakpoint set breakpoint\n"
.. " -T, --tag set tag"
end
local function setup_telescope()
require("telescope").setup()
@ -78,10 +105,10 @@ M.prepare = function()
if _GO_NVIM_CFG.dap_debug_gui then
utils.load_plugin("nvim-dap-ui", "dapui")
if _GO_NVIM_CFG.dap_debug_vt then
local vt = utils.load_plugin("nvim-dap-virtual-text")
vt.setup({ enabled_commands = true, all_frames = true })
end
end
if _GO_NVIM_CFG.dap_debug_vt then
local vt = utils.load_plugin("nvim-dap-virtual-text")
vt.setup({ enabled_commands = true, all_frames = true })
end
end
@ -94,6 +121,36 @@ M.run = function(...)
local args = { ... }
local mode = select(1, ...)
local ctl_opt = select(2, ...)
if mode == "test" or mode == "nearest" then
utils.warn("option test is deprecated, options are " .. opts .. " ")
return utils.info(help())
end
local optarg, optind = getopt.get_opts(args, opts, long_opts)
log(optarg, optind)
if optarg["h"] then
return utils.info(help())
end
if optarg["c"] then
local fpath = vim.fn.expand("%:p:h")
local out = vim.fn.systemlist(table.concat({
"go",
"test",
"-v",
"-cover",
"-covermode=atomic",
"-coverprofile=cover.out",
"-tags " .. _GO_NVIM_CFG.build_tags,
"-c",
fpath,
}, " "))
if #out ~= 0 then
utils.info("building " .. vim.inspect(out))
end
return
end
local guihua = utils.load_plugin("guihua.lua", "guihua.gui")
@ -104,11 +161,12 @@ M.run = function(...)
-- testopts = {"test", "nearest", "file", "stop", "restart"}
log("plugin loaded", mode, ctl_opt)
if mode == "stop" or ctl_opt == "stop" then
if optarg["s"] then
return require("go.dap").stop(true)
end
if mode == "restart" or ctl_opt == "restart" then
-- restart
if optarg["R"] then
require("go.dap").stop()
if ctl_opt == "restart" then
mode = mode
@ -125,17 +183,21 @@ M.run = function(...)
vim.notify("debug session already start, press c to continue", vim.lsp.log_levels.INFO)
return
end
local run_cur = optarg["r"]
if not run_cur then
keybind()
else
M.stop()
end
keybind()
if _GO_NVIM_CFG.dap_debug_gui then
if _GO_NVIM_CFG.dap_debug_gui and not run_cur then
require("dapui").setup()
if not require("dapui.windows").sidebar:is_open() then
require("dapui").open()
end
end
local port = 38697
local port = _GO_NVIM_CFG.dap_port
if _GO_NVIM_CFG.dap_port == nil or _GO_NVIM_CFG.dap_port == -1 then
math.randomseed(os.time())
port = 38000 + math.random(1, 1000)
@ -145,7 +207,7 @@ M.run = function(...)
local stdout = vim.loop.new_pipe(false)
local handle
local pid_or_err
local port = config.port or port
port = config.port or port
local host = config.host or "127.0.0.1"
@ -183,8 +245,11 @@ M.run = function(...)
end
end)
-- Wait 500ms for delve to start
vim.defer_fn(function()
if not optarg["r"] then
dap.repl.open()
end
vim.defer_fn(function()
callback({ type = "server", host = host, port = port })
end, 500)
end
@ -204,15 +269,19 @@ M.run = function(...)
local ns = require("go.ts.go").get_func_method_node_at_pos(row, col)
if empty(ns) then
log("ts not not found, debug while file")
if mode == "nearest" then
mode = "test"
end
end
local launch = require("go.launch")
local cfg_exist, cfg_file = launch.vs_launch()
log(mode, cfg_exist, cfg_file)
if mode == "test" then
-- if breakpoint is not set add breakpoint at current pos
local pts = require("dap.breakpoints").get()
if #pts == 0 then
require("dap").toggle_breakpoint()
end
if optarg["t"] then
dap_cfg.name = dap_cfg.name .. " test"
dap_cfg.mode = "test"
dap_cfg.request = "launch"
@ -220,8 +289,11 @@ M.run = function(...)
-- dap_cfg.program = "${file}"
dap_cfg.program = sep .. "${relativeFileDirname}"
dap.configurations.go = { dap_cfg }
log(dap_cfg)
dap.continue()
elseif mode == "nearest" then
elseif optarg["n"] then
dap_cfg.name = dap_cfg.name .. " test_nearest"
dap_cfg.mode = "test"
dap_cfg.request = "launch"
@ -230,7 +302,7 @@ M.run = function(...)
log(dap_cfg)
dap.configurations.go = { dap_cfg }
dap.continue()
elseif mode == "attach" then
elseif optarg["a"] then
dap_cfg.name = dap_cfg.name .. " attach"
dap_cfg.mode = "local"
dap_cfg.request = "attach"
@ -238,13 +310,21 @@ M.run = function(...)
log(dap_cfg)
dap.configurations.go = { dap_cfg }
dap.continue()
elseif mode == "package" then
elseif optarg["p"] then
dap_cfg.name = dap_cfg.name .. " package"
dap_cfg.request = "launch"
dap_cfg.program = sep .. "${fileDirname}"
log(dap_cfg)
dap.configurations.go = { dap_cfg }
dap.continue()
elseif run_cur then
dap_cfg.name = dap_cfg.name .. " run current"
-- dap_cfg.mode = "test"
dap_cfg.request = "launch"
dap_cfg.program = sep .. "${relativeFileDirname}"
log(dap_cfg)
dap.configurations.go = { dap_cfg }
dap.continue()
elseif cfg_exist then
log("using cfg")
launch.load()
@ -309,7 +389,9 @@ M.stop = function(unm)
end
local has_dapui, dapui = pcall(require, "dapui")
if has_dapui then
dapui.close()
if require("dapui.windows").sidebar ~= nil then
dapui.close()
end
end
end

Loading…
Cancel
Save