From 693d3bf1728ff6ced49cc31847c50889c32c37b0 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 7 Mar 2026 18:13:12 -0500 Subject: [PATCH] feat(commands): standalone :CP login/logout with auto-restore Problem: `:CP login` and `:CP logout` only worked as `:CP login`. Users with an active platform or in a registered buffer had to type the platform name redundantly. Solution: Parse standalone `login`/`logout`/`signup` as action commands with `requires_context = true`, resolving the platform from state (or auto-restoring from the current buffer). Add `vim.cmd.redraw()` after auto-restore so the screen updates before any prompt. Add `login`/ `logout` to top-level completion when a platform is active. --- doc/cp.nvim.txt | 2 ++ lua/cp/commands/init.lua | 32 ++++++++++++++++++++++++++------ plugin/cp.lua | 4 ++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/doc/cp.nvim.txt b/doc/cp.nvim.txt index 4fc684c..670bdab 100644 --- a/doc/cp.nvim.txt +++ b/doc/cp.nvim.txt @@ -453,6 +453,7 @@ COMMANDS *cp-commands* any previously saved credentials. If [platform] is omitted, uses the active platform. Examples: > + :CP login :CP login atcoder :CP login codeforces < @@ -460,6 +461,7 @@ COMMANDS *cp-commands* Remove stored credentials for a platform. If [platform] is omitted, uses the active platform. Examples: > + :CP logout :CP logout atcoder < :CP {platform} signup diff --git a/lua/cp/commands/init.lua b/lua/cp/commands/init.lua index 9ea34a4..8731a8f 100644 --- a/lua/cp/commands/init.lua +++ b/lua/cp/commands/init.lua @@ -326,6 +326,10 @@ local function parse_command(args) end end + if (first == 'login' or first == 'logout' or first == 'signup') and #args == 1 then + return { type = 'action', action = first, requires_context = true, platform = nil } + end + if #args == 1 then return { type = 'problem_jump', @@ -378,6 +382,7 @@ function M.handle_command(opts) if not restore.restore_from_current_file() then return end + vim.cmd.redraw() end local setup = require('cp.setup') @@ -421,20 +426,35 @@ function M.handle_command(opts) end vim.ui.open(url) elseif cmd.action == 'login' then - if not check_platform_enabled(cmd.platform) then + local p = cmd.platform or state.get_platform() + if not p then + logger.log('No platform active. Usage: :CP login', { level = vim.log.levels.ERROR }) return end - require('cp.credentials').login(cmd.platform) + if not check_platform_enabled(p) then + return + end + require('cp.credentials').login(p) elseif cmd.action == 'logout' then - if not check_platform_enabled(cmd.platform) then + local p = cmd.platform or state.get_platform() + if not p then + logger.log('No platform active. Usage: :CP logout', { level = vim.log.levels.ERROR }) return end - require('cp.credentials').logout(cmd.platform) + if not check_platform_enabled(p) then + return + end + require('cp.credentials').logout(p) elseif cmd.action == 'signup' then - local url = constants.SIGNUP_URLS[cmd.platform] + local p = cmd.platform or state.get_platform() + if not p then + logger.log('No platform active. Usage: :CP signup', { level = vim.log.levels.ERROR }) + return + end + local url = constants.SIGNUP_URLS[p] if not url then logger.log( - ("No signup URL available for '%s'"):format(cmd.platform), + ("No signup URL available for '%s'"):format(p), { level = vim.log.levels.WARN } ) return diff --git a/plugin/cp.lua b/plugin/cp.lua index 531a5f3..b1430f2 100644 --- a/plugin/cp.lua +++ b/plugin/cp.lua @@ -43,6 +43,10 @@ end, { vim.list_extend(candidates, platforms) table.insert(candidates, 'cache') table.insert(candidates, 'pick') + if platform then + table.insert(candidates, 'login') + table.insert(candidates, 'logout') + end if platform and contest_id then vim.list_extend( candidates,