1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
return {
{
"mfussenegger/nvim-dap",
event = "VeryLazy",
dependencies = {
"leoluz/nvim-dap-go",
},
config = function()
local dap = require("dap")
-- Find project root (where go.mod is)
local function find_project_root()
local current = vim.fn.getcwd()
local function find_go_mod(dir)
if vim.fn.filereadable(dir .. "/go.mod") == 1 then
return dir
end
local parent = vim.fn.fnamemodify(dir, ":h")
if parent == dir or parent == "" then
return nil
end
return find_go_mod(parent)
end
return find_go_mod(current)
end
-- Find all main.go files in project
local function find_main_files(root)
-- Use vim glob to find main.go files (excludes vendor)
local pattern = root .. "/**/main.go"
local files = vim.fn.glob(pattern, true, true)
-- Filter out vendor
local main_files = {}
for _, f in ipairs(files) do
if not string.match(f, "/vendor/") then
table.insert(main_files, f)
end
end
return main_files
end
-- Store last args per main file
local last_args = {}
-- Custom debug function with Telescope picker
local function debug_go_custom()
local root = find_project_root()
if not root then
vim.notify("Could not find project root (go.mod)", vim.log.levels.ERROR)
return
end
local main_files = find_main_files(root)
if #main_files == 0 then
vim.notify("No main.go files found", vim.log.levels.WARN)
return
end
-- Use Telescope to pick
local telescope = require("telescope")
local actions = require("telescope.actions")
local pickers = require("telescope.pickers")
local finders = require("telescope.finders")
local conf = require("telescope.config").values
-- Format: relative path + last args
local entries = {}
for _, f in ipairs(main_files) do
local rel = vim.fn.fnamemodify(f, ":.")
local args = last_args[f] or ""
table.insert(entries, { path = f, display = rel .. (args ~= "" and (" [" .. args .. "]") or "") })
end
pickers.new({}, {
prompt_title = "Select main.go",
finder = finders.new_table({
results = entries,
entry_maker = function(entry)
return {
value = entry.path,
display = entry.display,
ordinal = entry.display,
}
end,
}),
sorter = conf.generic_sorter({}),
attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function()
local selection = require("telescope.actions.state").get_selected_entry(prompt_bufnr)
local main_path = selection.value
-- Get last args or empty
local default_args = last_args[main_path] or ""
vim.api.nvim_buf_delete(prompt_bufnr, { force = true })
-- Ask for args
vim.ui.input({
prompt = "Args: ",
default = default_args,
}, function(input)
if input ~= nil then
-- Save args
last_args[main_path] = input
-- Parse args into table
local args_tbl = {}
if input ~= "" then
for w in string.gmatch(input, "[^%s]+") do
table.insert(args_tbl, w)
end
end
-- Build config
local config = {
type = "go",
name = "Debug: " .. vim.fn.fnamemodify(main_path, ":t"),
request = "launch",
program = main_path,
cwd = root,
outputMode = "remote",
args = args_tbl,
}
-- Check if already running
require("dap-go").setup() -- Ensure Go adapter is registered
if dap.session() then
dap.terminate(nil, nil, function()
dap.run(config)
end)
else
dap.run(config)
end
end
end)
end)
return true
end,
}):find()
end
-- Go adapter configuration
dap.configurations.go = {
{
type = "go",
name = "Debug",
request = "launch",
program = "${file}",
outputMode = "remote",
},
{
type = "go",
name = "Debug test",
request = "launch",
mode = "test",
program = "${file}",
outputMode = "remote",
},
{
type = "go",
name = "Debug test (package)",
request = "launch",
mode = "test",
program = "${workspaceFolder}",
outputMode = "remote",
},
}
-- Register custom command
vim.api.nvim_create_user_command("DapGoCustom", debug_go_custom, {})
end,
keys = {
{
"<F5>",
function()
require("dap").continue()
end,
desc = "Debug continue",
},
{
"<F17>",
function() -- S-F5
require("dap").restart()
end,
desc = "Debug restart",
},
{
"<F29>",
function() -- C-F5
require("dap").terminate()
end,
desc = "Debug terminate",
},
{
"<F8>",
function()
require("dap").step_over()
end,
desc = "Debug step over",
},
{
"<F7>",
function()
require("dap").step_into()
end,
desc = "Debug step into",
},
{
"<F19>",
function() -- S-F7
require("dap").step_out()
end,
desc = "Debug step out",
},
{
"<leader>dd",
function()
vim.cmd("DapGoCustom")
end,
desc = "Debug Go (custom)",
},
{
"<A-b>",
function()
require("dap").toggle_breakpoint()
end,
desc = "Toggle breakpoint",
},
{
"<F9>",
function()
local widgets = require("dap.ui.widgets")
widgets.centered_float(widgets.scopes)
end,
desc = "Debug scopes",
},
},
},
{
"rcarriga/nvim-dap-ui",
event = "VeryLazy",
dependencies = {
"mfussenegger/nvim-dap",
"nvim-neotest/nvim-nio",
},
config = function()
local dapui = require("dapui")
dapui.setup({
icons = {
expanded = "▼",
collapsed = "▶",
current_frame = "→",
},
controls = {
enabled = false,
},
mappings = {
open = "o",
remove = "d",
edit = "e",
repl = "r",
toggle = "t",
},
expand_lines = vim.fn.has("nvim-0.7"),
layouts = {
{
elements = {
"repl",
"scopes",
},
size = 0.3,
position = "bottom",
},
},
floating = {
max_height = nil,
max_width = nil,
border = "single",
mappings = {
close = { "q", "<Esc>" },
},
},
windows = { indent = 1 },
render = {
max_type_length = nil,
},
})
local dap = require("dap")
dap.listeners.after.event_initialized["dapui_config"] = function()
dapui.open()
end
end,
keys = {
{
"<F6>",
function()
require("dapui").toggle()
end,
desc = "Toggle debug UI",
},
{
"<Leader>dh",
function()
require("dap.ui.widgets").hover()
end,
desc = "Debug hover",
},
{
"<Leader>dp",
function()
require("dap.ui.widgets").preview()
end,
desc = "Debug preview",
},
},
},
{
"leoluz/nvim-dap-go",
dependencies = { "mfussenegger/nvim-dap" },
ft = "go",
keys = {
{
"<leader>dt",
function()
require("dap-go").debug_test()
end,
desc = "Debug test",
},
},
},
}
|