fix: synchronous problem fetch
This commit is contained in:
parent
91ce43e529
commit
b406c0ce4e
12 changed files with 140 additions and 352 deletions
|
|
@ -11,13 +11,10 @@
|
||||||
|
|
||||||
---@class ContestListData
|
---@class ContestListData
|
||||||
---@field contests table[]
|
---@field contests table[]
|
||||||
---@field cached_at number
|
|
||||||
|
|
||||||
---@class ContestData
|
---@class ContestData
|
||||||
---@field problems Problem[]
|
---@field problems Problem[]
|
||||||
---@field scraped_at string
|
|
||||||
---@field test_cases? CachedTestCase[]
|
---@field test_cases? CachedTestCase[]
|
||||||
---@field test_cases_cached_at? number
|
|
||||||
---@field timeout_ms? number
|
---@field timeout_ms? number
|
||||||
---@field memory_mb? number
|
---@field memory_mb? number
|
||||||
---@field interactive? boolean
|
---@field interactive? boolean
|
||||||
|
|
@ -121,7 +118,6 @@ function M.set_contest_data(platform, contest_id, problems)
|
||||||
|
|
||||||
cache_data[platform][contest_id] = {
|
cache_data[platform][contest_id] = {
|
||||||
problems = problems,
|
problems = problems,
|
||||||
scraped_at = os.date('%Y-%m-%d'),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
M.save()
|
M.save()
|
||||||
|
|
@ -194,7 +190,6 @@ function M.set_test_cases(
|
||||||
end
|
end
|
||||||
|
|
||||||
cache_data[platform][problem_key].test_cases = test_cases
|
cache_data[platform][problem_key].test_cases = test_cases
|
||||||
cache_data[platform][problem_key].test_cases_cached_at = os.time()
|
|
||||||
if timeout_ms then
|
if timeout_ms then
|
||||||
cache_data[platform][problem_key].timeout_ms = timeout_ms
|
cache_data[platform][problem_key].timeout_ms = timeout_ms
|
||||||
end
|
end
|
||||||
|
|
@ -273,7 +268,6 @@ function M.set_contest_list(platform, contests)
|
||||||
|
|
||||||
cache_data.contest_lists[platform] = {
|
cache_data.contest_lists[platform] = {
|
||||||
contests = contests,
|
contests = contests,
|
||||||
cached_at = os.time(),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
M.save()
|
M.save()
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ local platforms = constants.PLATFORMS
|
||||||
local actions = constants.ACTIONS
|
local actions = constants.ACTIONS
|
||||||
|
|
||||||
local function parse_command(args)
|
local function parse_command(args)
|
||||||
if #args == 0 then
|
if vim.tbl_isempty(args) then
|
||||||
return {
|
return {
|
||||||
type = 'restore_from_file',
|
type = 'restore_from_file',
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -270,7 +270,7 @@ function M.setup(user_config)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if #available_langs == 0 then
|
if vim.tbl_isemtpy(available_langs) then
|
||||||
error('No language configurations found')
|
error('No language configurations found')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ local function contest_picker(platform)
|
||||||
local fzf = require('fzf-lua')
|
local fzf = require('fzf-lua')
|
||||||
local contests = picker_utils.get_contests_for_platform(platform)
|
local contests = picker_utils.get_contests_for_platform(platform)
|
||||||
|
|
||||||
if #contests == 0 then
|
if vim.tbl_isempty(contests) then
|
||||||
vim.notify(
|
vim.notify(
|
||||||
('No contests found for platform: %s'):format(platform_display_name),
|
('No contests found for platform: %s'):format(platform_display_name),
|
||||||
vim.log.levels.WARN
|
vim.log.levels.WARN
|
||||||
|
|
@ -27,7 +27,7 @@ local function contest_picker(platform)
|
||||||
},
|
},
|
||||||
actions = {
|
actions = {
|
||||||
['default'] = function(selected)
|
['default'] = function(selected)
|
||||||
if not selected or #selected == 0 then
|
if vim.tbl_isempty(selected) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -65,7 +65,7 @@ function M.pick()
|
||||||
prompt = 'Select Platform> ',
|
prompt = 'Select Platform> ',
|
||||||
actions = {
|
actions = {
|
||||||
['default'] = function(selected)
|
['default'] = function(selected)
|
||||||
if not selected or #selected == 0 then
|
if vim.tbl_isempty(selected) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ local M = {}
|
||||||
local cache = require('cp.cache')
|
local cache = require('cp.cache')
|
||||||
local config = require('cp.config').get_config()
|
local config = require('cp.config').get_config()
|
||||||
local logger = require('cp.log')
|
local logger = require('cp.log')
|
||||||
local utils = require('cp.utils')
|
local scraper = require('cp.scraper')
|
||||||
|
|
||||||
---@class cp.PlatformItem
|
---@class cp.PlatformItem
|
||||||
---@field id string Platform identifier (e.g. "codeforces", "atcoder", "cses")
|
---@field id string Platform identifier (e.g. "codeforces", "atcoder", "cses")
|
||||||
|
|
@ -40,157 +40,33 @@ end
|
||||||
---@param platform string Platform identifier (e.g. "codeforces", "atcoder")
|
---@param platform string Platform identifier (e.g. "codeforces", "atcoder")
|
||||||
---@return cp.ContestItem[]
|
---@return cp.ContestItem[]
|
||||||
function M.get_contests_for_platform(platform)
|
function M.get_contests_for_platform(platform)
|
||||||
logger.log('loading contests...', vim.log.levels.INFO, true)
|
logger.log(('Loading %s contests..'):format(platform), vim.log.levels.INFO, true)
|
||||||
|
|
||||||
cache.load()
|
cache.load()
|
||||||
local cached_contests = cache.get_contest_list(platform)
|
|
||||||
if cached_contests then
|
|
||||||
return cached_contests
|
|
||||||
end
|
|
||||||
|
|
||||||
if not utils.setup_python_env() then
|
local picker_contests = cache.get_contest_list(platform) or {}
|
||||||
return {}
|
|
||||||
end
|
|
||||||
|
|
||||||
local plugin_path = utils.get_plugin_path()
|
if vim.tbl_isempty(picker_contests) then
|
||||||
local cmd = {
|
logger.log(('Cache miss on %s contests'):format(platform))
|
||||||
'uv',
|
local contests = scraper.scrape_contest_list(platform)
|
||||||
'run',
|
|
||||||
'--directory',
|
|
||||||
plugin_path,
|
|
||||||
'-m',
|
|
||||||
'scrapers.' .. platform,
|
|
||||||
'contests',
|
|
||||||
}
|
|
||||||
|
|
||||||
local result = vim
|
cache.set_contest_list(platform, contests)
|
||||||
.system(cmd, {
|
|
||||||
cwd = plugin_path,
|
|
||||||
text = true,
|
|
||||||
timeout = 30000,
|
|
||||||
})
|
|
||||||
:wait()
|
|
||||||
|
|
||||||
logger.log(('exit code: %d, stdout length: %d'):format(result.code, #(result.stdout or '')))
|
for _, contest in ipairs(contests or {}) do
|
||||||
if result.stderr and #result.stderr > 0 then
|
table.insert(picker_contests, {
|
||||||
logger.log(('stderr: %s'):format(result.stderr:sub(1, 200)))
|
id = contest.id,
|
||||||
end
|
name = contest.name,
|
||||||
|
display_name = contest.display_name,
|
||||||
if result.code ~= 0 then
|
|
||||||
logger.log(
|
|
||||||
('Failed to load contests: %s'):format(result.stderr or 'unknown error'),
|
|
||||||
vim.log.levels.ERROR
|
|
||||||
)
|
|
||||||
return {}
|
|
||||||
end
|
|
||||||
|
|
||||||
logger.log(('stdout preview: %s'):format(result.stdout:sub(1, 100)))
|
|
||||||
|
|
||||||
local ok, data = pcall(vim.json.decode, result.stdout)
|
|
||||||
if not ok then
|
|
||||||
logger.log(('JSON parse error: %s'):format(tostring(data)), vim.log.levels.ERROR)
|
|
||||||
return {}
|
|
||||||
end
|
|
||||||
if not data.success then
|
|
||||||
logger.log(
|
|
||||||
('Scraper returned success=false: %s'):format(data.error or 'no error message'),
|
|
||||||
vim.log.levels.ERROR
|
|
||||||
)
|
|
||||||
return {}
|
|
||||||
end
|
|
||||||
|
|
||||||
local contests = {}
|
|
||||||
for _, contest in ipairs(data.contests or {}) do
|
|
||||||
table.insert(contests, {
|
|
||||||
id = contest.id,
|
|
||||||
name = contest.name,
|
|
||||||
display_name = contest.display_name,
|
|
||||||
})
|
|
||||||
end
|
|
||||||
|
|
||||||
logger.log(('loaded %d contests'):format(#contests))
|
|
||||||
return contests
|
|
||||||
end
|
|
||||||
|
|
||||||
---@param platform string Platform identifier
|
|
||||||
---@param contest_id string Contest identifier
|
|
||||||
---@return cp.ProblemItem[]
|
|
||||||
function M.get_problems_for_contest(platform, contest_id)
|
|
||||||
logger.log('loading contest problems...', vim.log.levels.INFO, true)
|
|
||||||
|
|
||||||
local problems = {}
|
|
||||||
|
|
||||||
cache.load()
|
|
||||||
local contest_data = cache.get_contest_data(platform, contest_id)
|
|
||||||
if contest_data and contest_data.problems then
|
|
||||||
for _, problem in ipairs(contest_data.problems) do
|
|
||||||
table.insert(problems, {
|
|
||||||
id = problem.id,
|
|
||||||
name = problem.name,
|
|
||||||
display_name = problem.name,
|
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
return problems
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if not utils.setup_python_env() then
|
logger.log(
|
||||||
return problems
|
('Loaded %d %s contests.'):format(#picker_contests, platform),
|
||||||
end
|
vim.log.levels.INFO,
|
||||||
|
true
|
||||||
local plugin_path = utils.get_plugin_path()
|
)
|
||||||
local cmd = {
|
return picker_contests
|
||||||
'uv',
|
|
||||||
'run',
|
|
||||||
'--directory',
|
|
||||||
plugin_path,
|
|
||||||
'-m',
|
|
||||||
'scrapers.' .. platform,
|
|
||||||
'metadata',
|
|
||||||
contest_id,
|
|
||||||
}
|
|
||||||
|
|
||||||
local result = vim
|
|
||||||
.system(cmd, {
|
|
||||||
cwd = plugin_path,
|
|
||||||
text = true,
|
|
||||||
timeout = 30000,
|
|
||||||
})
|
|
||||||
:wait()
|
|
||||||
|
|
||||||
if result.code ~= 0 then
|
|
||||||
logger.log(
|
|
||||||
('Failed to scrape contest: %s'):format(result.stderr or 'unknown error'),
|
|
||||||
vim.log.levels.ERROR
|
|
||||||
)
|
|
||||||
return problems
|
|
||||||
end
|
|
||||||
|
|
||||||
local ok, data = pcall(vim.json.decode, result.stdout)
|
|
||||||
if not ok then
|
|
||||||
logger.log('Failed to parse contest data', vim.log.levels.ERROR)
|
|
||||||
return problems
|
|
||||||
end
|
|
||||||
if not data.success then
|
|
||||||
logger.log(data.error or 'Contest scraping failed', vim.log.levels.ERROR)
|
|
||||||
return problems
|
|
||||||
end
|
|
||||||
|
|
||||||
if not data.problems or #data.problems == 0 then
|
|
||||||
logger.log('Contest has no problems available', vim.log.levels.WARN)
|
|
||||||
return problems
|
|
||||||
end
|
|
||||||
|
|
||||||
cache.set_contest_data(platform, contest_id, data.problems)
|
|
||||||
|
|
||||||
for _, problem in ipairs(data.problems) do
|
|
||||||
table.insert(problems, {
|
|
||||||
id = problem.id,
|
|
||||||
name = problem.name,
|
|
||||||
display_name = problem.name,
|
|
||||||
})
|
|
||||||
end
|
|
||||||
|
|
||||||
return problems
|
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param platform string Platform identifier
|
---@param platform string Platform identifier
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ local function contest_picker(opts, platform)
|
||||||
local platform_display_name = constants.PLATFORM_DISPLAY_NAMES[platform] or platform
|
local platform_display_name = constants.PLATFORM_DISPLAY_NAMES[platform] or platform
|
||||||
local contests = picker_utils.get_contests_for_platform(platform)
|
local contests = picker_utils.get_contests_for_platform(platform)
|
||||||
|
|
||||||
if #contests == 0 then
|
if vim.tbl_isempty(contests) then
|
||||||
vim.notify(
|
vim.notify(
|
||||||
('No contests found for platform: %s'):format(platform_display_name),
|
('No contests found for platform: %s'):format(platform_display_name),
|
||||||
vim.log.levels.WARN
|
vim.log.levels.WARN
|
||||||
|
|
|
||||||
|
|
@ -66,9 +66,9 @@ end
|
||||||
local function parse_test_cases_from_cache(platform, contest_id, problem_id)
|
local function parse_test_cases_from_cache(platform, contest_id, problem_id)
|
||||||
local cache = require('cp.cache')
|
local cache = require('cp.cache')
|
||||||
cache.load()
|
cache.load()
|
||||||
local cached_test_cases = cache.get_test_cases(platform, contest_id, problem_id)
|
local cached_test_cases = cache.get_test_cases(platform, contest_id, problem_id) or {}
|
||||||
|
|
||||||
if not cached_test_cases or #cached_test_cases == 0 then
|
if vim.tbl_isempty(cached_test_cases) then
|
||||||
return {}
|
return {}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -299,9 +299,9 @@ function M.load_test_cases(state)
|
||||||
state.get_platform() or '',
|
state.get_platform() or '',
|
||||||
state.get_contest_id() or '',
|
state.get_contest_id() or '',
|
||||||
state.get_problem_id()
|
state.get_problem_id()
|
||||||
)
|
) or {}
|
||||||
|
|
||||||
if #test_cases == 0 then
|
if vim.tbl_isempty(test_cases) then
|
||||||
local input_file = state.get_input_file()
|
local input_file = state.get_input_file()
|
||||||
local expected_file = state.get_expected_file()
|
local expected_file = state.get_expected_file()
|
||||||
test_cases = parse_test_cases_from_files(input_file, expected_file)
|
test_cases = parse_test_cases_from_files(input_file, expected_file)
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,42 @@
|
||||||
local M = {}
|
local M = {}
|
||||||
local cache = require('cp.cache')
|
|
||||||
local utils = require('cp.utils')
|
local utils = require('cp.utils')
|
||||||
|
|
||||||
local function run_scraper(platform, subcommand, args, callback)
|
local logger = require('cp.log')
|
||||||
|
|
||||||
|
local function syshandle(result)
|
||||||
|
if result.code ~= 0 then
|
||||||
|
local msg = 'Scraper failed: ' .. (result.error or result.stderr or 'Unknown error')
|
||||||
|
logger.log(msg, vim.log.levels.ERROR)
|
||||||
|
return {
|
||||||
|
success = false,
|
||||||
|
error = msg,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
local ok, data = pcall(vim.json.decode, result.stdout)
|
||||||
|
if not ok then
|
||||||
|
local msg = 'Failed to parse scraper output: ' .. tostring(data)
|
||||||
|
logger.log(msg, vim.log.levels.ERROR)
|
||||||
|
return {
|
||||||
|
success = false,
|
||||||
|
error = msg,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
return {
|
||||||
|
success = true,
|
||||||
|
data = data,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
local function run_scraper(platform, subcommand, args, opts)
|
||||||
if not utils.setup_python_env() then
|
if not utils.setup_python_env() then
|
||||||
callback({ success = false, error = 'Python environment setup failed' })
|
local msg = 'Python environment setup failed'
|
||||||
return
|
logger.log(msg, vim.log.levels.ERROR)
|
||||||
|
return {
|
||||||
|
success = false,
|
||||||
|
message = msg,
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
local plugin_path = utils.get_plugin_path()
|
local plugin_path = utils.get_plugin_path()
|
||||||
|
|
@ -18,114 +49,96 @@ local function run_scraper(platform, subcommand, args, callback)
|
||||||
'scrapers.' .. platform,
|
'scrapers.' .. platform,
|
||||||
subcommand,
|
subcommand,
|
||||||
}
|
}
|
||||||
|
vim.list_extend(cmd, args)
|
||||||
|
|
||||||
for _, arg in ipairs(args or {}) do
|
local sysopts = {
|
||||||
table.insert(cmd, arg)
|
|
||||||
end
|
|
||||||
|
|
||||||
vim.system(cmd, {
|
|
||||||
cwd = plugin_path,
|
|
||||||
text = true,
|
text = true,
|
||||||
timeout = 30000,
|
timeout = 30000,
|
||||||
}, function(result)
|
}
|
||||||
if result.code ~= 0 then
|
|
||||||
callback({
|
|
||||||
success = false,
|
|
||||||
error = 'Scraper failed: ' .. (result.stderr or 'Unknown error'),
|
|
||||||
})
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local ok, data = pcall(vim.json.decode, result.stdout)
|
if opts.sync then
|
||||||
if not ok then
|
local result = vim.system(cmd, sysopts):wait()
|
||||||
callback({
|
return syshandle(result)
|
||||||
success = false,
|
else
|
||||||
error = 'Failed to parse scraper output: ' .. tostring(data),
|
vim.system(cmd, sysopts, function(result)
|
||||||
})
|
return opts.on_exit(syshandle(result))
|
||||||
return
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
callback(data)
|
|
||||||
end)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.scrape_contest_metadata(platform, contest_id, callback)
|
function M.scrape_contest_metadata(platform, contest_id, callback)
|
||||||
cache.load()
|
run_scraper(platform, 'metadata', { contest_id }, {
|
||||||
|
on_exit = function(result)
|
||||||
local cached = cache.get_contest_data(platform, contest_id)
|
if result.success and result.data.problems then
|
||||||
if cached then
|
callback(result.data.problems)
|
||||||
callback({ success = true, problems = cached.problems })
|
end
|
||||||
return
|
end,
|
||||||
end
|
})
|
||||||
|
|
||||||
run_scraper(platform, 'metadata', { contest_id }, function(result)
|
|
||||||
if result.success and result.problems then
|
|
||||||
cache.set_contest_data(platform, contest_id, result.problems)
|
|
||||||
end
|
|
||||||
callback(result)
|
|
||||||
end)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.scrape_contest_list(platform, callback)
|
function M.scrape_contest_list(platform)
|
||||||
cache.load()
|
local result = run_scraper(platform, 'contests', {}, { sync = true })
|
||||||
|
if not result.success or not result.data.contests then
|
||||||
local cached = cache.get_contest_list(platform)
|
logger.log(('Could not scrape contests list for platform %s: %s'):format(platform, result.msg))
|
||||||
if cached then
|
return {}
|
||||||
callback({ success = true, contests = cached })
|
|
||||||
return
|
|
||||||
end
|
end
|
||||||
|
|
||||||
run_scraper(platform, 'contests', {}, function(result)
|
return result.data.contests
|
||||||
if result.success and result.contests then
|
|
||||||
cache.set_contest_list(platform, result.contests)
|
|
||||||
end
|
|
||||||
callback(result)
|
|
||||||
end)
|
|
||||||
end
|
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 }, function(result)
|
run_scraper(platform, 'tests', { contest_id, problem_id }, {
|
||||||
if result.success and result.tests then
|
on_exit = function(result)
|
||||||
vim.schedule(function()
|
if result.success and result.data.tests then
|
||||||
local mkdir_ok = pcall(vim.fn.mkdir, 'io', 'p')
|
vim.schedule(function()
|
||||||
if mkdir_ok then
|
local mkdir_ok = pcall(vim.fn.mkdir, 'io', 'p')
|
||||||
local config = require('cp.config')
|
if mkdir_ok then
|
||||||
local base_name = config.default_filename(contest_id, problem_id)
|
local config = require('cp.config')
|
||||||
|
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.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(vim.fn.writefile, vim.split(input_content, '\n', true), input_file)
|
pcall(
|
||||||
pcall(vim.fn.writefile, vim.split(expected_content, '\n', true), expected_file)
|
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
|
||||||
end
|
end)
|
||||||
end)
|
|
||||||
|
|
||||||
local cached_tests = {}
|
local cached_tests = {}
|
||||||
for i, test_case in ipairs(result.tests) do
|
for i, test_case in ipairs(result.tests) do
|
||||||
table.insert(cached_tests, {
|
table.insert(cached_tests, {
|
||||||
index = i,
|
index = i,
|
||||||
input = test_case.input,
|
input = test_case.input,
|
||||||
expected = test_case.expected,
|
expected = test_case.expected,
|
||||||
})
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
cache.set_test_cases(
|
||||||
|
platform,
|
||||||
|
contest_id,
|
||||||
|
problem_id,
|
||||||
|
cached_tests,
|
||||||
|
result.timeout_ms,
|
||||||
|
result.memory_mb
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
cache.set_test_cases(
|
callback(result)
|
||||||
platform,
|
end,
|
||||||
contest_id,
|
})
|
||||||
problem_id,
|
|
||||||
cached_tests,
|
|
||||||
result.timeout_ms,
|
|
||||||
result.memory_mb
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
callback(result)
|
|
||||||
end)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
|
|
@ -28,10 +28,10 @@ function M.set_platform(platform)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- NOTE: this is backwards
|
||||||
function M.setup_contest(platform, contest_id, problem_id, language)
|
function M.setup_contest(platform, contest_id, problem_id, language)
|
||||||
if not state.get_platform() then
|
if not state.get_platform() then
|
||||||
logger.log('No platform configured. Use :CP <platform> <contest> [...] first.')
|
logger.log('No platform configured. Use :CP <platform> <contest> [...] first.')
|
||||||
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -42,6 +42,7 @@ function M.setup_contest(platform, contest_id, problem_id, language)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
state.set_contest_id(contest_id)
|
||||||
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)
|
||||||
|
|
@ -54,14 +55,13 @@ function M.setup_contest(platform, contest_id, problem_id, language)
|
||||||
end
|
end
|
||||||
|
|
||||||
local problems = result.problems
|
local problems = result.problems
|
||||||
if not problems or #problems == 0 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)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
logger.log(('found %d problems'):format(#problems))
|
logger.log(('found %d problems'):format(#problems))
|
||||||
|
|
||||||
state.set_contest_id(contest_id)
|
|
||||||
local target_problem = problem_id or problems[1].id
|
local target_problem = problem_id or problems[1].id
|
||||||
|
|
||||||
if problem_id then
|
if problem_id then
|
||||||
|
|
@ -81,6 +81,7 @@ function M.setup_contest(platform, contest_id, problem_id, language)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- NOTE: should setup buffer without a name, then save it with proper name later for immediate editing
|
||||||
M.setup_problem(contest_id, target_problem, language)
|
M.setup_problem(contest_id, target_problem, language)
|
||||||
|
|
||||||
M.scrape_remaining_problems(platform, contest_id, problems)
|
M.scrape_remaining_problems(platform, contest_id, problems)
|
||||||
|
|
@ -161,6 +162,7 @@ function M.setup_problem(contest_id, problem_id, language)
|
||||||
elseif vim.tbl_contains(config.scrapers, platform) then
|
elseif vim.tbl_contains(config.scrapers, platform) then
|
||||||
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
|
if result.success then
|
||||||
logger.log(('loaded %d test cases for %s'):format(#(result.tests or {}), problem_id))
|
logger.log(('loaded %d test cases for %s'):format(#(result.tests or {}), problem_id))
|
||||||
|
|
@ -194,7 +196,7 @@ function M.scrape_remaining_problems(platform, contest_id, problems)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if #missing_problems == 0 then
|
if vim.tbl_isempty(missing_problems) then
|
||||||
logger.log('all problems already cached')
|
logger.log('all problems already cached')
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -235,7 +235,7 @@ function M.toggle_run_panel(is_debug)
|
||||||
|
|
||||||
local function navigate_test_case(delta)
|
local function navigate_test_case(delta)
|
||||||
local test_state = run.get_run_panel_state()
|
local test_state = run.get_run_panel_state()
|
||||||
if #test_state.test_cases == 0 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) % #test_state.test_cases
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ describe('cp.execute', function()
|
||||||
|
|
||||||
vim.system = function(cmd, opts)
|
vim.system = function(cmd, opts)
|
||||||
table.insert(mock_system_calls, { cmd = cmd, opts = opts })
|
table.insert(mock_system_calls, { cmd = cmd, opts = opts })
|
||||||
if not cmd or #cmd == 0 then
|
if vim.tbl_isempty(cmd) then
|
||||||
return {
|
return {
|
||||||
wait = function()
|
wait = function()
|
||||||
return { code = 0, stdout = '', stderr = '' }
|
return { code = 0, stdout = '', stderr = '' }
|
||||||
|
|
|
||||||
|
|
@ -90,101 +90,4 @@ describe('cp.picker', function()
|
||||||
assert.equals('Beginner Contest 123 (ABC)', contests[1].display_name)
|
assert.equals('Beginner Contest 123 (ABC)', contests[1].display_name)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('get_problems_for_contest', function()
|
|
||||||
it('returns problems from cache when available', function()
|
|
||||||
local cache = require('cp.cache')
|
|
||||||
cache.load = function() end
|
|
||||||
cache.get_contest_data = function(_, _)
|
|
||||||
return {
|
|
||||||
problems = {
|
|
||||||
{ id = 'a', name = 'Problem A' },
|
|
||||||
{ id = 'b', name = 'Problem B' },
|
|
||||||
},
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
local problems = picker.get_problems_for_contest('test_platform', 'test_contest')
|
|
||||||
assert.is_table(problems)
|
|
||||||
assert.equals(2, #problems)
|
|
||||||
assert.equals('a', problems[1].id)
|
|
||||||
assert.equals('Problem A', problems[1].name)
|
|
||||||
assert.equals('Problem A', problems[1].display_name)
|
|
||||||
end)
|
|
||||||
|
|
||||||
it('falls back to scraping when cache miss', function()
|
|
||||||
local cache = require('cp.cache')
|
|
||||||
local utils = require('cp.utils')
|
|
||||||
|
|
||||||
cache.load = function() end
|
|
||||||
cache.get_contest_data = function(_, _)
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
cache.set_contest_data = function() end
|
|
||||||
|
|
||||||
utils.setup_python_env = function()
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
utils.get_plugin_path = function()
|
|
||||||
return '/tmp'
|
|
||||||
end
|
|
||||||
|
|
||||||
vim.system = function()
|
|
||||||
return {
|
|
||||||
wait = function()
|
|
||||||
return {
|
|
||||||
code = 0,
|
|
||||||
stdout = vim.json.encode({
|
|
||||||
success = true,
|
|
||||||
problems = {
|
|
||||||
{ id = 'x', name = 'Problem X' },
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
end,
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
picker = spec_helper.fresh_require('cp.pickers', { 'cp.pickers.init' })
|
|
||||||
|
|
||||||
local problems = picker.get_problems_for_contest('test_platform', 'test_contest')
|
|
||||||
assert.is_table(problems)
|
|
||||||
assert.equals(1, #problems)
|
|
||||||
assert.equals('x', problems[1].id)
|
|
||||||
end)
|
|
||||||
|
|
||||||
it('returns empty list when scraping fails', function()
|
|
||||||
local cache = require('cp.cache')
|
|
||||||
local utils = require('cp.utils')
|
|
||||||
|
|
||||||
cache.load = function() end
|
|
||||||
cache.get_contest_data = function(_, _)
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
|
|
||||||
utils.setup_python_env = function()
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
utils.get_plugin_path = function()
|
|
||||||
return '/tmp'
|
|
||||||
end
|
|
||||||
|
|
||||||
vim.system = function()
|
|
||||||
return {
|
|
||||||
wait = function()
|
|
||||||
return {
|
|
||||||
code = 1,
|
|
||||||
stderr = 'Scraping failed',
|
|
||||||
}
|
|
||||||
end,
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
picker = spec_helper.fresh_require('cp.pickers', { 'cp.pickers.init' })
|
|
||||||
|
|
||||||
local problems = picker.get_problems_for_contest('test_platform', 'test_contest')
|
|
||||||
assert.is_table(problems)
|
|
||||||
assert.equals(0, #problems)
|
|
||||||
end)
|
|
||||||
end)
|
|
||||||
end)
|
end)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue