-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathdebugger_runner.lua
311 lines (287 loc) · 10 KB
/
debugger_runner.lua
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
local lazy = require("flutter-tools.lazy")
local ui = lazy.require("flutter-tools.ui") ---@module "flutter-tools.ui"
local dev_tools = lazy.require("flutter-tools.dev_tools") ---@module "flutter-tools.dev_tools"
local config = lazy.require("flutter-tools.config") ---@module "flutter-tools.config"
local utils = lazy.require("flutter-tools.utils") ---@module "flutter-tools.utils"
local path = lazy.require("flutter-tools.utils.path") ---@module "flutter-tools.utils.path"
local vm_service_extensions = lazy.require("flutter-tools.runners.vm_service_extensions") ---@module "flutter-tools.runners.vm_service_extensions"
local success, dap = pcall(require, "dap")
if not success then
ui.notify(string.format("nvim-dap is not installed!\n%s", dap), ui.ERROR)
return
end
local fmt = string.format
---@type flutter.Runner
local DebuggerRunner = {}
local plugin_identifier = "flutter-tools"
local command_requests = {
restart = "hotRestart",
reload = "hotReload",
quit = "terminate",
}
function DebuggerRunner:is_running() return dap.session() ~= nil end
---@param paths table<string, string>
---@param is_flutter_project boolean
local function register_debug_adapter(paths, is_flutter_project)
if is_flutter_project then
dap.adapters.dart = {
type = "executable",
command = paths.flutter_bin,
args = { "debug-adapter" },
}
if path.is_windows then
-- https://github.com/mfussenegger/nvim-dap/wiki/Debug-Adapter-installation#dart
-- add this if on windows, otherwise server won't open successfully
dap.adapters.dart.options = {
detached = false,
}
end
local repl = require("dap.repl")
repl.commands = vim.tbl_extend("force", repl.commands, {
custom_commands = {
[".hot-reload"] = function() dap.session():request("hotReload") end,
[".hot-restart"] = function() dap.session():request("hotRestart") end,
},
})
else
dap.adapters.dart = {
type = "executable",
command = paths.dart_bin,
args = { "debug_adapter" },
}
end
end
---@param paths table<string, string>
---@param is_flutter_project boolean
---@param project_config flutter.ProjectConfig?
local function register_default_configurations(paths, is_flutter_project, project_config)
local program
if is_flutter_project then
if project_config and project_config.target then
program = project_config.target
else
program = "lib/main.dart"
end
require("dap").configurations.dart = {
{
type = "dart",
request = "launch",
name = "Launch flutter",
dartSdkPath = paths.dart_sdk,
flutterSdkPath = paths.flutter_sdk,
program = program,
},
{
type = "dart",
request = "attach",
name = "Connect flutter",
dartSdkPath = paths.dart_sdk,
flutterSdkPath = paths.flutter_sdk,
program = program,
},
}
else
if project_config and project_config.target then
program = project_config.target
else
local root_dir_name = vim.fn.fnamemodify(vim.fn.getcwd(), ":t")
program = path.join("bin", root_dir_name .. ".dart")
end
require("dap").configurations.dart = {
{
type = "dart",
request = "launch",
name = "Launch dart",
dartSdkPath = paths.dart_sdk,
program = program,
},
}
end
end
local function register_dap_listeners(on_run_data, on_run_exit)
local started = false
local before_start_logs = {}
dap.listeners.after["event_output"][plugin_identifier] = function(_, body)
on_run_data(started, before_start_logs, body)
end
local handle_termination = function()
if next(before_start_logs) ~= nil then on_run_exit(before_start_logs) end
end
dap.listeners.before["event_exited"][plugin_identifier] = function(_, _) handle_termination() end
dap.listeners.before["event_terminated"][plugin_identifier] = function(_, _) handle_termination() end
dap.listeners.before["event_app.started"][plugin_identifier] = function(_, _)
started = true
before_start_logs = {}
utils.emit_event(utils.events.APP_STARTED)
end
dap.listeners.before["event_dart.debuggerUris"][plugin_identifier] = function(_, body)
if body and body.vmServiceUri then dev_tools.register_profiler_url(body.vmServiceUri) end
end
dap.listeners.before["event_dart.serviceExtensionAdded"][plugin_identifier] = function(_, body)
if body and body.extensionRPC and body.isolateId then
vm_service_extensions.set_isolate_id(body.extensionRPC, body.isolateId)
end
end
dap.listeners.before["event_flutter.serviceExtensionStateChanged"][plugin_identifier] = function(
_,
body
)
if body and body.extension and body.value then
vm_service_extensions.set_service_extensions_state(body.extension, body.value)
end
end
end
function DebuggerRunner:run(
opts,
paths,
args,
cwd,
on_run_data,
on_run_exit,
is_flutter_project,
project_config,
last_launch_config
)
vm_service_extensions.reset()
---@type dap.Configuration
local selected_launch_config = nil
register_dap_listeners(
function(started, before_start_logs, body)
if body and body.output then
for line in body.output:gmatch("[^\r\n]+") do
if not started then table.insert(before_start_logs, line) end
on_run_data(body.category == "sterr", line)
end
end
end,
function(before_start_logs)
on_run_exit(before_start_logs, args, opts, project_config, selected_launch_config)
end
)
register_debug_adapter(paths, is_flutter_project)
local launch_configurations = {}
local launch_configuration_count = 0
if last_launch_config then
dap.run(last_launch_config)
return
else
register_default_configurations(paths, is_flutter_project, project_config)
if config.debugger.register_configurations then
config.debugger.register_configurations(paths)
end
local all_configurations = require("dap").configurations.dart
if not all_configurations then
ui.notify("No launch configuration for DAP found", ui.ERROR)
return
end
for _, c in ipairs(all_configurations) do
if c.request == "launch" then
table.insert(launch_configurations, c)
launch_configuration_count = launch_configuration_count + 1
end
end
end
if launch_configuration_count == 0 then
ui.notify("No launch configuration for DAP found", ui.ERROR)
return
else
require("dap.ui").pick_if_many(
launch_configurations,
"Select launch configuration",
function(item)
return fmt("%s : %s | %s", item.name, item.program or item.cwd, vim.inspect(item.args))
end,
function(launch_config)
if not launch_config then return end
launch_config = vim.deepcopy(launch_config)
if not launch_config.cwd then launch_config.cwd = cwd end
launch_config.args = vim.list_extend(launch_config.args or {}, args or {})
launch_config.dartSdkPath = paths.dart_sdk
launch_config.flutterSdkPath = paths.flutter_sdk
if config.debugger.evaluate_to_string_in_debug_views then
launch_config.evaluateToStringInDebugViews = true
end
selected_launch_config = launch_config
dap.run(launch_config)
end
)
end
end
function DebuggerRunner:attach(paths, args, cwd, on_run_data, on_run_exit)
vm_service_extensions.reset()
register_dap_listeners(function(started, before_start_logs, body)
if body and body.output then
for line in body.output:gmatch("[^\r\n]+") do
if not started then table.insert(before_start_logs, line) end
on_run_data(body.category == "sterr", line)
end
end
end, function(before_start_logs) on_run_exit(before_start_logs, args) end)
register_debug_adapter(paths, true)
local launch_configurations = {}
local launch_configuration_count = 0
register_default_configurations(paths, true)
if config.debugger.register_configurations then config.debugger.register_configurations(paths) end
local all_configurations = require("dap").configurations.dart
if not all_configurations then
ui.notify("No launch configuration for DAP found", ui.ERROR)
return
end
for _, c in ipairs(all_configurations) do
if c.request == "attach" then
table.insert(launch_configurations, c)
launch_configuration_count = launch_configuration_count + 1
end
end
if launch_configuration_count == 0 then
ui.notify("No launch configuration for DAP found", ui.ERROR)
return
else
require("dap.ui").pick_if_many(
launch_configurations,
"Select launch configuration",
function(item)
return fmt("%s : %s | %s", item.name, item.program or item.cwd, vim.inspect(item.args))
end,
function(launch_config)
if not launch_config then return end
launch_config = vim.deepcopy(launch_config)
if not launch_config.cwd then launch_config.cwd = cwd end
launch_config.args = vim.list_extend(launch_config.args or {}, args or {})
launch_config.dartSdkPath = paths.dart_sdk
launch_config.flutterSdkPath = paths.flutter_sdk
if config.debugger.evaluate_to_string_in_debug_views then
launch_config.evaluateToStringInDebugViews = true
end
dap.run(launch_config)
end
)
end
end
function DebuggerRunner:send(cmd, quiet)
if cmd == "open_dev_tools" then
dev_tools.open_dev_tools()
return
end
local request = command_requests[cmd]
if request ~= nil then
dap.session():request(request, nil, function() end)
return
end
local service_activation_params = vm_service_extensions.get_request_params(cmd)
if service_activation_params then
dap.session():request("callService", service_activation_params, function(err, _)
if err and not quiet then
ui.notify("Error calling service " .. cmd .. ": " .. err, ui.ERROR)
end
end)
return
end
if not quiet then
ui.notify("Command " .. cmd .. " is not yet implemented for DAP runner", ui.ERROR)
end
end
function DebuggerRunner:cleanup()
if dap.session() then dap.terminate() end
end
return DebuggerRunner