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

@ -1,6 +1,6 @@
---@class LanguageConfig
---@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 executable? string Executable name
---@field version? number Language version
@ -8,7 +8,7 @@
---@class PartialLanguageConfig
---@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 executable? string Executable name
---@field version? number Language version
@ -27,25 +27,22 @@
---@field timeout_ms? number
---@class Hooks
---@field before_run? fun(ctx: ProblemContext)
---@field before_test? fun(ctx: ProblemContext)
---@field before_debug? fun(ctx: ProblemContext)
---@field setup_code? fun(ctx: ProblemContext)
---@class TestPanelConfig
---@field diff_mode "vim"|"git" Diff backend to use
---@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
---@field command string Git executable name
---@field args string[] Additional git diff arguments
---@class DiffVimConfig
---@field enable_diffthis boolean Enable vim's diffthis
---@class DiffConfig
---@field git DiffGitConfig
---@field vim DiffVimConfig
---@class cp.Config
---@field contests table<string, ContestConfig>
@ -75,7 +72,7 @@ M.defaults = {
contests = {},
snippets = {},
hooks = {
before_run = nil,
before_test = nil,
before_debug = nil,
setup_code = nil,
},
@ -85,16 +82,14 @@ M.defaults = {
test_panel = {
diff_mode = 'vim',
toggle_key = 't',
status_format = 'compact',
next_test_key = '<c-n>',
prev_test_key = '<c-p>',
},
diff = {
git = {
command = 'git',
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
vim.validate({
before_run = {
user_config.hooks.before_run,
before_test = {
user_config.hooks.before_test,
{ 'function', 'nil' },
true,
},
@ -146,13 +141,26 @@ function M.setup(user_config)
end,
"diff_mode must be 'vim' or 'git'",
},
toggle_key = { user_config.test_panel.toggle_key, 'string', true },
status_format = {
user_config.test_panel.status_format,
toggle_key = {
user_config.test_panel.toggle_key,
function(value)
return vim.tbl_contains({ 'compact', 'verbose' }, value)
return type(value) == 'string' and value ~= ''
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
@ -160,7 +168,6 @@ function M.setup(user_config)
if user_config.diff then
vim.validate({
git = { user_config.diff.git, { 'table', 'nil' }, true },
vim = { user_config.diff.vim, { 'table', 'nil' }, true },
})
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'
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 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')
test_render.setup_highlights()
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
local function update_buffer_content(bufnr, lines)
@ -305,7 +305,6 @@ local function toggle_test_panel(is_debug)
end
else
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_win_call(test_windows.expected_win, function()
vim.cmd.diffthis()
@ -349,10 +348,10 @@ local function toggle_test_panel(is_debug)
refresh_test_panel()
end
vim.keymap.set('n', '<c-n>', function()
vim.keymap.set('n', config.test_panel.next_test_key, function()
navigate_test_case(1)
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)
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
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
config.hooks.before_debug(ctx)
end

View file

@ -172,7 +172,7 @@ local function run_single_test_case(ctx, contest_config, test_case)
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'

View file

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