chore(python-debugging): refactor python debugging setup
This commit is contained in:
		
							parent
							
								
									63d6426b87
								
							
						
					
					
						commit
						2f0d12ffe8
					
				| 
						 | 
					@ -2,27 +2,86 @@
 | 
				
			||||||
--
 | 
					--
 | 
				
			||||||
 | 
					
 | 
				
			||||||
return {
 | 
					return {
 | 
				
			||||||
  'mfussenegger/nvim-dap-python',
 | 
					  -- DEBUGGING
 | 
				
			||||||
  keys = {
 | 
					
 | 
				
			||||||
    {
 | 
					  -- DAP Client for nvim
 | 
				
			||||||
      '<leader>dPt',
 | 
					  -- - start the debugger with `<leader>dc`
 | 
				
			||||||
      function()
 | 
					  -- - add breakpoints with `<leader>db`
 | 
				
			||||||
        require('dap-python').test_method()
 | 
					  -- - terminate the debugger `<leader>dt`
 | 
				
			||||||
      end,
 | 
					  {
 | 
				
			||||||
      desc = 'Debug Method',
 | 
					    'mfussenegger/nvim-dap',
 | 
				
			||||||
      ft = 'python',
 | 
					    keys = {
 | 
				
			||||||
    },
 | 
					      {
 | 
				
			||||||
    {
 | 
					        '<leader>dc',
 | 
				
			||||||
      '<leader>dPc',
 | 
					        function()
 | 
				
			||||||
      function()
 | 
					          require('dap').continue()
 | 
				
			||||||
        require('dap-python').test_class()
 | 
					        end,
 | 
				
			||||||
      end,
 | 
					        desc = 'Start/Continue Debugger',
 | 
				
			||||||
      desc = 'Debug Class',
 | 
					      },
 | 
				
			||||||
      ft = 'python',
 | 
					      {
 | 
				
			||||||
 | 
					        '<leader>db',
 | 
				
			||||||
 | 
					        function()
 | 
				
			||||||
 | 
					          require('dap').toggle_breakpoint()
 | 
				
			||||||
 | 
					        end,
 | 
				
			||||||
 | 
					        desc = 'Add Breakpoint',
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
 | 
					      {
 | 
				
			||||||
 | 
					        '<leader>dt',
 | 
				
			||||||
 | 
					        function()
 | 
				
			||||||
 | 
					          require('dap').terminate()
 | 
				
			||||||
 | 
					        end,
 | 
				
			||||||
 | 
					        desc = 'Terminate Debugger',
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  config = function()
 | 
					
 | 
				
			||||||
    local path = require('mason-registry').get_package('debugpy'):get_install_path()
 | 
					  -- UI for the debugger
 | 
				
			||||||
    require('dap-python').setup(os.getenv 'PYENV_ROOT' .. '/versions/3.11.9/bin/python')
 | 
					  -- - the debugger UI is also automatically opened when starting/stopping the debugger
 | 
				
			||||||
  end,
 | 
					  -- - toggle debugger UI manually with `<leader>du`
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    'rcarriga/nvim-dap-ui',
 | 
				
			||||||
 | 
					    dependencies = { 'mfussenegger/nvim-dap', 'nvim-neotest/nvim-nio' },
 | 
				
			||||||
 | 
					    keys = {
 | 
				
			||||||
 | 
					      {
 | 
				
			||||||
 | 
					        '<leader>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
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue