|
| 1 | +-- debug.lua |
| 2 | +-- |
| 3 | +-- Shows how to use the DAP plugin to debug your code. |
| 4 | +-- |
| 5 | +-- Primarily focused on configuring the debugger for Go, but can |
| 6 | +-- be extended to other languages as well. That's why it's called |
| 7 | +-- kickstart.nvim and not kitchen-sink.nvim ;) |
| 8 | + |
| 9 | +return { |
| 10 | + -- NOTE: Yes, you can install new plugins here! |
| 11 | + 'mfussenegger/nvim-dap', |
| 12 | + -- NOTE: And you can specify dependencies as well |
| 13 | + dependencies = { |
| 14 | + -- Creates a beautiful debugger UI |
| 15 | + 'rcarriga/nvim-dap-ui', |
| 16 | + |
| 17 | + -- Required dependency for nvim-dap-ui |
| 18 | + 'nvim-neotest/nvim-nio', |
| 19 | + |
| 20 | + -- Installs the debug adapters for you |
| 21 | + 'williamboman/mason.nvim', |
| 22 | + 'jay-babu/mason-nvim-dap.nvim', |
| 23 | + |
| 24 | + -- Add your own debuggers here |
| 25 | + 'leoluz/nvim-dap-go', |
| 26 | + 'mfussenegger/nvim-dap-python', -- Uncomment to add Python support |
| 27 | + 'theHamsta/nvim-dap-virtual-text', -- Uncomment to add virtual text support |
| 28 | + }, |
| 29 | + keys = { |
| 30 | + -- Basic debugging keymaps, feel free to change to your liking! |
| 31 | + { |
| 32 | + 'g@', |
| 33 | + function() |
| 34 | + require('dap').continue() |
| 35 | + end, |
| 36 | + desc = 'Debug: Start/Continue', |
| 37 | + }, |
| 38 | + { |
| 39 | + '<F1>', |
| 40 | + function() |
| 41 | + require('dap').step_into() |
| 42 | + end, |
| 43 | + desc = 'Debug: Step Into', |
| 44 | + }, |
| 45 | + {Read debug.lua, lines 1 to 153 |
| 46 | + |
| 47 | + |
| 48 | + '<F2>', |
| 49 | + function() |
| 50 | + require('dap').step_over() |
| 51 | + end, |
| 52 | + desc = 'Debug: Step Over', |
| 53 | + }, |
| 54 | + { |
| 55 | + '<F3>', |
| 56 | + function() |
| 57 | + require('dap').step_out() |
| 58 | + end, |
| 59 | + desc = 'Debug: Step Out', |
| 60 | + }, |
| 61 | + { |
| 62 | + '<leader>.', |
| 63 | + function() |
| 64 | + require('dap').toggle_breakpoint() |
| 65 | + end, |
| 66 | + desc = 'Debug: Toggle Breakpoint', |
| 67 | + }, |
| 68 | + { |
| 69 | + '<leader>B', |
| 70 | + function() |
| 71 | + require('dap').set_breakpoint(vim.fn.input 'Breakpoint condition: ') |
| 72 | + end, |
| 73 | + desc = 'Debug: Set Breakpoint', |
| 74 | + }, |
| 75 | + -- Toggle to see last session result. Without this, you can't see session output in case of unhandled exception. |
| 76 | + { |
| 77 | + '<F7>', |
| 78 | + function() |
| 79 | + require('dapui').toggle() |
| 80 | + end, |
| 81 | + desc = 'Debug: See last session result.', |
| 82 | + }, |
| 83 | + }, |
| 84 | + config = function() |
| 85 | + local dap = require 'dap' |
| 86 | + local dapui = require 'dapui' |
| 87 | + |
| 88 | + require('mason-nvim-dap').setup { |
| 89 | + -- Makes a best effort to setup the various debuggers with |
| 90 | + -- reasonable debug configurations |
| 91 | + automatic_installation = true, |
| 92 | + |
| 93 | + -- You can provide additional configuration to the handlers, |
| 94 | + -- see mason-nvim-dap README for more information |
| 95 | + handlers = {}, |
| 96 | + |
| 97 | + -- You'll need to check that you have the required things installed |
| 98 | + -- online, please don't ask me how to install them :) |
| 99 | + ensure_installed = { |
| 100 | + -- Update this to ensure that you have the debuggers for the langs you want |
| 101 | + 'delve', |
| 102 | + 'debugpy', -- Python debugger |
| 103 | + }, |
| 104 | + } |
| 105 | + |
| 106 | + -- Dap UI setup |
| 107 | + -- For more information, see |:help nvim-dap-ui| |
| 108 | + dapui.setup { |
| 109 | + -- Set icons to characters that are more likely to work in every terminal. |
| 110 | + -- Feel free to remove or use ones that you like more! :) |
| 111 | + -- Don't feel like these are good choices. |
| 112 | + icons = { expanded = '▾', collapsed = '▸', current_frame = '*' }, |
| 113 | + controls = { |
| 114 | + icons = { |
| 115 | + pause = '⏸', |
| 116 | + play = '▶', |
| 117 | + step_into = '⏎', |
| 118 | + step_over = '⏭', |
| 119 | + step_out = '⏮', |
| 120 | + step_back = 'b', |
| 121 | + run_last = '▶▶', |
| 122 | + terminate = '⏹', |
| 123 | + disconnect = '⏏', |
| 124 | + }, |
| 125 | + }, |
| 126 | + } |
| 127 | + |
| 128 | + -- Change breakpoint icons |
| 129 | + -- vim.api.nvim_set_hl(0, 'DapBreak', { fg = '#e51400' }) |
| 130 | + -- vim.api.nvim_set_hl(0, 'DapStop', { fg = '#ffcc00' }) |
| 131 | + -- local breakpoint_icons = vim.g.have_nerd_font |
| 132 | + -- and { Breakpoint = '', BreakpointCondition = '', BreakpointRejected = '', LogPoint = '', Stopped = '' } |
| 133 | + -- or { Breakpoint = '●', BreakpointCondition = '⊜', BreakpointRejected = '⊘', LogPoint = '◆', Stopped = '⭔' } |
| 134 | + -- for type, icon in pairs(breakpoint_icons) do |
| 135 | + -- local tp = 'Dap' .. type |
| 136 | + -- local hl = (type == 'Stopped') and 'DapStop' or 'DapBreak' |
| 137 | + -- vim.fn.sign_define(tp, { text = icon, texthl = hl, numhl = hl }) |
| 138 | + -- end |
| 139 | + |
| 140 | + dap.listeners.after.event_initialized['dapui_config'] = dapui.open |
| 141 | + dap.listeners.before.event_terminated['dapui_config'] = dapui.close |
| 142 | + dap.listeners.before.event_exited['dapui_config'] = dapui.close |
| 143 | + |
| 144 | + -- Install golang specific config |
| 145 | + require('dap-go').setup { |
| 146 | + delve = { |
| 147 | + -- On Windows delve must be run attached or it crashes. |
| 148 | + -- See https://github.com/leoluz/nvim-dap-go/blob/main/README.md#configuring |
| 149 | + detached = vim.fn.has 'win32' == 0, |
| 150 | + }, |
| 151 | + } |
| 152 | + |
| 153 | + -- Python configuration |
| 154 | + require('dap-python').setup('python') -- Uses the Python in your PATH |
| 155 | + require('dap-python').test_runner = 'pytest' |
| 156 | + |
| 157 | + -- Make sure the configurations table exists |
| 158 | + dap.configurations.python = dap.configurations.python or {} |
| 159 | + |
| 160 | + -- Add some common Python debug configurations |
| 161 | + table.insert(dap.configurations.python, { |
| 162 | + type = 'python', |
| 163 | + request = 'launch', |
| 164 | + name = 'Launch file', |
| 165 | + program = '${file}', |
| 166 | + pythonPath = function() |
| 167 | + -- Try to detect python path from virtual environment |
| 168 | + local cwd = vim.fn.getcwd() |
| 169 | + if vim.fn.executable(cwd .. '/venv/bin/python') == 1 then |
| 170 | + return cwd .. '/venv/bin/python' |
| 171 | + elseif vim.fn.executable(cwd .. '/.venv/bin/python') == 1 then |
| 172 | + return cwd .. '/.venv/bin/python' |
| 173 | + else |
| 174 | + return 'python' |
| 175 | + end |
| 176 | + end, |
| 177 | + }) |
| 178 | + end, |
| 179 | +} |
0 commit comments