From f13a05c806fcacf84a650f42916796b83cdb52df Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 13 Sep 2025 00:39:53 -0500 Subject: [PATCH] feat: include versioning --- after/syntax/cp.vim | 10 +++++----- lua/cp/health.lua | 6 ++++++ lua/cp/init.lua | 2 +- lua/cp/scrape.lua | 34 +++++++++++----------------------- lua/cp/version.lua | 36 ++++++++++++++++++++++++++++++++++++ plugin/cp.lua | 1 + 6 files changed, 60 insertions(+), 29 deletions(-) create mode 100644 lua/cp/version.lua diff --git a/after/syntax/cp.vim b/after/syntax/cp.vim index 331d80f..d76f87e 100644 --- a/after/syntax/cp.vim +++ b/after/syntax/cp.vim @@ -2,11 +2,11 @@ if exists("b:current_syntax") finish endif -syntax match cpOutputCode /^\[code\]: .*/ -syntax match cpOutputTime /^\[time\]: .*/ -syntax match cpOutputDebug /^\[debug\]: .*/ -syntax match cpOutputMatchesTrue /^\[matches\]: true$/ -syntax match cpOutputMatchesFalse /^\[matches\]: false$/ +syntax match cpOutputCode /^\[code\]:/ +syntax match cpOutputTime /^\[time\]:/ +syntax match cpOutputDebug /^\[debug\]:/ +syntax match cpOutputMatchesTrue /^\[matches\]:\ze true$/ +syntax match cpOutputMatchesFalse /^\[matches\]:\ze false$/ highlight default link cpOutputCode DiagnosticInfo highlight default link cpOutputTime Comment diff --git a/lua/cp/health.lua b/lua/cp/health.lua index 464ee8d..c46ba1d 100644 --- a/lua/cp/health.lua +++ b/lua/cp/health.lua @@ -75,7 +75,13 @@ local function check_config() end function M.check() + local version = require("cp.version") vim.health.start("cp.nvim health check") + + vim.health.info("Version: " .. version.version) + if version.semver then + vim.health.info("Semantic version: v" .. version.semver.full) + end check_nvim_version() check_uv() diff --git a/lua/cp/init.lua b/lua/cp/init.lua index 4d26bb6..eae2965 100644 --- a/lua/cp/init.lua +++ b/lua/cp/init.lua @@ -54,7 +54,7 @@ local function setup_problem(problem_id, problem_letter) local ctx = problem.create_context(vim.g.cp_contest, problem_id, problem_letter, config) - local scrape_result = scrape.scrape_problem(vim.g.cp_contest, problem_id, problem_letter) + local scrape_result = scrape.scrape_problem(ctx) if not scrape_result.success then logger.log("scraping failed: " .. scrape_result.error, vim.log.levels.WARN) diff --git a/lua/cp/scrape.lua b/lua/cp/scrape.lua index 6b0c618..8456fce 100644 --- a/lua/cp/scrape.lua +++ b/lua/cp/scrape.lua @@ -35,23 +35,14 @@ local function setup_python_env() return true end -function M.scrape_problem(contest, problem_id, problem_letter) +---@param ctx ProblemContext +function M.scrape_problem(ctx) 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 + if vim.fn.filereadable(ctx.input_file) == 1 and vim.fn.filereadable(ctx.expected_file) == 1 then return { success = true, - problem_id = cache_problem_id, + problem_id = ctx.problem_name, test_count = 1, } end @@ -64,13 +55,13 @@ function M.scrape_problem(contest, problem_id, problem_letter) end local plugin_path = get_plugin_path() - local scraper_path = plugin_path .. "/scrapers/" .. contest .. ".py" + local scraper_path = plugin_path .. "/scrapers/" .. ctx.contest .. ".py" local args - if contest == "cses" then - args = { "uv", "run", scraper_path, problem_id } + if ctx.contest == "cses" then + args = { "uv", "run", scraper_path, ctx.problem_id } else - args = { "uv", "run", scraper_path, problem_id, problem_letter } + args = { "uv", "run", scraper_path, ctx.problem_id, ctx.problem_letter } end local result = vim.system(args, { @@ -98,9 +89,6 @@ function M.scrape_problem(contest, problem_id, problem_letter) return data end - local full_problem_id = data.problem_id:lower() - input_file = "io/" .. full_problem_id .. ".in" - expected_file = "io/" .. full_problem_id .. ".expected" if #data.test_cases > 0 then local all_inputs = {} @@ -119,13 +107,13 @@ function M.scrape_problem(contest, problem_id, problem_letter) end end - vim.fn.writefile(all_inputs, input_file) - vim.fn.writefile(all_outputs, expected_file) + vim.fn.writefile(all_inputs, ctx.input_file) + vim.fn.writefile(all_outputs, ctx.expected_file) end return { success = true, - problem_id = full_problem_id, + problem_id = ctx.problem_name, test_count = #data.test_cases, url = data.url, } diff --git a/lua/cp/version.lua b/lua/cp/version.lua new file mode 100644 index 0000000..a243646 --- /dev/null +++ b/lua/cp/version.lua @@ -0,0 +1,36 @@ +local M = {} + +local function get_git_version() + local plugin_path = debug.getinfo(1, "S").source:sub(2) + local plugin_root = vim.fn.fnamemodify(plugin_path, ":h:h:h") + + local result = vim.system({"git", "describe", "--tags", "--always", "--dirty"}, { + cwd = plugin_root, + text = true + }):wait() + + if result.code == 0 then + return result.stdout:gsub("\n", "") + else + return "unknown" + end +end + +local function parse_semver(version_string) + local semver = version_string:match("^v?(%d+%.%d+%.%d+)") + if semver then + local major, minor, patch = semver:match("(%d+)%.(%d+)%.(%d+)") + return { + full = semver, + major = tonumber(major), + minor = tonumber(minor), + patch = tonumber(patch) + } + end + return nil +end + +M.version = get_git_version() +M.semver = parse_semver(M.version) + +return M \ No newline at end of file diff --git a/plugin/cp.lua b/plugin/cp.lua index 9fc7e28..516ecd8 100644 --- a/plugin/cp.lua +++ b/plugin/cp.lua @@ -20,3 +20,4 @@ end, { end, commands) end, }) +