feat: more stuff

This commit is contained in:
Barrett Ruth 2025-09-19 12:31:19 -04:00
parent ab9a0f43b5
commit 5ca6b8b272
6 changed files with 67 additions and 52 deletions

View file

@ -85,7 +85,7 @@ Optional configuration with lazy.nvim: >
'g++', '-std=c++{version}', '-O2', '-Wall', '-Wextra', 'g++', '-std=c++{version}', '-O2', '-Wall', '-Wextra',
'-DLOCAL', '{source}', '-o', '{binary}', '-DLOCAL', '{source}', '-o', '{binary}',
}, },
run = { '{binary}' }, test = { '{binary}' },
debug = { debug = {
'g++', '-std=c++{version}', '-g3', 'g++', '-std=c++{version}', '-g3',
'-fsanitize=address,undefined', '-DLOCAL', '-fsanitize=address,undefined', '-DLOCAL',
@ -95,7 +95,7 @@ Optional configuration with lazy.nvim: >
extension = "cc", extension = "cc",
}, },
python = { python = {
run = { 'python3', '{source}' }, test = { 'python3', '{source}' },
debug = { 'python3', '{source}' }, debug = { 'python3', '{source}' },
extension = "py", extension = "py",
}, },
@ -104,11 +104,8 @@ Optional configuration with lazy.nvim: >
}, },
}, },
hooks = { hooks = {
before_run = function(ctx) vim.cmd.w() end, before_test = function(ctx) vim.cmd.w() end,
before_debug = function(ctx) before_debug = function(ctx) ... end,
-- ctx.problem_id, ctx.platform, ctx.source_file, etc.
vim.cmd.w()
end,
setup_code = function(ctx) setup_code = function(ctx)
vim.wo.foldmethod = "marker" vim.wo.foldmethod = "marker"
vim.wo.foldmarker = "{{{,}}}" vim.wo.foldmarker = "{{{,}}}"
@ -116,9 +113,10 @@ Optional configuration with lazy.nvim: >
end, end,
}, },
test_panel = { test_panel = {
diff_mode = "vim", -- "vim" or "git" diff_mode = "vim", -- "vim" or "git"
toggle_key = "t", -- key to toggle test panel toggle_key = "t", -- toggle test panel
status_format = "compact", -- "compact" or "verbose" next_test_key = "<c-n>", -- navigate to next test case
prev_test_key = "<c-p>", -- navigate to previous test case
}, },
diff = { diff = {
git = { git = {
@ -126,9 +124,6 @@ Optional configuration with lazy.nvim: >
args = {"diff", "--no-index", "--word-diff=plain", args = {"diff", "--no-index", "--word-diff=plain",
"--word-diff-regex=.", "--no-prefix"}, "--word-diff-regex=.", "--no-prefix"},
}, },
vim = {
enable_diffthis = true,
},
}, },
snippets = { ... }, -- LuaSnip snippets snippets = { ... }, -- LuaSnip snippets
filename = function(contest, contest_id, problem_id, config, language) ... end, filename = function(contest, contest_id, problem_id, config, language) ... end,
@ -168,7 +163,7 @@ Optional configuration with lazy.nvim: >
Fields: ~ Fields: ~
• {compile}? (`string[]`) Compile command template with • {compile}? (`string[]`) Compile command template with
`{version}`, `{source}`, `{binary}` placeholders. `{version}`, `{source}`, `{binary}` placeholders.
• {run} (`string[]`) Run command template. • {test} (`string[]`) Test execution command template.
• {debug}? (`string[]`) Debug compile command template. • {debug}? (`string[]`) Debug compile command template.
• {version}? (`number`) Language version (e.g. 20, 23 for C++). • {version}? (`number`) Language version (e.g. 20, 23 for C++).
• {extension} (`string`) File extension (e.g. "cc", "py"). • {extension} (`string`) File extension (e.g. "cc", "py").
@ -180,23 +175,38 @@ Optional configuration with lazy.nvim: >
• {diff_mode} (`string`, default: `"vim"`) Diff backend: "vim" or "git". • {diff_mode} (`string`, default: `"vim"`) Diff backend: "vim" or "git".
Git provides character-level precision, vim uses built-in diff. Git provides character-level precision, vim uses built-in diff.
• {toggle_key} (`string`, default: `"t"`) Key to toggle test panel. • {toggle_key} (`string`, default: `"t"`) Key to toggle test panel.
• {status_format} (`string`, default: `"compact"`) Status display format. • {next_test_key} (`string`, default: `"<c-n>"`) Key to navigate to next test case.
• {prev_test_key} (`string`, default: `"<c-p>"`) Key to navigate to previous test case.
*cp.DiffConfig* *cp.DiffConfig*
Fields: ~ Fields: ~
• {git} (`DiffGitConfig`) Git diff backend configuration. • {git} (`DiffGitConfig`) Git diff backend configuration.
• {vim} (`DiffVimConfig`) Vim diff backend configuration.
*cp.Hooks* *cp.Hooks*
Fields: ~ Fields: ~
• {before_test}? (`function`) Called before test panel opens.
`function(ctx: ProblemContext)`
• {before_debug}? (`function`) Called before debug compilation. • {before_debug}? (`function`) Called before debug compilation.
`function(ctx: ProblemContext)` `function(ctx: ProblemContext)`
• {setup_code}? (`function`) Called after source file is opened. • {setup_code}? (`function`) Called after source file is opened.
Used to configure buffer settings. Good for configuring buffer settings.
`function(ctx: ProblemContext)` `function(ctx: ProblemContext)`
*ProblemContext*
Fields: ~
• {contest} (`string`) Platform name (e.g. "atcoder", "codeforces")
• {contest_id} (`string`) Contest ID (e.g. "abc123", "1933")
• {problem_id}? (`string`) Problem ID (e.g. "a", "b") - nil for CSES
• {source_file} (`string`) Source filename (e.g. "abc123a.cpp")
• {binary_file} (`string`) Binary output path (e.g. "build/abc123a.run")
• {input_file} (`string`) Test input path (e.g. "io/abc123a.cpin")
• {output_file} (`string`) Program output path (e.g. "io/abc123a.cpout")
• {expected_file} (`string`) Expected output path (e.g. "io/abc123a.expected")
• {problem_name} (`string`) Display name (e.g. "abc123a")
WORKFLOW *cp-workflow* WORKFLOW *cp-workflow*
For the sake of consistency and simplicity, cp.nvim extracts contest/problem identifiers from For the sake of consistency and simplicity, cp.nvim extracts contest/problem identifiers from
@ -328,8 +338,8 @@ Test cases use competitive programming terminology:
Keymaps ~ Keymaps ~
*cp-test-keys* *cp-test-keys*
<c-n> Navigate to next test case <c-n> Navigate to next test case (configurable via test_panel.next_test_key)
<c-p> Navigate to previous test case <c-p> Navigate to previous test case (configurable via test_panel.prev_test_key)
q Exit test panel (restore layout) q Exit test panel (restore layout)
t Toggle test panel (configurable via test_panel.toggle_key) t Toggle test panel (configurable via test_panel.toggle_key)

