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
|
|
|
|
local utils = require('go.utils')
|
2021-08-30 06:37:36 +00:00
|
|
|
local log = utils.log
|
2021-07-10 11:06:10 +00:00
|
|
|
local function setup_telescope()
|
|
|
|
require('telescope').setup()
|
|
|
|
require('telescope').load_extension('dap')
|
|
|
|
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(),
|
|
|
|
["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()
|
|
|
|
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(),
|
|
|
|
["n|P"] = map_cr('<cmd>lua require"dap".pause()<CR>'):with_noremap():with_silent(),
|
|
|
|
["n|p"] = map_cr('<cmd>lua require"dap.ui.variables".hover()<CR>'):with_noremap():with_silent(),
|
2021-10-26 00:15:52 +00:00
|
|
|
["v|p"] = map_cr('<cmd>lua require"dap.ui.variables".visual_hover()<CR>'):with_noremap():with_silent()
|
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()
|
|
|
|
vim.g.dap_virtual_text = 'all frames'
|
|
|
|
utils.load_plugin('nvim-dap', "dap")
|
|
|
|
if _GO_NVIM_CFG.dap_debug_gui then
|
|
|
|
utils.load_plugin('nvim-dap-ui', "dapui")
|
|
|
|
utils.load_plugin('nvim-dap-virtual-text')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
M.breakpt = function()
|
|
|
|
M.prepare()
|
|
|
|
require"dap".toggle_breakpoint()
|
|
|
|
end
|
|
|
|
|
|
|
|
M.run = function(...)
|
|
|
|
keybind()
|
|
|
|
M.prepare()
|
|
|
|
local args = {...}
|
|
|
|
|
|
|
|
local mode = select(1, ...)
|
|
|
|
|
2021-08-30 06:37:36 +00:00
|
|
|
log("plugin loaded", mode)
|
2021-07-10 11:06:10 +00:00
|
|
|
if _GO_NVIM_CFG.dap_debug_gui then
|
|
|
|
require("dapui").setup()
|
|
|
|
require("dapui").open()
|
|
|
|
end
|
|
|
|
local dap = require "dap"
|
|
|
|
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
|
|
|
|
local port = 38697
|
|
|
|
handle, pid_or_err = vim.loop.spawn("dlv", {
|
2021-08-25 12:37:22 +00:00
|
|
|
stdio = {nil, stdout},
|
2021-07-10 11:06:10 +00:00
|
|
|
args = {"dap", "-l", "127.0.0.1:" .. port},
|
|
|
|
detached = true
|
|
|
|
}, 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
|
|
|
|
print("Delve exited with exit code: ", code)
|
|
|
|
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)
|
|
|
|
-- Wait 100ms for delve to start
|
|
|
|
vim.defer_fn(function()
|
|
|
|
dap.repl.open()
|
|
|
|
callback({type = "server", host = "127.0.0.1", port = port})
|
|
|
|
end, 100)
|
|
|
|
end
|
|
|
|
|
|
|
|
local dap_cfg = {
|
|
|
|
type = "go",
|
|
|
|
name = "Debug",
|
|
|
|
request = "launch",
|
2021-10-24 23:05:02 +00:00
|
|
|
dlvToolPath = vim.fn.exepath("dlv"),
|
|
|
|
buildFlags = get_build_flags()
|
|
|
|
|
2021-07-10 11:06:10 +00:00
|
|
|
}
|
2021-08-31 11:19:14 +00:00
|
|
|
|
2021-10-24 23:05:02 +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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-07-10 11:06:10 +00:00
|
|
|
if mode == 'test' then
|
|
|
|
dap_cfg.name = dap_cfg.name .. ' test'
|
|
|
|
dap_cfg.mode = "test"
|
2021-08-30 06:37:36 +00:00
|
|
|
-- dap_cfg.program = "${workspaceFolder}"
|
|
|
|
-- dap_cfg.program = "${file}"
|
|
|
|
dap_cfg.program = "./${relativeFileDirname}"
|
2021-07-10 13:53:33 +00:00
|
|
|
dap.configurations.go = {dap_cfg}
|
|
|
|
dap.continue()
|
2021-08-31 11:19:14 +00:00
|
|
|
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()
|
2021-07-10 11:06:10 +00:00
|
|
|
else
|
|
|
|
dap_cfg.program = "${file}"
|
|
|
|
dap_cfg.args = args
|
2021-07-10 13:53:33 +00:00
|
|
|
dap.configurations.go = {dap_cfg}
|
|
|
|
dap.continue()
|
2021-07-10 11:06:10 +00:00
|
|
|
end
|
2021-08-30 06:37:36 +00:00
|
|
|
log(args)
|
2021-07-10 11:06:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
M.stop = function()
|
2021-08-11 23:52:22 +00:00
|
|
|
local keys = {"r", "c", "n", "s", "o", "S", "u", "D", "C", "b", "P", "p"}
|
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-07-10 11:06:10 +00:00
|
|
|
require'dap'.disconnect()
|
2021-08-14 09:18:08 +00:00
|
|
|
require'dap'.close();
|
2021-07-10 11:06:10 +00:00
|
|
|
require"dap".repl.close()
|
|
|
|
require("dapui").close()
|
|
|
|
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-08-30 06:37:36 +00:00
|
|
|
program = "./${relativeFileDirname}",
|
2021-07-10 13:53:33 +00:00
|
|
|
dlvToolPath = vim.fn.exepath("dlv"),
|
2021-10-24 23:05:02 +00:00
|
|
|
args = args,
|
|
|
|
buildFlags = get_build_flags()
|
2021-07-10 13:53:33 +00:00
|
|
|
},
|
|
|
|
parse_result = function(lines)
|
|
|
|
return lines[#lines] == "FAIL" and 1 or 0
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
ok, ul = utils.load_plugin('vim-ultest', "ultest")
|
|
|
|
|
|
|
|
ul.setup({builders = builders})
|
|
|
|
end
|
|
|
|
|
2021-07-10 11:06:10 +00:00
|
|
|
return M
|