From 9c2be9c6b0f66ea020c741b008a42f598b7aef73 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Mon, 22 Sep 2025 19:11:11 -0400 Subject: [PATCH] feat: some more updates --- lua/cp/commands/init.lua | 7 ++--- lua/cp/setup/contest.lua | 8 ++++-- lua/cp/setup/init.lua | 52 +++++++++++++++++++++++-------------- lua/cp/setup/navigation.lua | 5 ++-- lua/cp/state.lua | 10 +++---- lua/cp/ui/panel.lua | 8 +++--- 6 files changed, 54 insertions(+), 36 deletions(-) diff --git a/lua/cp/commands/init.lua b/lua/cp/commands/init.lua index 1fcd161..0ef9c3a 100644 --- a/lua/cp/commands/init.lua +++ b/lua/cp/commands/init.lua @@ -86,10 +86,11 @@ local function parse_command(args) 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') 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 local problem_ids = vim.tbl_map(function(prob) return prob.id @@ -168,7 +169,7 @@ function M.handle_command(opts) if cmd.type == 'problem_switch' then 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 end end diff --git a/lua/cp/setup/contest.lua b/lua/cp/setup/contest.lua index 4618f21..7649330 100644 --- a/lua/cp/setup/contest.lua +++ b/lua/cp/setup/contest.lua @@ -9,8 +9,12 @@ function M.scrape_missing_problems(contest_id, missing_problems, config) logger.log(('scraping %d uncached problems...'):format(#missing_problems)) - local results = - scrape.scrape_problems_parallel(state.get_platform(), contest_id, missing_problems, config) + local results = scrape.scrape_problems_parallel( + state.get_platform() or '', + contest_id, + missing_problems, + config + ) local success_count = 0 local failed_problems = {} diff --git a/lua/cp/setup/init.lua b/lua/cp/setup/init.lua index 1a23c15..6ef5676 100644 --- a/lua/cp/setup/init.lua +++ b/lua/cp/setup/init.lua @@ -26,7 +26,7 @@ function M.set_platform(platform) end 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 first', vim.log.levels.ERROR) return end @@ -35,14 +35,15 @@ function M.setup_problem(contest_id, problem_id, language) local problem_name = contest_id .. (problem_id or '') 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() - 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 - 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 logger.log( '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 - 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 state.set_test_cases(cached_test_cases) logger.log(('using cached test cases (%d)'):format(#cached_test_cases)) - elseif vim.tbl_contains(config.scrapers, state.get_platform()) then - local platform_display_name = constants.PLATFORM_DISPLAY_NAMES[state.get_platform()] - or state.get_platform() + elseif vim.tbl_contains(config.scrapers, state.get_platform() or '') then + local platform_display_name = constants.PLATFORM_DISPLAY_NAMES[state.get_platform() or ''] + or (state.get_platform() or '') logger.log( ('Scraping %s %s %s for test cases, this may take a few seconds...'):format( platform_display_name, @@ -84,10 +85,15 @@ function M.setup_problem(contest_id, problem_id, language) state.set_test_cases(scrape_result.test_cases) 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 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) end @@ -130,27 +136,33 @@ function M.setup_problem(contest_id, problem_id, language) config.hooks.setup_code(ctx) 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)) end 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) return false end local config = config_module.get_config() - if not vim.tbl_contains(config.scrapers, state.get_platform()) then - logger.log('scraping disabled for ' .. state.get_platform(), vim.log.levels.WARN) + if not vim.tbl_contains(config.scrapers, state.get_platform() or '') then + logger.log('scraping disabled for ' .. (state.get_platform() or ''), vim.log.levels.WARN) return false 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 logger.log( 'failed to load contest metadata: ' .. (metadata_result.error or 'unknown error'), @@ -170,7 +182,7 @@ function M.setup_contest(contest_id, language) cache.load() local missing_problems = {} 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 table.insert(missing_problems, prob) end @@ -190,7 +202,7 @@ function M.setup_contest(contest_id, language) end 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 first', vim.log.levels.ERROR) return end @@ -226,7 +238,7 @@ function M.handle_full_setup(cmd) has_metadata = true else 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 problem_ids = vim.tbl_map(function(prob) return prob.id diff --git a/lua/cp/setup/navigation.lua b/lua/cp/setup/navigation.lua index 975bd9c..bab857b 100644 --- a/lua/cp/setup/navigation.lua +++ b/lua/cp/setup/navigation.lua @@ -15,7 +15,8 @@ end function M.navigate_problem(delta, language) 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 logger.log( '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 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 M.get_current_problem = get_current_problem diff --git a/lua/cp/state.lua b/lua/cp/state.lua index 96f7d2f..ae21fc5 100644 --- a/lua/cp/state.lua +++ b/lua/cp/state.lua @@ -1,8 +1,8 @@ local M = {} local state = { - platform = '', - contest_id = '', + platform = nil, + contest_id = nil, problem_id = nil, test_cases = nil, run_panel_active = false, @@ -66,12 +66,12 @@ function M.get_context() end function M.has_context() - return state.platform ~= '' and state.contest_id ~= '' + return state.platform and state.contest_id end function M.reset() - state.platform = '' - state.contest_id = '' + state.platform = nil + state.contest_id = nil state.problem_id = nil state.test_cases = nil state.run_panel_active = false diff --git a/lua/cp/ui/panel.lua b/lua/cp/ui/panel.lua index 2adaade..fd45ea4 100644 --- a/lua/cp/ui/panel.lua +++ b/lua/cp/ui/panel.lua @@ -33,7 +33,7 @@ function M.toggle_run_panel(is_debug) return end - if state.get_platform() == '' then + if not state.get_platform() then logger.log( 'No contest configured. Use :CP to set up first.', vim.log.levels.ERROR @@ -48,8 +48,8 @@ function M.toggle_run_panel(is_debug) local config = config_module.get_config() local ctx = problem.create_context( - state.get_platform(), - state.get_contest_id(), + state.get_platform() or '', + state.get_contest_id() or '', state.get_problem_id(), config ) @@ -173,7 +173,7 @@ function M.toggle_run_panel(is_debug) end 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) if compile_result.success then run.run_all_test_cases(ctx, contest_config, config)