2021-07-14 13:58:31 +00:00
|
|
|
local bind = require("go.keybind")
|
2021-07-10 11:06:10 +00:00
|
|
|
local map_cr = bind.map_cr
|
2021-12-16 22:02:29 +00:00
|
|
|
local utils = require("go.utils")
|
2021-08-30 06:37:36 +00:00
|
|
|
local log = utils.log
|
2021-12-16 22:02:29 +00:00
|
|
|
local sep = "." .. utils.sep()
|
2021-11-10 02:20:54 +00:00
|
|
|
|
2021-07-10 11:06:10 +00:00
|
|
|
local function setup_telescope()
|
2021-12-16 22:02:29 +00:00
|
|
|
require("telescope").setup()
|
|
|
|
require("telescope").load_extension("dap")
|
2021-07-10 11:06:10 +00:00
|
|
|
local ts_keys = {
|
2021-10-26 00:15:52 +00:00
|
|
|
["n|lb"] = map_cr('<cmd>lua require"telescope".extensions.dap.list_breakpoints{}'):with_noremap():with_silent(),
|
|
|
|
["n|tv"] = map_cr('<cmd>lua require"telescope".extensions.dap.variables{}'):with_noremap():with_silent(),
|
2021-12-16 22:02:29 +00:00
|
|
|
["n|bt"] = map_cr('<cmd>lua require"telescope".extensions.dap.frames{}'):with_noremap():with_silent(),
|
2021-07-10 11:06:10 +00:00
|
|
|
}
|
|
|
|
bind.nvim_load_mapping(ts_keys)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function keybind()
|
2021-12-16 22:02:29 +00:00
|
|
|
if not _GO_NVIM_CFG.dap_debug_keymap then
|
|
|
|
return
|
|
|
|
end
|
2021-07-10 11:06:10 +00:00
|
|
|
local keys = {
|
|
|
|
-- DAP --
|
|
|
|
-- run
|
|
|
|
["n|r"] = map_cr('<cmd>lua require"go.dap".run()<CR>'):with_noremap():with_silent(),
|
|
|
|
["n|c"] = map_cr('<cmd>lua require"dap".continue()<CR>'):with_noremap():with_silent(),
|
|
|
|
["n|n"] = map_cr('<cmd>lua require"dap".step_over()<CR>'):with_noremap():with_silent(),
|
|
|
|
["n|s"] = map_cr('<cmd>lua require"dap".step_into()<CR>'):with_noremap():with_silent(),
|
|
|
|
["n|o"] = map_cr('<cmd>lua require"dap".step_out()<CR>'):with_noremap():with_silent(),
|
|
|
|
["n|S"] = map_cr('<cmd>lua require"go.dap".stop()<CR>'):with_noremap():with_silent(),
|
|
|
|
["n|u"] = map_cr('<cmd>lua require"dap".up()<CR>'):with_noremap():with_silent(),
|
|
|
|
["n|D"] = map_cr('<cmd>lua require"dap".down()<CR>'):with_noremap():with_silent(),
|
|
|
|
["n|C"] = map_cr('<cmd>lua require"dap".run_to_cursor()<CR>'):with_noremap():with_silent(),
|
|
|
|
["n|b"] = map_cr('<cmd>lua require"dap".toggle_breakpoint()<CR>'):with_noremap():with_silent(),
|
2021-12-16 22:02:29 +00:00
|
|
|
["n|P"] = map_cr('<cmd>lua require"dap".pause()<CR>'):with_noremap():with_silent(),
|
2021-07-10 11:06:10 +00:00
|
|
|
--
|
|
|
|
}
|
2021-11-23 23:13:40 +00:00
|
|
|
if _GO_NVIM_CFG.dap_debug_gui then
|
|
|
|
keys["n|p"] = map_cr('<cmd>lua require("dapui").eval()'):with_noremap():with_silent()
|
|
|
|
keys["v|p"] = map_cr('<cmd>lua require("dapui").eval()'):with_noremap():with_silent()
|
|
|
|
keys["n|K"] = map_cr('<cmd>lua require("dapui").float_element()'):with_noremap():with_silent()
|
|
|
|
keys["n|B"] = map_cr('<cmd>lua require("dapui").float_element("breakpoints")'):with_noremap():with_silent()
|
|
|
|
keys["n|R"] = map_cr('<cmd>lua require("dapui").float_element("repl")'):with_noremap():with_silent()
|
|
|
|
keys["n|O"] = map_cr('<cmd>lua require("dapui").float_element("scopes")'):with_noremap():with_silent()
|
|
|
|
keys["n|a"] = map_cr('<cmd>lua require("dapui").float_element("stacks")'):with_noremap():with_silent()
|
|
|
|
keys["n|w"] = map_cr('<cmd>lua require("dapui").float_element("watches")'):with_noremap():with_silent()
|
|
|
|
else
|
|
|
|
keys["n|p"] = map_cr('<cmd>lua require"dap.ui.widgets".hover()<CR>'):with_noremap():with_silent()
|
|
|
|
keys["v|p"] = map_cr('<cmd>lua require"dap.ui.widgets".hover()<CR>'):with_noremap():with_silent()
|
|
|
|
end
|
2021-07-10 11:06:10 +00:00
|
|
|
bind.nvim_load_mapping(keys)
|
|
|
|
end
|
|
|
|
|
2021-10-24 23:05:02 +00:00
|
|
|
local function get_build_flags()
|
|
|
|
if _GO_NVIM_CFG.build_tags ~= "" then
|
|
|
|
return "-tags " .. _GO_NVIM_CFG.build_tags
|
|
|
|
else
|
|
|
|
return ""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-10 11:06:10 +00:00
|
|
|
local M = {}
|
|
|
|
|
|
|
|
M.prepare = function()
|
2021-12-16 22:02:29 +00:00
|
|
|
utils.load_plugin("nvim-dap", "dap")
|
|
|
|
vim.fn.sign_define("DapBreakpoint", {
|
2021-11-23 23:13:40 +00:00
|
|
|
text = _GO_NVIM_CFG.icons.breakpoint,
|
2021-12-16 22:02:29 +00:00
|
|
|
texthl = "",
|
|
|
|
linehl = "",
|
|
|
|
numhl = "",
|
2021-11-23 23:13:40 +00:00
|
|
|
})
|
2021-12-16 22:02:29 +00:00
|
|
|
vim.fn.sign_define("DapStopped", {
|
2021-11-23 23:13:40 +00:00
|
|
|
text = _GO_NVIM_CFG.icons.currentpos,
|
2021-12-16 22:02:29 +00:00
|
|
|
texthl = "",
|
|
|
|
linehl = "",
|
|
|
|
numhl = "",
|
2021-11-23 23:13:40 +00:00
|
|
|
})
|
|
|
|
|
2021-07-10 11:06:10 +00:00
|
|
|
if _GO_NVIM_CFG.dap_debug_gui then
|
2021-12-16 22:02:29 +00:00
|
|
|
utils.load_plugin("nvim-dap-ui", "dapui")
|
|
|
|
if _GO_NVIM_CFG.dap_debug_vt then
|
2021-12-16 22:59:47 +00:00
|
|
|
local vt = utils.load_plugin("nvim-dap-virtual-text")
|
|
|
|
vt.setup({ enabled_commands = true, all_frames = true })
|
|
|
|
end
|
2021-07-10 11:06:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
M.breakpt = function()
|
|
|
|
M.prepare()
|
2021-12-16 22:02:29 +00:00
|
|
|
require("dap").toggle_breakpoint()
|
2021-07-10 11:06:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
M.run = function(...)
|
2021-12-16 22:02:29 +00:00
|
|
|
local args = { ... }
|
2021-07-10 11:06:10 +00:00
|
|
|
local mode = select(1, ...)
|
2022-01-12 22:07:14 +00:00
|
|
|
local ctl_opt = select(2, ...)
|
2021-07-10 11:06:10 +00:00
|
|
|
|
2022-01-16 23:31:24 +00:00
|
|
|
local guihua = utils.load_plugin("guihua.lua", "guihua.gui")
|
|
|
|
|
|
|
|
local original_select = vim.ui.select
|
|
|
|
if guihua then
|
|
|
|
vim.ui.select = require("guihua.gui").select
|
|
|
|
end
|
|
|
|
|
2022-01-12 22:07:14 +00:00
|
|
|
-- testopts = {"test", "nearest", "file", "stop", "restart"}
|
2022-01-15 09:59:03 +00:00
|
|
|
log("plugin loaded", mode, ctl_opt)
|
2022-01-12 22:07:14 +00:00
|
|
|
if mode == "stop" or ctl_opt == "stop" then
|
2021-12-16 22:02:29 +00:00
|
|
|
return require("go.dap").stop(true)
|
2021-11-24 09:05:02 +00:00
|
|
|
end
|
2021-11-29 20:19:45 +00:00
|
|
|
|
2022-01-12 22:07:14 +00:00
|
|
|
if mode == "restart" or ctl_opt == "restart" then
|
2021-12-16 22:02:29 +00:00
|
|
|
require("go.dap").stop()
|
2022-01-12 22:07:14 +00:00
|
|
|
if ctl_opt == "restart" then
|
|
|
|
mode = mode
|
|
|
|
else
|
|
|
|
mode = M.pre_mode or "file"
|
|
|
|
end
|
2021-11-29 20:19:45 +00:00
|
|
|
else
|
|
|
|
M.pre_mode = mode
|
|
|
|
end
|
2022-01-12 22:07:14 +00:00
|
|
|
|
2022-01-15 09:59:03 +00:00
|
|
|
M.prepare()
|
2022-01-12 22:07:14 +00:00
|
|
|
local session = require("dap").session()
|
|
|
|
if session ~= nil and session.initialized == true then
|
|
|
|
vim.notify("debug session already start, press c to continue", vim.lsp.log_levels.INFO)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
keybind()
|
2021-11-29 20:19:45 +00:00
|
|
|
|
2021-07-10 11:06:10 +00:00
|
|
|
if _GO_NVIM_CFG.dap_debug_gui then
|
|
|
|
require("dapui").setup()
|
2022-01-12 22:07:14 +00:00
|
|
|
if not require("dapui.windows").sidebar:is_open() then
|
|
|
|
require("dapui").open()
|
|
|
|
end
|
2021-07-10 11:06:10 +00:00
|
|
|
end
|
2022-01-26 11:51:00 +00:00
|
|
|
|
|
|
|
local port = 38697
|
2022-01-30 05:02:18 +00:00
|
|
|
if _GO_NVIM_CFG.dap_port == nil or _GO_NVIM_CFG.dap_port == -1 then
|
2022-01-26 11:51:00 +00:00
|
|
|
math.randomseed(os.time())
|
|
|
|
port = 38000 + math.random(1, 1000)
|
|
|
|
end
|
2021-12-16 22:02:29 +00:00
|
|
|
local dap = require("dap")
|
2021-07-10 11:06:10 +00:00
|
|
|
dap.adapters.go = function(callback, config)
|
2021-08-25 12:37:22 +00:00
|
|
|
local stdout = vim.loop.new_pipe(false)
|
2021-07-10 11:06:10 +00:00
|
|
|
local handle
|
|
|
|
local pid_or_err
|
2022-01-26 11:51:00 +00:00
|
|
|
local port = config.port or port
|
|
|
|
|
|
|
|
local host = config.host or "127.0.0.1"
|
|
|
|
|
|
|
|
local addr = string.format("%s:%d", host, port)
|
2021-07-10 11:06:10 +00:00
|
|
|
handle, pid_or_err = vim.loop.spawn("dlv", {
|
2021-12-16 22:02:29 +00:00
|
|
|
stdio = { nil, stdout },
|
2022-01-26 11:51:00 +00:00
|
|
|
args = { "dap", "-l", addr },
|
2021-12-16 22:02:29 +00:00
|
|
|
detached = true,
|
2021-07-10 11:06:10 +00:00
|
|
|
}, function(code)
|
2021-08-25 12:37:22 +00:00
|
|
|
stdout:close()
|
2021-07-10 11:06:10 +00:00
|
|
|
handle:close()
|
2021-08-25 12:37:22 +00:00
|
|
|
if code ~= 0 then
|
2022-01-12 08:57:07 +00:00
|
|
|
vim.schedule(function()
|
|
|
|
vim.notify(string.format("Delve exited with exit code: %d", code), vim.lsp.log_levels.WARN)
|
|
|
|
end)
|
2021-08-25 12:37:22 +00:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
assert(handle, "Error running dlv: " .. tostring(pid_or_err))
|
2021-08-30 06:37:36 +00:00
|
|
|
stdout:read_start(function(err, chunk)
|
2021-08-25 12:37:22 +00:00
|
|
|
assert(not err, err)
|
|
|
|
if chunk then
|
2021-08-30 06:37:36 +00:00
|
|
|
vim.schedule(function()
|
2021-08-25 12:37:22 +00:00
|
|
|
require("dap.repl").append(chunk)
|
|
|
|
end)
|
|
|
|
end
|
2021-07-10 11:06:10 +00:00
|
|
|
end)
|
2022-01-12 08:57:07 +00:00
|
|
|
-- Wait 500ms for delve to start
|
2021-07-10 11:06:10 +00:00
|
|
|
vim.defer_fn(function()
|
|
|
|
dap.repl.open()
|
2022-01-26 11:51:00 +00:00
|
|
|
callback({ type = "server", host = host, port = port })
|
2022-01-12 08:57:07 +00:00
|
|
|
end, 500)
|
2021-07-10 11:06:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local dap_cfg = {
|
|
|
|
type = "go",
|
|
|
|
name = "Debug",
|
|
|
|
request = "launch",
|
2021-10-24 23:05:02 +00:00
|
|
|
dlvToolPath = vim.fn.exepath("dlv"),
|
2021-12-16 22:02:29 +00:00
|
|
|
buildFlags = get_build_flags(),
|
2021-07-10 11:06:10 +00:00
|
|
|
}
|
2021-08-31 11:19:14 +00:00
|
|
|
|
|
|
|
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
|
|
|
|
row, col = row, col + 1
|
|
|
|
|
2021-12-29 14:33:24 +00:00
|
|
|
local empty = utils.empty
|
2021-08-31 11:19:14 +00:00
|
|
|
local ns = require("go.ts.go").get_func_method_node_at_pos(row, col)
|
2021-12-29 14:33:24 +00:00
|
|
|
if empty(ns) then
|
2021-12-16 22:02:29 +00:00
|
|
|
log("ts not not found, debug while file")
|
|
|
|
if mode == "nearest" then
|
|
|
|
mode = "test"
|
2021-08-31 11:19:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-15 09:59:03 +00:00
|
|
|
local launch = require("go.launch")
|
|
|
|
local cfg_exist, cfg_file = launch.vs_launch()
|
|
|
|
log(mode, cfg_exist, cfg_file)
|
2021-12-16 22:02:29 +00:00
|
|
|
if mode == "test" then
|
|
|
|
dap_cfg.name = dap_cfg.name .. " test"
|
2021-07-10 11:06:10 +00:00
|
|
|
dap_cfg.mode = "test"
|
2021-08-30 06:37:36 +00:00
|
|
|
-- dap_cfg.program = "${workspaceFolder}"
|
|
|
|
-- dap_cfg.program = "${file}"
|
2021-11-10 02:20:54 +00:00
|
|
|
dap_cfg.program = sep .. "${relativeFileDirname}"
|
2021-12-16 22:02:29 +00:00
|
|
|
dap.configurations.go = { dap_cfg }
|
2021-07-10 13:53:33 +00:00
|
|
|
dap.continue()
|
2021-12-16 22:02:29 +00:00
|
|
|
elseif mode == "nearest" then
|
|
|
|
dap_cfg.name = dap_cfg.name .. " test_nearest"
|
2021-08-31 11:19:14 +00:00
|
|
|
dap_cfg.mode = "test"
|
2021-11-10 02:20:54 +00:00
|
|
|
dap_cfg.program = sep .. "${relativeFileDirname}"
|
2021-12-16 22:02:29 +00:00
|
|
|
dap_cfg.args = { "-test.run", "^" .. ns.name }
|
2021-08-31 11:19:14 +00:00
|
|
|
log(dap_cfg)
|
2021-12-16 22:02:29 +00:00
|
|
|
dap.configurations.go = { dap_cfg }
|
2021-08-31 11:19:14 +00:00
|
|
|
dap.continue()
|
2022-01-15 09:59:03 +00:00
|
|
|
elseif cfg_exist then
|
|
|
|
log("using cfg")
|
|
|
|
launch.load()
|
2022-01-15 11:14:56 +00:00
|
|
|
for _, cfg in ipairs(dap.configurations.go) do
|
|
|
|
cfg.dlvToolPath = vim.fn.exepath("dlv")
|
|
|
|
end
|
2022-01-15 09:59:03 +00:00
|
|
|
dap.continue()
|
2021-07-10 11:06:10 +00:00
|
|
|
else
|
2021-11-22 02:00:04 +00:00
|
|
|
dap_cfg.program = sep .. "${relativeFileDirname}"
|
2021-07-10 11:06:10 +00:00
|
|
|
dap_cfg.args = args
|
2021-12-16 22:02:29 +00:00
|
|
|
dap.configurations.go = { dap_cfg }
|
2021-07-10 13:53:33 +00:00
|
|
|
dap.continue()
|
2021-07-10 11:06:10 +00:00
|
|
|
end
|
2021-08-30 06:37:36 +00:00
|
|
|
log(args)
|
2022-01-16 23:31:24 +00:00
|
|
|
|
|
|
|
vim.ui.select = original_select
|
2021-07-10 11:06:10 +00:00
|
|
|
end
|
|
|
|
|
2021-11-29 20:19:45 +00:00
|
|
|
local unmap = function()
|
2021-12-16 22:02:29 +00:00
|
|
|
if not _GO_NVIM_CFG.dap_debug_keymap then
|
|
|
|
return
|
|
|
|
end
|
2021-11-23 23:13:40 +00:00
|
|
|
local keys = {
|
2021-12-16 22:02:29 +00:00
|
|
|
"r",
|
|
|
|
"c",
|
|
|
|
"n",
|
|
|
|
"s",
|
|
|
|
"o",
|
|
|
|
"S",
|
|
|
|
"u",
|
|
|
|
"D",
|
|
|
|
"C",
|
|
|
|
"b",
|
|
|
|
"P",
|
|
|
|
"p",
|
|
|
|
"K",
|
|
|
|
"B",
|
|
|
|
"R",
|
|
|
|
"O",
|
|
|
|
"a",
|
|
|
|
"w",
|
2021-11-23 23:13:40 +00:00
|
|
|
}
|
2021-07-10 11:06:10 +00:00
|
|
|
for _, value in pairs(keys) do
|
2021-08-11 23:52:22 +00:00
|
|
|
local cmd = "silent! unmap " .. value
|
2021-07-10 11:06:10 +00:00
|
|
|
vim.cmd(cmd)
|
|
|
|
end
|
|
|
|
|
2021-08-11 23:52:22 +00:00
|
|
|
vim.cmd([[silent! vunmap p]])
|
2021-11-29 20:19:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
M.stop = function(unm)
|
|
|
|
if unm then
|
|
|
|
unmap()
|
|
|
|
end
|
|
|
|
|
2022-01-16 23:31:24 +00:00
|
|
|
local has_dap, dap = pcall(require, "dap")
|
|
|
|
if has_dap then
|
|
|
|
require("dap").disconnect()
|
|
|
|
require("dap").close()
|
|
|
|
require("dap").repl.close()
|
|
|
|
end
|
2021-11-29 20:19:45 +00:00
|
|
|
local has_dapui, dapui = pcall(require, "dapui")
|
|
|
|
if has_dapui then
|
|
|
|
dapui.close()
|
|
|
|
end
|
2021-07-10 11:06:10 +00:00
|
|
|
end
|
|
|
|
|
2021-07-10 13:53:33 +00:00
|
|
|
function M.ultest_post()
|
|
|
|
vim.g.ultest_use_pty = 1
|
|
|
|
local builders = {
|
|
|
|
["go#richgo"] = function(cmd)
|
|
|
|
local args = {}
|
|
|
|
for i = 3, #cmd, 1 do
|
|
|
|
local arg = cmd[i]
|
|
|
|
if vim.startswith(arg, "-") then
|
|
|
|
arg = "-test." .. string.sub(arg, 2)
|
|
|
|
end
|
|
|
|
args[#args + 1] = arg
|
|
|
|
end
|
2021-10-24 23:05:02 +00:00
|
|
|
|
2021-07-10 13:53:33 +00:00
|
|
|
return {
|
|
|
|
dap = {
|
|
|
|
type = "go",
|
|
|
|
request = "launch",
|
|
|
|
mode = "test",
|
2021-11-10 02:20:54 +00:00
|
|
|
program = sep .. "${relativeFileDirname}",
|
2021-07-10 13:53:33 +00:00
|
|
|
dlvToolPath = vim.fn.exepath("dlv"),
|
2021-10-24 23:05:02 +00:00
|
|
|
args = args,
|
2021-12-16 22:02:29 +00:00
|
|
|
buildFlags = get_build_flags(),
|
2021-07-10 13:53:33 +00:00
|
|
|
},
|
|
|
|
parse_result = function(lines)
|
|
|
|
return lines[#lines] == "FAIL" and 1 or 0
|
2021-12-16 22:02:29 +00:00
|
|
|
end,
|
2021-07-10 13:53:33 +00:00
|
|
|
}
|
2021-12-16 22:02:29 +00:00
|
|
|
end,
|
2021-07-10 13:53:33 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 22:59:47 +00:00
|
|
|
ul = utils.load_plugin("vim-ultest", "ultest")
|
|
|
|
if ul then
|
|
|
|
ul.setup({ builders = builders })
|
|
|
|
end
|
2021-07-10 13:53:33 +00:00
|
|
|
end
|
|
|
|
|
2021-07-10 11:06:10 +00:00
|
|
|
return M
|