fix: show errors when reload after installation fails

This commit is contained in:
Stephan Seitz 2022-12-27 20:39:33 +01:00
parent 0598450eb8
commit 29304e16bb
2 changed files with 10 additions and 7 deletions

View file

@ -232,7 +232,7 @@ end
---Recurses through all modules including submodules ---Recurses through all modules including submodules
---@param accumulator function called for each module ---@param accumulator function called for each module
---@param root {[string]: TSModule} root configuration table to start at ---@param root {[string]: TSModule}|nil root configuration table to start at
---@param path string|nil prefix path ---@param path string|nil prefix path
local function recurse_modules(accumulator, root, path) local function recurse_modules(accumulator, root, path)
root = root or config.modules root = root or config.modules
@ -529,7 +529,7 @@ function M.reattach_module(mod_name, bufnr, lang)
end end
---Gets available modules ---Gets available modules
---@param root {[string]:TSModule} table to find modules ---@param root {[string]:TSModule}|nil table to find modules
function M.available_modules(root) function M.available_modules(root)
local modules = {} local modules = {}

View file

@ -44,15 +44,18 @@ end
---@param lang string ---@param lang string
---@return function ---@return function
local function reattach_if_possible_fn(lang) local function reattach_if_possible_fn(lang, error_on_fail)
return function() return function()
for _, buf in ipairs(vim.api.nvim_list_bufs()) do for _, buf in ipairs(vim.api.nvim_list_bufs()) do
if parsers.get_buf_lang(buf) == lang then if parsers.get_buf_lang(buf) == lang then
vim._ts_remove_language(lang) vim._ts_remove_language(lang)
local ok = pcall(vim.treesitter.language.require_language, lang) local ok, err = pcall(vim.treesitter.language.require_language, lang)
if not ok and error_on_fail then
vim.notify("Could not load parser for " .. lang .. ": " .. vim.inspect(err))
end
for _, mod in ipairs(require("nvim-treesitter.configs").available_modules()) do for _, mod in ipairs(require("nvim-treesitter.configs").available_modules()) do
if ok then if ok then
require("nvim-treesitter.configs").reattach_module(mod, buf) require("nvim-treesitter.configs").reattach_module(mod, buf, lang)
else else
require("nvim-treesitter.configs").detach_module(mod, buf) require("nvim-treesitter.configs").detach_module(mod, buf)
end end
@ -404,7 +407,7 @@ local function run_install(cache_folder, install_folder, lang, repo, with_sync,
end, end,
}, },
{ -- auto-attach modules after installation { -- auto-attach modules after installation
cmd = reattach_if_possible_fn(lang), cmd = reattach_if_possible_fn(lang, true),
}, },
}) })
if not from_local_path then if not from_local_path then
@ -605,7 +608,7 @@ function M.uninstall(...)
end, end,
}, },
{ -- auto-reattach or detach modules after uninstallation { -- auto-reattach or detach modules after uninstallation
cmd = reattach_if_possible_fn(lang), cmd = reattach_if_possible_fn(lang, false),
}, },
} }
M.iter_cmd(command_list, 1, lang, "Treesitter parser for " .. lang .. " has been uninstalled") M.iter_cmd(command_list, 1, lang, "Treesitter parser for " .. lang .. " has been uninstalled")