fix a lot of logic

This commit is contained in:
Barrett Ruth 2025-10-01 15:15:04 -04:00
parent b406c0ce4e
commit 62af1965f8
4 changed files with 57 additions and 86 deletions

View file

@ -62,9 +62,8 @@ local function parse_command(args)
if vim.tbl_contains(platforms, first) then
if #filtered_args == 1 then
return {
type = 'platform_only',
platform = first,
language = language,
type = 'error',
message = 'Too few arguments - specify a contest.',
}
elseif #filtered_args == 2 then
return {
@ -147,12 +146,6 @@ function M.handle_command(opts)
return
end
if cmd.type == 'platform_only' then
local setup = require('cp.setup')
setup.set_platform(cmd.platform)
return
end
if cmd.type == 'contest_setup' then
local setup = require('cp.setup')
if setup.set_platform(cmd.platform) then

View file

@ -69,9 +69,13 @@ end
function M.scrape_contest_metadata(platform, contest_id, callback)
run_scraper(platform, 'metadata', { contest_id }, {
on_exit = function(result)
if result.success and result.data.problems then
callback(result.data.problems)
if not result.success then
logger.log(
('Failed to scrape metadata for %s contest %s - aborting.'):format(platform, contest_id)
)
return
end
callback(result.data)
end,
})
end
@ -89,54 +93,37 @@ end
function M.scrape_problem_tests(platform, contest_id, problem_id, callback)
run_scraper(platform, 'tests', { contest_id, problem_id }, {
on_exit = function(result)
if result.success and result.data.tests then
vim.schedule(function()
local mkdir_ok = pcall(vim.fn.mkdir, 'io', 'p')
if mkdir_ok then
local config = require('cp.config')
local base_name = config.default_filename(contest_id, problem_id)
for i, test_case in ipairs(result.tests) do
local input_file = 'io/' .. base_name .. '.' .. i .. '.cpin'
local expected_file = 'io/' .. base_name .. '.' .. i .. '.cpout'
local input_content = test_case.input:gsub('\r', '')
local expected_content = test_case.expected:gsub('\r', '')
pcall(
vim.fn.writefile,
vim.split(input_content, '\n', { trimempty = true }),
input_file
)
pcall(
vim.fn.writefile,
vim.split(expected_content, '\n', { trimempty = true }),
expected_file
)
end
end
end)
local cached_tests = {}
for i, test_case in ipairs(result.tests) do
table.insert(cached_tests, {
index = i,
input = test_case.input,
expected = test_case.expected,
})
end
cache.set_test_cases(
platform,
contest_id,
problem_id,
cached_tests,
result.timeout_ms,
result.memory_mb
if not result.success or not result.data.tests then
logger.log(
'Failed to load tests: ' .. (result.msg or 'unknown error'),
vim.log.levels.ERROR
)
return {}
end
callback(result)
vim.schedule(function()
vim.system({ 'mkdir', '-p', 'build', 'io' }):wait()
local config = require('cp.config')
local base_name = config.default_filename(contest_id, problem_id)
for i, test_case in ipairs(result.data.tests) do
local input_file = 'io/' .. base_name .. '.' .. i .. '.cpin'
local expected_file = 'io/' .. base_name .. '.' .. i .. '.cpout'
local input_content = test_case.input:gsub('\r', '')
local expected_content = test_case.expected:gsub('\r', '')
pcall(vim.fn.writefile, vim.split(input_content, '\n', { trimempty = true }), input_file)
pcall(
vim.fn.writefile,
vim.split(expected_content, '\n', { trimempty = true }),
expected_file
)
end
end)
callback(result.data)
end,
})
end

View file

@ -38,7 +38,7 @@ function M.setup_contest(platform, contest_id, problem_id, language)
local config = config_module.get_config()
if not vim.tbl_contains(config.scrapers, platform) then
logger.log('scraping disabled for ' .. platform, vim.log.levels.WARN)
logger.log(('Scraping disabled for %s - aborting'):format(platform), vim.log.levels.WARN)
return
end
@ -46,14 +46,6 @@ function M.setup_contest(platform, contest_id, problem_id, language)
logger.log('fetching contests problems...', vim.log.levels.INFO, true)
scraper.scrape_contest_metadata(platform, contest_id, function(result)
if not result.success then
logger.log(
'failed to load contest metadata: ' .. (result.error or 'unknown error'),
vim.log.levels.ERROR
)
return
end
local problems = result.problems
if vim.tbl_isempty(problems) then
logger.log('no problems found in contest', vim.log.levels.ERROR)
@ -101,8 +93,6 @@ function M.setup_problem(contest_id, problem_id, language)
state.set_contest_id(contest_id)
state.set_problem_id(problem_id)
-- TODO: why comment this out
-- state.set_active_panel('run')
vim.schedule(function()
local ok, err = pcall(function()
@ -159,29 +149,30 @@ function M.setup_problem(contest_id, problem_id, language)
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
else
logger.log('loading test cases...')
-- TODO: caching should be here, not in scrpaer.lua
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
state.set_test_cases(result.tests or {})
cached_tests = {}
for i, test_case in ipairs(result.tests or {}) do
table.insert(cached_tests, {
index = i,
input = test_case.input,
expected = test_case.expected,
})
end
cache.set_test_cases(
platform,
contest_id,
problem_id,
cached_tests,
result.timeout_ms,
result.memory_mb
)
end)
else
logger.log(('scraping disabled for %s'):format(platform))
state.set_test_cases({})
end
end

View file

@ -238,7 +238,7 @@ function M.toggle_run_panel(is_debug)
if vim.tbl_isempty(test_state.test_cases) then
return
end
test_state.current_index = (test_state.current_index + delta) % #test_state.test_cases
test_state.current_index = (test_state.current_index + delta - 1) % #test_state.test_cases + 1
refresh_run_panel()
end