feat(cli): :CP <problem_id>

This commit is contained in:
Barrett Ruth 2025-10-05 12:59:50 -04:00
parent 91864b2992
commit 44bfc7317d
3 changed files with 41 additions and 12 deletions

View file

@ -14,6 +14,7 @@ local actions = constants.ACTIONS
---@field message? string
---@field contest? string
---@field platform? string
---@field problem_id? string
--- Turn raw args into normalized structure to later dispatch
---@param args string[] The raw command-line mode args
@ -70,16 +71,14 @@ local function parse_command(args)
end
end
if state.get_platform() and state.get_contest_id() then
local cache = require('cp.cache')
cache.load()
if #args == 1 then
return {
type = 'error',
message = ("invalid subcommand '%s'"):format(first),
type = 'problem_jump',
problem_id = first,
}
end
return { type = 'error', message = 'Unknown command or no contest context' }
return { type = 'error', message = 'Unknown command or no contest context.' }
end
--- Core logic for handling `:CP ...` commands
@ -113,6 +112,30 @@ function M.handle_command(opts)
local picker = require('cp.commands.picker')
picker.handle_pick_action()
end
elseif cmd.type == 'problem_jump' then
local platform = state.get_platform()
local contest_id = state.get_contest_id()
local problem_id = cmd.problem_id
if not (platform and contest_id) then
logger.log('No contest is currently active.', vim.log.levels.ERROR)
return
end
local cache = require('cp.cache')
cache.load()
local contest_data = cache.get_contest_data(platform, contest_id)
if not (contest_data and contest_data.index_map and contest_data.index_map[problem_id]) then
logger.log(
("%s contest '%s' has no problem '%s'."):format(platform, contest_id, problem_id),
vim.log.levels.ERROR
)
return
end
local setup = require('cp.setup')
setup.setup_contest(platform, contest_id, problem_id)
elseif cmd.type == 'cache' then
local cache_commands = require('cp.commands.cache')
cache_commands.handle_cache_command(cmd)