From 287defca424d53655116e6f37356dbde040076f5 Mon Sep 17 00:00:00 2001 From: "Radu Macocian (admac)" Date: Fri, 26 Jun 2026 10:29:35 +0200 Subject: [PATCH] added go dap --- .config/nvim/after/plugin/dap.lua | 9 ++- .config/nvim/lua/dap/dap-go.lua | 115 ++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 .config/nvim/lua/dap/dap-go.lua diff --git a/.config/nvim/after/plugin/dap.lua b/.config/nvim/after/plugin/dap.lua index 48bc41e..25f3420 100644 --- a/.config/nvim/after/plugin/dap.lua +++ b/.config/nvim/after/plugin/dap.lua @@ -10,6 +10,7 @@ dap.listeners.after.event_exited["dapui_config"] = function() dapui.close() end local dotnet = require("dap.dap-dotnet") local odoo = require("dap.dap-odoo") local haskell = require("dap.dap-haskell") +local go = require("dap.dap-go") vim.keymap.set("n", "5", function() @@ -39,7 +40,13 @@ vim.keymap.set("n", "b", function() dap.toggle_breakpoint() end) vim.keymap.set("n", "B", function() dap.set_breakpoint(vim.fn.input("Breakpoint condition: ")) end) vim.keymap.set("n", "qd", function() dap.terminate() end) vim.keymap.set("n", "rd", function() dap.restart() end) -vim.keymap.set("n", "dt", odoo.run_odoo_test_at_cursor, { desc = "Debug Odoo Test unde Cursor" }) +vim.keymap.set("n", "dt", function() + if vim.bo.filetype == "go" then + go.debug_test_at_cursor() + else + odoo.run_odoo_test_at_cursor() + end +end, { desc = "Debug Test under Cursor" }) dap.configurations.python = { { diff --git a/.config/nvim/lua/dap/dap-go.lua b/.config/nvim/lua/dap/dap-go.lua new file mode 100644 index 0000000..80c0613 --- /dev/null +++ b/.config/nvim/lua/dap/dap-go.lua @@ -0,0 +1,115 @@ +local dap = require("dap") + +local M = {} + +-- Locate the delve binary (prefer mason, fall back to PATH). +local function dlv_command() + local mason = vim.fn.expand("$HOME/.local/share/nvim/mason/bin/dlv") + if vim.fn.filereadable(mason) == 1 then + return mason + end + return "dlv" +end + +-- Adapter: run delve in DAP server mode on a free port. +dap.adapters.go = { + type = "server", + port = "${port}", + executable = { + command = dlv_command(), + args = { "dap", "-l", "127.0.0.1:${port}" }, + }, +} + +-- Configurations +dap.configurations.go = { + { + type = "go", + request = "launch", + name = "Debug (current file)", + program = "${file}", + }, + { + type = "go", + request = "launch", + name = "Debug package", + program = "${fileDirname}", + }, + { + type = "go", + request = "launch", + name = "Debug package (with args)", + program = "${fileDirname}", + args = function() + local input = vim.fn.input("Args: ") + return vim.split(input, " ", { trimempty = true }) + end, + }, + { + type = "go", + request = "launch", + mode = "test", + name = "Debug test (current file)", + program = "${file}", + }, + { + type = "go", + request = "launch", + mode = "test", + name = "Debug test (package)", + program = "${fileDirname}", + }, + { + type = "go", + request = "attach", + mode = "local", + name = "Attach to process", + processId = require("dap.utils").pick_process, + }, +} + +-- Debug the test function under the cursor using delve's -test.run filter. +local function debug_test_at_cursor() + local ok, node = pcall(vim.treesitter.get_node) + if not ok or not node then + vim.notify("Tree-sitter parser not found. Ensure you are in a Go buffer.", vim.log.levels.ERROR) + return + end + + local func_name = nil + local attempts = 0 + while node do + if node:type() == "function_declaration" then + for child in node:iter_children() do + if child:type() == "identifier" then + func_name = vim.treesitter.get_node_text(child, 0) + break + end + end + break + end + node = node:parent() + attempts = attempts + 1 + if attempts > 50 then + break + end + end + + if not func_name then + vim.notify("Cursor is not inside a test function.", vim.log.levels.WARN) + return + end + + dap.run({ + type = "go", + request = "launch", + mode = "test", + name = "Debug test: " .. func_name, + program = "${fileDirname}", + args = { "-test.run", "^" .. func_name .. "$" }, + }) +end + +M.debug_test_at_cursor = debug_test_at_cursor + +return M