support coc-flutter/coc-flutter-tools or flutter-tools
This commit is contained in:
parent
8da655bcae
commit
6f3818cc42
137
lua/init.lua
137
lua/init.lua
@ -8,6 +8,9 @@ local global_state = require("telescope.state")
|
|||||||
local utils = require("nvim-flutter-companion.utils")
|
local utils = require("nvim-flutter-companion.utils")
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
local config = {
|
||||||
|
use_coc = true
|
||||||
|
}
|
||||||
|
|
||||||
local callbacks = {nil, nil}
|
local callbacks = {nil, nil}
|
||||||
local flutter_busy = false
|
local flutter_busy = false
|
||||||
@ -20,6 +23,20 @@ local get_config_path = function()
|
|||||||
return utils.create_path(vim.fn.getcwd(), ".flutter-companion")
|
return utils.create_path(vim.fn.getcwd(), ".flutter-companion")
|
||||||
end
|
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 load_table = function(path)
|
||||||
local file, err = io.open(path, "r")
|
local file, err = io.open(path, "r")
|
||||||
if err or file == nil then
|
if err or file == nil then
|
||||||
@ -64,56 +81,6 @@ local save_table = function(data, filename)
|
|||||||
file:close()
|
file:close()
|
||||||
end
|
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 get_devices = function(result, type)
|
||||||
local devices = {}
|
local devices = {}
|
||||||
for _, line in pairs(result) do
|
for _, line in pairs(result) do
|
||||||
@ -144,12 +111,7 @@ local parse_devices_output = function(job, callback)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local list_devices = function(callback)
|
local list_devices = function(callback)
|
||||||
local cmd = "flutter"
|
local job = Job:new({command = get_flutter_command(), args = {"devices"}})
|
||||||
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"}})
|
|
||||||
job:after_success(
|
job:after_success(
|
||||||
vim.schedule_wrap(
|
vim.schedule_wrap(
|
||||||
function()
|
function()
|
||||||
@ -161,12 +123,7 @@ local list_devices = function(callback)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function list_emulators(callback)
|
local function list_emulators(callback)
|
||||||
local cmd = "flutter"
|
local job = Job:new({command = get_flutter_command(), args = {"emulators"}})
|
||||||
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"}})
|
|
||||||
job:after_success(
|
job:after_success(
|
||||||
vim.schedule_wrap(
|
vim.schedule_wrap(
|
||||||
function()
|
function()
|
||||||
@ -208,7 +165,7 @@ local stop_spinner = function(set_inactive)
|
|||||||
require("notify").dismiss()
|
require("notify").dismiss()
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.flutter_list(is_emulators, opts)
|
function M.flutter_list(is_emulators)
|
||||||
if flutter_busy == true then
|
if flutter_busy == true then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@ -248,6 +205,7 @@ function M.flutter_list(is_emulators, opts)
|
|||||||
else
|
else
|
||||||
global_state.set_global_key("selected_entry", nil)
|
global_state.set_global_key("selected_entry", nil)
|
||||||
|
|
||||||
|
local opts = {}
|
||||||
pickers.new(
|
pickers.new(
|
||||||
opts,
|
opts,
|
||||||
{
|
{
|
||||||
@ -291,4 +249,57 @@ function M.flutter_list(is_emulators, opts)
|
|||||||
)
|
)
|
||||||
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
|
||||||
|
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
|
return M
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
local Path = require "plenary.path"
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
M.iff = function(b, l, r)
|
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.not_directory_sep = M.iff(M.is_windows, "/", "\\\\")
|
||||||
|
|
||||||
M.create_path = function(...)
|
M.create_path = function(...)
|
||||||
local Path = require "plenary.path"
|
return Path:new({...}):absolute():gsub(M.not_directory_sep, M.directory_sep):gsub(
|
||||||
local ret =
|
|
||||||
Path:new({...}):absolute():gsub(M.not_directory_sep, M.directory_sep):gsub(
|
|
||||||
M.directory_sep .. M.directory_sep,
|
M.directory_sep .. M.directory_sep,
|
||||||
M.directory_sep
|
M.directory_sep
|
||||||
)
|
)
|
||||||
return ret
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
Loading…
x
Reference in New Issue
Block a user