Merge pull request #106 from barrett-ruth/feat/picker-contests

Feat/picker contests
This commit is contained in:
Barrett Ruth 2025-09-25 02:58:50 +02:00 committed by GitHub
commit 383c59a2ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 30 additions and 74 deletions

View file

@ -373,11 +373,6 @@ Picker Controls ~
<c-p> / <up> Navigate to previous item
/ Start filtering/searching items
Notes ~
• Contest lists are fetched dynamically using scrapers with a TTL of 1 day
• Use <c-r> to force refresh
• Large contest lists may take time to load
==============================================================================
RUN PANEL *cp-run*

View file

@ -12,12 +12,10 @@
---@class ContestListData
---@field contests table[]
---@field cached_at number
---@field expires_at number
---@class ContestData
---@field problems Problem[]
---@field scraped_at string
---@field expires_at? number
---@field test_cases? CachedTestCase[]
---@field test_cases_cached_at? number
---@field timeout_ms? number
@ -39,28 +37,6 @@ 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,
codeforces = 24 * 60 * 60,
atcoder = 24 * 60 * 60,
}
---@param contest_data ContestData
---@param platform string
---@return boolean
local function is_cache_valid(contest_data, platform)
vim.validate({
contest_data = { contest_data, 'table' },
platform = { platform, 'string' },
})
if contest_data.expires_at and os.time() >= contest_data.expires_at then
return false
end
return true
end
function M.load()
if loaded then
return
@ -125,10 +101,6 @@ function M.get_contest_data(platform, contest_id)
return nil
end
if not is_cache_valid(contest_data, platform) then
return nil
end
return contest_data
end
@ -146,11 +118,9 @@ function M.set_contest_data(platform, contest_id, problems)
cache_data[platform] = {}
end
local ttl = CONTEST_LIST_TTL[platform] or (24 * 60 * 60)
cache_data[platform][contest_id] = {
problems = problems,
scraped_at = os.date('%Y-%m-%d'),
expires_at = os.time() + ttl,
}
M.save()
@ -296,12 +266,7 @@ function M.get_contest_list(platform)
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
return cache_data.contest_lists[platform].contests
end
---@param platform string
@ -316,11 +281,9 @@ function M.set_contest_list(platform, contests)
cache_data.contest_lists = {}
end
local ttl = CONTEST_LIST_TTL[platform] or (24 * 60 * 60)
cache_data.contest_lists[platform] = {
contests = contests,
cached_at = os.time(),
expires_at = os.time() + ttl,
}
M.save()

View file

@ -41,7 +41,7 @@ function M.setup_contest(platform, contest_id, problem_id, language)
return
end
logger.log(('fetching contest %s %s...'):format(platform, contest_id))
logger.log('fetching contests problems...', vim.log.levels.INFO, true)
scraper.scrape_contest_metadata(platform, contest_id, function(result)
if not result.success then
@ -99,35 +99,6 @@ function M.setup_problem(contest_id, problem_id, language)
state.set_contest_id(contest_id)
state.set_problem_id(problem_id)
local cached_tests = cache.get_test_cases(platform, contest_id, problem_id)
if cached_tests then
state.set_test_cases(cached_tests)
logger.log(('using cached test cases (%d)'):format(#cached_tests))
elseif vim.tbl_contains(config.scrapers, platform) then
logger.log('loading test cases...')
scraper.scrape_problem_tests(platform, contest_id, problem_id, function(result)
if result.success then
logger.log(('loaded %d test cases for %s'):format(#(result.tests or {}), problem_id))
if state.get_problem_id() == problem_id then
state.set_test_cases(result.tests)
end
else
logger.log(
'failed to load tests: ' .. (result.error or 'unknown error'),
vim.log.levels.ERROR
)
if state.get_problem_id() == problem_id then
state.set_test_cases({})
end
end
end)
else
logger.log(('scraping disabled for %s'):format(platform))
state.set_test_cases({})
end
state.set_run_panel_active(false)
vim.schedule(function()
@ -180,6 +151,34 @@ function M.setup_problem(contest_id, problem_id, language)
logger.log(('setup error: %s'):format(err), vim.log.levels.ERROR)
end
end)
local cached_tests = cache.get_test_cases(platform, contest_id, problem_id)
if cached_tests then
state.set_test_cases(cached_tests)
logger.log(('using cached test cases (%d)'):format(#cached_tests))
elseif vim.tbl_contains(config.scrapers, platform) then
logger.log('loading test cases...')
scraper.scrape_problem_tests(platform, contest_id, problem_id, function(result)
if result.success then
logger.log(('loaded %d test cases for %s'):format(#(result.tests or {}), problem_id))
if state.get_problem_id() == problem_id then
state.set_test_cases(result.tests)
end
else
logger.log(
'failed to load tests: ' .. (result.error or 'unknown error'),
vim.log.levels.ERROR
)
if state.get_problem_id() == problem_id then
state.set_test_cases({})
end
end
end)
else
logger.log(('scraping disabled for %s'):format(platform))
state.set_test_cases({})
end
end
function M.scrape_remaining_problems(platform, contest_id, problems)

View file

@ -85,7 +85,6 @@ describe('cp.cache', function()
local result = cache.get_contest_data('cses', 'test_contest')
assert.is_not_nil(result)
assert.is_not_nil(result.expires_at)
end)
end)