feat: update outdated variable references

This commit is contained in:
Barrett Ruth 2025-09-15 14:04:31 -04:00
parent e1ad439781
commit 17cdbf0a50
4 changed files with 45 additions and 54 deletions

View file

@ -77,7 +77,7 @@ Optional configuration with lazy.nvim: >
opts = { opts = {
debug = false, debug = false,
contests = { contests = {
default = { codeforces = {
cpp = { cpp = {
compile = { compile = {
'g++', '-std=c++{version}', '-O2', '-Wall', '-Wextra', 'g++', '-std=c++{version}', '-O2', '-Wall', '-Wextra',
@ -89,7 +89,7 @@ Optional configuration with lazy.nvim: >
'-fsanitize=address,undefined', '-DLOCAL', '-fsanitize=address,undefined', '-DLOCAL',
'{source}', '-o', '{binary}', '{source}', '-o', '{binary}',
}, },
version = 20, version = 23,
extension = "cc", extension = "cc",
}, },
python = { python = {
@ -97,9 +97,9 @@ Optional configuration with lazy.nvim: >
debug = { 'python3', '{source}' }, debug = { 'python3', '{source}' },
extension = "py", extension = "py",
}, },
default_language = "cpp",
timeout_ms = 2000, timeout_ms = 2000,
}, },
codeforces = { cpp = { version = 23 } },
}, },
hooks = { hooks = {
before_run = function(ctx) vim.cmd.w() end, before_run = function(ctx) vim.cmd.w() end,
@ -115,7 +115,7 @@ Optional configuration with lazy.nvim: >
}, },
snippets = { ... }, -- LuaSnip snippets snippets = { ... }, -- LuaSnip snippets
tile = function(source_buf, input_buf, output_buf) ... end, 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. • {tile}? (`function`) Custom window arrangement function.
`function(source_buf, input_buf, output_buf)` `function(source_buf, input_buf, output_buf)`
• {filename}? (`function`) Custom filename generation function. • {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* *cp.ContestConfig*
@ -243,7 +245,7 @@ Example: Setting up and solving AtCoder contest ABC324
3. Start with problem A: > 3. Start with problem A: >
:CP 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: > 4. Code your solution, then test: >
:CP run :CP run
@ -270,13 +272,13 @@ FILE STRUCTURE *cp-files*
cp.nvim creates the following file structure upon problem setup: 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/ build/
{contest_id}{problem_id}.run " Compiled binary {problem_id}.run " Compiled binary
io/ io/
{contest_id}{problem_id}.cpin " Test input {problem_id}.cpin " Test input
{contest_id}{problem_id}.cpout " Program output {problem_id}.cpout " Program output
{contest_id}{problem_id}.expected " Expected output {problem_id}.expected " Expected output
The plugin automatically manages this structure and navigation between problems The plugin automatically manages this structure and navigation between problems
maintains proper file associations. maintains proper file associations.

View file

@ -58,13 +58,7 @@
---@field filename? fun(contest: string, contest_id: string, problem_id?: string, config: cp.Config, language?: string): string ---@field filename? fun(contest: string, contest_id: string, problem_id?: string, config: cp.Config, language?: string): string
local M = {} local M = {}
local languages = require("cp.languages")
local filetype_to_language = {
cc = "cpp",
c = "cpp",
py = "python",
py3 = "python",
}
---@type cp.Config ---@type cp.Config
M.defaults = { M.defaults = {
@ -109,13 +103,13 @@ function M.setup(user_config)
for contest_name, contest_config in pairs(user_config.contests) do for contest_name, contest_config in pairs(user_config.contests) do
for lang_name, lang_config in pairs(contest_config) do for lang_name, lang_config in pairs(contest_config) do
if type(lang_config) == "table" and lang_config.extension then 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( error(
("Invalid extension '%s' for language '%s' in contest '%s'. Valid extensions: %s"):format( ("Invalid extension '%s' for language '%s' in contest '%s'. Valid extensions: %s"):format(
lang_config.extension, lang_config.extension,
lang_name, lang_name,
contest_name, contest_name,
table.concat(vim.tbl_keys(filetype_to_language), ", ") table.concat(vim.tbl_keys(languages.filetype_to_language), ", ")
) )
) )
end end
@ -129,45 +123,20 @@ function M.setup(user_config)
return config return config
end end
---@param contest string
---@param contest_id string ---@param contest_id string
---@param problem_id? string ---@param problem_id? string
---@param config cp.Config
---@param language? string
---@return string ---@return string
local function default_filename(contest, contest_id, problem_id, config, language) local function default_filename(contest_id, problem_id)
vim.validate({ vim.validate({
contest = { contest, "string" },
contest_id = { contest_id, "string" }, contest_id = { contest_id, "string" },
problem_id = { problem_id, { "string", "nil" }, true }, 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 if problem_id then
full_problem_id = full_problem_id .. problem_id:lower() return problem_id:lower()
else
return contest_id:lower()
end end
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 end
M.default_filename = default_filename M.default_filename = default_filename

View file

@ -8,7 +8,6 @@ M.filetype_to_language = {
cc = M.CPP, cc = M.CPP,
cxx = M.CPP, cxx = M.CPP,
cpp = M.CPP, cpp = M.CPP,
c = M.CPP,
py = M.PYTHON, py = M.PYTHON,
py3 = M.PYTHON, py3 = M.PYTHON,
} }

View file

@ -26,9 +26,30 @@ function M.create_context(contest, contest_id, problem_id, config, language)
language = { language, { "string", "nil" }, true }, language = { language, { "string", "nil" }, true },
}) })
local filename_fn = config.filename or require("cp.config").default_filename local contest_config = config.contests[contest]
local source_file = filename_fn(contest, contest_id, problem_id, config, language) if not contest_config then
local base_name = vim.fn.fnamemodify(source_file, ":t:r") 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 { return {
contest = contest, contest = contest,