From cc279166cbea63bc10b2c993d89b0f4e8c52cc47 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Thu, 5 Mar 2026 19:13:06 -0500 Subject: [PATCH] feat(commands): implement `:CP open [problem|contest|standings]` Problem: There was no way to open a problem or contest page in the browser from within the plugin; users had to manually navigate to the platform URL. Solution: Add `contest_url` and `standings_url` to `MetadataResult` and persist them via `cache.set_contest_data`. Add `cache.get_open_urls` to assemble all three URLs from cache. Wire up `:CP open` in `commands/init.lua` to call `vim.ui.open` on the resolved URL, warning when none is available (e.g. CSES standings). --- lua/cp/cache.lua | 29 ++++++++++++++++++++++++++++- lua/cp/commands/init.lua | 20 ++++++++++++++++++++ lua/cp/constants.lua | 1 + lua/cp/setup.lua | 9 ++++++++- scrapers/atcoder.py | 2 ++ scrapers/codeforces.py | 2 ++ scrapers/cses.py | 2 ++ scrapers/models.py | 2 ++ 8 files changed, 65 insertions(+), 2 deletions(-) diff --git a/lua/cp/cache.lua b/lua/cp/cache.lua index eb82afa..3370532 100644 --- a/lua/cp/cache.lua +++ b/lua/cp/cache.lua @@ -10,6 +10,8 @@ ---@field name string ---@field display_name string ---@field url string +---@field contest_url string +---@field standings_url string ---@class ContestSummary ---@field display_name string @@ -148,12 +150,16 @@ end ---@param contest_id string ---@param problems Problem[] ---@param url string -function M.set_contest_data(platform, contest_id, problems, url) +---@param contest_url string +---@param standings_url string +function M.set_contest_data(platform, contest_id, problems, url, contest_url, standings_url) vim.validate({ platform = { platform, 'string' }, contest_id = { contest_id, 'string' }, problems = { problems, 'table' }, url = { url, 'string' }, + contest_url = { contest_url, 'string' }, + standings_url = { standings_url, 'string' }, }) cache_data[platform] = cache_data[platform] or {} @@ -165,6 +171,8 @@ function M.set_contest_data(platform, contest_id, problems, url) problems = problems, index_map = {}, url = url, + contest_url = contest_url, + standings_url = standings_url, } for i, p in ipairs(out.problems) do out.index_map[p.id] = i @@ -174,6 +182,25 @@ function M.set_contest_data(platform, contest_id, problems, url) M.save() end +---@param platform string? +---@param contest_id string? +---@param problem_id string? +---@return { problem: string|nil, contest: string|nil, standings: string|nil }|nil +function M.get_open_urls(platform, contest_id, problem_id) + if not platform or not contest_id then + return nil + end + if not cache_data[platform] or not cache_data[platform][contest_id] then + return nil + end + local cd = cache_data[platform][contest_id] + return { + problem = cd.url ~= '' and problem_id and string.format(cd.url, problem_id) or nil, + contest = cd.contest_url ~= '' and cd.contest_url or nil, + standings = cd.standings_url ~= '' and cd.standings_url or nil, + } +end + ---@param platform string ---@param contest_id string function M.clear_contest_data(platform, contest_id) diff --git a/lua/cp/commands/init.lua b/lua/cp/commands/init.lua index 5006cc9..6570748 100644 --- a/lua/cp/commands/init.lua +++ b/lua/cp/commands/init.lua @@ -245,6 +245,12 @@ local function parse_command(args) debug = debug, mode = mode, } + elseif first == 'open' then + local target = args[2] or 'problem' + if not vim.tbl_contains({ 'problem', 'contest', 'standings' }, target) then + return { type = 'error', message = 'Usage: :CP open [problem|contest|standings]' } + end + return { type = 'action', action = 'open', requires_context = true, subcommand = target } elseif first == 'pick' then local language = nil if #args >= 3 and args[2] == '--lang' then @@ -375,6 +381,20 @@ function M.handle_command(opts) require('cp.race').start(cmd.platform, cmd.contest, cmd.language) elseif cmd.action == 'race_stop' then require('cp.race').stop() + elseif cmd.action == 'open' then + local cache = require('cp.cache') + cache.load() + local urls = + cache.get_open_urls(state.get_platform(), state.get_contest_id(), state.get_problem_id()) + local url = urls and urls[cmd.subcommand] + if not url or url == '' then + logger.log( + ("No URL available for '%s'"):format(cmd.subcommand), + { level = vim.log.levels.WARN } + ) + return + end + vim.ui.open(url) elseif cmd.action == 'login' then require('cp.credentials').login(cmd.platform) elseif cmd.action == 'logout' then diff --git a/lua/cp/constants.lua b/lua/cp/constants.lua index 21e8f62..b0dad5b 100644 --- a/lua/cp/constants.lua +++ b/lua/cp/constants.lua @@ -13,6 +13,7 @@ M.ACTIONS = { 'race', 'stress', 'submit', + 'open', } M.PLATFORM_DISPLAY_NAMES = { diff --git a/lua/cp/setup.lua b/lua/cp/setup.lua index a954d62..09b1f3b 100644 --- a/lua/cp/setup.lua +++ b/lua/cp/setup.lua @@ -243,7 +243,14 @@ function M.setup_contest(platform, contest_id, problem_id, language) contest_id, vim.schedule_wrap(function(result) local problems = result.problems or {} - cache.set_contest_data(platform, contest_id, problems, result.url) + cache.set_contest_data( + platform, + contest_id, + problems, + result.url, + result.contest_url or '', + result.standings_url or '' + ) local prov = state.get_provisional() if not prov or prov.platform ~= platform or prov.contest_id ~= contest_id then return diff --git a/scrapers/atcoder.py b/scrapers/atcoder.py index 966940a..b25ea7e 100644 --- a/scrapers/atcoder.py +++ b/scrapers/atcoder.py @@ -606,6 +606,8 @@ class AtcoderScraper(BaseScraper): contest_id=contest_id, problems=problems, url=f"https://atcoder.jp/contests/{contest_id}/tasks/{contest_id}_%s", + contest_url=f"https://atcoder.jp/contests/{contest_id}", + standings_url=f"https://atcoder.jp/contests/{contest_id}/standings", ) except Exception as e: return self._metadata_error(str(e)) diff --git a/scrapers/codeforces.py b/scrapers/codeforces.py index 43d7328..d2e7083 100644 --- a/scrapers/codeforces.py +++ b/scrapers/codeforces.py @@ -223,6 +223,8 @@ class CodeforcesScraper(BaseScraper): contest_id=contest_id, problems=problems, url=f"https://codeforces.com/contest/{contest_id}/problem/%s", + contest_url=f"https://codeforces.com/contest/{contest_id}", + standings_url=f"https://codeforces.com/contest/{contest_id}/standings", ) except Exception as e: return self._metadata_error(str(e)) diff --git a/scrapers/cses.py b/scrapers/cses.py index cbf76e2..bd29af4 100644 --- a/scrapers/cses.py +++ b/scrapers/cses.py @@ -218,6 +218,8 @@ class CSESScraper(BaseScraper): contest_id=contest_id, problems=problems, url="https://cses.fi/problemset/task/%s", + contest_url="https://cses.fi/problemset", + standings_url="", ) async def scrape_contest_list(self) -> ContestListResult: diff --git a/scrapers/models.py b/scrapers/models.py index 4dafc64..2d579cb 100644 --- a/scrapers/models.py +++ b/scrapers/models.py @@ -42,6 +42,8 @@ class MetadataResult(ScrapingResult): contest_id: str = "" problems: list[ProblemSummary] = Field(default_factory=list) url: str + contest_url: str = "" + standings_url: str = "" model_config = ConfigDict(extra="forbid")