fix a lot of logic
This commit is contained in:
parent
b406c0ce4e
commit
62af1965f8
4 changed files with 57 additions and 86 deletions
|
|
@ -62,9 +62,8 @@ local function parse_command(args)
|
||||||
if vim.tbl_contains(platforms, first) then
|
if vim.tbl_contains(platforms, first) then
|
||||||
if #filtered_args == 1 then
|
if #filtered_args == 1 then
|
||||||
return {
|
return {
|
||||||
type = 'platform_only',
|
type = 'error',
|
||||||
platform = first,
|
message = 'Too few arguments - specify a contest.',
|
||||||
language = language,
|
|
||||||
}
|
}
|
||||||
elseif #filtered_args == 2 then
|
elseif #filtered_args == 2 then
|
||||||
return {
|
return {
|
||||||
|
|
@ -147,12 +146,6 @@ function M.handle_command(opts)
|
||||||
return
|
return
|
||||||
end
|
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
|
if cmd.type == 'contest_setup' then
|
||||||
local setup = require('cp.setup')
|
local setup = require('cp.setup')
|
||||||
if setup.set_platform(cmd.platform) then
|
if setup.set_platform(cmd.platform) then
|
||||||
|
|
|
||||||
|
|
@ -69,9 +69,13 @@ end
|
||||||
function M.scrape_contest_metadata(platform, contest_id, callback)
|
function M.scrape_contest_metadata(platform, contest_id, callback)
|
||||||
run_scraper(platform, 'metadata', { contest_id }, {
|
run_scraper(platform, 'metadata', { contest_id }, {
|
||||||
on_exit = function(result)
|
on_exit = function(result)
|
||||||
if result.success and result.data.problems then
|
if not result.success then
|
||||||
callback(result.data.problems)
|
logger.log(
|
||||||
|
('Failed to scrape metadata for %s contest %s - aborting.'):format(platform, contest_id)
|
||||||
|
)
|
||||||
|
return
|
||||||
end
|
end
|
||||||
|
callback(result.data)
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
@ -89,54 +93,37 @@ end
|
||||||
function M.scrape_problem_tests(platform, contest_id, problem_id, callback)
|
function M.scrape_problem_tests(platform, contest_id, problem_id, callback)
|
||||||
run_scraper(platform, 'tests', { contest_id, problem_id }, {
|
run_scraper(platform, 'tests', { contest_id, problem_id }, {
|
||||||
on_exit = function(result)
|
on_exit = function(result)
|
||||||
if result.success and result.data.tests then
|
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
|
||||||
|
|
||||||
vim.schedule(function()
|
vim.schedule(function()
|
||||||
local mkdir_ok = pcall(vim.fn.mkdir, 'io', 'p')
|
vim.system({ 'mkdir', '-p', 'build', 'io' }):wait()
|
||||||
if mkdir_ok then
|
|
||||||
local config = require('cp.config')
|
local config = require('cp.config')
|
||||||
local base_name = config.default_filename(contest_id, problem_id)
|
local base_name = config.default_filename(contest_id, problem_id)
|
||||||
|
|
||||||
for i, test_case in ipairs(result.tests) do
|
for i, test_case in ipairs(result.data.tests) do
|
||||||
local input_file = 'io/' .. base_name .. '.' .. i .. '.cpin'
|
local input_file = 'io/' .. base_name .. '.' .. i .. '.cpin'
|
||||||
local expected_file = 'io/' .. base_name .. '.' .. i .. '.cpout'
|
local expected_file = 'io/' .. base_name .. '.' .. i .. '.cpout'
|
||||||
|
|
||||||
local input_content = test_case.input:gsub('\r', '')
|
local input_content = test_case.input:gsub('\r', '')
|
||||||
local expected_content = test_case.expected:gsub('\r', '')
|
local expected_content = test_case.expected:gsub('\r', '')
|
||||||
|
|
||||||
pcall(
|
pcall(vim.fn.writefile, vim.split(input_content, '\n', { trimempty = true }), input_file)
|
||||||
vim.fn.writefile,
|
|
||||||
vim.split(input_content, '\n', { trimempty = true }),
|
|
||||||
input_file
|
|
||||||
)
|
|
||||||
pcall(
|
pcall(
|
||||||
vim.fn.writefile,
|
vim.fn.writefile,
|
||||||
vim.split(expected_content, '\n', { trimempty = true }),
|
vim.split(expected_content, '\n', { trimempty = true }),
|
||||||
expected_file
|
expected_file
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local cached_tests = {}
|
callback(result.data)
|
||||||
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
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
callback(result)
|
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ function M.setup_contest(platform, contest_id, problem_id, language)
|
||||||
local config = config_module.get_config()
|
local config = config_module.get_config()
|
||||||
|
|
||||||
if not vim.tbl_contains(config.scrapers, platform) then
|
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
|
return
|
||||||
end
|
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)
|
logger.log('fetching contests problems...', vim.log.levels.INFO, true)
|
||||||
|
|
||||||
scraper.scrape_contest_metadata(platform, contest_id, function(result)
|
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
|
local problems = result.problems
|
||||||
if vim.tbl_isempty(problems) then
|
if vim.tbl_isempty(problems) then
|
||||||
logger.log('no problems found in contest', vim.log.levels.ERROR)
|
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_contest_id(contest_id)
|
||||||
state.set_problem_id(problem_id)
|
state.set_problem_id(problem_id)
|
||||||
-- TODO: why comment this out
|
|
||||||
-- state.set_active_panel('run')
|
|
||||||
|
|
||||||
vim.schedule(function()
|
vim.schedule(function()
|
||||||
local ok, err = pcall(function()
|
local ok, err = pcall(function()
|
||||||
|
|
@ -159,29 +149,30 @@ function M.setup_problem(contest_id, problem_id, language)
|
||||||
if cached_tests then
|
if cached_tests then
|
||||||
state.set_test_cases(cached_tests)
|
state.set_test_cases(cached_tests)
|
||||||
logger.log(('using cached test cases (%d)'):format(#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...')
|
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)
|
scraper.scrape_problem_tests(platform, contest_id, problem_id, function(result)
|
||||||
if result.success then
|
state.set_test_cases(result.tests or {})
|
||||||
logger.log(('loaded %d test cases for %s'):format(#(result.tests or {}), problem_id))
|
|
||||||
if state.get_problem_id() == problem_id then
|
cached_tests = {}
|
||||||
state.set_test_cases(result.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
|
end
|
||||||
else
|
|
||||||
logger.log(
|
cache.set_test_cases(
|
||||||
'failed to load tests: ' .. (result.error or 'unknown error'),
|
platform,
|
||||||
vim.log.levels.ERROR
|
contest_id,
|
||||||
|
problem_id,
|
||||||
|
cached_tests,
|
||||||
|
result.timeout_ms,
|
||||||
|
result.memory_mb
|
||||||
)
|
)
|
||||||
if state.get_problem_id() == problem_id then
|
|
||||||
state.set_test_cases({})
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end)
|
end)
|
||||||
else
|
|
||||||
logger.log(('scraping disabled for %s'):format(platform))
|
|
||||||
state.set_test_cases({})
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -238,7 +238,7 @@ function M.toggle_run_panel(is_debug)
|
||||||
if vim.tbl_isempty(test_state.test_cases) then
|
if vim.tbl_isempty(test_state.test_cases) then
|
||||||
return
|
return
|
||||||
end
|
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()
|
refresh_run_panel()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue