feat: caching
This commit is contained in:
parent
64c7559c78
commit
40117c2cf1
10 changed files with 764 additions and 175 deletions
233
lua/cp/init.lua
233
lua/cp/init.lua
|
|
@ -5,6 +5,7 @@ local scrape = require("cp.scrape")
|
|||
local window = require("cp.window")
|
||||
local logger = require("cp.log")
|
||||
local problem = require("cp.problem")
|
||||
local cache = require("cp.cache")
|
||||
|
||||
local M = {}
|
||||
local config = {}
|
||||
|
|
@ -14,27 +15,33 @@ if not vim.fn.has("nvim-0.10.0") then
|
|||
return M
|
||||
end
|
||||
|
||||
local competition_types = { "atcoder", "codeforces", "cses" }
|
||||
local platforms = { "atcoder", "codeforces", "cses" }
|
||||
local actions = { "run", "debug", "diff", "next", "prev" }
|
||||
|
||||
local function setup_contest(contest_type)
|
||||
if not vim.tbl_contains(competition_types, contest_type) then
|
||||
logger.log(
|
||||
("unknown contest type. Available: [%s]"):format(table.concat(competition_types, ", ")),
|
||||
vim.log.levels.ERROR
|
||||
)
|
||||
local function set_platform(platform)
|
||||
if not vim.tbl_contains(platforms, platform) then
|
||||
logger.log(("unknown platform. Available: [%s]"):format(table.concat(platforms, ", ")), vim.log.levels.ERROR)
|
||||
return false
|
||||
end
|
||||
|
||||
vim.g.cp = vim.g.cp or {}
|
||||
vim.g.cp.platform = platform
|
||||
vim.fn.mkdir("build", "p")
|
||||
vim.fn.mkdir("io", "p")
|
||||
return true
|
||||
end
|
||||
|
||||
---@param contest_id string
|
||||
---@param problem_id? string
|
||||
local function setup_problem(contest_id, problem_id)
|
||||
if not vim.g.cp or not vim.g.cp.platform then
|
||||
logger.log("no platform set. run :CP <platform> <contest> first", vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
|
||||
vim.g.cp_contest = contest_type
|
||||
vim.fn.mkdir("build", "p")
|
||||
vim.fn.mkdir("io", "p")
|
||||
logger.log(("set up %s contest environment"):format(contest_type))
|
||||
end
|
||||
|
||||
local function setup_problem(contest_id, problem_id)
|
||||
if not vim.g.cp_contest then
|
||||
logger.log("no contest mode set. run :CP <contest> first", vim.log.levels.ERROR)
|
||||
return
|
||||
local metadata_result = scrape.scrape_contest_metadata(vim.g.cp.platform, contest_id)
|
||||
if not metadata_result.success then
|
||||
logger.log("failed to load contest metadata: " .. (metadata_result.error or "unknown error"), vim.log.levels.WARN)
|
||||
end
|
||||
|
||||
if vim.g.cp_diff_mode then
|
||||
|
|
@ -52,18 +59,19 @@ local function setup_problem(contest_id, problem_id)
|
|||
|
||||
vim.cmd("silent only")
|
||||
|
||||
vim.g.cp_contest_id = contest_id
|
||||
vim.g.cp_problem_id = problem_id
|
||||
vim.g.cp.contest_id = contest_id
|
||||
vim.g.cp.problem_id = problem_id
|
||||
|
||||
local ctx = problem.create_context(vim.g.cp_contest, contest_id, problem_id, config)
|
||||
local ctx = problem.create_context(vim.g.cp.platform, contest_id, problem_id, config)
|
||||
|
||||
local scrape_result = scrape.scrape_problem(ctx)
|
||||
|
||||
if not scrape_result.success then
|
||||
logger.log("scraping failed: " .. scrape_result.error, vim.log.levels.WARN)
|
||||
logger.log("scraping failed: " .. (scrape_result.error or "unknown error"), vim.log.levels.WARN)
|
||||
logger.log("you can manually add test cases to io/ directory", vim.log.levels.INFO)
|
||||
else
|
||||
logger.log(("scraped %d test case(s) for %s"):format(scrape_result.test_count, scrape_result.problem_id))
|
||||
local test_count = scrape_result.test_count or 0
|
||||
logger.log(("scraped %d test case(s) for %s"):format(test_count, scrape_result.problem_id))
|
||||
end
|
||||
|
||||
vim.cmd.e(ctx.source_file)
|
||||
|
|
@ -71,8 +79,8 @@ local function setup_problem(contest_id, problem_id)
|
|||
if vim.api.nvim_buf_get_lines(0, 0, -1, true)[1] == "" then
|
||||
local has_luasnip, luasnip = pcall(require, "luasnip")
|
||||
if has_luasnip then
|
||||
vim.api.nvim_buf_set_lines(0, 0, -1, false, { vim.g.cp_contest })
|
||||
vim.api.nvim_win_set_cursor(0, { 1, #vim.g.cp_contest })
|
||||
vim.api.nvim_buf_set_lines(0, 0, -1, false, { vim.g.cp.platform })
|
||||
vim.api.nvim_win_set_cursor(0, { 1, #vim.g.cp.platform })
|
||||
vim.cmd.startinsert({ bang = true })
|
||||
|
||||
vim.schedule(function()
|
||||
|
|
@ -82,7 +90,7 @@ local function setup_problem(contest_id, problem_id)
|
|||
vim.cmd.stopinsert()
|
||||
end)
|
||||
else
|
||||
vim.api.nvim_input(("i%s<c-space><esc>"):format(vim.g.cp_contest))
|
||||
vim.api.nvim_input(("i%s<c-space><esc>"):format(vim.g.cp.platform))
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -123,12 +131,12 @@ local function run_problem()
|
|||
config.hooks.before_run(problem_id)
|
||||
end
|
||||
|
||||
if not vim.g.cp_contest then
|
||||
logger.log("no contest mode set", vim.log.levels.ERROR)
|
||||
if not vim.g.cp_platform then
|
||||
logger.log("no platform set", vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
|
||||
local contest_config = config.contests[vim.g.cp_contest]
|
||||
local contest_config = config.contests[vim.g.cp_platform]
|
||||
|
||||
vim.schedule(function()
|
||||
local ctx = problem.create_context(vim.g.cp_contest, vim.g.cp_contest_id, vim.g.cp_problem_id, config)
|
||||
|
|
@ -147,12 +155,12 @@ local function debug_problem()
|
|||
config.hooks.before_debug(problem_id)
|
||||
end
|
||||
|
||||
if not vim.g.cp_contest then
|
||||
logger.log("no contest mode set", vim.log.levels.ERROR)
|
||||
if not vim.g.cp_platform then
|
||||
logger.log("no platform set", vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
|
||||
local contest_config = config.contests[vim.g.cp_contest]
|
||||
local contest_config = config.contests[vim.g.cp_platform]
|
||||
|
||||
vim.schedule(function()
|
||||
local ctx = problem.create_context(vim.g.cp_contest, vim.g.cp_contest_id, vim.g.cp_problem_id, config)
|
||||
|
|
@ -193,6 +201,64 @@ local function diff_problem()
|
|||
end
|
||||
end
|
||||
|
||||
---@param delta number 1 for next, -1 for prev
|
||||
local function navigate_problem(delta)
|
||||
if not vim.g.cp_platform or not vim.g.cp_contest_id then
|
||||
logger.log("no contest set. run :CP <platform> <contest> first", vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
|
||||
cache.load()
|
||||
local contest_data = cache.get_contest_data(vim.g.cp_platform, vim.g.cp_contest_id)
|
||||
if not contest_data or not contest_data.problems then
|
||||
logger.log("no contest metadata found. set up a problem first to cache contest data", vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
|
||||
local problems = contest_data.problems
|
||||
local current_problem_id
|
||||
|
||||
if vim.g.cp_platform == "cses" then
|
||||
current_problem_id = vim.g.cp_contest_id
|
||||
else
|
||||
current_problem_id = vim.g.cp_problem_id
|
||||
end
|
||||
|
||||
if not current_problem_id then
|
||||
logger.log("no current problem set", vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
|
||||
local current_index = nil
|
||||
for i, problem in ipairs(problems) do
|
||||
if problem.id == current_problem_id then
|
||||
current_index = i
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if not current_index then
|
||||
logger.log("current problem not found in contest", vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
|
||||
local new_index = current_index + delta
|
||||
|
||||
if new_index < 1 or new_index > #problems then
|
||||
local direction = delta > 0 and "next" or "previous"
|
||||
logger.log(("no %s problem available"):format(direction), vim.log.levels.INFO)
|
||||
return
|
||||
end
|
||||
|
||||
local new_problem = problems[new_index]
|
||||
|
||||
if vim.g.cp_platform == "cses" then
|
||||
setup_problem(new_problem.id)
|
||||
else
|
||||
setup_problem(vim.g.cp_contest_id, new_problem.id)
|
||||
end
|
||||
end
|
||||
|
||||
local initialized = false
|
||||
|
||||
function M.is_initialized()
|
||||
|
|
@ -210,43 +276,92 @@ function M.setup(user_config)
|
|||
initialized = true
|
||||
end
|
||||
|
||||
function M.handle_command(opts)
|
||||
local args = opts.fargs
|
||||
local function parse_command(args)
|
||||
if #args == 0 then
|
||||
logger.log("Usage: :CP <contest|problem_id|run|debug|diff>", vim.log.levels.ERROR)
|
||||
return { type = "error", message = "Usage: :CP <platform> <contest> [problem] | :CP <action> | :CP <problem>" }
|
||||
end
|
||||
|
||||
local first = args[1]
|
||||
|
||||
if vim.tbl_contains(actions, first) then
|
||||
return { type = "action", action = first }
|
||||
end
|
||||
|
||||
if vim.tbl_contains(platforms, first) then
|
||||
if #args == 1 then
|
||||
return { type = "platform_only", platform = first }
|
||||
elseif #args == 2 then
|
||||
return { type = "contest_setup", platform = first, contest = args[2] }
|
||||
elseif #args == 3 then
|
||||
return { type = "full_setup", platform = first, contest = args[2], problem = args[3] }
|
||||
else
|
||||
return { type = "error", message = "Too many arguments" }
|
||||
end
|
||||
end
|
||||
|
||||
if vim.g.cp and vim.g.cp.platform and vim.g.cp.contest_id then
|
||||
return { type = "problem_switch", problem = first }
|
||||
end
|
||||
|
||||
return { type = "error", message = "Unknown command or no contest context" }
|
||||
end
|
||||
|
||||
function M.handle_command(opts)
|
||||
local cmd = parse_command(opts.fargs)
|
||||
|
||||
if cmd.type == "error" then
|
||||
logger.log(cmd.message, vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
|
||||
local cmd = args[1]
|
||||
if cmd.type == "action" then
|
||||
if cmd.action == "run" then
|
||||
run_problem()
|
||||
elseif cmd.action == "debug" then
|
||||
debug_problem()
|
||||
elseif cmd.action == "diff" then
|
||||
diff_problem()
|
||||
elseif cmd.action == "next" then
|
||||
navigate_problem(1)
|
||||
elseif cmd.action == "prev" then
|
||||
navigate_problem(-1)
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
if vim.tbl_contains(competition_types, cmd) then
|
||||
if args[2] then
|
||||
setup_contest(cmd)
|
||||
if (cmd == "atcoder" or cmd == "codeforces") and args[3] then
|
||||
setup_problem(args[2], args[3])
|
||||
if cmd.type == "platform_only" then
|
||||
set_platform(cmd.platform)
|
||||
return
|
||||
end
|
||||
|
||||
if cmd.type == "contest_setup" then
|
||||
if set_platform(cmd.platform) then
|
||||
vim.g.cp.contest_id = cmd.contest
|
||||
local metadata_result = scrape.scrape_contest_metadata(cmd.platform, cmd.contest)
|
||||
if not metadata_result.success then
|
||||
logger.log("failed to load contest metadata: " .. (metadata_result.error or "unknown error"), vim.log.levels.WARN)
|
||||
else
|
||||
setup_problem(args[2])
|
||||
logger.log(("loaded %d problems for %s %s"):format(#metadata_result.problems, cmd.platform, cmd.contest))
|
||||
end
|
||||
else
|
||||
setup_contest(cmd)
|
||||
end
|
||||
elseif cmd == "run" then
|
||||
run_problem()
|
||||
elseif cmd == "debug" then
|
||||
debug_problem()
|
||||
elseif cmd == "diff" then
|
||||
diff_problem()
|
||||
elseif vim.g.cp_contest and not vim.tbl_contains(competition_types, cmd) then
|
||||
if (vim.g.cp_contest == "atcoder" or vim.g.cp_contest == "codeforces") and args[2] then
|
||||
setup_problem(cmd, args[2])
|
||||
else
|
||||
setup_problem(cmd)
|
||||
return
|
||||
end
|
||||
|
||||
if cmd.type == "full_setup" then
|
||||
if set_platform(cmd.platform) then
|
||||
vim.g.cp.contest_id = cmd.contest
|
||||
setup_problem(cmd.contest, cmd.problem)
|
||||
end
|
||||
else
|
||||
logger.log(
|
||||
("unknown contest type '%s'. Available: [%s]"):format(cmd, table.concat(competition_types, ", ")),
|
||||
vim.log.levels.ERROR
|
||||
)
|
||||
return
|
||||
end
|
||||
|
||||
if cmd.type == "problem_switch" then
|
||||
if vim.g.cp.platform == "cses" then
|
||||
setup_problem(cmd.problem)
|
||||
else
|
||||
setup_problem(vim.g.cp.contest_id, cmd.problem)
|
||||
end
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue