diff --git a/lua/nvim-flutter-companion/init.lua b/lua/nvim-flutter-companion/init.lua index 01013f2..634fc29 100644 --- a/lua/nvim-flutter-companion/init.lua +++ b/lua/nvim-flutter-companion/init.lua @@ -180,6 +180,22 @@ local stop_spinner = function(set_inactive) vim.notify.dismiss() end +function M.flutter_clear(is_emulator) + local config_path = get_config_path() + local config_data = load_table(config_path) + if is_emulator then + if config_data.emulator ~= nil then + config_data.emulator = nil + save_table(config_data, config_path) + end + else + if config_data.device ~= nil then + config_data.device = nil + save_table(config_data, config_path) + end + end +end + function M.flutter_list(is_emulators) if flutter_busy == true then return @@ -264,31 +280,49 @@ function M.flutter_list(is_emulators) ) end +M.flutter_run = function(is_emulator) + local config_path = get_config_path() + local config_data = load_table(config_path) + if is_emulator then + if config_data.emulator == nil then + if callbacks[2] == nil then + callbacks[2] = function() + M.flutter_run(is_emulator) + end + M.flutter_list(true) + end + else + local job = + Job:new( + {command = get_flutter_command(), args = {"emulators", "--launch", config_data.emulator.id}} + ) + job:start() + end + else + if config_data.device == nil then + if callbacks[1] == nil then + callbacks[1] = function() + M.flutter_run(is_emulator) + end + M.flutter_list(false) + end + else + flutter_run_device(config_data.device.id) + end + end +end + M.setup = function(opts) opts = opts or {} config.use_coc = vim.F.if_nil(opts.use_coc, config.use_coc) end _G.Flutter_Companion_Clear_Selected_Device = function() - local config_path = get_config_path() - local config_data = load_table(config_path) - if config_data.device ~= nil then - config_data.device = nil - save_table(config_data, config_path) - end + M.flutter_clear(false) end _G.Flutter_Companion_Run_Selected_Device = function() - local config_path = get_config_path() - local config_data = load_table(config_path) - if config_data.device == nil then - if callbacks[1] == nil then - callbacks[1] = _G.Flutter_Companion_Run_Selected_Device - M.flutter_list(false) - end - else - flutter_run_device(config_data.device.id) - end + M.flutter_run(false) end _G.Flutter_Companion_Select_Device = function() @@ -296,29 +330,11 @@ _G.Flutter_Companion_Select_Device = function() end _G.Flutter_Companion_Clear_Selected_Emulator = function() - local config_path = get_config_path() - local config_data = load_table(config_path) - if config_data.emulator ~= nil then - config_data.emulator = nil - save_table(config_data, config_path) - end + M.flutter_clear(true) end _G.Flutter_Companion_Run_Selected_Emulator = function() - local config_path = get_config_path() - local config_data = load_table(config_path) - if config_data.emulator == nil then - if callbacks[2] == nil then - callbacks[2] = _G.Flutter_Companion_Run_Selected_Emulator - M.flutter_list(true) - end - else - local job = - Job:new( - {command = get_flutter_command(), args = {"emulators", "--launch", config_data.emulator.id}} - ) - job:start() - end + M.flutter_run(true) end _G.Flutter_Companion_Select_Emulator = function()