2022-09-05 05:47:08 +00:00
|
|
|
local bind = require('go.keybind')
|
|
|
|
local utils = require('go.utils')
|
2021-08-30 06:37:36 +00:00
|
|
|
local log = utils.log
|
2022-09-05 05:47:08 +00:00
|
|
|
local sep = '.' .. utils.sep()
|
|
|
|
local getopt = require('go.alt_getopt')
|
2022-06-30 05:34:48 +00:00
|
|
|
local dapui_setuped
|
2022-07-01 10:30:43 +00:00
|
|
|
local keys = {}
|
2022-03-07 06:09:42 +00:00
|
|
|
local long_opts = {
|
2022-09-05 05:47:08 +00:00
|
|
|
compile = 'c',
|
|
|
|
run = 'r',
|
|
|
|
attach = 'a',
|
|
|
|
test = 't',
|
|
|
|
restart = 'R',
|
|
|
|
stop = 's',
|
|
|
|
help = 'h',
|
|
|
|
nearest = 'n',
|
|
|
|
package = 'p',
|
|
|
|
file = 'f',
|
|
|
|
breakpoint = 'b',
|
|
|
|
tag = 'T',
|
2022-03-07 06:09:42 +00:00
|
|
|
}
|
2022-09-05 05:47:08 +00:00
|
|
|
local opts = 'tcraRsnpfsbhT:'
|
2022-03-07 06:09:42 +00:00
|
|
|
local function help()
|
2022-09-05 05:47:08 +00:00
|
|
|
return 'Usage: GoDebug [OPTION]\n'
|
|
|
|
.. 'Options:\n'
|
|
|
|
.. ' -c, --compile compile\n'
|
|
|
|
.. ' -r, --run run\n'
|
|
|
|
.. ' -t, --test run tests\n'
|
|
|
|
.. ' -R, --restart restart\n'
|
|
|
|
.. ' -s, --stop stop\n'
|
|
|
|
.. ' -h, --help display this help and exit\n'
|
|
|
|
.. ' -n, --nearest debug nearest file\n'
|
|
|
|
.. ' -p, --package debug package\n'
|
|
|
|
.. ' -f, --file display file\n'
|
|
|
|
.. ' -b, --breakpoint set breakpoint'
|
2022-03-07 06:09:42 +00:00
|
|
|
end
|
2021-11-10 02:20:54 +00:00
|
|
|
|
2022-06-30 02:41:20 +00:00
|
|
|
-- not sure if anyone still use telescope for debug
|
2021-07-10 11:06:10 +00:00
|
|
|
local function setup_telescope()
|
2022-09-05 05:47:08 +00:00
|
|
|
require('telescope').setup()
|
|
|
|
require('telescope').load_extension('dap')
|
2021-07-10 11:06:10 +00:00
|
|
|
local ts_keys = {
|
2022-09-05 05:47:08 +00:00
|
|
|
['n|lb'] = '<cmd>lua require"telescope".extensions.dap.list_breakpoints{}',
|
|
|
|
['n|tv'] = '<cmd>lua require"telescope".extensions.dap.variables{}',
|
|
|
|
['n|bt'] = '<cmd>lua require"telescope".extensions.dap.frames{}',
|
2021-07-10 11:06:10 +00:00
|
|
|
}
|
|
|
|
bind.nvim_load_mapping(ts_keys)
|
|
|
|
end
|
|
|
|
|
2022-07-05 22:28:29 +00:00
|
|
|
local keymaps_backup
|
2021-07-10 11:06:10 +00:00
|
|
|
local function keybind()
|
2021-12-16 22:02:29 +00:00
|
|
|
if not _GO_NVIM_CFG.dap_debug_keymap then
|
|
|
|
return
|
|
|
|
end
|
2022-07-05 22:28:29 +00:00
|
|
|
-- TODO: put keymaps back
|
2022-09-05 05:47:08 +00:00
|
|
|
keymaps_backup = vim.api.nvim_get_keymap('n')
|
2022-07-01 10:30:43 +00:00
|
|
|
keys = {
|
2021-07-10 11:06:10 +00:00
|
|
|
-- DAP --
|
|
|
|
-- run
|
2022-09-05 05:47:08 +00:00
|
|
|
['r'] = { f = require('go.dap').run, desc = 'run' },
|
|
|
|
['c'] = { f = require('dap').continue, desc = 'continue' },
|
|
|
|
['n'] = { f = require('dap').step_over, desc = 'step_over' },
|
|
|
|
['s'] = { f = require('dap').step_into, desc = 'step_into' },
|
|
|
|
['o'] = { f = require('dap').step_out, desc = 'step_out' },
|
2023-01-31 04:39:45 +00:00
|
|
|
['S'] = {
|
|
|
|
f = function()
|
|
|
|
require('go.dap').stop(true)
|
|
|
|
end,
|
|
|
|
desc = 'stop debug session',
|
|
|
|
},
|
2022-09-05 05:47:08 +00:00
|
|
|
['u'] = { f = require('dap').up, desc = 'up' },
|
|
|
|
['D'] = { f = require('dap').down, desc = 'down' },
|
|
|
|
['C'] = { f = require('dap').run_to_cursor, desc = 'run_to_cursor' },
|
|
|
|
['b'] = { f = require('dap').toggle_breakpoint, desc = 'toggle_breakpoint' },
|
|
|
|
['P'] = { f = require('dap').pause, desc = 'pause' },
|
2021-07-10 11:06:10 +00:00
|
|
|
--
|
|
|
|
}
|
2021-11-23 23:13:40 +00:00
|
|
|
if _GO_NVIM_CFG.dap_debug_gui then
|
2022-09-05 05:47:08 +00:00
|
|
|
keys['p'] = { f = require('dapui').eval, m = { 'n', 'v' }, desc = 'eval' }
|
|
|
|
keys['K'] = { f = require('dapui').float_element, desc = 'float_element' }
|
|
|
|
keys['B'] = {
|
2022-07-04 02:13:46 +00:00
|
|
|
f = function()
|
2022-09-05 05:47:08 +00:00
|
|
|
require('dapui').float_element('breakpoints')
|
2022-07-04 02:13:46 +00:00
|
|
|
end,
|
2022-07-09 09:26:31 +00:00
|
|
|
desc = "float_element('breakpoints')",
|
2022-07-04 02:13:46 +00:00
|
|
|
}
|
2022-09-05 05:47:08 +00:00
|
|
|
keys['R'] = {
|
2022-07-04 02:13:46 +00:00
|
|
|
f = function()
|
2022-09-05 05:47:08 +00:00
|
|
|
require('dapui').float_element('repl')
|
2022-07-04 02:13:46 +00:00
|
|
|
end,
|
2022-07-09 09:26:31 +00:00
|
|
|
desc = "float_element('repl')",
|
2022-07-04 02:13:46 +00:00
|
|
|
}
|
2022-09-05 05:47:08 +00:00
|
|
|
keys['O'] = {
|
2022-07-04 02:13:46 +00:00
|
|
|
f = function()
|
2022-09-05 05:47:08 +00:00
|
|
|
require('dapui').float_element('scopes')
|
2022-07-04 02:13:46 +00:00
|
|
|
end,
|
2022-07-09 09:26:31 +00:00
|
|
|
desc = "float_element('scopes')",
|
2022-07-04 02:13:46 +00:00
|
|
|
}
|
2022-09-05 05:47:08 +00:00
|
|
|
keys['a'] = {
|
2022-07-04 02:13:46 +00:00
|
|
|
f = function()
|
2022-09-05 05:47:08 +00:00
|
|
|
require('dapui').float_element('stacks')
|
2022-07-04 02:13:46 +00:00
|
|
|
end,
|
2022-07-09 09:26:31 +00:00
|
|
|
desc = "float_element('stacks')",
|
2022-07-04 02:13:46 +00:00
|
|
|
}
|
2022-09-05 05:47:08 +00:00
|
|
|
keys['w'] = {
|
2022-07-04 02:13:46 +00:00
|
|
|
f = function()
|
2022-09-05 05:47:08 +00:00
|
|
|
require('dapui').float_element('watches')
|
2022-07-04 02:13:46 +00:00
|
|
|
end,
|
2022-07-09 09:26:31 +00:00
|
|
|
desc = "float_element('watches')",
|
2022-07-04 02:13:46 +00:00
|
|
|
}
|
2021-11-23 23:13:40 +00:00
|
|
|
else
|
2022-09-05 05:47:08 +00:00
|
|
|
keys['p'] = { f = require('dap.ui.widgets').hover, m = { 'n', 'v' }, desc = 'hover' }
|
2021-11-23 23:13:40 +00:00
|
|
|
end
|
2021-07-10 11:06:10 +00:00
|
|
|
bind.nvim_load_mapping(keys)
|
|
|
|
end
|
|
|
|
|
2022-08-06 03:45:01 +00:00
|
|
|
local function get_test_build_tags()
|
2022-09-05 05:47:08 +00:00
|
|
|
local get_build_tags = require('go.gotest').get_build_tags
|
2022-06-15 04:11:10 +00:00
|
|
|
local tags = get_build_tags({})
|
|
|
|
if tags then
|
|
|
|
return tags
|
2021-10-24 23:05:02 +00:00
|
|
|
else
|
2022-09-05 05:47:08 +00:00
|
|
|
return ''
|
2021-10-24 23:05:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-10 11:06:10 +00:00
|
|
|
local M = {}
|
|
|
|
|
2022-07-01 10:30:43 +00:00
|
|
|
function M.debug_keys()
|
|
|
|
local keymap_help = {}
|
|
|
|
for key, val in pairs(keys) do
|
2022-07-04 02:13:46 +00:00
|
|
|
-- local m = vim.fn.matchlist(val, [[\v(\p+)\.(\p+\(\p*\))]]) -- match last function e.g.float_element("repl")
|
2022-07-01 10:30:43 +00:00
|
|
|
|
2022-09-05 05:47:08 +00:00
|
|
|
table.insert(keymap_help, key .. ' -> ' .. val.desc)
|
2022-07-01 10:30:43 +00:00
|
|
|
end
|
|
|
|
|
2022-09-05 05:47:08 +00:00
|
|
|
local guihua = utils.load_plugin('guihua.lua', 'guihua.listview')
|
2022-07-01 10:30:43 +00:00
|
|
|
|
|
|
|
if guihua then
|
2022-09-05 05:47:08 +00:00
|
|
|
local ListView = require('guihua.listview')
|
2022-07-01 10:30:43 +00:00
|
|
|
return ListView:new({
|
2022-09-05 05:47:08 +00:00
|
|
|
loc = 'top_center',
|
2023-03-09 23:38:06 +00:00
|
|
|
border = 'rounded',
|
2022-07-01 10:30:43 +00:00
|
|
|
prompt = true,
|
|
|
|
enter = true,
|
|
|
|
rect = { height = 20, width = 50 },
|
|
|
|
data = keymap_help,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2022-09-05 05:47:08 +00:00
|
|
|
local close_events = { 'CursorMoved', 'CursorMovedI', 'BufHidden', 'InsertCharPre' }
|
|
|
|
local config = { close_events = close_events, focusable = true, border = 'single' }
|
|
|
|
vim.lsp.util.open_floating_preview(keymap_help, 'lua', config)
|
2022-07-01 10:30:43 +00:00
|
|
|
end
|
|
|
|
|
2021-07-10 11:06:10 +00:00
|
|
|
M.prepare = function()
|
2022-09-05 05:47:08 +00:00
|
|
|
utils.load_plugin('nvim-dap', 'dap')
|
2022-04-16 12:09:15 +00:00
|
|
|
if _GO_NVIM_CFG.icons ~= false then
|
2022-09-05 05:47:08 +00:00
|
|
|
vim.fn.sign_define('DapBreakpoint', {
|
2022-04-16 12:09:15 +00:00
|
|
|
text = _GO_NVIM_CFG.icons.breakpoint,
|
2022-09-05 05:47:08 +00:00
|
|
|
texthl = '',
|
|
|
|
linehl = '',
|
|
|
|
numhl = '',
|
2022-04-16 12:09:15 +00:00
|
|
|
})
|
2022-09-05 05:47:08 +00:00
|
|
|
vim.fn.sign_define('DapStopped', {
|
2022-04-16 12:09:15 +00:00
|
|
|
text = _GO_NVIM_CFG.icons.currentpos,
|
2022-09-05 05:47:08 +00:00
|
|
|
texthl = '',
|
|
|
|
linehl = '',
|
|
|
|
numhl = '',
|
2022-04-16 12:09:15 +00:00
|
|
|
})
|
|
|
|
end
|
2023-01-12 11:48:51 +00:00
|
|
|
local dapui_cfg = _GO_NVIM_CFG.dap_debug_gui
|
|
|
|
if dapui_cfg then
|
2022-09-05 05:47:08 +00:00
|
|
|
utils.load_plugin('nvim-dap-ui', 'dapui')
|
2022-06-30 05:34:48 +00:00
|
|
|
if dapui_setuped ~= true then
|
2023-01-12 11:48:51 +00:00
|
|
|
if dapui_cfg == true then
|
|
|
|
dapui_cfg = {}
|
|
|
|
end
|
|
|
|
require('dapui').setup(dapui_cfg)
|
2022-06-30 05:34:48 +00:00
|
|
|
dapui_setuped = true
|
|
|
|
end
|
2022-03-07 06:09:42 +00:00
|
|
|
end
|
|
|
|
if _GO_NVIM_CFG.dap_debug_vt then
|
2023-01-12 11:48:51 +00:00
|
|
|
if _GO_NVIM_CFG.dap_debug_vt == true then
|
|
|
|
_GO_NVIM_CFG.dap_debug_vt = { enabled_commands = true, all_frames = true }
|
|
|
|
end
|
2022-09-05 05:47:08 +00:00
|
|
|
local vt = utils.load_plugin('nvim-dap-virtual-text')
|
2023-01-12 11:48:51 +00:00
|
|
|
vt.setup(_GO_NVIM_CFG.dap_debug_vt)
|
2021-07-10 11:06:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
M.breakpt = function()
|
|
|
|
M.prepare()
|
2022-09-05 05:47:08 +00:00
|
|
|
require('dap').toggle_breakpoint()
|
2021-07-10 11:06:10 +00:00
|
|
|
end
|
|
|
|
|
2022-05-15 11:43:59 +00:00
|
|
|
M.save_brks = function()
|
|
|
|
M.prepare()
|
2022-09-05 05:47:08 +00:00
|
|
|
local bks = require('dap.breakpoints').get()
|
2022-05-13 15:07:44 +00:00
|
|
|
local all_bks = {}
|
|
|
|
if bks and next(bks) then
|
2022-09-05 05:47:08 +00:00
|
|
|
local _, fld = require('go.project').setup()
|
2022-05-13 15:07:44 +00:00
|
|
|
for bufnr, bk in pairs(bks) do
|
|
|
|
local uri = vim.uri_from_bufnr(bufnr)
|
2022-05-15 12:05:12 +00:00
|
|
|
local _bk = {}
|
2022-05-15 11:43:59 +00:00
|
|
|
for _, value in pairs(bk) do
|
2022-05-15 12:05:12 +00:00
|
|
|
table.insert(_bk, { line = value.line })
|
2022-05-15 11:43:59 +00:00
|
|
|
end
|
2022-05-15 12:05:12 +00:00
|
|
|
all_bks[uri] = _bk
|
2022-05-13 15:07:44 +00:00
|
|
|
end
|
2022-09-05 05:47:08 +00:00
|
|
|
local bkfile = fld .. utils.sep() .. 'breakpoints.lua'
|
|
|
|
local writeStr = 'return ' .. vim.inspect(all_bks)
|
2022-05-13 15:07:44 +00:00
|
|
|
|
2022-09-05 05:47:08 +00:00
|
|
|
local writeLst = vim.split(writeStr, '\n')
|
2022-05-13 15:07:44 +00:00
|
|
|
|
2022-09-05 05:47:08 +00:00
|
|
|
vim.fn.writefile(writeLst, bkfile, 'b')
|
2022-05-13 15:07:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-15 11:43:59 +00:00
|
|
|
M.load_brks = function()
|
2022-05-14 11:24:19 +00:00
|
|
|
M.prepare()
|
2022-09-05 05:47:08 +00:00
|
|
|
local _, brkfile = require('go.project').project_existed()
|
2022-05-14 11:24:19 +00:00
|
|
|
if vim.fn.filereadable(brkfile) == 0 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local f = assert(loadfile(brkfile))
|
|
|
|
local brks = f()
|
|
|
|
for uri, brk in pairs(brks) do
|
|
|
|
local bufnr = vim.uri_to_bufnr(uri)
|
|
|
|
if not vim.api.nvim_buf_is_loaded(bufnr) then
|
|
|
|
vim.fn.bufload(bufnr)
|
|
|
|
end
|
|
|
|
for index, lnum in ipairs(brk) do
|
2022-09-05 05:47:08 +00:00
|
|
|
require('dap.breakpoints').set({}, bufnr, lnum.line)
|
2022-05-14 11:24:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
M.clear_bks = function()
|
2022-09-05 05:47:08 +00:00
|
|
|
utils.load_plugin('nvim-dap', 'dap')
|
2022-05-14 11:24:19 +00:00
|
|
|
|
2022-09-05 05:47:08 +00:00
|
|
|
require('dap.breakpoints').clear()
|
2022-05-14 11:24:19 +00:00
|
|
|
M.save_bks()
|
2022-09-05 05:47:08 +00:00
|
|
|
local _, brkfile = require('go.project').project_existed()
|
2022-05-13 15:07:44 +00:00
|
|
|
if vim.fn.filereadable(brkfile) == 0 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local f = assert(loadfile(brkfile))
|
|
|
|
local brks = f()
|
|
|
|
for uri, brk in pairs(brks) do
|
|
|
|
local bufnr = vim.uri_to_bufnr(uri)
|
|
|
|
if not vim.api.nvim_buf_is_loaded(bufnr) then
|
|
|
|
vim.fn.bufload(bufnr)
|
|
|
|
end
|
2022-06-01 11:29:13 +00:00
|
|
|
for _, lnum in ipairs(brk) do
|
2022-09-05 05:47:08 +00:00
|
|
|
require('dap.breakpoints').set({}, bufnr, lnum.line)
|
2022-05-13 15:07:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-30 05:34:48 +00:00
|
|
|
local function dapui_opened()
|
2022-09-05 05:47:08 +00:00
|
|
|
local lys = require('dapui.windows').layouts or {}
|
2022-06-30 05:34:48 +00:00
|
|
|
local opened = false
|
|
|
|
for _, ly in ipairs(lys) do
|
|
|
|
if ly:is_open() == true then
|
|
|
|
opened = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return opened
|
|
|
|
end
|
|
|
|
|
2022-04-20 02:00:49 +00:00
|
|
|
local stdout, stderr, handle
|
2021-07-10 11:06:10 +00:00
|
|
|
M.run = function(...)
|
2021-12-16 22:02:29 +00:00
|
|
|
local args = { ... }
|
2022-09-05 05:47:08 +00:00
|
|
|
local mode = 'test'
|
2022-03-07 06:09:42 +00:00
|
|
|
|
|
|
|
local optarg, optind = getopt.get_opts(args, opts, long_opts)
|
|
|
|
log(optarg, optind)
|
|
|
|
|
2022-09-05 05:47:08 +00:00
|
|
|
if optarg['h'] then
|
2022-03-07 06:09:42 +00:00
|
|
|
return utils.info(help())
|
|
|
|
end
|
|
|
|
|
2022-09-05 05:47:08 +00:00
|
|
|
if optarg['c'] then
|
|
|
|
local fpath = vim.fn.expand('%:p:h')
|
2022-03-07 06:09:42 +00:00
|
|
|
local out = vim.fn.systemlist(table.concat({
|
2022-09-05 05:47:08 +00:00
|
|
|
'go',
|
|
|
|
'test',
|
|
|
|
'-v',
|
|
|
|
'-cover',
|
|
|
|
'-covermode=atomic',
|
|
|
|
'-coverprofile=cover.out',
|
|
|
|
'-tags ' .. _GO_NVIM_CFG.build_tags,
|
|
|
|
'-c',
|
2022-03-07 06:09:42 +00:00
|
|
|
fpath,
|
2022-09-05 05:47:08 +00:00
|
|
|
}, ' '))
|
2022-03-07 06:09:42 +00:00
|
|
|
if #out ~= 0 then
|
2022-09-05 05:47:08 +00:00
|
|
|
utils.info('building ' .. vim.inspect(out))
|
2022-03-07 06:09:42 +00:00
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
2021-07-10 11:06:10 +00:00
|
|
|
|
2022-09-05 05:47:08 +00:00
|
|
|
if optarg['b'] then
|
|
|
|
return require('dap').toggle_breakpoint()
|
2022-06-30 02:41:20 +00:00
|
|
|
end
|
|
|
|
|
2022-09-05 05:47:08 +00:00
|
|
|
local guihua = utils.load_plugin('guihua.lua', 'guihua.gui')
|
2022-01-16 23:31:24 +00:00
|
|
|
|
|
|
|
local original_select = vim.ui.select
|
|
|
|
if guihua then
|
2022-09-05 05:47:08 +00:00
|
|
|
vim.ui.select = require('guihua.gui').select
|
2022-01-16 23:31:24 +00:00
|
|
|
end
|
|
|
|
|
2022-01-12 22:07:14 +00:00
|
|
|
-- testopts = {"test", "nearest", "file", "stop", "restart"}
|
2022-09-05 05:47:08 +00:00
|
|
|
log('plugin loaded', mode, optarg)
|
2022-04-16 12:09:15 +00:00
|
|
|
|
2022-09-05 05:47:08 +00:00
|
|
|
if optarg['s'] and (optarg['t'] or optarg['r']) then
|
2022-04-16 12:09:15 +00:00
|
|
|
M.stop(false)
|
2022-09-05 05:47:08 +00:00
|
|
|
elseif optarg['s'] then
|
2022-04-16 12:09:15 +00:00
|
|
|
return M.stop(true)
|
2021-11-24 09:05:02 +00:00
|
|
|
end
|
2021-11-29 20:19:45 +00:00
|
|
|
|
2022-03-07 06:09:42 +00:00
|
|
|
-- restart
|
2022-09-05 05:47:08 +00:00
|
|
|
if optarg['R'] then
|
2022-06-16 01:38:37 +00:00
|
|
|
M.stop(false)
|
2022-09-05 05:47:08 +00:00
|
|
|
if optarg['t'] then
|
|
|
|
mode = 'test'
|
2022-01-12 22:07:14 +00:00
|
|
|
else
|
2022-09-05 05:47:08 +00:00
|
|
|
mode = M.pre_mode or 'file'
|
2022-01-12 22:07:14 +00:00
|
|
|
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-09-05 05:47:08 +00:00
|
|
|
local session = require('dap').session()
|
2022-01-12 22:07:14 +00:00
|
|
|
if session ~= nil and session.initialized == true then
|
2022-09-05 05:47:08 +00:00
|
|
|
if not optarg['R'] then
|
|
|
|
utils.info('debug session already started, press c to continue')
|
2022-06-30 05:34:48 +00:00
|
|
|
return
|
|
|
|
else
|
2022-09-05 05:47:08 +00:00
|
|
|
utils.info('debug session already started, press c to restart and stop the session')
|
2022-06-30 05:34:48 +00:00
|
|
|
end
|
2022-01-12 22:07:14 +00:00
|
|
|
end
|
2022-06-30 05:34:48 +00:00
|
|
|
|
2022-09-05 05:47:08 +00:00
|
|
|
local run_cur = optarg['r'] -- undocumented mode, smartrun current program in interactive mode
|
2022-06-30 02:41:20 +00:00
|
|
|
-- e.g. edit and run
|
2022-05-29 13:07:39 +00:00
|
|
|
local testfunc
|
2022-06-30 02:41:20 +00:00
|
|
|
|
2022-03-07 06:09:42 +00:00
|
|
|
if not run_cur then
|
|
|
|
keybind()
|
|
|
|
else
|
2022-06-30 02:41:20 +00:00
|
|
|
M.stop() -- rerun
|
2022-09-05 05:47:08 +00:00
|
|
|
testfunc = require('go.gotest').get_test_func_name()
|
|
|
|
if testfunc and not string.find(testfunc.name, '[T|t]est') then
|
|
|
|
log('no test func found', testfunc.name)
|
2022-06-30 02:41:20 +00:00
|
|
|
testfunc = nil -- no test func avalible, run main
|
2022-05-29 13:07:39 +00:00
|
|
|
end
|
2022-03-07 06:09:42 +00:00
|
|
|
end
|
2022-01-12 22:07:14 +00:00
|
|
|
|
2022-03-07 06:09:42 +00:00
|
|
|
if _GO_NVIM_CFG.dap_debug_gui and not run_cur then
|
2023-01-28 07:52:55 +00:00
|
|
|
if not dapui_opened() then
|
2022-09-05 05:47:08 +00:00
|
|
|
require('dapui').open()
|
2022-01-12 22:07:14 +00:00
|
|
|
end
|
2021-07-10 11:06:10 +00:00
|
|
|
end
|
2022-01-26 11:51:00 +00:00
|
|
|
|
2022-03-07 06:09:42 +00:00
|
|
|
local port = _GO_NVIM_CFG.dap_port
|
2022-12-28 06:14:21 +00:00
|
|
|
if _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
|
2022-09-05 05:47:08 +00:00
|
|
|
local dap = require('dap')
|
2022-12-28 06:14:21 +00:00
|
|
|
|
|
|
|
local con_options = {
|
|
|
|
max_retries = _GO_NVIM_CFG.dap_retries,
|
|
|
|
initialize_timeout_sec = _GO_NVIM_CFG.dap_timeout,
|
|
|
|
}
|
2021-07-10 11:06:10 +00:00
|
|
|
dap.adapters.go = function(callback, config)
|
2022-04-20 02:00:49 +00:00
|
|
|
stdout = vim.loop.new_pipe(false)
|
|
|
|
stderr = vim.loop.new_pipe(false)
|
2021-07-10 11:06:10 +00:00
|
|
|
local pid_or_err
|
2022-03-07 06:09:42 +00:00
|
|
|
port = config.port or port
|
2022-01-26 11:51:00 +00:00
|
|
|
|
2022-09-05 05:47:08 +00:00
|
|
|
local host = config.host or '127.0.0.1'
|
2022-01-26 11:51:00 +00:00
|
|
|
|
2022-09-05 05:47:08 +00:00
|
|
|
local addr = string.format('%s:%d', host, port)
|
2022-04-20 02:00:49 +00:00
|
|
|
local function onread(err, data)
|
|
|
|
if err then
|
2022-05-29 13:07:39 +00:00
|
|
|
log(err, data)
|
2022-04-20 02:00:49 +00:00
|
|
|
-- print('ERROR: ', err)
|
2023-02-12 20:55:00 +00:00
|
|
|
vim.notify('dlv exited with code ' + tostring(err), vim.log.levels.WARN)
|
2022-04-20 02:00:49 +00:00
|
|
|
end
|
2022-09-05 05:47:08 +00:00
|
|
|
if not data or data == '' then
|
2022-04-20 02:00:49 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
if data:find("couldn't start") then
|
2022-06-30 02:41:20 +00:00
|
|
|
vim.schedule(function()
|
|
|
|
utils.error(data)
|
|
|
|
end)
|
2022-04-20 02:00:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
vim.schedule(function()
|
2022-09-05 05:47:08 +00:00
|
|
|
require('dap.repl').append(data)
|
2022-04-20 02:00:49 +00:00
|
|
|
end)
|
|
|
|
end
|
2022-02-14 04:59:31 +00:00
|
|
|
|
2022-09-05 05:47:08 +00:00
|
|
|
handle, pid_or_err = vim.loop.spawn('dlv', {
|
2022-04-20 02:00:49 +00:00
|
|
|
stdio = { nil, stdout, stderr },
|
2022-09-05 05:47:08 +00:00
|
|
|
args = { 'dap', '-l', addr },
|
2022-12-28 06:14:21 +00:00
|
|
|
initialize_timeout_sec = con_options.initialize_timeout_sec,
|
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
|
|
|
if code ~= 0 then
|
2022-01-12 08:57:07 +00:00
|
|
|
vim.schedule(function()
|
2022-09-05 05:47:08 +00:00
|
|
|
log('Dlv exited', code)
|
2023-02-12 20:55:00 +00:00
|
|
|
vim.notify(string.format('Delve exited with exit code: %d', code), vim.log.levels.WARN)
|
2022-12-28 06:14:21 +00:00
|
|
|
_GO_NVIM_CFG.dap_port = _GO_NVIM_CFG.dap_port + 1
|
2022-01-12 08:57:07 +00:00
|
|
|
end)
|
2021-08-25 12:37:22 +00:00
|
|
|
end
|
2022-02-14 04:59:31 +00:00
|
|
|
|
2022-07-01 09:06:02 +00:00
|
|
|
_ = stdout and stdout:close()
|
|
|
|
_ = stderr and stderr:close()
|
|
|
|
_ = handle and handle:close()
|
2022-04-20 02:00:49 +00:00
|
|
|
stdout = nil
|
|
|
|
stderr = nil
|
|
|
|
handle = nil
|
2021-08-25 12:37:22 +00:00
|
|
|
end)
|
2022-09-05 05:47:08 +00:00
|
|
|
assert(handle, 'Error running dlv: ' .. tostring(pid_or_err))
|
2022-04-20 02:00:49 +00:00
|
|
|
stdout:read_start(onread)
|
|
|
|
stderr:read_start(onread)
|
2022-03-07 06:09:42 +00:00
|
|
|
|
|
|
|
vim.defer_fn(function()
|
2022-12-28 06:14:21 +00:00
|
|
|
callback({ type = 'server', host = host, port = port, options = con_options })
|
2022-05-29 13:07:39 +00:00
|
|
|
end, 1000)
|
2021-07-10 11:06:10 +00:00
|
|
|
end
|
|
|
|
|
2022-08-06 03:45:01 +00:00
|
|
|
log(get_test_build_tags())
|
2021-07-10 11:06:10 +00:00
|
|
|
local dap_cfg = {
|
2022-09-05 05:47:08 +00:00
|
|
|
type = 'go',
|
|
|
|
name = 'Debug',
|
|
|
|
request = 'launch',
|
|
|
|
dlvToolPath = vim.fn.exepath('dlv'),
|
2022-08-06 03:45:01 +00:00
|
|
|
buildFlags = get_test_build_tags(),
|
2022-12-28 06:14:21 +00:00
|
|
|
options = con_options,
|
2021-07-10 11:06:10 +00:00
|
|
|
}
|
2021-08-31 11:19:14 +00:00
|
|
|
|
2021-12-29 14:33:24 +00:00
|
|
|
local empty = utils.empty
|
2022-05-29 13:07:39 +00:00
|
|
|
|
2022-09-05 05:47:08 +00:00
|
|
|
local launch = require('go.launch')
|
2022-01-15 09:59:03 +00:00
|
|
|
local cfg_exist, cfg_file = launch.vs_launch()
|
|
|
|
log(mode, cfg_exist, cfg_file)
|
2022-03-07 06:09:42 +00:00
|
|
|
|
|
|
|
-- if breakpoint is not set add breakpoint at current pos
|
2022-09-05 05:47:08 +00:00
|
|
|
local pts = require('dap.breakpoints').get()
|
2022-06-15 04:11:10 +00:00
|
|
|
if utils.empty(pts) then
|
2023-02-12 20:55:00 +00:00
|
|
|
vim.notify('no breakpoint set, add breakpoint at current line', vim.log.levels.INFO)
|
2022-09-05 05:47:08 +00:00
|
|
|
require('dap').set_breakpoint()
|
2022-03-07 06:09:42 +00:00
|
|
|
end
|
|
|
|
|
2022-09-05 05:47:08 +00:00
|
|
|
testfunc = require('go.gotest').get_test_func_name()
|
2022-06-30 02:41:20 +00:00
|
|
|
log(testfunc)
|
2022-06-03 09:07:05 +00:00
|
|
|
|
|
|
|
if testfunc then
|
2022-09-05 05:47:08 +00:00
|
|
|
if testfunc.name ~= 'main' then
|
|
|
|
optarg['t'] = true
|
2022-06-30 02:41:20 +00:00
|
|
|
end
|
2022-06-03 09:07:05 +00:00
|
|
|
end
|
2022-09-05 05:47:08 +00:00
|
|
|
if optarg['t'] then
|
2023-02-21 11:11:28 +00:00
|
|
|
local tblcase_ns = require('go.gotest').get_testcase_name()
|
|
|
|
|
|
|
|
log(tblcase_ns)
|
|
|
|
if not tblcase_ns then
|
|
|
|
vim.notify('no test case found', vim.log.levels.DEBUG)
|
|
|
|
end
|
|
|
|
|
|
|
|
local tbl_name = ''
|
|
|
|
if tblcase_ns and tblcase_ns.name then
|
|
|
|
vim.notify('running test case: ' .. tblcase_ns.name)
|
|
|
|
tbl_name = tblcase_ns.name
|
|
|
|
tbl_name = tbl_name:gsub('"', '') -- remove "
|
|
|
|
tbl_name = tbl_name:gsub(' ', '_') -- remove space
|
|
|
|
tbl_name = tbl_name:gsub('/', '//')
|
|
|
|
tbl_name = tbl_name:gsub('%(', '\\(')
|
|
|
|
tbl_name = tbl_name:gsub('%)', '\\)')
|
|
|
|
tbl_name = '/' .. tbl_name
|
|
|
|
end
|
|
|
|
|
|
|
|
log(tblcase_ns)
|
|
|
|
|
2022-09-05 05:47:08 +00:00
|
|
|
dap_cfg.name = dap_cfg.name .. ' test'
|
|
|
|
dap_cfg.mode = 'test'
|
|
|
|
dap_cfg.request = 'launch'
|
|
|
|
dap_cfg.program = sep .. '${relativeFileDirname}'
|
2022-06-02 09:07:28 +00:00
|
|
|
|
2022-05-29 13:07:39 +00:00
|
|
|
if testfunc then
|
2022-09-05 05:47:08 +00:00
|
|
|
if testfunc.name:lower():find('bench') then
|
|
|
|
dap_cfg.args = { '-test.bench', '^' .. testfunc.name .. '$' }
|
|
|
|
else
|
2023-01-12 11:48:51 +00:00
|
|
|
dap_cfg.args = { '-test.run=^' .. testfunc.name .. '$' .. tbl_name }
|
2023-01-11 03:53:05 +00:00
|
|
|
-- dap_cfg.args = { [[-test.run=^TestTransactionCheckEngine_Check$/should_process]]}
|
2022-09-05 05:47:08 +00:00
|
|
|
end
|
2022-05-29 13:07:39 +00:00
|
|
|
end
|
2021-12-16 22:02:29 +00:00
|
|
|
dap.configurations.go = { dap_cfg }
|
2021-07-10 13:53:33 +00:00
|
|
|
dap.continue()
|
2022-09-05 05:47:08 +00:00
|
|
|
elseif optarg['n'] then
|
|
|
|
local ns = require('go.ts.go').get_func_method_node_at_pos()
|
2022-05-29 13:07:39 +00:00
|
|
|
if empty(ns) then
|
2022-09-05 05:47:08 +00:00
|
|
|
log('ts not not found, debug while file')
|
2022-05-29 13:07:39 +00:00
|
|
|
end
|
2022-09-05 05:47:08 +00:00
|
|
|
dap_cfg.name = dap_cfg.name .. ' test_nearest'
|
|
|
|
dap_cfg.mode = 'test'
|
|
|
|
dap_cfg.request = 'launch'
|
|
|
|
dap_cfg.program = sep .. '${relativeFileDirname}'
|
2022-06-15 04:11:10 +00:00
|
|
|
if not empty(ns) then
|
2022-09-05 05:47:08 +00:00
|
|
|
dap_cfg.args = { '-test.run', '^' .. ns.name }
|
2022-05-29 13:07:39 +00:00
|
|
|
end
|
2021-12-16 22:02:29 +00:00
|
|
|
dap.configurations.go = { dap_cfg }
|
2021-08-31 11:19:14 +00:00
|
|
|
dap.continue()
|
2022-09-05 05:47:08 +00:00
|
|
|
elseif optarg['a'] then
|
|
|
|
dap_cfg.name = dap_cfg.name .. ' attach'
|
|
|
|
dap_cfg.mode = 'local'
|
|
|
|
dap_cfg.request = 'attach'
|
|
|
|
dap_cfg.processId = require('dap.utils').pick_process
|
2022-02-14 04:59:31 +00:00
|
|
|
dap.configurations.go = { dap_cfg }
|
|
|
|
dap.continue()
|
2022-09-05 05:47:08 +00:00
|
|
|
elseif optarg['p'] then
|
|
|
|
dap_cfg.name = dap_cfg.name .. ' package'
|
|
|
|
dap_cfg.mode = 'test'
|
|
|
|
dap_cfg.request = 'launch'
|
|
|
|
dap_cfg.program = sep .. '${fileDirname}'
|
2022-02-14 04:59:31 +00:00
|
|
|
dap.configurations.go = { dap_cfg }
|
|
|
|
dap.continue()
|
2022-03-07 06:09:42 +00:00
|
|
|
elseif run_cur then
|
2022-09-05 05:47:08 +00:00
|
|
|
dap_cfg.name = dap_cfg.name .. ' run current'
|
|
|
|
dap_cfg.request = 'launch'
|
|
|
|
dap_cfg.mode = 'debug'
|
2022-05-29 13:07:39 +00:00
|
|
|
if testfunc then
|
2022-09-05 05:47:08 +00:00
|
|
|
dap_cfg.args = { '-test.run', '^' .. testfunc.name .. '$' }
|
|
|
|
dap_cfg.mode = 'test'
|
2022-05-29 13:07:39 +00:00
|
|
|
end
|
2022-09-05 05:47:08 +00:00
|
|
|
dap_cfg.program = sep .. '${relativeFileDirname}'
|
2022-03-07 06:09:42 +00:00
|
|
|
dap.configurations.go = { dap_cfg }
|
|
|
|
dap.continue()
|
2022-05-29 13:07:39 +00:00
|
|
|
-- dap.run_to_cursor()
|
2022-01-15 09:59:03 +00:00
|
|
|
elseif cfg_exist then
|
2022-09-05 05:47:08 +00:00
|
|
|
log('using launch cfg')
|
2022-01-15 09:59:03 +00:00
|
|
|
launch.load()
|
2022-06-30 02:41:20 +00:00
|
|
|
log(dap.configurations.go)
|
2022-01-15 11:14:56 +00:00
|
|
|
for _, cfg in ipairs(dap.configurations.go) do
|
2022-09-05 05:47:08 +00:00
|
|
|
cfg.dlvToolPath = vim.fn.exepath('dlv')
|
2022-01-15 11:14:56 +00:00
|
|
|
end
|
2022-01-15 09:59:03 +00:00
|
|
|
dap.continue()
|
2022-06-30 02:41:20 +00:00
|
|
|
else -- no args
|
2022-09-05 05:47:08 +00:00
|
|
|
log('debug main')
|
|
|
|
dap_cfg.program = sep .. '${relativeFileDirname}'
|
2021-07-10 11:06:10 +00:00
|
|
|
dap_cfg.args = args
|
2022-09-05 05:47:08 +00:00
|
|
|
dap_cfg.mode = 'debug'
|
|
|
|
dap_cfg.request = 'launch'
|
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
|
2022-06-30 02:41:20 +00:00
|
|
|
log(dap_cfg, args, optarg)
|
|
|
|
|
|
|
|
M.pre_mode = dap_cfg.mode or M.pre_mode
|
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
|
2022-07-12 13:11:46 +00:00
|
|
|
local unmap_keys = {
|
2022-09-05 05:47:08 +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
|
|
|
}
|
2022-07-12 13:11:46 +00:00
|
|
|
for _, value in pairs(unmap_keys) do
|
2022-09-05 05:47:08 +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]])
|
2022-07-07 13:46:36 +00:00
|
|
|
|
2022-07-12 13:11:46 +00:00
|
|
|
for _, k in pairs(unmap_keys) do
|
2022-07-07 13:46:36 +00:00
|
|
|
for _, v in pairs(keymaps_backup or {}) do
|
|
|
|
if v.lhs == k then
|
|
|
|
local nr = (v.noremap == 1)
|
|
|
|
local sl = (v.slient == 1)
|
|
|
|
local exp = (v.expr == 1)
|
|
|
|
local mode = v.mode
|
2022-09-05 05:47:08 +00:00
|
|
|
local desc = v.desc or 'go-dap'
|
|
|
|
if v.mode == ' ' then
|
|
|
|
mode = { 'n', 'v' }
|
2022-07-07 13:46:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
log(v)
|
2023-02-15 20:36:49 +00:00
|
|
|
vim.keymap.set(
|
|
|
|
mode,
|
|
|
|
v.lhs,
|
|
|
|
v.rhs or v.callback,
|
|
|
|
{ noremap = nr, silent = sl, expr = exp, desc = desc }
|
|
|
|
)
|
2022-07-07 13:46:36 +00:00
|
|
|
-- vim.api.nvim_set_keymap('n', v.lhs, v.rhs, {noremap=nr, silent=sl, expr=exp})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
keymaps_backup = {}
|
2021-11-29 20:19:45 +00:00
|
|
|
end
|
|
|
|
|
2022-06-30 05:34:48 +00:00
|
|
|
M.disconnect_dap = function()
|
2022-09-05 05:47:08 +00:00
|
|
|
local has_dap, dap = pcall(require, 'dap')
|
2022-01-16 23:31:24 +00:00
|
|
|
if has_dap then
|
2022-05-29 13:07:39 +00:00
|
|
|
dap.disconnect()
|
2022-09-05 05:47:08 +00:00
|
|
|
vim.cmd('sleep 100m') -- allow cleanup
|
2022-06-16 01:38:37 +00:00
|
|
|
else
|
2022-09-05 05:47:08 +00:00
|
|
|
vim.notify('dap not found')
|
2022-01-16 23:31:24 +00:00
|
|
|
end
|
2022-06-30 05:34:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
M.stop = function(unm)
|
|
|
|
if unm then
|
|
|
|
unmap()
|
|
|
|
end
|
|
|
|
M.disconnect_dap()
|
|
|
|
|
2022-09-05 05:47:08 +00:00
|
|
|
local has_dapui, dapui = pcall(require, 'dapui')
|
2021-11-29 20:19:45 +00:00
|
|
|
if has_dapui then
|
2022-06-30 05:34:48 +00:00
|
|
|
if dapui_opened() then
|
2023-01-28 07:52:55 +00:00
|
|
|
log('closing dapui')
|
2022-03-07 06:09:42 +00:00
|
|
|
dapui.close()
|
|
|
|
end
|
2021-11-29 20:19:45 +00:00
|
|
|
end
|
2022-04-20 02:00:49 +00:00
|
|
|
|
|
|
|
if stdout then
|
|
|
|
stdout:close()
|
|
|
|
stdout = nil
|
|
|
|
end
|
|
|
|
if stderr then
|
|
|
|
stderr:close()
|
|
|
|
stderr = nil
|
|
|
|
end
|
|
|
|
if handle then
|
|
|
|
handle:close()
|
|
|
|
handle = nil
|
|
|
|
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 = {
|
2022-09-05 05:47:08 +00:00
|
|
|
['go#richgo'] = function(cmd)
|
2021-07-10 13:53:33 +00:00
|
|
|
local args = {}
|
|
|
|
for i = 3, #cmd, 1 do
|
|
|
|
local arg = cmd[i]
|
2022-09-05 05:47:08 +00:00
|
|
|
if vim.startswith(arg, '-') then
|
|
|
|
arg = '-test.' .. string.sub(arg, 2)
|
2021-07-10 13:53:33 +00:00
|
|
|
end
|
|
|
|
args[#args + 1] = arg
|
|
|
|
end
|
2021-10-24 23:05:02 +00:00
|
|
|
|
2021-07-10 13:53:33 +00:00
|
|
|
return {
|
|
|
|
dap = {
|
2022-09-05 05:47:08 +00:00
|
|
|
type = 'go',
|
|
|
|
request = 'launch',
|
|
|
|
mode = 'test',
|
|
|
|
program = sep .. '${relativeFileDirname}',
|
|
|
|
dlvToolPath = vim.fn.exepath('dlv'),
|
2021-10-24 23:05:02 +00:00
|
|
|
args = args,
|
2022-08-06 03:45:01 +00:00
|
|
|
buildFlags = get_test_build_tags(),
|
2021-07-10 13:53:33 +00:00
|
|
|
},
|
|
|
|
parse_result = function(lines)
|
2022-09-05 05:47:08 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-09-05 05:47:08 +00:00
|
|
|
local ul = utils.load_plugin('vim-ultest', 'ultest')
|
2021-12-16 22:59:47 +00:00
|
|
|
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
|