support coc-flutter/coc-flutter-tools or flutter-tools

This commit is contained in:
Scott E. Graves 2022-07-29 11:04:37 -05:00
parent 8da655bcae
commit 6f3818cc42
2 changed files with 76 additions and 67 deletions

View File

@ -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

View File

@ -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