Skip to content

Commit

Permalink
fix: select device only once when starting (#424)
Browse files Browse the repository at this point in the history
  • Loading branch information
sidlatau authored Dec 28, 2024
1 parent 2b6a3c0 commit a526c30
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 38 deletions.
34 changes: 26 additions & 8 deletions lua/flutter-tools/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ local current_device = nil

---@class flutter.Runner
---@field is_running fun(runner: flutter.Runner):boolean
---@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?)
---@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?)
---@field cleanup fun(funner: flutter.Runner)
---@field send fun(runner: flutter.Runner, cmd:string, quiet: boolean?)

Expand Down Expand Up @@ -83,14 +83,19 @@ end
---@param result string[]
---@param cli_args string[]
---@param project_config flutter.ProjectConfig?
local function on_run_exit(result, cli_args, project_config)
---@param launch_config dap.Configuration?
local function on_run_exit(result, cli_args, project_config, launch_config)
local matched_error, msg = has_recoverable_error(result)
if matched_error then
local lines = devices.to_selection_entries(result)
ui.select({
title = ("Flutter run (%s)"):format(msg),
lines = lines,
on_select = function(device) devices.select_device(device, cli_args, project_config) end,
on_select = function(device)
vim.list_extend(cli_args, { "-d", device.id })
if launch_config then vim.list_extend(launch_config.args, { "-d", device.id }) end
M.run({ cli_args = cli_args }, project_config, launch_config)
end,
})
end
shutdown()
Expand Down Expand Up @@ -245,7 +250,8 @@ end

---@param opts RunOpts
---@param project_conf flutter.ProjectConfig?
local function run(opts, project_conf)
---@param launch_config dap.Configuration?
local function run(opts, project_conf, launch_config)
opts = opts or {}
executable.get(function(paths)
local args = opts.cli_args or get_run_args(opts, project_conf)
Expand All @@ -272,19 +278,31 @@ local function run(opts, project_conf)
ui.notify("Starting dart project...")
end
runner = use_debugger_runner(opts.force_debug) and debugger_runner or job_runner
runner:run(paths, args, cwd, on_run_data, on_run_exit, is_flutter_project, project_conf)
runner:run(
paths,
args,
cwd,
on_run_data,
on_run_exit,
is_flutter_project,
project_conf,
launch_config
)
end)
end

---Run the flutter application
---@param opts RunOpts
---@param project_conf flutter.ProjectConfig?
function M.run(opts, project_conf)
---@param launch_config dap.Configuration?
function M.run(opts, project_conf, launch_config)
if M.is_running() then return ui.notify("Flutter is already running!") end
if project_conf then
run(opts, project_conf)
run(opts, project_conf, launch_config)
else
select_project_config(function(selected_project_conf) run(opts, selected_project_conf) end)
select_project_config(
function(selected_project_conf) run(opts, selected_project_conf, launch_config) end
)
end
end

Expand Down
19 changes: 2 additions & 17 deletions lua/flutter-tools/devices.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,6 @@ function M.to_selection_entries(result, device_type)
end, devices)
end

---@param project_config flutter.ProjectConfig?
function M.select_device(device, args, project_config)
if not device then return ui.notify("Sorry there is no device on this line") end
if device.type == EMULATOR then
M.launch_emulator(device)
else
if args then
vim.list_extend(args, { "-d", device.id })
commands.run({ cli_args = args }, project_config)
else
commands.run({ device = device }, project_config)
end
end
end

-----------------------------------------------------------------------------//
-- Emulators
-----------------------------------------------------------------------------//
Expand Down Expand Up @@ -120,7 +105,7 @@ local function show_emulators(result)
ui.select({
title = "Flutter emulators",
lines = lines,
on_select = M.select_device,
on_select = function(emulator) M.launch_emulator(emulator) end,
})
end
end
Expand Down Expand Up @@ -148,7 +133,7 @@ local function show_devices(job)
ui.select({
title = "Flutter devices",
lines = lines,
on_select = M.select_device,
on_select = function(device) commands.run({ device = device }) end,
})
end
end
Expand Down
38 changes: 26 additions & 12 deletions lua/flutter-tools/runners/debugger_runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,12 @@ function DebuggerRunner:run(
on_run_data,
on_run_exit,
is_flutter_project,
project_config
project_config,
last_launch_config
)
---@type dap.Configuration
local selected_launch_config = nil

local started = false
local before_start_logs = {}
vm_service_extensions.reset()
Expand All @@ -124,7 +128,9 @@ function DebuggerRunner:run(
end

local handle_termination = function()
if next(before_start_logs) ~= nil then on_run_exit(before_start_logs, args, project_config) end
if next(before_start_logs) ~= nil then
on_run_exit(before_start_logs, args, project_config, selected_launch_config)
end
end

dap.listeners.before["event_exited"][plugin_identifier] = function(_, _) handle_termination() end
Expand Down Expand Up @@ -158,17 +164,24 @@ function DebuggerRunner:run(
register_debug_adapter(paths, is_flutter_project)
local launch_configurations = {}
local launch_configuration_count = 0
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)
if last_launch_config then
dap.run(last_launch_config)
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
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

Expand All @@ -192,6 +205,7 @@ function DebuggerRunner:run(
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
)
Expand Down
2 changes: 1 addition & 1 deletion lua/flutter-tools/runners/job_runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function JobRunner:run(
dev_tools.handle_log(data)
end),
on_stderr = vim.schedule_wrap(function(_, data, _) on_run_data(true, data) end),
on_exit = vim.schedule_wrap(function(j, _) on_run_exit(j:result(), args) end),
on_exit = vim.schedule_wrap(function(j, _) on_run_exit(j:result(), args, project_config) end),
})
run_job:start()
end
Expand Down

1 comment on commit a526c30

@Kiruel
Copy link

@Kiruel Kiruel commented on a526c30 Jan 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this one !!

Please sign in to comment.