refactor(install): replace status enum with boolean

This commit is contained in:
MeanderingProgrammer 2025-07-20 15:36:06 -07:00 committed by Christian Clason
parent d116118add
commit 3650b4ef6a

View file

@ -439,13 +439,7 @@ local function try_install_lang(lang, cache_dir, install_dir, generate)
logger:info('Language installed') logger:info('Language installed')
end end
---@alias InstallStatus local installing = {} ---@type table<string,boolean?>
--- | 'installing'
--- | 'installed'
--- | 'failed'
--- | 'timeout'
local install_status = {} ---@type table<string,InstallStatus?>
---@async ---@async
---@param lang string ---@param lang string
@ -453,29 +447,21 @@ local install_status = {} ---@type table<string,InstallStatus?>
---@param install_dir string ---@param install_dir string
---@param force? boolean ---@param force? boolean
---@param generate? boolean ---@param generate? boolean
---@return InstallStatus status ---@return boolean success
local function install_lang(lang, cache_dir, install_dir, force, generate) local function install_lang(lang, cache_dir, install_dir, force, generate)
if not force and vim.list_contains(config.get_installed(), lang) then if not force and vim.list_contains(config.get_installed(), lang) then
return 'installed' return true
end elseif installing[lang] then
local success = vim.wait(INSTALL_TIMEOUT, function()
if install_status[lang] then return not installing[lang]
if install_status[lang] == 'installing' then end)
vim.wait(INSTALL_TIMEOUT, function() return success
return install_status[lang] ~= 'installing'
end)
install_status[lang] = 'timeout'
end
else else
install_status[lang] = 'installing' installing[lang] = true
local err = try_install_lang(lang, cache_dir, install_dir, generate) local err = try_install_lang(lang, cache_dir, install_dir, generate)
install_status[lang] = err and 'failed' or 'installed' installing[lang] = nil
return not err
end end
local status = install_status[lang]
assert(status and status ~= 'installing')
install_status[lang] = nil
return status
end end
--- Reload the parser table and user modifications in case of update --- Reload the parser table and user modifications in case of update
@ -511,8 +497,8 @@ local function install(languages, options)
for _, lang in ipairs(languages) do for _, lang in ipairs(languages) do
tasks[#tasks + 1] = a.async(--[[@async]] function() tasks[#tasks + 1] = a.async(--[[@async]] function()
a.schedule() a.schedule()
local status = install_lang(lang, cache_dir, install_dir, options.force, options.generate) local success = install_lang(lang, cache_dir, install_dir, options.force, options.generate)
if status ~= 'failed' then if success then
done = done + 1 done = done + 1
end end
end) end)