From b3ffef7341f5ef6d5ed987a32ce4877d78f151a9 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Mon, 15 Sep 2025 14:11:12 -0400 Subject: [PATCH] more typechecking --- doc/cp.txt | 18 +++--------------- lua/cp/config.lua | 20 ++++++-------------- lua/cp/execute.lua | 8 ++++---- lua/cp/init.lua | 4 ++-- 4 files changed, 15 insertions(+), 35 deletions(-) diff --git a/doc/cp.txt b/doc/cp.txt index cb8dc28..62cc2e1 100644 --- a/doc/cp.txt +++ b/doc/cp.txt @@ -160,24 +160,12 @@ Optional configuration with lazy.nvim: > Fields: ~ • {before_run}? (`function`) Called before `:CP run`. - `function(ctx: HookContext)` + `function(ctx: ProblemContext)` • {before_debug}? (`function`) Called before `:CP debug`. - `function(ctx: HookContext)` + `function(ctx: ProblemContext)` • {setup_code}? (`function`) Called after source file is opened. Used to configure buffer settings. - `function(ctx: HookContext)` - -*cp.HookContext* - - Fields: ~ - • {problem_id} (`string`) Problem identifier (e.g. "a", "b"). - • {platform} (`string`) Platform name (e.g. "codeforces"). - • {contest_id} (`string`) Contest identifier (e.g. "1933"). - • {source_file} (`string`) Path to source file. - • {input_file} (`string`) Path to input file (.cpin). - • {output_file} (`string`) Path to output file (.cpout). - • {expected_file} (`string`) Path to expected output file. - • {contest_config} (`table`) Contest configuration. + `function(ctx: ProblemContext)` WORKFLOW *cp-workflow* diff --git a/lua/cp/config.lua b/lua/cp/config.lua index e318db9..d32e26c 100644 --- a/lua/cp/config.lua +++ b/lua/cp/config.lua @@ -26,20 +26,10 @@ ---@field default_language? string ---@field timeout_ms? number ----@class HookContext ----@field problem_id string ----@field platform string ----@field contest_id string ----@field source_file string ----@field input_file string ----@field output_file string ----@field expected_file string ----@field contest_config table - ---@class Hooks ----@field before_run? fun(ctx: HookContext) ----@field before_debug? fun(ctx: HookContext) ----@field setup_code? fun(ctx: HookContext) +---@field before_run? fun(ctx: ProblemContext) +---@field before_debug? fun(ctx: ProblemContext) +---@field setup_code? fun(ctx: ProblemContext) ---@class cp.Config ---@field contests table @@ -103,7 +93,9 @@ 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(languages.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, diff --git a/lua/cp/execute.lua b/lua/cp/execute.lua index a0874f1..78358bc 100644 --- a/lua/cp/execute.lua +++ b/lua/cp/execute.lua @@ -103,9 +103,9 @@ local function compile_generic(language_config, substitutions) local compile_cmd = substitute_template(language_config.compile, substitutions) logger.log(("compiling: %s"):format(table.concat(compile_cmd, " "))) - local start_time = vim.loop.hrtime() + local start_time = vim.uv.hrtime() local result = vim.system(compile_cmd, { text = true }):wait() - local compile_time = (vim.loop.hrtime() - start_time) / 1000000 + local compile_time = (vim.uv.hrtime() - start_time) / 1000000 if result.code == 0 then logger.log(("compilation successful (%.1fms)"):format(compile_time)) @@ -129,7 +129,7 @@ local function execute_command(cmd, input_data, timeout_ms) logger.log(("executing: %s"):format(table.concat(cmd, " "))) - local start_time = vim.loop.hrtime() + local start_time = vim.uv.hrtime() local result = vim.system(cmd, { stdin = input_data, @@ -137,7 +137,7 @@ local function execute_command(cmd, input_data, timeout_ms) text = true, }):wait() - local end_time = vim.loop.hrtime() + local end_time = vim.uv.hrtime() local execution_time = (end_time - start_time) / 1000000 local actual_code = result.code or 0 diff --git a/lua/cp/init.lua b/lua/cp/init.lua index f78abb0..adb801d 100644 --- a/lua/cp/init.lua +++ b/lua/cp/init.lua @@ -41,8 +41,8 @@ local function set_platform(platform) end state.platform = platform - vim.fn.mkdir("build", "p") - vim.fn.mkdir("io", "p") + vim.fs.mkdir("build", { parents = true }) + vim.fs.mkdir("io", { parents = true }) return true end