dotfiles/symlinks/config/nvim/plugin/dap.lua

181 lines
4.7 KiB
Lua
Raw Permalink Normal View History

2021-02-25 16:06:52 +00:00
-------------------------------------------------------------------------------
-- - NeoVim DAP (Debug Adapter Protocol) config -
-------------------------------------------------------------------------------
local dap = require("dap")
2022-04-28 13:53:54 +00:00
vim.api.nvim_create_autocmd("FileType", {
pattern = "dap-repl",
2022-04-29 07:46:45 +00:00
callback = function(_)
2022-04-28 13:53:54 +00:00
require("dap.ext.autocompl").attach()
end,
})
2021-02-25 16:06:52 +00:00
-- Nvim DAP Treesitter integration
2021-11-05 09:56:28 +00:00
require("nvim-dap-virtual-text").setup()
2021-03-03 17:07:02 +00:00
-- Keymaps
2022-04-28 13:53:54 +00:00
vim.keymap.set("n", "<leader>db", dap.toggle_breakpoint)
vim.keymap.set("n", "<leader>dc", dap.continue)
vim.keymap.set("n", "<leader>dso", dap.step_over)
vim.keymap.set("n", "<leader>dsi", dap.step_into)
2021-03-03 17:07:02 +00:00
2022-05-03 16:56:58 +00:00
-- Neovim Lua debugging
vim.api.nvim_create_user_command("NeovimDebugStart", function()
require("osv").launch()
end, { desc = "Start debugging neovim" })
2022-05-03 16:56:58 +00:00
vim.api.nvim_create_user_command("NeovimDebugThis", function()
require("osv").run_this()
end, { desc = "Start debugging this neovim lua script" })
2022-05-03 16:56:58 +00:00
2022-05-07 11:28:18 +00:00
-- Vscode launch.json support
vim.api.nvim_create_user_command("DapLoadVsCodeLaunch", function(args)
require("dap.ext.vscode").load_launchjs(args.args)
end, { desc = "Load VsCode launch.json for DAP" })
2022-05-07 11:28:18 +00:00
2021-11-05 09:56:28 +00:00
-- Nvim DAP UI
2022-03-23 10:40:13 +00:00
local dapui = require("dapui")
dapui.setup()
2021-11-05 09:56:28 +00:00
2021-03-03 17:07:02 +00:00
-- Debugger Hover map
local api = vim.api
local keymap_restore = {}
dap.listeners.after["event_initialized"]["me"] = function()
2022-04-28 13:53:54 +00:00
for _, buf in pairs(api.nvim_list_bufs()) do
local keymaps = api.nvim_buf_get_keymap(buf, "n")
for _, keymap in pairs(keymaps) do
if keymap.lhs == "K" then
table.insert(keymap_restore, keymap)
vim.keymap.del("n", "K", { buffer = buf })
end
end
end
vim.keymap.set({ "n", "v" }, "K", function()
dapui.eval()
end)
2021-03-03 17:07:02 +00:00
end
dap.listeners.after["event_terminated"]["me"] = function()
2022-04-28 13:53:54 +00:00
for _, keymap in pairs(keymap_restore) do
2022-04-30 21:26:57 +00:00
vim.keymap.set(
keymap.mode,
keymap.lhs,
keymap.callback or keymap.rhs,
{ silent = keymap.silent == 1, buffer = keymap.buffer }
)
2022-04-28 13:53:54 +00:00
end
keymap_restore = {}
2021-03-03 17:07:02 +00:00
end
2022-03-23 10:40:13 +00:00
2022-05-03 16:56:58 +00:00
-- dap utils
local function executable_input()
return vim.fn.input("Path to executable: ", vim.fn.getcwd() .. "/", "file")
end
local function args_input()
return vim.split(vim.fn.input("Args: "), " ")
end
2022-03-23 10:40:13 +00:00
-- Additional servers
dap.adapters.lldb = {
2022-04-28 13:53:54 +00:00
type = "executable",
2023-07-07 12:24:26 +00:00
command = "codelldb",
2022-04-28 13:53:54 +00:00
name = "lldb",
2022-03-23 10:40:13 +00:00
}
2022-05-03 16:56:58 +00:00
dap.adapters.nlua = function(callback, config)
callback({ type = "server", host = config.host, port = config.port })
end
2022-03-23 10:40:13 +00:00
dap.configurations.cpp = {
2022-04-28 13:53:54 +00:00
{
name = "Launch",
type = "lldb",
request = "launch",
2022-05-03 16:56:58 +00:00
program = executable_input,
2022-04-28 13:53:54 +00:00
cwd = "${workspaceFolder}",
stopOnEntry = false,
args = {},
-- 💀
-- if you change `runInTerminal` to true, you might need to change the yama/ptrace_scope setting:
--
-- echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
--
-- Otherwise you might get the following error:
--
-- Error on launch: Failed to attach to the target process
--
-- But you should be aware of the implications:
-- https://www.kernel.org/doc/html/latest/admin-guide/LSM/Yama.html
2022-03-23 10:40:13 +00:00
2022-04-28 13:53:54 +00:00
runInTerminal = false,
-- 💀
-- If you use `runInTerminal = true` and resize the terminal window,
-- lldb-vscode will receive a `SIGWINCH` signal which can cause problems
-- To avoid that uncomment the following option
-- See https://github.com/mfussenegger/nvim-dap/issues/236#issuecomment-1066306073
postRunCommands = { "process handle -p true -s false -n false SIGWINCH" },
},
{
name = "Launch with args",
type = "lldb",
request = "launch",
2022-05-03 16:56:58 +00:00
program = executable_input,
2022-04-28 13:53:54 +00:00
cwd = "${workspaceFolder}",
stopOnEntry = false,
2022-05-03 16:56:58 +00:00
args = args_input,
2022-04-28 13:53:54 +00:00
runInTerminal = false,
postRunCommands = { "process handle -p true -s false -n false SIGWINCH" },
},
{
name = "Attach to process",
type = "lldb",
request = "attach",
pid = require("dap.utils").pick_process,
args = {},
},
{
name = "Attach to PID",
type = "lldb",
request = "attach",
pid = function()
return tonumber(vim.fn.input("PID: "))
end,
args = {},
},
2022-03-23 10:40:13 +00:00
}
dap.configurations.c = dap.configurations.cpp
dap.configurations.rust = dap.configurations.cpp
2022-04-30 21:26:57 +00:00
dap.configurations.zig = dap.configurations.cpp
2022-03-23 10:40:13 +00:00
2022-05-03 16:56:58 +00:00
dap.configurations.lua = {
{
type = "nlua",
request = "attach",
name = "Attach to running Neovim instance",
host = function()
local value = vim.fn.input("Host [127.0.0.1]: ")
if value ~= "" then
return value
end
return "127.0.0.1"
end,
port = function()
local val = tonumber(vim.fn.input("Port: "))
assert(val, "Please provide a port number")
return val
end,
},
}
2022-03-23 10:40:13 +00:00
dap.listeners.after.event_initialized["dapui_config"] = function()
2022-04-28 13:53:54 +00:00
dapui.open()
2022-03-23 10:40:13 +00:00
end
dap.listeners.before.event_terminated["dapui_config"] = function()
2022-04-28 13:53:54 +00:00
dapui.close()
2022-03-23 10:40:13 +00:00
end
dap.listeners.before.event_exited["dapui_config"] = function()
2022-04-28 13:53:54 +00:00
dapui.close()
2022-03-23 10:40:13 +00:00
end