diff --git a/lua/flutter-tools/commands.lua b/lua/flutter-tools/commands.lua index a08741b..14476fc 100644 --- a/lua/flutter-tools/commands.lua +++ b/lua/flutter-tools/commands.lua @@ -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?) @@ -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() @@ -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) @@ -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 diff --git a/lua/flutter-tools/devices.lua b/lua/flutter-tools/devices.lua index 2b8a6fb..8f11d78 100644 --- a/lua/flutter-tools/devices.lua +++ b/lua/flutter-tools/devices.lua @@ -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 -----------------------------------------------------------------------------// @@ -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 @@ -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 diff --git a/lua/flutter-tools/runners/debugger_runner.lua b/lua/flutter-tools/runners/debugger_runner.lua index 5b0ec55..8f10ccf 100644 --- a/lua/flutter-tools/runners/debugger_runner.lua +++ b/lua/flutter-tools/runners/debugger_runner.lua @@ -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() @@ -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 @@ -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 @@ -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 ) diff --git a/lua/flutter-tools/runners/job_runner.lua b/lua/flutter-tools/runners/job_runner.lua index 37d4e27..b3a5d5a 100644 --- a/lua/flutter-tools/runners/job_runner.lua +++ b/lua/flutter-tools/runners/job_runner.lua @@ -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