refactor: rewrite installation using jobs and async

Replace sync variants with callback support
This commit is contained in:
Lewis Russell 2023-05-22 14:35:25 +01:00 committed by Christian Clason
parent 5aa2984a02
commit cde679e435
15 changed files with 951 additions and 709 deletions

View file

@ -1,7 +1,6 @@
local M = {}
---@class TSConfig
---@field sync_install boolean
---@field auto_install boolean
---@field ensure_install string[]
---@field ignore_install string[]
@ -9,7 +8,6 @@ local M = {}
---@type TSConfig
local config = {
sync_install = false,
auto_install = false,
ensure_install = {},
ignore_install = {},
@ -38,7 +36,11 @@ function M.setup(user_data)
and not vim.list_contains(M.installed_parsers(), lang)
and not vim.list_contains(config.ignore_install, lang)
then
require('nvim-treesitter.install').install(lang)
require('nvim-treesitter.install').install(lang, nil, function()
-- Need to pcall since 'FileType' can be triggered multiple times
-- per buffer
pcall(vim.treesitter.start, args.buf, lang)
end)
end
end,
})
@ -48,9 +50,7 @@ function M.setup(user_data)
local to_install = M.norm_languages(config.ensure_install, { ignored = true, installed = true })
if #to_install > 0 then
require('nvim-treesitter.install').install(to_install, {
with_sync = config.sync_install,
})
require('nvim-treesitter.install').install(to_install)
end
end
end
@ -63,9 +63,10 @@ function M.get_install_dir(dir_name)
local dir = vim.fs.joinpath(config.install_dir, dir_name)
if not vim.loop.fs_stat(dir) then
local ok, error = pcall(vim.fn.mkdir, dir, 'p', '0755')
local ok, err = pcall(vim.fn.mkdir, dir, 'p', '0755')
if not ok then
vim.notify(error, vim.log.levels.ERROR)
local log = require('nvim-treesitter.log')
log.error(err --[[@as string]])
end
end
return dir
@ -105,35 +106,37 @@ function M.norm_languages(languages, skip)
end
languages = parsers.get_available()
end
--TODO(clason): skip and warn on unavailable parser
for i, tier in ipairs(parsers.tiers) do
if vim.list_contains(languages, tier) then
languages = vim.iter.filter(function(l)
return l ~= tier
end, languages)
end, languages) --[[@as string[] ]]
vim.list_extend(languages, parsers.get_available(i))
end
end
--TODO(clason): support skipping tiers
if skip and skip.ignored then
local ignored = config.ignore_install
languages = vim.iter.filter(function(v)
return not vim.list_contains(ignored, v)
end, languages)
end, languages) --[[@as string[] ]]
end
if skip and skip.installed then
local installed = M.installed_parsers()
languages = vim.iter.filter(function(v)
return not vim.list_contains(installed, v)
end, languages)
end, languages) --[[@as string[] ]]
end
if skip and skip.missing then
local installed = M.installed_parsers()
languages = vim.iter.filter(function(v)
return vim.list_contains(installed, v)
end, languages)
end, languages) --[[@as string[] ]]
end
return languages