From 2f0d12ffe8c4d6b3e85ad4c0fcb6aa8f2b300389 Mon Sep 17 00:00:00 2001 From: Juliano Barbosa Date: Fri, 23 Aug 2024 08:52:09 -0300 Subject: [PATCH] chore(python-debugging): refactor python debugging setup --- lua/custom/plugins/python-debugging.lua | 101 +++++++++++++++++++----- 1 file changed, 80 insertions(+), 21 deletions(-) diff --git a/lua/custom/plugins/python-debugging.lua b/lua/custom/plugins/python-debugging.lua index f143e8d2..721da151 100644 --- a/lua/custom/plugins/python-debugging.lua +++ b/lua/custom/plugins/python-debugging.lua @@ -2,27 +2,86 @@ -- return { - 'mfussenegger/nvim-dap-python', - keys = { - { - 'dPt', - function() - require('dap-python').test_method() - end, - desc = 'Debug Method', - ft = 'python', - }, - { - 'dPc', - function() - require('dap-python').test_class() - end, - desc = 'Debug Class', - ft = 'python', + -- DEBUGGING + + -- DAP Client for nvim + -- - start the debugger with `dc` + -- - add breakpoints with `db` + -- - terminate the debugger `dt` + { + 'mfussenegger/nvim-dap', + keys = { + { + 'dc', + function() + require('dap').continue() + end, + desc = 'Start/Continue Debugger', + }, + { + 'db', + function() + require('dap').toggle_breakpoint() + end, + desc = 'Add Breakpoint', + }, + { + 'dt', + function() + require('dap').terminate() + end, + desc = 'Terminate Debugger', + }, }, }, - config = function() - local path = require('mason-registry').get_package('debugpy'):get_install_path() - require('dap-python').setup(os.getenv 'PYENV_ROOT' .. '/versions/3.11.9/bin/python') - end, + + -- UI for the debugger + -- - the debugger UI is also automatically opened when starting/stopping the debugger + -- - toggle debugger UI manually with `du` + { + 'rcarriga/nvim-dap-ui', + dependencies = { 'mfussenegger/nvim-dap', 'nvim-neotest/nvim-nio' }, + keys = { + { + 'du', + function() + require('dapui').toggle() + end, + desc = 'Toggle Debugger UI', + }, + }, + -- automatically open/close the DAP UI when starting/stopping the debugger + config = function() + local listener = require('dap').listeners + listener.after.event_initialized['dapui_config'] = function() + require('dapui').open() + end + listener.before.event_terminated['dapui_config'] = function() + require('dapui').close() + end + listener.before.event_exited['dapui_config'] = function() + require('dapui').close() + end + end, + }, + + -- Configuration for the python debugger + -- - configures debugpy for us + -- - uses the debugpy installation from mason + { + 'mfussenegger/nvim-dap-python', + dependencies = 'mfussenegger/nvim-dap', + config = function() + -- fix: E5108: Error executing lua .../Local/nvim-data/lazy/nvim-dap-ui/lua/dapui/controls.lua:14: attempt to index local 'element' (a nil value) + -- see: https://github.com/rcarriga/nvim-dap-ui/issues/279#issuecomment-1596258077 + local dap, dapui = require 'dap', require 'dapui' + dapui.setup() + -- uses the debugypy installation by mason + local debugpyPythonPath = require('mason-registry').get_package('debugpy'):get_install_path() .. '/venv/bin/python3' + require('dap-python').setup(debugpyPythonPath, {}) ---@diagnostic disable-line: missing-fields + end, + }, } + +-- The line beneath this is called `modeline`. See `:help modeline` +-- vim: ts=2 sts=2 sw=2 et