View file

@ -1,6 +1,6 @@
---@class LanguageConfig ---@class LanguageConfig
---@field compile? string[] Compile command template ---@field compile? string[] Compile command template
---@field run string[] Run command template ---@field test string[] Test execution command template
---@field debug? string[] Debug command template ---@field debug? string[] Debug command template
---@field executable? string Executable name ---@field executable? string Executable name
---@field version? number Language version ---@field version? number Language version
@ -8,7 +8,7 @@
---@class PartialLanguageConfig ---@class PartialLanguageConfig
---@field compile? string[] Compile command template ---@field compile? string[] Compile command template
---@field run? string[] Run command template ---@field test? string[] Test execution command template
---@field debug? string[] Debug command template ---@field debug? string[] Debug command template
---@field executable? string Executable name ---@field executable? string Executable name
---@field version? number Language version ---@field version? number Language version
@ -27,25 +27,22 @@
---@field timeout_ms? number ---@field timeout_ms? number
---@class Hooks ---@class Hooks
---@field before_run? fun(ctx: ProblemContext) ---@field before_test? fun(ctx: ProblemContext)
---@field before_debug? fun(ctx: ProblemContext) ---@field before_debug? fun(ctx: ProblemContext)
---@field setup_code? fun(ctx: ProblemContext) ---@field setup_code? fun(ctx: ProblemContext)
---@class TestPanelConfig ---@class TestPanelConfig
---@field diff_mode "vim"|"git" Diff backend to use ---@field diff_mode "vim"|"git" Diff backend to use
---@field toggle_key string Key to toggle test panel ---@field toggle_key string Key to toggle test panel
---@field status_format "compact"|"verbose" Status display format ---@field next_test_key string Key to navigate to next test case
---@field prev_test_key string Key to navigate to previous test case
---@class DiffGitConfig ---@class DiffGitConfig
---@field command string Git executable name ---@field command string Git executable name
---@field args string[] Additional git diff arguments ---@field args string[] Additional git diff arguments
---@class DiffVimConfig
---@field enable_diffthis boolean Enable vim's diffthis
---@class DiffConfig ---@class DiffConfig
---@field git DiffGitConfig ---@field git DiffGitConfig
---@field vim DiffVimConfig
---@class cp.Config ---@class cp.Config
---@field contests table<string, ContestConfig> ---@field contests table<string, ContestConfig>
@ -75,7 +72,7 @@ M.defaults = {
contests = {}, contests = {},
snippets = {}, snippets = {},
hooks = { hooks = {
before_run = nil, before_test = nil,
before_debug = nil, before_debug = nil,
setup_code = nil, setup_code = nil,
}, },
@ -85,16 +82,14 @@ M.defaults = {
test_panel = { test_panel = {
diff_mode = 'vim', diff_mode = 'vim',
toggle_key = 't', toggle_key = 't',
status_format = 'compact', next_test_key = '<c-n>',
prev_test_key = '<c-p>',
}, },
diff = { diff = {
git = { git = {
command = 'git', command = 'git',
args = { 'diff', '--no-index', '--word-diff=plain', '--word-diff-regex=.', '--no-prefix' }, args = { 'diff', '--no-index', '--word-diff=plain', '--word-diff-regex=.', '--no-prefix' },
}, },
vim = {
enable_diffthis = true,
},
}, },
} }
@ -119,8 +114,8 @@ function M.setup(user_config)
if user_config.hooks then if user_config.hooks then
vim.validate({ vim.validate({
before_run = { before_test = {
user_config.hooks.before_run, user_config.hooks.before_test,
{ 'function', 'nil' }, { 'function', 'nil' },
true, true,
}, },
@ -146,13 +141,26 @@ function M.setup(user_config)
end, end,
"diff_mode must be 'vim' or 'git'", "diff_mode must be 'vim' or 'git'",
}, },
toggle_key = { user_config.test_panel.toggle_key, 'string', true }, toggle_key = {
status_format = { user_config.test_panel.toggle_key,
user_config.test_panel.status_format,
function(value) function(value)
return vim.tbl_contains({ 'compact', 'verbose' }, value) return type(value) == 'string' and value ~= ''
end, end,
"status_format must be 'compact' or 'verbose'", 'toggle_key must be a non-empty string',
},
next_test_key = {
user_config.test_panel.next_test_key,
function(value)
return type(value) == 'string' and value ~= ''
end,
'next_test_key must be a non-empty string',
},
prev_test_key = {
user_config.test_panel.prev_test_key,
function(value)
return type(value) == 'string' and value ~= ''
end,
'prev_test_key must be a non-empty string',
}, },
}) })
end end
@ -160,7 +168,6 @@ function M.setup(user_config)
if user_config.diff then if user_config.diff then
vim.validate({ vim.validate({
git = { user_config.diff.git, { 'table', 'nil' }, true }, git = { user_config.diff.git, { 'table', 'nil' }, true },
vim = { user_config.diff.vim, { 'table', 'nil' }, true },
}) })
end end

