Skip to content

Commit a526c30

Browse files
authoredDec 28, 2024
fix: select device only once when starting (#424)
1 parent 2b6a3c0 commit a526c30

File tree

4 files changed

+55
-38
lines changed

4 files changed

+55
-38
lines changed
 

‎lua/flutter-tools/commands.lua

+26-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ local current_device = nil
2222

2323
---@class flutter.Runner
2424
---@field is_running fun(runner: flutter.Runner):boolean
25-
---@field run fun(runner: flutter.Runner, paths:table, args:table, cwd:string, on_run_data:fun(is_err:boolean, data:string), on_run_exit:fun(data:string[], args: table, project_conf: flutter.ProjectConfig?), is_flutter_project: boolean, project_conf: flutter.ProjectConfig?)
25+
---@field run fun(runner: flutter.Runner, paths:table, args:table, cwd:string, on_run_data:fun(is_err:boolean, data:string), on_run_exit:fun(data:string[], args: table, project_conf: flutter.ProjectConfig?,launch_config: dap.Configuration?), is_flutter_project: boolean, project_conf: flutter.ProjectConfig?, launch_config: dap.Configuration?)
2626
---@field cleanup fun(funner: flutter.Runner)
2727
---@field send fun(runner: flutter.Runner, cmd:string, quiet: boolean?)
2828

@@ -83,14 +83,19 @@ end
8383
---@param result string[]
8484
---@param cli_args string[]
8585
---@param project_config flutter.ProjectConfig?
86-
local function on_run_exit(result, cli_args, project_config)
86+
---@param launch_config dap.Configuration?
87+
local function on_run_exit(result, cli_args, project_config, launch_config)
8788
local matched_error, msg = has_recoverable_error(result)
8889
if matched_error then
8990
local lines = devices.to_selection_entries(result)
9091
ui.select({
9192
title = ("Flutter run (%s)"):format(msg),
9293
lines = lines,
93-
on_select = function(device) devices.select_device(device, cli_args, project_config) end,
94+
on_select = function(device)
95+
vim.list_extend(cli_args, { "-d", device.id })
96+
if launch_config then vim.list_extend(launch_config.args, { "-d", device.id }) end
97+
M.run({ cli_args = cli_args }, project_config, launch_config)
98+
end,
9499
})
95100
end
96101
shutdown()
@@ -245,7 +250,8 @@ end
245250

246251
---@param opts RunOpts
247252
---@param project_conf flutter.ProjectConfig?
248-
local function run(opts, project_conf)
253+
---@param launch_config dap.Configuration?
254+
local function run(opts, project_conf, launch_config)
249255
opts = opts or {}
250256
executable.get(function(paths)
251257
local args = opts.cli_args or get_run_args(opts, project_conf)
@@ -272,19 +278,31 @@ local function run(opts, project_conf)
272278
ui.notify("Starting dart project...")
273279
end
274280
runner = use_debugger_runner(opts.force_debug) and debugger_runner or job_runner
275-
runner:run(paths, args, cwd, on_run_data, on_run_exit, is_flutter_project, project_conf)
281+
runner:run(
282+
paths,
283+
args,
284+
cwd,
285+
on_run_data,
286+
on_run_exit,
287+
is_flutter_project,
288+
project_conf,
289+
launch_config
290+
)
276291
end)
277292
end
278293

279294
---Run the flutter application
280295
---@param opts RunOpts
281296
---@param project_conf flutter.ProjectConfig?
282-
function M.run(opts, project_conf)
297+
---@param launch_config dap.Configuration?
298+
function M.run(opts, project_conf, launch_config)
283299
if M.is_running() then return ui.notify("Flutter is already running!") end
284300
if project_conf then
285-
run(opts, project_conf)
301+
run(opts, project_conf, launch_config)
286302
else
287-
select_project_config(function(selected_project_conf) run(opts, selected_project_conf) end)
303+
select_project_config(
304+
function(selected_project_conf) run(opts, selected_project_conf, launch_config) end
305+
)
288306
end
289307
end
290308

‎lua/flutter-tools/devices.lua

+2-17
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,6 @@ function M.to_selection_entries(result, device_type)
7575
end, devices)
7676
end
7777

