feat(cses): integrate metadata command format in lua

This commit is contained in:
Barrett Ruth 2025-09-20 14:05:40 -04:00
parent 8e13b8c61d
commit 35545a1ad2
4 changed files with 19 additions and 54 deletions

View file

@ -700,12 +700,11 @@ local function parse_command(args)
} }
elseif #filtered_args == 2 then elseif #filtered_args == 2 then
if first == 'cses' then if first == 'cses' then
return { logger.log(
type = 'cses_problem', 'CSES requires both category and problem ID. Usage: :CP cses <category> <problem_id>',
platform = first, vim.log.levels.ERROR
problem = filtered_args[2], )
language = language, return { type = 'error' }
}
else else
return { return {
type = 'contest_setup', type = 'contest_setup',
@ -851,22 +850,6 @@ function M.handle_command(opts)
return return
end end
if cmd.type == 'cses_problem' then
if set_platform(cmd.platform) then
if vim.tbl_contains(config.scrapers, cmd.platform) then
local metadata_result = scrape.scrape_contest_metadata(cmd.platform, '')
if not metadata_result.success then
logger.log(
'failed to load contest metadata: ' .. (metadata_result.error or 'unknown error'),
vim.log.levels.WARN
)
end
end
setup_problem(cmd.problem, nil, cmd.language)
end
return
end
if cmd.type == 'problem_switch' then if cmd.type == 'problem_switch' then
if state.platform == 'cses' then if state.platform == 'cses' then
setup_problem(cmd.problem, nil, cmd.language) setup_problem(cmd.problem, nil, cmd.language)

View file

@ -89,29 +89,16 @@ function M.scrape_contest_metadata(platform, contest_id)
local plugin_path = get_plugin_path() local plugin_path = get_plugin_path()
local args local args = {
if platform == 'cses' then 'uv',
args = { 'run',
'uv', '--directory',
'run', plugin_path,
'--directory', '-m',
plugin_path, 'scrapers.' .. platform,
'-m', 'metadata',
'scrapers.' .. platform, contest_id,
'metadata', }
}
else
args = {
'uv',
'run',
'--directory',
plugin_path,
'-m',
'scrapers.' .. platform,
'metadata',
contest_id,
}
end
local result = vim local result = vim
.system(args, { .system(args, {
@ -140,12 +127,7 @@ function M.scrape_contest_metadata(platform, contest_id)
return data return data
end end
local problems_list local problems_list = data.problems or {}
if platform == 'cses' then
problems_list = data.categories and data.categories['CSES Problem Set'] or {}
else
problems_list = data.problems or {}
end
cache.set_contest_data(platform, contest_id, problems_list) cache.set_contest_data(platform, contest_id, problems_list)
return { return {

View file

@ -96,7 +96,7 @@ describe('cp command parsing', function()
end) end)
it('handles cses problem command', function() it('handles cses problem command', function()
local opts = { fargs = { 'cses', '1234' } } local opts = { fargs = { 'cses', 'sorting_and_searching', '1234' } }
assert.has_no_errors(function() assert.has_no_errors(function()
cp.handle_command(opts) cp.handle_command(opts)

View file

@ -214,7 +214,7 @@ describe('cp.scrape', function()
end) end)
it('constructs correct command for cses metadata', function() it('constructs correct command for cses metadata', function()
scrape.scrape_contest_metadata('cses', 'problemset') scrape.scrape_contest_metadata('cses', 'sorting_and_searching')
local metadata_call = nil local metadata_call = nil
for _, call in ipairs(mock_system_calls) do for _, call in ipairs(mock_system_calls) do
@ -227,7 +227,7 @@ describe('cp.scrape', function()
assert.is_not_nil(metadata_call) assert.is_not_nil(metadata_call)
assert.equals('uv', metadata_call.cmd[1]) assert.equals('uv', metadata_call.cmd[1])
assert.is_true(vim.tbl_contains(metadata_call.cmd, 'metadata')) assert.is_true(vim.tbl_contains(metadata_call.cmd, 'metadata'))
assert.is_false(vim.tbl_contains(metadata_call.cmd, 'problemset')) assert.is_true(vim.tbl_contains(metadata_call.cmd, 'sorting_and_searching'))
end) end)
it('handles subprocess execution failure', function() it('handles subprocess execution failure', function()