View file

@ -278,7 +278,7 @@ function M.run_problem(ctx, contest_config, is_debug)
input_data = table.concat(vim.fn.readfile(ctx.input_file), '\n') .. '\n' input_data = table.concat(vim.fn.readfile(ctx.input_file), '\n') .. '\n'
end end
local run_cmd = build_command(language_config.run, language_config.executable, substitutions) local run_cmd = build_command(language_config.test, language_config.executable, substitutions)
local exec_result = execute_command(run_cmd, input_data, contest_config.timeout_ms) local exec_result = execute_command(run_cmd, input_data, contest_config.timeout_ms)
local formatted_output = format_output(exec_result, ctx.expected_file, is_debug) local formatted_output = format_output(exec_result, ctx.expected_file, is_debug)

View file

@ -232,7 +232,7 @@ local function toggle_test_panel(is_debug)
local test_render = require('cp.test_render') local test_render = require('cp.test_render')
test_render.setup_highlights() test_render.setup_highlights()
local test_state = test_module.get_test_panel_state() local test_state = test_module.get_test_panel_state()
return test_render.render_test_list(test_state, config.test_panel) return test_render.render_test_list(test_state)
end end
local function update_buffer_content(bufnr, lines) local function update_buffer_content(bufnr, lines)
@ -305,7 +305,6 @@ local function toggle_test_panel(is_debug)
end end
else else
update_buffer_content(test_buffers.actual_buf, actual_lines) update_buffer_content(test_buffers.actual_buf, actual_lines)
vim.api.nvim_set_option_value('diff', true, { win = test_windows.expected_win })
vim.api.nvim_set_option_value('diff', true, { win = test_windows.actual_win }) vim.api.nvim_set_option_value('diff', true, { win = test_windows.actual_win })
vim.api.nvim_win_call(test_windows.expected_win, function() vim.api.nvim_win_call(test_windows.expected_win, function()
vim.cmd.diffthis() vim.cmd.diffthis()
@ -349,10 +348,10 @@ local function toggle_test_panel(is_debug)
refresh_test_panel() refresh_test_panel()
end end
vim.keymap.set('n', '<c-n>', function() vim.keymap.set('n', config.test_panel.next_test_key, function()
navigate_test_case(1) navigate_test_case(1)
end, { buffer = test_buffers.tab_buf, silent = true }) end, { buffer = test_buffers.tab_buf, silent = true })
vim.keymap.set('n', '<c-p>', function() vim.keymap.set('n', config.test_panel.prev_test_key, function()
navigate_test_case(-1) navigate_test_case(-1)
end, { buffer = test_buffers.tab_buf, silent = true }) end, { buffer = test_buffers.tab_buf, silent = true })
@ -365,6 +364,10 @@ local function toggle_test_panel(is_debug)
end, { buffer = buf, silent = true }) end, { buffer = buf, silent = true })
end end
if config.hooks and config.hooks.before_test then
config.hooks.before_test(ctx)
end
if is_debug and config.hooks and config.hooks.before_debug then if is_debug and config.hooks and config.hooks.before_debug then
config.hooks.before_debug(ctx) config.hooks.before_debug(ctx)
end end

View file

@ -172,7 +172,7 @@ local function run_single_test_case(ctx, contest_config, test_case)
end end
end end
local run_cmd = build_command(language_config.run, language_config.executable, substitutions) local run_cmd = build_command(language_config.test, language_config.executable, substitutions)
local stdin_content = test_case.input .. '\n' local stdin_content = test_case.input .. '\n'

View file

@ -1,6 +1,3 @@
---@class TestRenderConfig
---@field status_format "compact"|"verbose"
---@class StatusInfo ---@class StatusInfo
---@field text string ---@field text string
---@field highlight_group string ---@field highlight_group string
@ -32,10 +29,8 @@ end
---Render test cases list with improved layout ---Render test cases list with improved layout
---@param test_state TestPanelState ---@param test_state TestPanelState
---@param config? TestRenderConfig
---@return string[] ---@return string[]
function M.render_test_list(test_state, config) function M.render_test_list(test_state)
config = config or { status_format = 'compact' }
local lines = {} local lines = {}
for i, test_case in ipairs(test_state.test_cases) do for i, test_case in ipairs(test_state.test_cases) do