179 lines
4.0 KiB
Lua
179 lines
4.0 KiB
Lua
local config = require("tiny-debugger.config")
|
|
local help = require("tiny-debugger.help")
|
|
local ui = require("tiny-debugger.ui")
|
|
local virtual_text = require("tiny-debugger.virtual_text")
|
|
|
|
local M = {}
|
|
|
|
local active = false
|
|
local stashed_maps = {}
|
|
|
|
local function stash_mapping(buf, lhs)
|
|
local maps = vim.api.nvim_buf_get_keymap(buf, "n")
|
|
for _, map in ipairs(maps) do
|
|
if map.lhs == lhs then
|
|
stashed_maps[lhs] = map
|
|
return
|
|
end
|
|
end
|
|
stashed_maps[lhs] = nil
|
|
end
|
|
|
|
local function restore_mapping(buf, lhs)
|
|
local map = stashed_maps[lhs]
|
|
if map then
|
|
local rhs = map.rhs or map.callback
|
|
if rhs then
|
|
vim.keymap.set("n", lhs, rhs, {
|
|
buffer = buf,
|
|
silent = map.silent == 1,
|
|
noremap = map.noremap == 1,
|
|
expr = map.expr == 1,
|
|
desc = map.desc,
|
|
})
|
|
end
|
|
else
|
|
pcall(vim.keymap.del, "n", lhs, { buffer = buf })
|
|
end
|
|
end
|
|
|
|
-- actions table: key name → function
|
|
-- populated by other modules via M.register_action
|
|
local actions = {}
|
|
|
|
function M.register_action(name, fn)
|
|
actions[name] = fn
|
|
end
|
|
|
|
-- default actions that map directly to dap calls
|
|
local function setup_default_actions()
|
|
local dap = require("dap")
|
|
actions.continue = function() dap.continue() end
|
|
actions.step_over = function() dap.step_over() end
|
|
actions.step_into = function() dap.step_into() end
|
|
actions.step_out = function() dap.step_out() end
|
|
actions.run_to_cursor = function() dap.run_to_cursor() end
|
|
actions.terminate = function()
|
|
dap.terminate()
|
|
M.exit()
|
|
end
|
|
actions.restart = function() dap.restart() end
|
|
actions.help = function()
|
|
if help.is_open() then
|
|
help.close()
|
|
else
|
|
help.open()
|
|
end
|
|
end
|
|
|
|
actions.watch = function()
|
|
local expr
|
|
if vim.fn.mode() == "v" or vim.fn.mode() == "V" then
|
|
vim.cmd('noautocmd normal! "vy')
|
|
expr = vim.fn.getreg("v")
|
|
else
|
|
expr = vim.fn.expand("<cexpr>")
|
|
end
|
|
if expr and expr ~= "" then
|
|
require("dapui").elements.watches.add(expr)
|
|
end
|
|
end
|
|
|
|
actions.hover = function()
|
|
require("dapui").eval()
|
|
end
|
|
|
|
actions.eval = function()
|
|
require("dapui").float_element("repl")
|
|
end
|
|
end
|
|
|
|
-- actions that also get visual mode mappings
|
|
local visual_actions = { watch = true, hover = true }
|
|
|
|
local function set_keymaps(buf)
|
|
local keys = config.opts.keys
|
|
for name, key in pairs(keys) do
|
|
if key and actions[name] then
|
|
stash_mapping(buf, key)
|
|
vim.keymap.set("n", key, actions[name], { buffer = buf, silent = true, desc = "tiny-debugger: " .. name })
|
|
if visual_actions[name] then
|
|
vim.keymap.set("v", key, actions[name], { buffer = buf, silent = true, desc = "tiny-debugger: " .. name })
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
local function clear_keymaps(buf)
|
|
local keys = config.opts.keys
|
|
for name, key in pairs(keys) do
|
|
if key and actions[name] then
|
|
pcall(vim.keymap.del, "n", key, { buffer = buf })
|
|
if visual_actions[name] then
|
|
pcall(vim.keymap.del, "v", key, { buffer = buf })
|
|
end
|
|
restore_mapping(buf, key)
|
|
end
|
|
end
|
|
stashed_maps = {}
|
|
end
|
|
|
|
local saved_cursor_hl = nil
|
|
|
|
local function set_cursor_highlight()
|
|
saved_cursor_hl = vim.api.nvim_get_hl(0, { name = "Cursor" })
|
|
vim.api.nvim_set_hl(0, "Cursor", { bg = "#ff6600", fg = "#000000" })
|
|
end
|
|
|
|
local function restore_cursor_highlight()
|
|
if saved_cursor_hl then
|
|
vim.api.nvim_set_hl(0, "Cursor", saved_cursor_hl)
|
|
saved_cursor_hl = nil
|
|
else
|
|
vim.api.nvim_set_hl(0, "Cursor", {})
|
|
end
|
|
end
|
|
|
|
function M.enter()
|
|
if active then return end
|
|
active = true
|
|
|
|
setup_default_actions()
|
|
|
|
local buf = vim.api.nvim_get_current_buf()
|
|
set_keymaps(buf)
|
|
set_cursor_highlight()
|
|
ui.open()
|
|
virtual_text.enable()
|
|
|
|
if config.opts.help_on_enter then
|
|
help.open()
|
|
end
|
|
end
|
|
|
|
function M.exit()
|
|
if not active then return end
|
|
active = false
|
|
|
|
local buf = vim.api.nvim_get_current_buf()
|
|
clear_keymaps(buf)
|
|
restore_cursor_highlight()
|
|
help.close()
|
|
ui.close()
|
|
virtual_text.disable()
|
|
end
|
|
|
|
function M.toggle()
|
|
if active then
|
|
M.exit()
|
|
else
|
|
M.enter()
|
|
end
|
|
end
|
|
|
|
function M.is_active()
|
|
return active
|
|
end
|
|
|
|
return M
|