feat: some more updates

This commit is contained in:
Barrett Ruth 2025-09-22 19:11:11 -04:00
parent 5a6902633f
commit 9c2be9c6b0
6 changed files with 54 additions and 36 deletions

View file

@ -86,10 +86,11 @@ local function parse_command(args)
end end
end end
if state.get_platform() ~= '' and state.get_contest_id() ~= '' then if state.get_platform() and state.get_contest_id() then
local cache = require('cp.cache') local cache = require('cp.cache')
cache.load() cache.load()
local contest_data = cache.get_contest_data(state.get_platform(), state.get_contest_id()) local contest_data =
cache.get_contest_data(state.get_platform() or '', state.get_contest_id() or '')
if contest_data and contest_data.problems then if contest_data and contest_data.problems then
local problem_ids = vim.tbl_map(function(prob) local problem_ids = vim.tbl_map(function(prob)
return prob.id return prob.id
@ -168,7 +169,7 @@ function M.handle_command(opts)
if cmd.type == 'problem_switch' then if cmd.type == 'problem_switch' then
local setup = require('cp.setup') local setup = require('cp.setup')
setup.setup_problem(state.get_contest_id(), cmd.problem, cmd.language) setup.setup_problem(state.get_contest_id() or '', cmd.problem, cmd.language)
return return
end end
end end

View file

@ -9,8 +9,12 @@ function M.scrape_missing_problems(contest_id, missing_problems, config)
logger.log(('scraping %d uncached problems...'):format(#missing_problems)) logger.log(('scraping %d uncached problems...'):format(#missing_problems))
local results = local results = scrape.scrape_problems_parallel(
scrape.scrape_problems_parallel(state.get_platform(), contest_id, missing_problems, config) state.get_platform() or '',
contest_id,
missing_problems,
config
)
local success_count = 0 local success_count = 0
local failed_problems = {} local failed_problems = {}

View file

@ -26,7 +26,7 @@ function M.set_platform(platform)
end end
function M.setup_problem(contest_id, problem_id, language) function M.setup_problem(contest_id, problem_id, language)
if state.get_platform() == '' then if not state.get_platform() then
logger.log('no platform set. run :CP <platform> <contest> first', vim.log.levels.ERROR) logger.log('no platform set. run :CP <platform> <contest> first', vim.log.levels.ERROR)
return return
end end
@ -35,14 +35,15 @@ function M.setup_problem(contest_id, problem_id, language)
local problem_name = contest_id .. (problem_id or '') local problem_name = contest_id .. (problem_id or '')
logger.log(('setting up problem: %s'):format(problem_name)) logger.log(('setting up problem: %s'):format(problem_name))
local ctx = problem.create_context(state.get_platform(), contest_id, problem_id, config, language) local ctx =
problem.create_context(state.get_platform() or '', contest_id, problem_id, config, language)
if vim.tbl_contains(config.scrapers, state.get_platform()) then if vim.tbl_contains(config.scrapers, state.get_platform() or '') then
cache.load() cache.load()
local existing_contest_data = cache.get_contest_data(state.get_platform(), contest_id) local existing_contest_data = cache.get_contest_data(state.get_platform() or '', contest_id)
if not existing_contest_data then if not existing_contest_data then
local metadata_result = scrape.scrape_contest_metadata(state.get_platform(), contest_id) local metadata_result = scrape.scrape_contest_metadata(state.get_platform() or '', contest_id)
if not metadata_result.success then if not metadata_result.success then
logger.log( logger.log(
'failed to load contest metadata: ' .. (metadata_result.error or 'unknown error'), 'failed to load contest metadata: ' .. (metadata_result.error or 'unknown error'),
@ -52,13 +53,13 @@ function M.setup_problem(contest_id, problem_id, language)
end end
end end
local cached_test_cases = cache.get_test_cases(state.get_platform(), contest_id, problem_id) local cached_test_cases = cache.get_test_cases(state.get_platform() or '', contest_id, problem_id)
if cached_test_cases then if cached_test_cases then
state.set_test_cases(cached_test_cases) state.set_test_cases(cached_test_cases)
logger.log(('using cached test cases (%d)'):format(#cached_test_cases)) logger.log(('using cached test cases (%d)'):format(#cached_test_cases))
elseif vim.tbl_contains(config.scrapers, state.get_platform()) then elseif vim.tbl_contains(config.scrapers, state.get_platform() or '') then
local platform_display_name = constants.PLATFORM_DISPLAY_NAMES[state.get_platform()] local platform_display_name = constants.PLATFORM_DISPLAY_NAMES[state.get_platform() or '']
or state.get_platform() or (state.get_platform() or '')
logger.log( logger.log(
('Scraping %s %s %s for test cases, this may take a few seconds...'):format( ('Scraping %s %s %s for test cases, this may take a few seconds...'):format(
platform_display_name, platform_display_name,
@ -84,10 +85,15 @@ function M.setup_problem(contest_id, problem_id, language)
state.set_test_cases(scrape_result.test_cases) state.set_test_cases(scrape_result.test_cases)
if scrape_result.test_cases then if scrape_result.test_cases then
cache.set_test_cases(state.get_platform(), contest_id, problem_id, scrape_result.test_cases) cache.set_test_cases(
state.get_platform() or '',
contest_id,
problem_id,
scrape_result.test_cases
)
end end
else else
logger.log(('scraping disabled for %s'):format(state.get_platform())) logger.log(('scraping disabled for %s'):format(state.get_platform() or ''))
state.set_test_cases(nil) state.set_test_cases(nil)
end end
@ -130,27 +136,33 @@ function M.setup_problem(contest_id, problem_id, language)
config.hooks.setup_code(ctx) config.hooks.setup_code(ctx)
end end
cache.set_file_state(vim.fn.expand('%:p'), state.get_platform(), contest_id, problem_id, language) cache.set_file_state(
vim.fn.expand('%:p'),
state.get_platform() or '',
contest_id,
problem_id,
language
)
logger.log(('switched to problem %s'):format(ctx.problem_name)) logger.log(('switched to problem %s'):format(ctx.problem_name))
end end
function M.setup_contest(contest_id, language) function M.setup_contest(contest_id, language)
if state.get_platform() == '' then if not state.get_platform() then
logger.log('no platform set', vim.log.levels.ERROR) logger.log('no platform set', vim.log.levels.ERROR)
return false return false
end end
local config = config_module.get_config() local config = config_module.get_config()
if not vim.tbl_contains(config.scrapers, state.get_platform()) then if not vim.tbl_contains(config.scrapers, state.get_platform() or '') then
logger.log('scraping disabled for ' .. state.get_platform(), vim.log.levels.WARN) logger.log('scraping disabled for ' .. (state.get_platform() or ''), vim.log.levels.WARN)
return false return false
end end
logger.log(('setting up contest %s %s'):format(state.get_platform(), contest_id)) logger.log(('setting up contest %s %s'):format(state.get_platform() or '', contest_id))
local metadata_result = scrape.scrape_contest_metadata(state.get_platform(), contest_id) local metadata_result = scrape.scrape_contest_metadata(state.get_platform() or '', contest_id)
if not metadata_result.success then if not metadata_result.success then
logger.log( logger.log(
'failed to load contest metadata: ' .. (metadata_result.error or 'unknown error'), 'failed to load contest metadata: ' .. (metadata_result.error or 'unknown error'),
@ -170,7 +182,7 @@ function M.setup_contest(contest_id, language)
cache.load() cache.load()
local missing_problems = {} local missing_problems = {}
for _, prob in ipairs(problems) do for _, prob in ipairs(problems) do
local cached_tests = cache.get_test_cases(state.get_platform(), contest_id, prob.id) local cached_tests = cache.get_test_cases(state.get_platform() or '', contest_id, prob.id)
if not cached_tests then if not cached_tests then
table.insert(missing_problems, prob) table.insert(missing_problems, prob)
end end
@ -190,7 +202,7 @@ function M.setup_contest(contest_id, language)
end end
function M.navigate_problem(delta, language) function M.navigate_problem(delta, language)
if state.get_platform() == '' or state.get_contest_id() == '' then if not state.get_platform() or not state.get_contest_id() then
logger.log('no contest set. run :CP <platform> <contest> first', vim.log.levels.ERROR) logger.log('no contest set. run :CP <platform> <contest> first', vim.log.levels.ERROR)
return return
end end
@ -226,7 +238,7 @@ function M.handle_full_setup(cmd)
has_metadata = true has_metadata = true
else else
cache.load() cache.load()
local contest_data = cache.get_contest_data(cmd.platform, cmd.contest) local contest_data = cache.get_contest_data(cmd.platform or '', cmd.contest)
if contest_data and contest_data.problems then if contest_data and contest_data.problems then
problem_ids = vim.tbl_map(function(prob) problem_ids = vim.tbl_map(function(prob)
return prob.id return prob.id

View file

@ -15,7 +15,8 @@ end
function M.navigate_problem(delta, language) function M.navigate_problem(delta, language)
cache.load() cache.load()
local contest_data = cache.get_contest_data(state.get_platform(), state.get_contest_id()) local contest_data =
cache.get_contest_data(state.get_platform() or '', state.get_contest_id() or '')
if not contest_data or not contest_data.problems then if not contest_data or not contest_data.problems then
logger.log( logger.log(
'no contest metadata found. set up a problem first to cache contest data', 'no contest metadata found. set up a problem first to cache contest data',
@ -55,7 +56,7 @@ function M.navigate_problem(delta, language)
local new_problem = problems[new_index] local new_problem = problems[new_index]
local setup = require('cp.setup') local setup = require('cp.setup')
setup.setup_problem(state.get_contest_id(), new_problem.id, language) setup.setup_problem(state.get_contest_id() or '', new_problem.id, language)
end end
M.get_current_problem = get_current_problem M.get_current_problem = get_current_problem

View file

@ -1,8 +1,8 @@
local M = {} local M = {}
local state = { local state = {
platform = '', platform = nil,
contest_id = '', contest_id = nil,
problem_id = nil, problem_id = nil,
test_cases = nil, test_cases = nil,
run_panel_active = false, run_panel_active = false,
@ -66,12 +66,12 @@ function M.get_context()
end end
function M.has_context() function M.has_context()
return state.platform ~= '' and state.contest_id ~= '' return state.platform and state.contest_id
end end
function M.reset() function M.reset()
state.platform = '' state.platform = nil
state.contest_id = '' state.contest_id = nil
state.problem_id = nil state.problem_id = nil
state.test_cases = nil state.test_cases = nil
state.run_panel_active = false state.run_panel_active = false

View file

@ -33,7 +33,7 @@ function M.toggle_run_panel(is_debug)
return return
end end
if state.get_platform() == '' then if not state.get_platform() then
logger.log( logger.log(
'No contest configured. Use :CP <platform> <contest> <problem> to set up first.', 'No contest configured. Use :CP <platform> <contest> <problem> to set up first.',
vim.log.levels.ERROR vim.log.levels.ERROR
@ -48,8 +48,8 @@ function M.toggle_run_panel(is_debug)
local config = config_module.get_config() local config = config_module.get_config()
local ctx = problem.create_context( local ctx = problem.create_context(
state.get_platform(), state.get_platform() or '',
state.get_contest_id(), state.get_contest_id() or '',
state.get_problem_id(), state.get_problem_id(),
config config
) )
@ -173,7 +173,7 @@ function M.toggle_run_panel(is_debug)
end end
local execute = require('cp.runner.execute') local execute = require('cp.runner.execute')
local contest_config = config.contests[state.get_platform()] local contest_config = config.contests[state.get_platform() or '']
local compile_result = execute.compile_problem(ctx, contest_config, is_debug) local compile_result = execute.compile_problem(ctx, contest_config, is_debug)
if compile_result.success then if compile_result.success then
run.run_all_test_cases(ctx, contest_config, config) run.run_all_test_cases(ctx, contest_config, config)