From e9c8f498914c5f377a07cf153953b324aaef8ca1 Mon Sep 17 00:00:00 2001 From: dlond Date: Thu, 22 May 2025 05:29:19 +1200 Subject: [PATCH] added dap for c/c++ & python --- lua/custom/plugins/debug.lua | 130 +++++++++++++++++++++++++++++++++++ lua/custom/plugins/init.lua | 11 +-- 2 files changed, 136 insertions(+), 5 deletions(-) create mode 100644 lua/custom/plugins/debug.lua diff --git a/lua/custom/plugins/debug.lua b/lua/custom/plugins/debug.lua new file mode 100644 index 00000000..50a8b6f8 --- /dev/null +++ b/lua/custom/plugins/debug.lua @@ -0,0 +1,130 @@ +-- ~/dlond/nvim/lua/custom/plugins/debug.lua +-- Debug Adapter Protocol (DAP) setup, integrating kickstart's UI preferences + +return { + -- Main DAP plugin + { + 'mfussenegger/nvim-dap', + dependencies = { + -- UI for nvim-dap + { + 'rcarriga/nvim-dap-ui', + dependencies = { 'nvim-neotest/nvim-nio' }, -- nvim-dap-ui often needs nio + config = function() + local dapui = require 'dapui' + dapui.setup { + -- Borrowed icon and control settings from kickstart/plugins/debug.lua + icons = { expanded = '▾', collapsed = '▸', current_frame = '*' }, + controls = { + icons = { + pause = '⏸', + play = '▶', + step_into = '⏎', + step_over = '⏭', + step_out = '⏮', + step_back = 'b', -- Kickstart uses 'b', nvim-dap default might be different + run_last = '▶▶', + terminate = '⏹', + disconnect = '⏏', + }, + }, + -- You can customize layouts, floating window behavior, etc. + -- layouts = { ... }, + -- floating = { ... }, + } + + -- Automatically open/close dapui when DAP session starts/stops + local dap = require 'dap' + dap.listeners.after.event_initialized['dapui_config'] = function() + dapui.open() + end + dap.listeners.before.event_terminated['dapui_config'] = function() + dapui.close() + end + dap.listeners.before.event_exited['dapui_config'] = function() + dapui.close() + end + end, + }, + -- Optional: Virtual text for DAP (shows variable values inline) + -- { 'theHamsta/nvim-dap-virtual-text', opts = {} }, + + -- If you need Go debugging, you would add 'leoluz/nvim-dap-go' here + -- and call its setup in the nvim-dap config function. + -- { 'leoluz/nvim-dap-go' }, + }, + config = function() + local dap = require 'dap' + + -- Configure the LLDB DAP adapter for C/C++ + -- Assumes 'lldb-dap' executable is in PATH (from pkgs.llvmPackages_XX.lldb) + dap.adapters.lldb = { + type = 'executable', + command = 'lldb-dap', + name = 'lldb-dap (Nix)', + } + dap.configurations.cpp = { + { + name = 'Launch C/C++ (lldb-dap)', + type = 'lldb', + request = 'launch', + program = function() + return vim.fn.input('Path to executable: ', vim.fn.getcwd() .. '/', 'file') + end, + cwd = '${workspaceFolder}', + stopOnEntry = false, + args = {}, + -- Ensure your C/C++ project is compiled with debug symbols (e.g., -g flag with clang/gcc) + }, + } + dap.configurations.c = dap.configurations.cpp -- Use same config for C + + -- Python DAP configuration (using debugpy) + -- Ensure python3Packages.debugpy is in your home.packages + dap.adapters.python = { + type = 'executable', + command = 'python', -- Should be the python from your Nix env + args = { '-m', 'debugpy.adapter' }, + } + dap.configurations.python = { + { + type = 'python', + request = 'launch', + name = 'Launch Python file', + program = '${file}', -- Debug the current file + pythonPath = function() + local venv = os.getenv 'VIRTUAL_ENV' + if venv then + return venv .. '/bin/python' + end + -- Fallback to trying to find python3, then python in PATH + -- This could be made more robust by getting python path from Nix if needed + local py3 = vim.fn.executable 'python3' + if py3 ~= 0 and py3 ~= '' then + return py3 + end + return 'python' + end, + }, + } + + -- If you added 'leoluz/nvim-dap-go' as a dependency: + -- require('dap-go').setup() -- Call its setup function + + -- Your preferred DAP keybindings + vim.keymap.set('n', 'db', dap.toggle_breakpoint, { desc = 'DAP: Toggle [B]reakpoint' }) + vim.keymap.set('n', 'dc', dap.continue, { desc = 'DAP: [C]ontinue' }) + vim.keymap.set('n', 'dj', dap.step_into, { desc = 'DAP: Step [I]nto (j)' }) + vim.keymap.set('n', 'dk', dap.step_over, { desc = 'DAP: Step [O]ver (k)' }) + vim.keymap.set('n', 'do', dap.step_out, { desc = 'DAP: Step [O]ut' }) + vim.keymap.set('n', 'dr', dap.repl.open, { desc = 'DAP: Open [R]EPL' }) + vim.keymap.set('n', 'dl', dap.run_last, { desc = 'DAP: Run [L]ast' }) + vim.keymap.set('n', 'du', function() + require('dapui').toggle() + end, { desc = 'DAP: Toggle [U]I' }) + vim.keymap.set('n', 'dt', dap.terminate, { desc = 'DAP: [T]erminate' }) + -- Kickstart's to toggle UI (can be added if you like it) + -- vim.keymap.set('n', '', function() require('dapui').toggle() end, { desc = 'Debug: Toggle UI' }) + end, + }, +} diff --git a/lua/custom/plugins/init.lua b/lua/custom/plugins/init.lua index 6bd0b736..f2a2987b 100644 --- a/lua/custom/plugins/init.lua +++ b/lua/custom/plugins/init.lua @@ -3,11 +3,12 @@ -- -- See the kickstart.nvim README for more information return { - { import = 'custom.plugins.theme' }, - { import = 'custom.plugins.telescope' }, - { import = 'custom.plugins.lsp' }, - { import = 'custom.plugins.formatting' }, - { import = 'custom.plugins.treesitter' }, { import = 'custom.plugins.completion' }, + { import = 'custom.plugins.debug' }, + { import = 'custom.plugins.formatting' }, + { import = 'custom.plugins.lsp' }, { import = 'custom.plugins.nvim-tmux-navigator' }, + { import = 'custom.plugins.telescope' }, + { import = 'custom.plugins.theme' }, + { import = 'custom.plugins.treesitter' }, }