From 6f3818cc420af1189aafd3e4e85e239fe909681d Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Fri, 29 Jul 2022 11:04:37 -0500 Subject: [PATCH] support coc-flutter/coc-flutter-tools or flutter-tools --- lua/init.lua | 137 ++++++++++++---------- lua/nvim-flutter-companion/utils/init.lua | 6 +- 2 files changed, 76 insertions(+), 67 deletions(-) diff --git a/lua/init.lua b/lua/init.lua index 143c780..82bf996 100644 --- a/lua/init.lua +++ b/lua/init.lua @@ -8,6 +8,9 @@ local global_state = require("telescope.state") local utils = require("nvim-flutter-companion.utils") local M = {} +local config = { + use_coc = true +} local callbacks = {nil, nil} local flutter_busy = false @@ -20,6 +23,20 @@ local get_config_path = function() return utils.create_path(vim.fn.getcwd(), ".flutter-companion") end +local get_flutter_command = function() + local cmd = "flutter" .. utils.iff(utils.is_windows, ".bat", "") + return cmd +end + +local flutter_run_device = function(id) + if config.use_coc then + vim.api.nvim_command("CocCommand flutter.run -d " .. id) + return + end + + vim.api.nvim_command("FlutterRun -d " .. id) +end + local load_table = function(path) local file, err = io.open(path, "r") if err or file == nil then @@ -64,56 +81,6 @@ local save_table = function(data, filename) file:close() end -_G.Flutter_Coc_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 -end - -_G.Flutter_Coc_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_Coc_Run_Selected_Device - vim.api.nvim_command("Telescope coc flutter_devices") - end - else - vim.api.nvim_command("CocCommand flutter.run -d " .. config_data.device.id) - end -end - -_G.Flutter_Coc_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 -end - -_G.Flutter_Coc_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_Coc_Run_Selected_Emulator - vim.api.nvim_command("Telescope coc flutter_emulators") - end - else - local cmd = "flutter" - if vim.fn.has("win32") == 1 or vim.fn.has("win64") == 1 then - cmd = cmd .. ".bat" - end - - local job = Job:new({command = cmd, args = {"emulators", "--launch", config_data.emulator.id}}) - job:start() - end -end - local get_devices = function(result, type) local devices = {} for _, line in pairs(result) do @@ -144,12 +111,7 @@ local parse_devices_output = function(job, callback) end local list_devices = function(callback) - local cmd = "flutter" - if vim.fn.has("win32") == 1 or vim.fn.has("win64") == 1 then - cmd = cmd .. ".bat" - end - - local job = Job:new({command = cmd, args = {"devices"}}) + local job = Job:new({command = get_flutter_command(), args = {"devices"}}) job:after_success( vim.schedule_wrap( function() @@ -161,12 +123,7 @@ local list_devices = function(callback) end local function list_emulators(callback) - local cmd = "flutter" - if vim.fn.has("win32") == 1 or vim.fn.has("win64") == 1 then - cmd = cmd .. ".bat" - end - - local job = Job:new({command = cmd, args = {"emulators"}}) + local job = Job:new({command = get_flutter_command(), args = {"emulators"}}) job:after_success( vim.schedule_wrap( function() @@ -208,7 +165,7 @@ local stop_spinner = function(set_inactive) require("notify").dismiss() end -function M.flutter_list(is_emulators, opts) +function M.flutter_list(is_emulators) if flutter_busy == true then return end @@ -248,6 +205,7 @@ function M.flutter_list(is_emulators, opts) else global_state.set_global_key("selected_entry", nil) + local opts = {} pickers.new( opts, { @@ -291,4 +249,57 @@ function M.flutter_list(is_emulators, opts) ) 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 +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 +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 +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 +end + return M diff --git a/lua/nvim-flutter-companion/utils/init.lua b/lua/nvim-flutter-companion/utils/init.lua index 5337b49..9447b95 100644 --- a/lua/nvim-flutter-companion/utils/init.lua +++ b/lua/nvim-flutter-companion/utils/init.lua @@ -1,3 +1,4 @@ +local Path = require "plenary.path" local M = {} M.iff = function(b, l, r) @@ -13,13 +14,10 @@ M.directory_sep = M.iff(M.is_windows, "\\\\", "/") M.not_directory_sep = M.iff(M.is_windows, "/", "\\\\") M.create_path = function(...) - local Path = require "plenary.path" - local ret = - Path:new({...}):absolute():gsub(M.not_directory_sep, M.directory_sep):gsub( + return Path:new({...}):absolute():gsub(M.not_directory_sep, M.directory_sep):gsub( M.directory_sep .. M.directory_sep, M.directory_sep ) - return ret end return M