diff --git a/lua/config/keymaps.lua b/lua/config/keymaps.lua index 8818478..efefa1b 100644 --- a/lua/config/keymaps.lua +++ b/lua/config/keymaps.lua @@ -41,7 +41,8 @@ vim.keymap.set("n", "f", ":lua require('conform').format()") local status_ok, dap = pcall(require, "dap") if status_ok then vim.keymap.set("n", "cdb", dap.toggle_breakpoint, { desc = "Debug: Toggle Breakpoint" }) - vim.keymap.set("n", "cdc", dap.continue, { desc = "Debug: Start/Continue" }) + --vim.keymap.set("n", "cdc", dap.continue, { desc = "Debug: Start/Continue" }) + -- new start debug keymap in debugger.lua vim.keymap.set("n", "cdi", dap.step_into, { desc = "Debug: Step Into" }) vim.keymap.set("n", "cdo", dap.step_over, { desc = "Debug: Step Over" }) vim.keymap.set("n", "cdO", dap.step_out, { desc = "Debug: Step Out" }) diff --git a/lua/plugins/debugger.lua b/lua/plugins/debugger.lua index 5dad865..ee2058f 100644 --- a/lua/plugins/debugger.lua +++ b/lua/plugins/debugger.lua @@ -1,3 +1,109 @@ + +local function fzf_pick_executable() + return coroutine.create(function(coro) + local current_cwd = vim.fn.getcwd():gsub("\\", "/") + + require("fzf-lua").files({ + cwd = current_cwd, + cmd = "fd --type x --hidden --no-ignore", + + previewer = false, + header = false, + prompt = "Select Executable ❯ ", + + winopts = { + title = " Select Executable ", + title_pos = "center", + height = 0.40, + width = 0.60, + }, + + actions = { + ["default"] = function(selected) + if selected and selected[1] then + -- 1. Strip icons and formatting spaces to isolate the file string + local entry = require("fzf-lua.path").entry_to_file(selected[1], { cwd = current_cwd }) + + -- 2. Automatically resolve it to an absolute path (handles relative or absolute safely) + local full_path = vim.fn.fnamemodify(entry.path, ":p") + + -- 3. Clean up slashes and hand it to DAP + coroutine.resume(coro, full_path:gsub("\\", "/")) + else + coroutine.resume(coro, vim.fn.empty()) + end + end + } + }) + end) +end + + +local function start_debug_session() + + + local dap = require("dap") + -- 3. Platform-agnostic Launcher Configuration + if not is_windows then + dap.configurations.cpp = { + { + name = "Launch file", + type = "codelldb", + request = "launch", + program = fzf_pick_executable, + cwd = '${workspaceFolder}', + stopOnEntry = false, + args = {}, + }, + } + else + local launch_json = vim.fn.getcwd() .. "/.vscode/launch.json" + if vim.fn.filereadable(launch_json) == 1 then + dap.configurations.cpp = {} + -- 1. CRUCIAL FIX: Alias the adapters directly so nvim-dap doesn't complain about a missing engine + --if dap.adapters.cppdbg then + -- dap.adapters.cppvsdbg = dap.adapters.cppdbg + --elseif dap.adapters.codelldb then + -- dap.adapters.cppvsdbg = dap.adapters.codelldb + --end + -- 2. Map types inside launch.json to your C++ filetypes + vscode.load_launchjs(nil, { + cppvsdbg = { "cpp", "c" }, + cppdbg = { "cpp", "c" }, + codelldb = { "cpp", "c" }, + }) + else + -- No launch.json exists. Re-inject your FZF picker template rules + dap.configurations.cpp = { + { + name = "Launch file", + type = "cppdbg", + request = "launch", + program = fzf_pick_executable, + cwd = "${workspaceFolder}", + stopOnEntry = false, + + -- Crucial configurations for Microsoft cpptools: + -- On Windows, this automatically uses MinGW/MSVC underlying debugger engines + MIMode = is_windows and "gdb" or "lldb", + miDebuggerPath = is_windows and "gdb.exe" or nil, -- Uses system path for gdb on Windows + + setupCommands = { + { + text = "-enable-pretty-printing", + description = "enable pretty printing", + ignoreFailures = false, + }, + }, + }, + } + end + end + dap.continue() +end + +vim.keymap.set("n", "cdc", start_debug_session, { desc = "Debug: Start/Continue with Smart Config" }) + return { "mfussenegger/nvim-dap", dependencies = { @@ -27,36 +133,95 @@ return { dap.listeners.before.event_exited["dapui_config"] = function() dapui.close() end -- Step 2 & 3 configurations will go here... - -- Define the adapter connection + -- 1. Detect operating system + local is_windows = vim.fn.has("win32") == 1 + local ext = is_windows and ".exe" or "" + + -- 2. Platform-agnostic CodeLLDB Adapter setup + local mason_registry = require("mason-registry") + local codelldb = mason_registry.get_package("codelldb") + local extension_path = codelldb:get_install_path() .. "/extension/" + + -- CodeLLDB executable is in different subfolders depending on Mason's package version + local codelldb_path = extension_path .. "adapter/codelldb" .. ext + + + local cpptools_pkg = mason_registry.get_package("cpptools") + local cpptools_path = cpptools_pkg:get_install_path() + .. "/extension/debugAdapters/bin/OpenDebugAD7" + .. ext + + dap.adapters.codelldb = { type = 'server', port = "${port}", executable = { - -- Adjust this path if Mason uses a different default location on your OS - command = vim.fn.stdpath("data") .. "/mason/bin/codelldb", + command = codelldb_path, args = {"--port", "${port}"}, + detached = not is_windows, } } - -- Configure C++ configurations - dap.configurations.cpp = { - { - name = "Launch file", - type = "codelldb", - request = "launch", - program = function() - return vim.fn.input('Path to executable: ', vim.fn.getcwd() .. '/', 'file') - end, - cwd = '${workspaceFolder}', - stopOnEntry = false, - args = {}, + dap.adapters.cppdbg = { + id = "cppdbg", + type = "executable", + command = cpptools_path, + -- For Windows subprocess tracking stability + options = { + detached = not is_windows, }, } - -- Re-use C++ configuration for C language + dap.adapters.cppvsdbg = { + id = "cppdbg", + type = "executable", + command = cpptools_path, + -- For Windows subprocess tracking stability + options = { + detached = not is_windows, + }, + } + + -- 3. Platform-agnostic Launcher Configuration + if not is_windows then + dap.configurations.cpp = { + { + name = "Launch file", + type = "codelldb", + request = "launch", + program = fzf_pick_executable, + cwd = '${workspaceFolder}', + stopOnEntry = false, + args = {}, + }, + } + else + dap.configurations.cpp = { + { + name = "Launch file", + type = "cppdbg", + request = "launch", + program = fzf_pick_executable, + cwd = "${workspaceFolder}", + stopOnEntry = false, + + -- Crucial configurations for Microsoft cpptools: + -- On Windows, this automatically uses MinGW/MSVC underlying debugger engines + MIMode = is_windows and "gdb" or "lldb", + miDebuggerPath = is_windows and "gdb.exe" or nil, -- Uses system path for gdb on Windows + + setupCommands = { + { + text = "-enable-pretty-printing", + description = "enable pretty printing", + ignoreFailures = false, + }, + }, + }, + } + end + dap.configurations.c = dap.configurations.cpp - - end }