From c9ab7443d6eed2501446be9de0b69fabcc759060 Mon Sep 17 00:00:00 2001 From: Marc Jakobi Date: Mon, 17 Jun 2024 18:20:34 +0200 Subject: [PATCH] fix(auto_install): fall back to luarocks.org query if cache is not populated --- lua/rocks_treesitter/highlight.lua | 37 ++++++++++++++++++------------ 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/lua/rocks_treesitter/highlight.lua b/lua/rocks_treesitter/highlight.lua index df43659..b37afa1 100644 --- a/lua/rocks_treesitter/highlight.lua +++ b/lua/rocks_treesitter/highlight.lua @@ -36,11 +36,17 @@ local function is_installed(lang) end ---@param lang string ----@return Rock[] -local function find_parser_rock(lang) +---@param callback fun(rock: Rock[]) +local function find_parser_rock(lang, callback) local rock_name = "tree-sitter-" .. lang local rocks = api.try_get_cached_rocks() - return rocks[rock_name] or {} + if rocks and rocks[rock_name] then + callback(rocks[rock_name]) + end + -- Cache may not have been populated. Query luarocks. + api.query_luarocks_rocks(function(luarocks_rocks) + callback(luarocks_rocks[rock_name] or {}) + end) end ---@param rock Rock @@ -97,18 +103,19 @@ local function do_highlight(lang) vim.treesitter.start() return end - local rocks = find_parser_rock(lang) - if vim.tbl_isempty(rocks) then - return - end - if config.auto_install == "prompt" then - vim.schedule(function() - prompt_auto_install(rocks) - end) - elseif config.auto_install then - -- TODO: replace "dev" with nil once we have tagged releases - api.install(rocks[1].name, "dev", try_start_highlight) - end + find_parser_rock(lang, function(rocks) + if vim.tbl_isempty(rocks) then + return + end + if config.auto_install == "prompt" then + vim.schedule(function() + prompt_auto_install(rocks) + end) + elseif config.auto_install then + -- TODO: replace "dev" with nil once we have tagged releases + api.install(rocks[1].name, "dev", try_start_highlight) + end + end) end function highlight.create_autocmd()