Skip to content

Commit 8761100

Browse files
feat: add DAP configuration for Python and enhance debugging setup
1 parent 1fdec71 commit 8761100

File tree

3 files changed

+228
-16
lines changed

3 files changed

+228
-16
lines changed

debug.lua.bak

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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+
}

lua/custom/plugins/trouble.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ return {
7878
config = function(_, opts)
7979
require('trouble').setup(opts)
8080
-- Add which-key group
81-
-- local ok, which_key = pcall(require, 'which-key')
81+
local ok, which_key = pcall(require, 'which-key')
8282
-- if ok then
8383
-- -- Use standard which-key format that's known to work
8484
-- which_key.register({

lua/kickstart/plugins/debug.lua

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ return {
2323

2424
-- Add your own debuggers here
2525
'leoluz/nvim-dap-go',
26+
'mfussenegger/nvim-dap-python',
27+
'theHamsta/nvim-dap-virtual-text',
2628
},
2729
keys = {
2830
-- Basic debugging keymaps, feel free to change to your liking!
2931
{
30-
'<F5>',
32+
'g@',
3133
function()
3234
require('dap').continue()
3335
end,
@@ -55,7 +57,7 @@ return {
5557
desc = 'Debug: Step Out',
5658
},
5759
{
58-
'<leader>b',
60+
'<leader>.',
5961
function()
6062
require('dap').toggle_breakpoint()
6163
end,
@@ -95,6 +97,7 @@ return {
9597
ensure_installed = {
9698
-- Update this to ensure that you have the debuggers for the langs you want
9799
'delve',
100+
'debugpy', -- Python debugger
98101
},
99102
}
100103

@@ -120,18 +123,6 @@ return {
120123
},
121124
}
122125

123-
-- Change breakpoint icons
124-
-- vim.api.nvim_set_hl(0, 'DapBreak', { fg = '#e51400' })
125-
-- vim.api.nvim_set_hl(0, 'DapStop', { fg = '#ffcc00' })
126-
-- local breakpoint_icons = vim.g.have_nerd_font
127-
-- and { Breakpoint = '', BreakpointCondition = '', BreakpointRejected = '', LogPoint = '', Stopped = '' }
128-
-- or { Breakpoint = '●', BreakpointCondition = '⊜', BreakpointRejected = '⊘', LogPoint = '◆', Stopped = '⭔' }
129-
-- for type, icon in pairs(breakpoint_icons) do
130-
-- local tp = 'Dap' .. type
131-
-- local hl = (type == 'Stopped') and 'DapStop' or 'DapBreak'
132-
-- vim.fn.sign_define(tp, { text = icon, texthl = hl, numhl = hl })
133-
-- end
134-
135126
dap.listeners.after.event_initialized['dapui_config'] = dapui.open
136127
dap.listeners.before.event_terminated['dapui_config'] = dapui.close
137128
dap.listeners.before.event_exited['dapui_config'] = dapui.close
@@ -140,9 +131,51 @@ return {
140131
require('dap-go').setup {
141132
delve = {
142133
-- On Windows delve must be run attached or it crashes.
143-
-- See https://github.com/leoluz/nvim-dap-go/blob/main/README.md#configuring
134+
-- See https://github.com/leoluz/nvim-dap-go/blob/main/README.md\#configuring
144135
detached = vim.fn.has 'win32' == 0,
145136
},
146137
}
138+
139+
-- Python configuration
140+
require('dap-python').setup('python')
141+
require('dap-python').test_runner = 'pytest'
142+
143+
-- Initialize the Python configuration table if it doesn't exist
144+
dap.configurations.python = dap.configurations.python or {
145+
{
146+
-- The first three options are required by nvim-dap
147+
type = 'python'; -- the type here established the link to the adapter definition: `dap.adapters.python`
148+
request = 'launch';
149+
name = "Launch file";
150+
151+
-- Options below are for debugpy, see https://github.com/microsoft/debugpy/wiki/Debug-configuration-settings for supported options
152+
153+
program = "${file}"; -- This configuration will launch the current file if used.
154+
pythonPath = function()
155+
-- debugpy supports launching an application with a different interpreter then the one used to launch debugpy itself.
156+
-- The code below looks for a `venv` or `.venv` folder in the current directly and uses the python within.
157+
-- You could adapt this - to for example use the `VIRTUAL_ENV` environment variable.
158+
local cwd = vim.fn.getcwd()
159+
if vim.fn.executable(cwd .. '/venv/bin/python') == 1 then
160+
return cwd .. '/venv/bin/python'
161+
elseif vim.fn.executable(cwd .. '/.venv/bin/python') == 1 then
162+
return cwd .. '/.venv/bin/python'
163+
else
164+
return '/usr/bin/python'
165+
end
166+
end;
167+
},
168+
}
169+
170+
-- Add a basic Python configuration if none exists
171+
-- if #dap.configurations.python == 0 then
172+
-- table.insert(dap.configurations.python, {
173+
-- type = 'python',
174+
-- request = 'launch',
175+
-- name = 'Launch file',
176+
-- program = '${file}',
177+
-- pythonPath = 'python'
178+
-- })
179+
-- end
147180
end,
148181
}

0 commit comments

Comments
 (0)