From 17cdbf0a5036f26e4f86a306ecf0193b48b5772e Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Mon, 15 Sep 2025 14:04:31 -0400 Subject: [PATCH] feat: update outdated variable references --- doc/cp.txt | 24 +++++++++++----------- lua/cp/config.lua | 47 ++++++++------------------------------------ lua/cp/languages.lua | 1 - lua/cp/problem.lua | 27 ++++++++++++++++++++++--- 4 files changed, 45 insertions(+), 54 deletions(-) diff --git a/doc/cp.txt b/doc/cp.txt index 72ec0fd..cb8dc28 100644 --- a/doc/cp.txt +++ b/doc/cp.txt @@ -77,7 +77,7 @@ Optional configuration with lazy.nvim: > opts = { debug = false, contests = { - default = { + codeforces = { cpp = { compile = { 'g++', '-std=c++{version}', '-O2', '-Wall', '-Wextra', @@ -89,7 +89,7 @@ Optional configuration with lazy.nvim: > '-fsanitize=address,undefined', '-DLOCAL', '{source}', '-o', '{binary}', }, - version = 20, + version = 23, extension = "cc", }, python = { @@ -97,9 +97,9 @@ Optional configuration with lazy.nvim: > debug = { 'python3', '{source}' }, extension = "py", }, + default_language = "cpp", timeout_ms = 2000, }, - codeforces = { cpp = { version = 23 } }, }, hooks = { before_run = function(ctx) vim.cmd.w() end, @@ -115,7 +115,7 @@ Optional configuration with lazy.nvim: > }, snippets = { ... }, -- LuaSnip snippets tile = function(source_buf, input_buf, output_buf) ... end, - filename = function(contest, problem_id, problem_letter) ... end, + filename = function(contest, contest_id, problem_id, config, language) ... end, } } < @@ -131,7 +131,9 @@ Optional configuration with lazy.nvim: > • {tile}? (`function`) Custom window arrangement function. `function(source_buf, input_buf, output_buf)` • {filename}? (`function`) Custom filename generation function. - `function(contest, problem_id, problem_letter)` + `function(contest, contest_id, problem_id, config, language)` + Should return full filename with extension. + (default: uses problem_id or contest_id) *cp.ContestConfig* @@ -243,7 +245,7 @@ Example: Setting up and solving AtCoder contest ABC324 3. Start with problem A: > :CP a -< This creates abc324a.cc and scrapes test cases +< This creates a.cc and scrapes test cases 4. Code your solution, then test: > :CP run @@ -270,13 +272,13 @@ FILE STRUCTURE *cp-files* cp.nvim creates the following file structure upon problem setup: - {contest_id}{problem_id}.cc " Source file (e.g. abc324a.cc) + {problem_id}.{ext} " Source file (e.g. a.cc, b.py) build/ - {contest_id}{problem_id}.run " Compiled binary + {problem_id}.run " Compiled binary io/ - {contest_id}{problem_id}.cpin " Test input - {contest_id}{problem_id}.cpout " Program output - {contest_id}{problem_id}.expected " Expected output + {problem_id}.cpin " Test input + {problem_id}.cpout " Program output + {problem_id}.expected " Expected output The plugin automatically manages this structure and navigation between problems maintains proper file associations. diff --git a/lua/cp/config.lua b/lua/cp/config.lua index 66ba2e7..e318db9 100644 --- a/lua/cp/config.lua +++ b/lua/cp/config.lua @@ -58,13 +58,7 @@ ---@field filename? fun(contest: string, contest_id: string, problem_id?: string, config: cp.Config, language?: string): string local M = {} - -local filetype_to_language = { - cc = "cpp", - c = "cpp", - py = "python", - py3 = "python", -} +local languages = require("cp.languages") ---@type cp.Config M.defaults = { @@ -109,13 +103,13 @@ function M.setup(user_config) for contest_name, contest_config in pairs(user_config.contests) do for lang_name, lang_config in pairs(contest_config) do if type(lang_config) == "table" and lang_config.extension then - if not vim.tbl_contains(vim.tbl_keys(filetype_to_language), lang_config.extension) then + if not vim.tbl_contains(vim.tbl_keys(languages.filetype_to_language), lang_config.extension) then error( ("Invalid extension '%s' for language '%s' in contest '%s'. Valid extensions: %s"):format( lang_config.extension, lang_name, contest_name, - table.concat(vim.tbl_keys(filetype_to_language), ", ") + table.concat(vim.tbl_keys(languages.filetype_to_language), ", ") ) ) end @@ -129,45 +123,20 @@ function M.setup(user_config) return config end ----@param contest string ---@param contest_id string ---@param problem_id? string ----@param config cp.Config ----@param language? string ---@return string -local function default_filename(contest, contest_id, problem_id, config, language) +local function default_filename(contest_id, problem_id) vim.validate({ - contest = { contest, "string" }, contest_id = { contest_id, "string" }, problem_id = { problem_id, { "string", "nil" }, true }, - config = { config, "table" }, - language = { language, { "string", "nil" }, true }, }) - local full_problem_id = contest_id:lower() - if contest == "atcoder" or contest == "codeforces" then - if problem_id then - full_problem_id = full_problem_id .. problem_id:lower() - end + if problem_id then + return problem_id:lower() + else + return contest_id:lower() end - - local contest_config = config.contests[contest] - if not contest_config then - error(("No contest config found for '%s'"):format(contest)) - end - - local target_language = language or contest_config.default_language - local language_config = contest_config[target_language] - - if not language_config then - error(("No language config found for '%s' in contest '%s'"):format(target_language, contest)) - end - - if not language_config.extension then - error(("No extension configured for language '%s' in contest '%s'"):format(target_language, contest)) - end - - return full_problem_id .. "." .. language_config.extension end M.default_filename = default_filename diff --git a/lua/cp/languages.lua b/lua/cp/languages.lua index 6884287..134d312 100644 --- a/lua/cp/languages.lua +++ b/lua/cp/languages.lua @@ -8,7 +8,6 @@ M.filetype_to_language = { cc = M.CPP, cxx = M.CPP, cpp = M.CPP, - c = M.CPP, py = M.PYTHON, py3 = M.PYTHON, } diff --git a/lua/cp/problem.lua b/lua/cp/problem.lua index caeac2c..088b908 100644 --- a/lua/cp/problem.lua +++ b/lua/cp/problem.lua @@ -26,9 +26,30 @@ function M.create_context(contest, contest_id, problem_id, config, language) language = { language, { "string", "nil" }, true }, }) - local filename_fn = config.filename or require("cp.config").default_filename - local source_file = filename_fn(contest, contest_id, problem_id, config, language) - local base_name = vim.fn.fnamemodify(source_file, ":t:r") + local contest_config = config.contests[contest] + if not contest_config then + error(("No contest config found for '%s'"):format(contest)) + end + + local target_language = language or contest_config.default_language + local language_config = contest_config[target_language] + if not language_config then + error(("No language config found for '%s' in contest '%s'"):format(target_language, contest)) + end + if not language_config.extension then + error(("No extension configured for language '%s' in contest '%s'"):format(target_language, contest)) + end + + local base_name + if config.filename then + local source_file = config.filename(contest, contest_id, problem_id, config, language) + base_name = vim.fn.fnamemodify(source_file, ":t:r") + else + local default_filename = require("cp.config").default_filename + base_name = default_filename(contest_id, problem_id) + end + + local source_file = base_name .. "." .. language_config.extension return { contest = contest,