From bf0c7e74ec386d920b8e38051c6a039736d68a41 Mon Sep 17 00:00:00 2001 From: Janis Hutz Date: Thu, 9 Oct 2025 14:28:14 +0200 Subject: [PATCH] [DAP] Improve --- nvim/lua/plugins/testing/dap.lua | 49 +------------------ nvim/lua/plugins/testing/debuggers/gdb.lua | 46 +++++++++++++++++ nvim/lua/plugins/testing/debuggers/python.lua | 48 ++++++++++++++++++ 3 files changed, 96 insertions(+), 47 deletions(-) create mode 100644 nvim/lua/plugins/testing/debuggers/gdb.lua create mode 100644 nvim/lua/plugins/testing/debuggers/python.lua diff --git a/nvim/lua/plugins/testing/dap.lua b/nvim/lua/plugins/testing/dap.lua index b8e2d71..df87363 100755 --- a/nvim/lua/plugins/testing/dap.lua +++ b/nvim/lua/plugins/testing/dap.lua @@ -58,52 +58,7 @@ return { { text = "", texthl = "DapStopped", linehl = "DapStopped", numhl = "DapStopped" } ) - dap.configurations.python = { - { - type = "python", - request = "launch", - name = "Launch file", - - -- Options below are for debugpy, see https://github.com/microsoft/debugpy/wiki/Debug-configuration-settings for supported options - - program = "${file}", -- This configuration will launch the current file if used. - pythonPath = function() - local cwd = vim.fn.getcwd() - if vim.fn.executable(cwd .. "/venv/bin/python") == 1 then - return cwd .. "/venv/bin/python" - elseif vim.fn.executable(cwd .. "/.venv/bin/python") == 1 then - return cwd .. "/.venv/bin/python" - else - return "/usr/bin/python" - end - end, - }, - } - - dap.adapters.python = function(cb, config) - if config.request == "attach" then - ---@diagnostic disable-next-line: undefined-field - local port = (config.connect or config).port - ---@diagnostic disable-next-line: undefined-field - local host = (config.connect or config).host or "127.0.0.1" - cb({ - type = "server", - port = assert(port, "`connect.port` is required for a python `attach` configuration"), - host = host, - options = { - source_filetype = "python", - }, - }) - else - cb({ - type = "executable", - command = vim.fn.stdpath("data") .. "/mason/packages/debugpy/venv/bin/python", - args = { "-m", "debugpy.adapter" }, - options = { - source_filetype = "python", - }, - }) - end - end + require("plugins.testing.debuggers.python") + require("plugins.testing.debuggers.gdb") end, } diff --git a/nvim/lua/plugins/testing/debuggers/gdb.lua b/nvim/lua/plugins/testing/debuggers/gdb.lua new file mode 100644 index 0000000..3e38c9b --- /dev/null +++ b/nvim/lua/plugins/testing/debuggers/gdb.lua @@ -0,0 +1,46 @@ +local dap = require("dap") +dap.adapters.gdb = { + type = "executable", + command = "gdb", + args = { "--interpreter=dap", "--eval-command", "set print pretty on" }, +} + +dap.configurations.c = { + { + name = "Launch", + type = "gdb", + request = "launch", + program = function() + return vim.fn.input("Path to executable: ", vim.fn.getcwd() .. "/", "file") + end, + args = {}, -- provide arguments if needed + cwd = "${workspaceFolder}", + stopAtBeginningOfMainSubprogram = false, + }, + { + name = "Select and attach to process", + type = "gdb", + request = "attach", + program = function() + return vim.fn.input("Path to executable: ", vim.fn.getcwd() .. "/", "file") + end, + pid = function() + local name = vim.fn.input("Executable name (filter): ") + return require("dap.utils").pick_process({ filter = name }) + end, + cwd = "${workspaceFolder}", + }, + { + name = "Attach to gdbserver :1234", + type = "gdb", + request = "attach", + target = "localhost:1234", + program = function() + return vim.fn.input("Path to executable: ", vim.fn.getcwd() .. "/", "file") + end, + cwd = "${workspaceFolder}", + }, +} + +dap.configurations.cpp = dap.configurations.c +dap.configurations.rust = dap.configurations.c diff --git a/nvim/lua/plugins/testing/debuggers/python.lua b/nvim/lua/plugins/testing/debuggers/python.lua new file mode 100644 index 0000000..4bac9bc --- /dev/null +++ b/nvim/lua/plugins/testing/debuggers/python.lua @@ -0,0 +1,48 @@ +local dap = require("dap") +dap.configurations.python = { + { + type = "python", + request = "launch", + name = "Launch file", + + -- Options below are for debugpy, see https://github.com/microsoft/debugpy/wiki/Debug-configuration-settings for supported options + + program = "${file}", -- This configuration will launch the current file if used. + pythonPath = function() + local cwd = vim.fn.getcwd() + if vim.fn.executable(cwd .. "/venv/bin/python") == 1 then + return cwd .. "/venv/bin/python" + elseif vim.fn.executable(cwd .. "/.venv/bin/python") == 1 then + return cwd .. "/.venv/bin/python" + else + return "/usr/bin/python" + end + end, + }, +} + +dap.adapters.python = function(cb, config) + if config.request == "attach" then + ---@diagnostic disable-next-line: undefined-field + local port = (config.connect or config).port + ---@diagnostic disable-next-line: undefined-field + local host = (config.connect or config).host or "127.0.0.1" + cb({ + type = "server", + port = assert(port, "`connect.port` is required for a python `attach` configuration"), + host = host, + options = { + source_filetype = "python", + }, + }) + else + cb({ + type = "executable", + command = vim.fn.stdpath("data") .. "/mason/packages/debugpy/venv/bin/python", + args = { "-m", "debugpy.adapter" }, + options = { + source_filetype = "python", + }, + }) + end +end