78-
---@param project_config flutter.ProjectConfig?
79-
function M.select_device(device, args, project_config)
80-
if not device then return ui.notify("Sorry there is no device on this line") end
81-
if device.type == EMULATOR then
82-
M.launch_emulator(device)
83-
else
84-
if args then
85-
vim.list_extend(args, { "-d", device.id })
86-
commands.run({ cli_args = args }, project_config)
87-
else
88-
commands.run({ device = device }, project_config)
89-
end
90-
end
91-
end
92-
9378
-----------------------------------------------------------------------------//
9479
-- Emulators
9580
-----------------------------------------------------------------------------//
@@ -120,7 +105,7 @@ local function show_emulators(result)
120105
ui.select({
121106
title = "Flutter emulators",
122107
lines = lines,
123-
on_select = M.select_device,
108+
on_select = function(emulator) M.launch_emulator(emulator) end,
124109
})
125110
end
126111
end
@@ -148,7 +133,7 @@ local function show_devices(job)
148133
ui.select({
149134
title = "Flutter devices",
150135
lines = lines,
151-
on_select = M.select_device,
136+
on_select = function(device) commands.run({ device = device }) end,
152137
})
153138
end
154139
end

‎lua/flutter-tools/runners/debugger_runner.lua

+26-12
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,12 @@ function DebuggerRunner:run(
109109
on_run_data,
110110
on_run_exit,
111111
is_flutter_project,
112-
project_config
112+
project_config,
113+
last_launch_config
113114
)
115+
---@type dap.Configuration
116+
local selected_launch_config = nil
117+
114118
local started = false
115119
local before_start_logs = {}
116120
vm_service_extensions.reset()
@@ -124,7 +128,9 @@ function DebuggerRunner:run(
124128
end
125129

126130
local handle_termination = function()
127-
if next(before_start_logs) ~= nil then on_run_exit(before_start_logs, args, project_config) end
131+
if next(before_start_logs) ~= nil then
132+
on_run_exit(before_start_logs, args, project_config, selected_launch_config)
133+
end
128134
end
129135

130136
dap.listeners.before["event_exited"][plugin_identifier] = function(_, _) handle_termination() end
@@ -158,17 +164,24 @@ function DebuggerRunner:run(
158164
register_debug_adapter(paths, is_flutter_project)
159165
local launch_configurations = {}
160166
local launch_configuration_count = 0
161-
register_default_configurations(paths, is_flutter_project, project_config)
162-
if config.debugger.register_configurations then config.debugger.register_configurations(paths) end
163-
local all_configurations = require("dap").configurations.dart
164-
if not all_configurations then
165-
ui.notify("No launch configuration for DAP found", ui.ERROR)
167+
if last_launch_config then
168+
dap.run(last_launch_config)
166169
return
167-
end
168-
for _, c in ipairs(all_configurations) do
169-
if c.request == "launch" then
170-
table.insert(launch_configurations, c)
171-
launch_configuration_count = launch_configuration_count + 1
170+
else
171+
register_default_configurations(paths, is_flutter_project, project_config)
172+
if config.debugger.register_configurations then
173+
config.debugger.register_configurations(paths)
174+
end
175+
local all_configurations = require("dap").configurations.dart
176+
if not all_configurations then
177+
ui.notify("No launch configuration for DAP found", ui.ERROR)
178+
return
179+
end
180+
for _, c in ipairs(all_configurations) do
181+
if c.request == "launch" then
182+
table.insert(launch_configurations, c)
183+
launch_configuration_count = launch_configuration_count + 1
184+
end
172185
end
173186
end
174187

@@ -192,6 +205,7 @@ function DebuggerRunner:run(
192205
if config.debugger.evaluate_to_string_in_debug_views then
193206
launch_config.evaluateToStringInDebugViews = true
194207
end
208+
selected_launch_config = launch_config
195209
dap.run(launch_config)
196210
end
197211
)

‎lua/flutter-tools/runners/job_runner.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function JobRunner:run(
5353
dev_tools.handle_log(data)
5454
end),
5555
on_stderr = vim.schedule_wrap(function(_, data, _) on_run_data(true, data) end),
56-
on_exit = vim.schedule_wrap(function(j, _) on_run_exit(j:result(), args) end),
56+
on_exit = vim.schedule_wrap(function(j, _) on_run_exit(j:result(), args, project_config) end),
5757
})
5858
run_job:start()
5959
end

0 commit comments

Comments
 (0)