From 1b8365265d39470d27ca0f90fc28a65fda9c1960 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sun, 21 Sep 2025 11:36:06 -0400 Subject: [PATCH] fix(ci): unused variables --- lua/cp/cache.lua | 65 ++++++++++++++++++++++++++++++++++++ lua/cp/init.lua | 2 +- lua/cp/pickers/init.lua | 7 ++++ lua/cp/pickers/telescope.lua | 47 +++++++++++++------------- lua/cp/scrape.lua | 1 - spec/fzf_lua_spec.lua | 2 +- 6 files changed, 97 insertions(+), 27 deletions(-) diff --git a/lua/cp/cache.lua b/lua/cp/cache.lua index 5d8f6b8..04d7dbd 100644 --- a/lua/cp/cache.lua +++ b/lua/cp/cache.lua @@ -7,6 +7,12 @@ ---@class CacheData ---@field [string] table ---@field file_states? table +---@field contest_lists? table + +---@class ContestListData +---@field contests table[] +---@field cached_at number +---@field expires_at number ---@class ContestData ---@field problems Problem[] @@ -33,6 +39,12 @@ local cache_file = vim.fn.stdpath('data') .. '/cp-nvim.json' local cache_data = {} local loaded = false +local CONTEST_LIST_TTL = { + cses = 7 * 24 * 60 * 60, -- 1 week + codeforces = 24 * 60 * 60, -- 1 day + atcoder = 24 * 60 * 60, -- 1 day +} + ---@param platform string ---@return number? local function get_expiry_date(platform) @@ -277,4 +289,57 @@ function M.set_file_state(file_path, platform, contest_id, problem_id, language) M.save() end +---@param platform string +---@return table[]? +function M.get_contest_list(platform) + vim.validate({ + platform = { platform, 'string' }, + }) + + if not cache_data.contest_lists or not cache_data.contest_lists[platform] then + return nil + end + + local contest_list_data = cache_data.contest_lists[platform] + if os.time() >= contest_list_data.expires_at then + return nil + end + + return contest_list_data.contests +end + +---@param platform string +---@param contests table[] +function M.set_contest_list(platform, contests) + vim.validate({ + platform = { platform, 'string' }, + contests = { contests, 'table' }, + }) + + if not cache_data.contest_lists then + cache_data.contest_lists = {} + end + + local ttl = CONTEST_LIST_TTL[platform] or (24 * 60 * 60) -- Default 1 day + cache_data.contest_lists[platform] = { + contests = contests, + cached_at = os.time(), + expires_at = os.time() + ttl, + } + + M.save() +end + +---@param platform string +function M.clear_contest_list(platform) + vim.validate({ + platform = { platform, 'string' }, + }) + + if cache_data.contest_lists and cache_data.contest_lists[platform] then + cache_data.contest_lists[platform] = nil + M.save() + end +end + return M diff --git a/lua/cp/init.lua b/lua/cp/init.lua index 7d82cc6..b8188f9 100644 --- a/lua/cp/init.lua +++ b/lua/cp/init.lua @@ -708,7 +708,7 @@ local function handle_pick_action() end if config.picker == 'telescope' then - local ok, telescope = pcall(require, 'telescope') + local ok = pcall(require, 'telescope') if not ok then logger.log( 'Telescope not available. Install telescope.nvim or change picker config', diff --git a/lua/cp/pickers/init.lua b/lua/cp/pickers/init.lua index e2920ef..a21da8a 100644 --- a/lua/cp/pickers/init.lua +++ b/lua/cp/pickers/init.lua @@ -37,6 +37,12 @@ end local function get_contests_for_platform(platform) local contests = {} + cache.load() + local cached_contests = cache.get_contest_list(platform) + if cached_contests then + return cached_contests + end + if not utils.setup_python_env() then return contests end @@ -81,6 +87,7 @@ local function get_contests_for_platform(platform) }) end + cache.set_contest_list(platform, contests) return contests end diff --git a/lua/cp/pickers/telescope.lua b/lua/cp/pickers/telescope.lua index 604bbf8..57ef5d8 100644 --- a/lua/cp/pickers/telescope.lua +++ b/lua/cp/pickers/telescope.lua @@ -1,22 +1,29 @@ local finders = require('telescope.finders') local pickers = require('telescope.pickers') -local telescope = require('telescope') local conf = require('telescope.config').values local action_state = require('telescope.actions.state') local actions = require('telescope.actions') local picker_utils = require('cp.pickers') -local function platform_picker(opts) - opts = opts or {} +local function problem_picker(opts, platform, contest_id) + local constants = require('cp.constants') + local platform_display_name = constants.PLATFORM_DISPLAY_NAMES[platform] or platform + local problems = picker_utils.get_problems_for_contest(platform, contest_id) - local platforms = picker_utils.get_platforms() + if #problems == 0 then + vim.notify( + ('No problems found for contest: %s %s'):format(platform_display_name, contest_id), + vim.log.levels.WARN + ) + return + end pickers .new(opts, { - prompt_title = 'Select Platform', + prompt_title = ('Select Problem (%s %s)'):format(platform_display_name, contest_id), finder = finders.new_table({ - results = platforms, + results = problems, entry_maker = function(entry) return { value = entry, @@ -26,13 +33,13 @@ local function platform_picker(opts) end, }), sorter = conf.generic_sorter(opts), - attach_mappings = function(prompt_bufnr, map) + attach_mappings = function(prompt_bufnr) actions.select_default:replace(function() local selection = action_state.get_selected_entry() actions.close(prompt_bufnr) if selection then - contest_picker(opts, selection.value.id) + picker_utils.setup_problem(platform, contest_id, selection.value.id) end end) return true @@ -68,7 +75,7 @@ local function contest_picker(opts, platform) end, }), sorter = conf.generic_sorter(opts), - attach_mappings = function(prompt_bufnr, map) + attach_mappings = function(prompt_bufnr) actions.select_default:replace(function() local selection = action_state.get_selected_entry() actions.close(prompt_bufnr) @@ -83,24 +90,16 @@ local function contest_picker(opts, platform) :find() end -local function problem_picker(opts, platform, contest_id) - local constants = require('cp.constants') - local platform_display_name = constants.PLATFORM_DISPLAY_NAMES[platform] or platform - local problems = picker_utils.get_problems_for_contest(platform, contest_id) +local function platform_picker(opts) + opts = opts or {} - if #problems == 0 then - vim.notify( - ('No problems found for contest: %s %s'):format(platform_display_name, contest_id), - vim.log.levels.WARN - ) - return - end + local platforms = picker_utils.get_platforms() pickers .new(opts, { - prompt_title = ('Select Problem (%s %s)'):format(platform_display_name, contest_id), + prompt_title = 'Select Platform', finder = finders.new_table({ - results = problems, + results = platforms, entry_maker = function(entry) return { value = entry, @@ -110,13 +109,13 @@ local function problem_picker(opts, platform, contest_id) end, }), sorter = conf.generic_sorter(opts), - attach_mappings = function(prompt_bufnr, map) + attach_mappings = function(prompt_bufnr) actions.select_default:replace(function() local selection = action_state.get_selected_entry() actions.close(prompt_bufnr) if selection then - picker_utils.setup_problem(platform, contest_id, selection.value.id) + contest_picker(opts, selection.value.id) end end) return true diff --git a/lua/cp/scrape.lua b/lua/cp/scrape.lua index 88ab166..9f7e35b 100644 --- a/lua/cp/scrape.lua +++ b/lua/cp/scrape.lua @@ -13,7 +13,6 @@ local M = {} local cache = require('cp.cache') -local logger = require('cp.log') local problem = require('cp.problem') local utils = require('cp.utils') diff --git a/spec/fzf_lua_spec.lua b/spec/fzf_lua_spec.lua index f22e35e..2b5af04 100644 --- a/spec/fzf_lua_spec.lua +++ b/spec/fzf_lua_spec.lua @@ -6,7 +6,7 @@ describe('cp.fzf_lua', function() package.preload['fzf-lua'] = function() return { - fzf_exec = function(entries, opts) end, + fzf_exec = function() end, } end end)