added go dap

This commit is contained in:
Radu Macocian (admac)
2026-06-26 10:29:35 +02:00
parent c6739ea0da
commit 287defca42
2 changed files with 123 additions and 1 deletions
+115
View File
@@ -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