local misc = require("core.misc") local map = misc.map --- select a program to execute ---@return thread coroutine containing the picker local function select_program() return coroutine.create(function(coro) ---@diagnostic disable-next-line: param-type-mismatch local entries = vim.fn.readdir(".", [[v:val !~ '^\.']]) vim.ui.select(entries, { prompt = "Select the executable to run:" }, function(choice) coroutine.resume(coro, choice) end) end) end local keymap_restore = {} --- make the default hover binding work for nvim-dap instead of lsp local function set_hover_bind() for _, buf in pairs(vim.api.nvim_list_bufs()) do local keymaps = vim.api.nvim_buf_get_keymap(buf, 'n') for _, keymap in pairs(keymaps) do if keymap.lhs == "K" then table.insert(keymap_restore, keymap) vim.api.nvim_buf_del_keymap(buf, 'n', 'K') end end end vim.keymap.set('n', 'K', require("dap.ui.widgets").hover, { silent = true }) end --- revert the hover bind back to whatever it was local function unset_hover_bind() for _, keymap in pairs(keymap_restore) do if keymap.rhs then vim.api.nvim_buf_set_keymap(keymap.buffer, keymap.mode, keymap.lhs, keymap.rhs, { silent = keymap.silent == 1 }) elseif keymap.callback then vim.keymap.set(keymap.mode, keymap.lhs, keymap.callback, { buffer = keymap.buffer, silent = keymap.silent == 1 }) end end keymap_restore = {} end return { "mfussenegger/nvim-dap", requires = { "mason-org/mason.nvim", "nvim-telescope/telescope.nvim" }, disable = not vim.fn.has("nvim-0.9.5"), branch = "0.10.0", function() local dap = require("dap") -- define codelldb dap.adapters.codelldb = { type = "executable", command = "codelldb", } -- define the c configuration for codelldb dap.configurations.c = { { name = "Launch file", type = "codelldb", request = "launch", program = select_program, cwd = "${workspaceFolder}", stopOnEntry = false } } -- and define them for cpp, zig, and rust dap.configurations.cpp = dap.configurations.c dap.configurations.zig = dap.configurations.c dap.configurations.rust = dap.configurations.c -- keybinds map("n", "ec", dap.continue, { desc = "dap continue " }) map("n", "el", dap.run_last, { desc = "dap run last" }) map("n", "et", function() dap.terminate() unset_hover_bind() end, { desc = "dap terminate " }) map("n", "eb", require("dap.breakpoints").toggle, { desc = "dap toggle breakpoint" }) map("n", "e]", dap.step_over, { desc = "dap step over" }) map("n", "e[", dap.step_back, { desc = "dap step back" }) map("n", "er", dap.repl.toggle, { desc = "dap repl toggle" }) map("n", "eR", dap.restart, { desc = "dap restart" }) -- events dap.listeners.after['event_initialized']['me'] = set_hover_bind dap.listeners.after['event_terminated']['me'] = unset_hover_bind end }