Files
nvim/plugins/nvim-dap.lua
T

152 lines
4.0 KiB
Lua

local dap = require("dap")
local log = require("log")
vim.api.nvim_set_hl(0, "DebugPC", {
bg = "NONE",
fg = "NONE",
})
vim.api.nvim_create_user_command(
"Debug",
---@param opts vim.api.keyset.create_user_command.command_args
function(opts)
local cfgs = dap.configurations[vim.bo.filetype]
if not cfgs then
log.error(
"No configurations available for filetype %s",
vim.bo.filetype
)
return
end
local function run_config(cfg)
local all_args = vim.fn.split(opts.args)
cfg.program = all_args[1]
local args = {}
for i = 2, #all_args do
table.insert(args, all_args[i])
end
cfg.args = args
dap.run(cfg)
end
if #cfgs == 1 then
run_config(cfgs[1])
return
end
local names = {}
for _, c in ipairs(cfgs) do
table.insert(names, c.name)
end
vim.ui.select(names, {
prompt = "Select DAP configuration to use:",
}, function(choice, idx)
if choice and idx then
run_config(cfgs[idx])
end
end)
end,
{
nargs = "+",
---@param ArgLead string
---@param CmdLine string
complete = function(ArgLead, CmdLine, CursorPos)
local _, spaces = CmdLine:sub(1, CursorPos):gsub("%s+", "")
if spaces == 1 then
return vim.fn.getcompletion(ArgLead, "file")
end
end,
}
)
-- https://sourceware.org/gdb/current/onlinedocs/gdb#Debugger-Adapter-Protocol
dap.adapters.gdb = {
type = "executable",
command = "gdb",
args = { "--interpreter=dap" },
}
dap.adapters.lldb = {
type = "executable",
command = "lldb-dap",
}
dap.adapters.python = {
type = "executable",
command = "python",
args = { "-m", "debugpy.adapter" },
}
dap.configurations.c = {
{
type = "gdb",
request = "launch",
name = "Launch",
program = function()
local path = vim.fn.input({
prompt = "Path to executable: ",
default = vim.fn.getcwd() .. "/",
completion = "file",
})
return (path and path ~= "") and path or dap.ABORT
end,
cwd = "${workspaceFolder}",
stopAtBeginningOfMainSubprogram = false,
},
}
-- dap.configurations.c = {
-- {
-- type = "lldb",
-- request = "launch",
-- name = "Launch",
-- program = function()
-- local path = vim.fn.input({
-- prompt = "Path to executable: ",
-- default = vim.fn.getcwd() .. "/",
-- completion = "file",
-- })
-- return (path and path ~= "") and path or dap.ABORT
-- end,
-- cwd = "${workspaceFolder}",
-- stopAtBeginningOfMainSubprogram = false,
-- console = "internalConsole",
-- },
-- }
dap.configurations.cpp = dap.configurations.c
dap.configurations.python = {
{
type = "python",
request = "launch",
name = "Launch",
program = function()
local path = vim.fn.input({
prompt = "Path to executable: ",
default = vim.fn.getcwd() .. "/",
completion = "file",
})
return (path and path ~= "") and path or dap.ABORT
end,
cwd = "${workspaceFolder}",
},
}
vim.keymap.set("n", "<Leader>db", dap.toggle_breakpoint)
vim.keymap.set("n", "<Leader>df", dap.focus_frame)
vim.keymap.set("n", "<PageUp>", dap.up)
vim.keymap.set("n", "<PageDown>", dap.down)
vim.keymap.set({ "n", "x" }, "<Leader>dh", require("dap.hover"))
vim.keymap.set({ "n", "x" }, "<Leader>dr", dap.repl.toggle)
vim.keymap.set("n", "<F2>", dap.step_into)
vim.keymap.set("n", "<F3>", dap.step_over)
vim.keymap.set("n", "<F4>", dap.step_out)
vim.keymap.set("n", "<F5>", dap.continue)
vim.keymap.set("n", "<F9>", dap.terminate)