Merge pull request #10 from barrett-ruth/feat/cache-scrapes
fix: "cache" scrapes
This commit is contained in:
commit
244839da2e
6 changed files with 75 additions and 44 deletions
|
|
@ -58,6 +58,7 @@ example, with lazy.nvim (https://github.com/folke/lazy.nvim):
|
||||||
local s = ls.snippet
|
local s = ls.snippet
|
||||||
|
|
||||||
require('cp').setup({
|
require('cp').setup({
|
||||||
|
debug = false,
|
||||||
contests = {
|
contests = {
|
||||||
default = {
|
default = {
|
||||||
cpp_version = 20,
|
cpp_version = 20,
|
||||||
|
|
@ -102,6 +103,9 @@ hooks Functions called at specific events
|
||||||
before_run Called before :CP run
|
before_run Called before :CP run
|
||||||
before_debug Called before :CP debug
|
before_debug Called before :CP debug
|
||||||
|
|
||||||
|
debug Show info messages during operation
|
||||||
|
(default: false, silent operation)
|
||||||
|
|
||||||
WORKFLOW *cp-workflow*
|
WORKFLOW *cp-workflow*
|
||||||
|
|
||||||
1. Set up contest environment: >
|
1. Set up contest environment: >
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ M.defaults = {
|
||||||
before_run = nil,
|
before_run = nil,
|
||||||
before_debug = nil,
|
before_debug = nil,
|
||||||
},
|
},
|
||||||
|
debug = false,
|
||||||
}
|
}
|
||||||
|
|
||||||
---@param base_config table
|
---@param base_config table
|
||||||
|
|
@ -56,6 +57,7 @@ function M.setup(user_config)
|
||||||
contests = { user_config.contests, { "table", "nil" }, true },
|
contests = { user_config.contests, { "table", "nil" }, true },
|
||||||
snippets = { user_config.snippets, { "table", "nil" }, true },
|
snippets = { user_config.snippets, { "table", "nil" }, true },
|
||||||
hooks = { user_config.hooks, { "table", "nil" }, true },
|
hooks = { user_config.hooks, { "table", "nil" }, true },
|
||||||
|
debug = { user_config.debug, { "boolean", "nil" }, true },
|
||||||
})
|
})
|
||||||
|
|
||||||
if user_config.hooks then
|
if user_config.hooks then
|
||||||
|
|
|
||||||
|
|
@ -3,16 +3,13 @@ local snippets = require("cp.snippets")
|
||||||
local execute = require("cp.execute")
|
local execute = require("cp.execute")
|
||||||
local scrape = require("cp.scrape")
|
local scrape = require("cp.scrape")
|
||||||
local window = require("cp.window")
|
local window = require("cp.window")
|
||||||
|
local logger = require("cp.log")
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
local config = {}
|
local config = {}
|
||||||
|
|
||||||
local function log(msg, level)
|
|
||||||
vim.notify(("[cp.nvim]: %s"):format(msg), level or vim.log.levels.INFO)
|
|
||||||
end
|
|
||||||
|
|
||||||
if not vim.fn.has("nvim-0.10.0") then
|
if not vim.fn.has("nvim-0.10.0") then
|
||||||
log("cp.nvim requires nvim-0.10.0+", vim.log.levels.ERROR)
|
logger.log("cp.nvim requires nvim-0.10.0+", vim.log.levels.ERROR)
|
||||||
return M
|
return M
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -20,7 +17,7 @@ local competition_types = { "atcoder", "codeforces", "cses" }
|
||||||
|
|
||||||
local function setup_contest(contest_type)
|
local function setup_contest(contest_type)
|
||||||
if not vim.tbl_contains(competition_types, contest_type) then
|
if not vim.tbl_contains(competition_types, contest_type) then
|
||||||
log(
|
logger.log(
|
||||||
("unknown contest type. Available: [%s]"):format(table.concat(competition_types, ", ")),
|
("unknown contest type. Available: [%s]"):format(table.concat(competition_types, ", ")),
|
||||||
vim.log.levels.ERROR
|
vim.log.levels.ERROR
|
||||||
)
|
)
|
||||||
|
|
@ -30,12 +27,12 @@ local function setup_contest(contest_type)
|
||||||
vim.g.cp_contest = contest_type
|
vim.g.cp_contest = contest_type
|
||||||
vim.fn.mkdir("build", "p")
|
vim.fn.mkdir("build", "p")
|
||||||
vim.fn.mkdir("io", "p")
|
vim.fn.mkdir("io", "p")
|
||||||
log(("set up %s contest environment"):format(contest_type))
|
logger.log(("set up %s contest environment"):format(contest_type))
|
||||||
end
|
end
|
||||||
|
|
||||||
local function setup_problem(problem_id, problem_letter)
|
local function setup_problem(problem_id, problem_letter)
|
||||||
if not vim.g.cp_contest then
|
if not vim.g.cp_contest then
|
||||||
log("no contest mode set. run :CP <contest> first", vim.log.levels.ERROR)
|
logger.log("no contest mode set. run :CP <contest> first", vim.log.levels.ERROR)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -57,10 +54,10 @@ local function setup_problem(problem_id, problem_letter)
|
||||||
local scrape_result = scrape.scrape_problem(vim.g.cp_contest, problem_id, problem_letter)
|
local scrape_result = scrape.scrape_problem(vim.g.cp_contest, problem_id, problem_letter)
|
||||||
|
|
||||||
if not scrape_result.success then
|
if not scrape_result.success then
|
||||||
log("scraping failed: " .. scrape_result.error, vim.log.levels.WARN)
|
logger.log("scraping failed: " .. scrape_result.error, vim.log.levels.WARN)
|
||||||
log("you can manually add test cases to io/ directory", vim.log.levels.INFO)
|
logger.log("you can manually add test cases to io/ directory", vim.log.levels.INFO)
|
||||||
else
|
else
|
||||||
log(("scraped %d test case(s) for %s"):format(scrape_result.test_count, scrape_result.problem_id))
|
logger.log(("scraped %d test case(s) for %s"):format(scrape_result.test_count, scrape_result.problem_id))
|
||||||
end
|
end
|
||||||
|
|
||||||
local full_problem_id = scrape_result.success and scrape_result.problem_id
|
local full_problem_id = scrape_result.success and scrape_result.problem_id
|
||||||
|
|
@ -115,13 +112,13 @@ local function setup_problem(problem_id, problem_letter)
|
||||||
window.clearcol()
|
window.clearcol()
|
||||||
vim.cmd.wincmd("h")
|
vim.cmd.wincmd("h")
|
||||||
|
|
||||||
log(("switched to problem %s"):format(full_problem_id))
|
logger.log(("switched to problem %s"):format(full_problem_id))
|
||||||
end
|
end
|
||||||
|
|
||||||
local function get_current_problem()
|
local function get_current_problem()
|
||||||
local filename = vim.fn.expand("%:t:r")
|
local filename = vim.fn.expand("%:t:r")
|
||||||
if filename == "" then
|
if filename == "" then
|
||||||
log("no file open", vim.log.levels.ERROR)
|
logger.log("no file open", vim.log.levels.ERROR)
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
return filename
|
return filename
|
||||||
|
|
@ -138,7 +135,7 @@ local function run_problem()
|
||||||
end
|
end
|
||||||
|
|
||||||
if not vim.g.cp_contest then
|
if not vim.g.cp_contest then
|
||||||
log("no contest mode set", vim.log.levels.ERROR)
|
logger.log("no contest mode set", vim.log.levels.ERROR)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -161,7 +158,7 @@ local function debug_problem()
|
||||||
end
|
end
|
||||||
|
|
||||||
if not vim.g.cp_contest then
|
if not vim.g.cp_contest then
|
||||||
log("no contest mode set", vim.log.levels.ERROR)
|
logger.log("no contest mode set", vim.log.levels.ERROR)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -178,7 +175,7 @@ local function diff_problem()
|
||||||
window.restore_layout(vim.g.cp_saved_layout)
|
window.restore_layout(vim.g.cp_saved_layout)
|
||||||
vim.g.cp_diff_mode = false
|
vim.g.cp_diff_mode = false
|
||||||
vim.g.cp_saved_layout = nil
|
vim.g.cp_saved_layout = nil
|
||||||
log("exited diff mode")
|
logger.log("exited diff mode")
|
||||||
else
|
else
|
||||||
local problem_id = get_current_problem()
|
local problem_id = get_current_problem()
|
||||||
if not problem_id then
|
if not problem_id then
|
||||||
|
|
@ -191,7 +188,7 @@ local function diff_problem()
|
||||||
local input = ("%s/io/%s.in"):format(base_fp, problem_id)
|
local input = ("%s/io/%s.in"):format(base_fp, problem_id)
|
||||||
|
|
||||||
if vim.fn.filereadable(expected) == 0 then
|
if vim.fn.filereadable(expected) == 0 then
|
||||||
log(("No expected output file found: %s"):format(expected), vim.log.levels.ERROR)
|
logger.log(("No expected output file found: %s"):format(expected), vim.log.levels.ERROR)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -203,7 +200,7 @@ local function diff_problem()
|
||||||
window.setup_diff_layout(actual_output, expected, input)
|
window.setup_diff_layout(actual_output, expected, input)
|
||||||
|
|
||||||
vim.g.cp_diff_mode = true
|
vim.g.cp_diff_mode = true
|
||||||
log("entered diff mode")
|
logger.log("entered diff mode")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -219,6 +216,7 @@ function M.setup(user_config)
|
||||||
end
|
end
|
||||||
|
|
||||||
config = config_module.setup(user_config)
|
config = config_module.setup(user_config)
|
||||||
|
logger.set_config(config)
|
||||||
snippets.setup(config)
|
snippets.setup(config)
|
||||||
initialized = true
|
initialized = true
|
||||||
end
|
end
|
||||||
|
|
@ -226,7 +224,7 @@ end
|
||||||
function M.handle_command(opts)
|
function M.handle_command(opts)
|
||||||
local args = opts.fargs
|
local args = opts.fargs
|
||||||
if #args == 0 then
|
if #args == 0 then
|
||||||
log("Usage: :CP <contest|problem_id|run|debug|diff>", vim.log.levels.ERROR)
|
logger.log("Usage: :CP <contest|problem_id|run|debug|diff>", vim.log.levels.ERROR)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -249,22 +247,17 @@ function M.handle_command(opts)
|
||||||
debug_problem()
|
debug_problem()
|
||||||
elseif cmd == "diff" then
|
elseif cmd == "diff" then
|
||||||
diff_problem()
|
diff_problem()
|
||||||
else
|
elseif vim.g.cp_contest and not vim.tbl_contains(competition_types, cmd) then
|
||||||
local similar_contests = vim.tbl_filter(function(contest)
|
if (vim.g.cp_contest == "atcoder" or vim.g.cp_contest == "codeforces") and args[2] then
|
||||||
return contest:find(cmd:sub(1, 3), 1, true) == 1
|
setup_problem(cmd, args[2])
|
||||||
end, competition_types)
|
|
||||||
|
|
||||||
if #similar_contests > 0 then
|
|
||||||
log(
|
|
||||||
("unknown contest type '%s'. Did you mean: %s"):format(cmd, table.concat(similar_contests, ", ")),
|
|
||||||
vim.log.levels.ERROR
|
|
||||||
)
|
|
||||||
else
|
else
|
||||||
log(
|
setup_problem(cmd)
|
||||||
("unknown contest type '%s'. Available: [%s]"):format(cmd, table.concat(competition_types, ", ")),
|
|
||||||
vim.log.levels.ERROR
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
logger.log(
|
||||||
|
("unknown contest type '%s'. Available: [%s]"):format(cmd, table.concat(competition_types, ", ")),
|
||||||
|
vim.log.levels.ERROR
|
||||||
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
16
lua/cp/log.lua
Normal file
16
lua/cp/log.lua
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
local config = nil
|
||||||
|
|
||||||
|
function M.set_config(user_config)
|
||||||
|
config = user_config
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.log(msg, level)
|
||||||
|
level = level or vim.log.levels.INFO
|
||||||
|
if not config or config.debug or level >= vim.log.levels.WARN then
|
||||||
|
vim.notify(("[cp.nvim]: %s"):format(msg), level)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
local M = {}
|
local M = {}
|
||||||
|
local logger = require("cp.log")
|
||||||
|
|
||||||
local function get_plugin_path()
|
local function get_plugin_path()
|
||||||
local plugin_path = debug.getinfo(1, "S").source:sub(2)
|
local plugin_path = debug.getinfo(1, "S").source:sub(2)
|
||||||
|
|
@ -9,16 +10,12 @@ local function ensure_io_directory()
|
||||||
vim.fn.mkdir("io", "p")
|
vim.fn.mkdir("io", "p")
|
||||||
end
|
end
|
||||||
|
|
||||||
local function log(msg, level)
|
|
||||||
vim.notify(("[cp.nvim]: %s"):format(msg), level or vim.log.levels.INFO)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function setup_python_env()
|
local function setup_python_env()
|
||||||
local plugin_path = get_plugin_path()
|
local plugin_path = get_plugin_path()
|
||||||
local venv_dir = plugin_path .. "/.venv"
|
local venv_dir = plugin_path .. "/.venv"
|
||||||
|
|
||||||
if vim.fn.executable("uv") == 0 then
|
if vim.fn.executable("uv") == 0 then
|
||||||
log(
|
logger.log(
|
||||||
"uv is not installed. Install it to enable problem scraping: https://docs.astral.sh/uv/",
|
"uv is not installed. Install it to enable problem scraping: https://docs.astral.sh/uv/",
|
||||||
vim.log.levels.WARN
|
vim.log.levels.WARN
|
||||||
)
|
)
|
||||||
|
|
@ -26,13 +23,13 @@ local function setup_python_env()
|
||||||
end
|
end
|
||||||
|
|
||||||
if vim.fn.isdirectory(venv_dir) == 0 then
|
if vim.fn.isdirectory(venv_dir) == 0 then
|
||||||
log("setting up Python environment for scrapers...")
|
logger.log("setting up Python environment for scrapers...")
|
||||||
local result = vim.system({ "uv", "sync" }, { cwd = plugin_path, text = true }):wait()
|
local result = vim.system({ "uv", "sync" }, { cwd = plugin_path, text = true }):wait()
|
||||||
if result.code ~= 0 then
|
if result.code ~= 0 then
|
||||||
log("failed to setup Python environment: " .. result.stderr, vim.log.levels.ERROR)
|
logger.log("failed to setup Python environment: " .. result.stderr, vim.log.levels.ERROR)
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
log("python environment setup complete")
|
logger.log("python environment setup complete")
|
||||||
end
|
end
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
|
@ -41,6 +38,24 @@ end
|
||||||
function M.scrape_problem(contest, problem_id, problem_letter)
|
function M.scrape_problem(contest, problem_id, problem_letter)
|
||||||
ensure_io_directory()
|
ensure_io_directory()
|
||||||
|
|
||||||
|
local cache_problem_id = problem_id:lower()
|
||||||
|
if contest == "atcoder" or contest == "codeforces" then
|
||||||
|
if problem_letter then
|
||||||
|
cache_problem_id = cache_problem_id .. problem_letter:lower()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local input_file = "io/" .. cache_problem_id .. ".in"
|
||||||
|
local expected_file = "io/" .. cache_problem_id .. ".expected"
|
||||||
|
|
||||||
|
if vim.fn.filereadable(input_file) == 1 and vim.fn.filereadable(expected_file) == 1 then
|
||||||
|
return {
|
||||||
|
success = true,
|
||||||
|
problem_id = cache_problem_id,
|
||||||
|
test_count = 1,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
if not setup_python_env() then
|
if not setup_python_env() then
|
||||||
return {
|
return {
|
||||||
success = false,
|
success = false,
|
||||||
|
|
@ -84,8 +99,8 @@ function M.scrape_problem(contest, problem_id, problem_letter)
|
||||||
end
|
end
|
||||||
|
|
||||||
local full_problem_id = data.problem_id:lower()
|
local full_problem_id = data.problem_id:lower()
|
||||||
local input_file = "io/" .. full_problem_id .. ".in"
|
input_file = "io/" .. full_problem_id .. ".in"
|
||||||
local expected_file = "io/" .. full_problem_id .. ".expected"
|
expected_file = "io/" .. full_problem_id .. ".expected"
|
||||||
|
|
||||||
if #data.test_cases > 0 then
|
if #data.test_cases > 0 then
|
||||||
local all_inputs = {}
|
local all_inputs = {}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
local M = {}
|
local M = {}
|
||||||
|
local logger = require("cp.log")
|
||||||
|
|
||||||
function M.setup(config)
|
function M.setup(config)
|
||||||
local ok, ls = pcall(require, "luasnip")
|
local ok, ls = pcall(require, "luasnip")
|
||||||
if not ok then
|
if not ok then
|
||||||
vim.notify("[cp.nvim]: LuaSnip not available - snippets disabled", vim.log.levels.INFO)
|
logger.log("LuaSnip not available - snippets disabled", vim.log.levels.INFO)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue