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
|
return {
{
"mfussenegger/nvim-dap",
config = function()
local dap = require("dap")
dap.adapters.go = {
type = "server",
port = "${port}",
executable = {
command = "dlv",
args = { "dap", "-l", "127.0.0.1:${port}" },
},
}
-- Хранение истории аргументов для каждой точки входа
local _args_history = {}
-- Функция для запроса аргументов у пользователя с историей (возвращает строку для парсинга)
local function get_args(entry_point)
local cache_key = entry_point
local default = _args_history[cache_key] or ""
local prompt = "Arguments for " .. entry_point .. ": "
local input = vim.fn.input(prompt, default)
if input and #input > 0 then
_args_history[cache_key] = input
end
return _args_history[cache_key]
end
-- Генерация конфигураций для Go на основе точек входа
local function generate_go_configs()
local workspace_folder = vim.fn.getcwd()
-- Проверка существования .env файла в корне проекта
local env_file = nil
if vim.fn.filereadable(workspace_folder .. "/.env") == 1 then
env_file = "${workspaceFolder}/.env"
end
local configs = {}
local entry_points = {}
-- Поиск точки входа: main.go в корне или cmd/*/main.go
if vim.fn.filereadable(workspace_folder .. "/main.go") == 1 then
table.insert(entry_points, {
name = "main",
path = "${workspaceFolder}",
})
end
-- Поиск cmd/*/main.go
local cmd_dirs = vim.fn.glob(workspace_folder .. "/cmd/*", 0, 1)
for _, cmd_dir in ipairs(cmd_dirs) do
local main_path = cmd_dir .. "/main.go"
if vim.fn.filereadable(main_path) == 1 then
local entry_name = vim.fn.fnamemodify(cmd_dir, ":t")
table.insert(entry_points, {
name = entry_name,
path = cmd_dir,
})
end
end
-- Генерация конфигураций для каждой точки входа
for _, entry in ipairs(entry_points) do
-- Базовая конфигурация без аргументов
table.insert(configs, {
type = "go",
name = "Debug " .. entry.name,
request = "launch",
program = entry.path,
cwd = "${workspaceFolder}",
envFile = env_file,
buildFlags = "",
outputMode = "remote",
})
-- Конфигурация с аргументами (функция для запроса при запуске)
table.insert(configs, {
type = "go",
name = "Debug " .. entry.name .. " (args)",
request = "launch",
program = entry.path,
cwd = "${workspaceFolder}",
envFile = env_file,
buildFlags = "",
outputMode = "remote",
args = function()
local args_str = get_args(entry.name)
-- Разбиваем строку аргументов на массив
local args = {}
for arg in args_str:gmatch("%S+") do
table.insert(args, arg)
end
return args
end,
})
end
-- Remote attach конфигурация для подключения к уже запущенному delve
table.insert(configs, {
type = "go",
name = "Remote attach (127.0.0.1:43000)",
request = "attach",
mode = "remote",
port = "43000",
host = "127.0.0.1",
})
return configs
end
-- Установка конфигураций Go
dap.configurations.go = generate_go_configs()
-- Автообновление конфигураций при открытии Go файла
vim.api.nvim_create_autocmd("BufReadPost", {
pattern = "*.go",
callback = function()
dap.configurations.go = generate_go_configs()
end,
})
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",
},
{
"<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",
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",
-- Не используем автоconfig - конфигурации управляются в nvim-dap
keys = {
{
"<leader>dt",
function()
require("dap-go").debug_test()
end,
desc = "Debug test",
},
},
},
}
|