feat: :CP test refactor
This commit is contained in:
parent
b2e1ea2c58
commit
fb240fd501
10 changed files with 97 additions and 200 deletions
134
lua/cp/init.lua
134
lua/cp/init.lua
|
|
@ -4,7 +4,6 @@ local config_module = require("cp.config")
|
|||
local snippets = require("cp.snippets")
|
||||
local execute = require("cp.execute")
|
||||
local scrape = require("cp.scrape")
|
||||
local window = require("cp.window")
|
||||
local logger = require("cp.log")
|
||||
local problem = require("cp.problem")
|
||||
local cache = require("cp.cache")
|
||||
|
|
@ -133,13 +132,6 @@ local function setup_problem(contest_id, problem_id, language)
|
|||
config.hooks.setup_code(ctx)
|
||||
end
|
||||
|
||||
local src_buf = vim.api.nvim_get_current_buf()
|
||||
local input_buf = vim.fn.bufnr(ctx.input_file, true)
|
||||
local output_buf = vim.fn.bufnr(ctx.output_file, true)
|
||||
|
||||
local tile_fn = config.tile or window.default_tile
|
||||
tile_fn(src_buf, input_buf, output_buf)
|
||||
|
||||
logger.log(("switched to problem %s"):format(ctx.problem_name))
|
||||
end
|
||||
|
||||
|
|
@ -152,60 +144,7 @@ local function get_current_problem()
|
|||
return filename
|
||||
end
|
||||
|
||||
local function run_problem()
|
||||
local problem_id = get_current_problem()
|
||||
if not problem_id then
|
||||
return
|
||||
end
|
||||
|
||||
logger.log(("running problem: %s"):format(problem_id))
|
||||
|
||||
if not state.platform then
|
||||
logger.log(
|
||||
"No contest configured. Use :CP <platform> <contest> <problem> to set up first.",
|
||||
vim.log.levels.ERROR
|
||||
)
|
||||
return
|
||||
end
|
||||
|
||||
local contest_config = config.contests[state.platform]
|
||||
local ctx = problem.create_context(state.platform, state.contest_id, state.problem_id, config)
|
||||
|
||||
if config.hooks and config.hooks.before_run then
|
||||
config.hooks.before_run(ctx)
|
||||
end
|
||||
|
||||
vim.schedule(function()
|
||||
execute.run_problem(ctx, contest_config, false)
|
||||
vim.cmd.checktime()
|
||||
end)
|
||||
end
|
||||
|
||||
local function debug_problem()
|
||||
local problem_id = get_current_problem()
|
||||
if not problem_id then
|
||||
return
|
||||
end
|
||||
|
||||
if not state.platform then
|
||||
logger.log("no platform set", vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
|
||||
local contest_config = config.contests[state.platform]
|
||||
local ctx = problem.create_context(state.platform, state.contest_id, state.problem_id, config)
|
||||
|
||||
if config.hooks and config.hooks.before_debug then
|
||||
config.hooks.before_debug(ctx)
|
||||
end
|
||||
|
||||
vim.schedule(function()
|
||||
execute.run_problem(ctx, contest_config, true)
|
||||
vim.cmd.checktime()
|
||||
end)
|
||||
end
|
||||
|
||||
local function toggle_test_panel()
|
||||
local function toggle_test_panel(is_debug)
|
||||
if state.test_panel_active then
|
||||
if state.saved_session then
|
||||
vim.cmd(("source %s"):format(state.saved_session))
|
||||
|
|
@ -225,11 +164,6 @@ local function toggle_test_panel()
|
|||
return
|
||||
end
|
||||
|
||||
if state.platform == "codeforces" then
|
||||
logger.log("test panel not yet supported for codeforces", vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
|
||||
local problem_id = get_current_problem()
|
||||
if not problem_id then
|
||||
return
|
||||
|
|
@ -347,14 +281,14 @@ local function toggle_test_panel()
|
|||
return
|
||||
end
|
||||
|
||||
local expected_lines = {}
|
||||
|
||||
local expected_text = current_test.expected
|
||||
for _, line in ipairs(vim.split(expected_text, "\n", { plain = true, trimempty = true })) do
|
||||
table.insert(expected_lines, line)
|
||||
end
|
||||
local expected_lines = vim.split(expected_text, "\n", { plain = true, trimempty = true })
|
||||
|
||||
vim.api.nvim_buf_set_lines(test_buffers.expected_buf, 0, -1, false, expected_lines)
|
||||
|
||||
if vim.fn.has("nvim-0.8.0") == 1 then
|
||||
vim.api.nvim_set_option_value("winbar", "Expected", { win = test_windows.expected_win })
|
||||
end
|
||||
end
|
||||
|
||||
local function update_actual_pane()
|
||||
|
|
@ -366,27 +300,32 @@ local function toggle_test_panel()
|
|||
end
|
||||
|
||||
local actual_lines = {}
|
||||
local enable_diff = false
|
||||
|
||||
if current_test.actual then
|
||||
for _, line in ipairs(vim.split(current_test.actual, "\n", { plain = true, trimempty = true })) do
|
||||
table.insert(actual_lines, line)
|
||||
end
|
||||
|
||||
if current_test.status == "fail" then
|
||||
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 })
|
||||
else
|
||||
vim.api.nvim_set_option_value("diff", false, { win = test_windows.expected_win })
|
||||
vim.api.nvim_set_option_value("diff", false, { win = test_windows.actual_win })
|
||||
end
|
||||
actual_lines = vim.split(current_test.actual, "\n", { plain = true, trimempty = true })
|
||||
enable_diff = current_test.status == "fail"
|
||||
else
|
||||
table.insert(actual_lines, "(not run yet)")
|
||||
|
||||
vim.api.nvim_set_option_value("diff", false, { win = test_windows.expected_win })
|
||||
vim.api.nvim_set_option_value("diff", false, { win = test_windows.actual_win })
|
||||
actual_lines = { "(not run yet)" }
|
||||
end
|
||||
|
||||
vim.api.nvim_buf_set_lines(test_buffers.actual_buf, 0, -1, false, actual_lines)
|
||||
|
||||
if vim.fn.has("nvim-0.8.0") == 1 then
|
||||
vim.api.nvim_set_option_value("winbar", "Actual", { win = test_windows.actual_win })
|
||||
end
|
||||
|
||||
vim.api.nvim_set_option_value("diff", enable_diff, { win = test_windows.expected_win })
|
||||
vim.api.nvim_set_option_value("diff", enable_diff, { win = test_windows.actual_win })
|
||||
|
||||
if enable_diff then
|
||||
vim.api.nvim_win_call(test_windows.expected_win, function()
|
||||
vim.cmd("diffthis")
|
||||
end)
|
||||
vim.api.nvim_win_call(test_windows.actual_win, function()
|
||||
vim.cmd("diffthis")
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
local function refresh_test_panel()
|
||||
|
|
@ -430,9 +369,13 @@ local function toggle_test_panel()
|
|||
end, { buffer = buf, silent = true })
|
||||
end
|
||||
|
||||
if is_debug and config.hooks and config.hooks.before_debug then
|
||||
config.hooks.before_debug(ctx)
|
||||
end
|
||||
|
||||
local execute_module = require("cp.execute")
|
||||
local contest_config = config.contests[state.platform]
|
||||
if execute_module.compile_problem(ctx, contest_config) then
|
||||
if execute_module.compile_problem(ctx, contest_config, is_debug) then
|
||||
test_module.run_all_test_cases(ctx, contest_config)
|
||||
end
|
||||
|
||||
|
|
@ -515,6 +458,7 @@ local function parse_command(args)
|
|||
end
|
||||
|
||||
local language = nil
|
||||
local debug = false
|
||||
|
||||
for i, arg in ipairs(args) do
|
||||
local lang_match = arg:match("^--lang=(.+)$")
|
||||
|
|
@ -526,17 +470,19 @@ local function parse_command(args)
|
|||
else
|
||||
return { type = "error", message = "--lang requires a value" }
|
||||
end
|
||||
elseif arg == "--debug" then
|
||||
debug = true
|
||||
end
|
||||
end
|
||||
|
||||
local filtered_args = vim.tbl_filter(function(arg)
|
||||
return not (arg:match("^--lang") or arg == language)
|
||||
return not (arg:match("^--lang") or arg == language or arg == "--debug")
|
||||
end, args)
|
||||
|
||||
local first = filtered_args[1]
|
||||
|
||||
if vim.tbl_contains(actions, first) then
|
||||
return { type = "action", action = first, language = language }
|
||||
return { type = "action", action = first, language = language, debug = debug }
|
||||
end
|
||||
|
||||
if vim.tbl_contains(platforms, first) then
|
||||
|
|
@ -604,12 +550,8 @@ function M.handle_command(opts)
|
|||
end
|
||||
|
||||
if cmd.type == "action" then
|
||||
if cmd.action == "run" then
|
||||
run_problem()
|
||||
elseif cmd.action == "debug" then
|
||||
debug_problem()
|
||||
elseif cmd.action == "test" then
|
||||
toggle_test_panel()
|
||||
if cmd.action == "test" then
|
||||
toggle_test_panel(cmd.debug)
|
||||
elseif cmd.action == "next" then
|
||||
navigate_problem(1, cmd.language)
|
||||
elseif cmd.action == "prev" then
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue