feat: refactors
This commit is contained in:
parent
d4c5f08b5f
commit
4c5c44742e
3 changed files with 98 additions and 93 deletions
|
|
@ -142,7 +142,7 @@ function M.run(cmd, stdin, timeout_ms, memory_mb, on_complete)
|
||||||
or lower:find('enomem', 1, true)
|
or lower:find('enomem', 1, true)
|
||||||
local near_cap = peak_mb >= (0.90 * memory_mb)
|
local near_cap = peak_mb >= (0.90 * memory_mb)
|
||||||
|
|
||||||
local mled = (peak_mb >= memory_mb) or near_cap or (oom_hint and not tled)
|
local mled = (peak_mb >= memory_mb) or near_cap or (oom_hint ~= nil and not tled)
|
||||||
|
|
||||||
if tled then
|
if tled then
|
||||||
logger.log(('Execution timed out in %.1fms.'):format(dt))
|
logger.log(('Execution timed out in %.1fms.'):format(dt))
|
||||||
|
|
|
||||||
|
|
@ -101,7 +101,7 @@ end
|
||||||
|
|
||||||
---@param test_case RanTestCase
|
---@param test_case RanTestCase
|
||||||
---@param debug boolean?
|
---@param debug boolean?
|
||||||
---@param on_complete fun(result: { status: "pass"|"fail"|"tle"|"mle", actual: string, actual_highlights: Highlight[], error: string, stderr: string, time_ms: number, code: integer, ok: boolean, signal: string, tled: boolean, mled: boolean, rss_mb: number })
|
---@param on_complete fun(result: { status: "pass"|"fail"|"tle"|"mle", actual: string, actual_highlights: Highlight[], error: string, stderr: string, time_ms: number, code: integer, ok: boolean, signal: string?, tled: boolean, mled: boolean, rss_mb: number })
|
||||||
local function run_single_test_case(test_case, debug, on_complete)
|
local function run_single_test_case(test_case, debug, on_complete)
|
||||||
local source_file = state.get_source_file()
|
local source_file = state.get_source_file()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -81,114 +81,119 @@ function M.toggle_interactive(interactor_cmd)
|
||||||
|
|
||||||
local execute = require('cp.runner.execute')
|
local execute = require('cp.runner.execute')
|
||||||
local run = require('cp.runner.run')
|
local run = require('cp.runner.run')
|
||||||
local compile_result = execute.compile_problem()
|
|
||||||
if not compile_result.success then
|
|
||||||
run.handle_compilation_failure(compile_result.output)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local binary = state.get_binary_file()
|
local function restore_session()
|
||||||
if not binary or binary == '' then
|
|
||||||
logger.log('No binary produced.', vim.log.levels.ERROR)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local cmdline
|
|
||||||
if interactor_cmd and interactor_cmd ~= '' then
|
|
||||||
local interactor = interactor_cmd
|
|
||||||
if not interactor:find('/') then
|
|
||||||
interactor = './' .. interactor
|
|
||||||
end
|
|
||||||
if vim.fn.executable(interactor) ~= 1 then
|
|
||||||
logger.log(
|
|
||||||
("Interactor '%s' is not executable."):format(interactor_cmd),
|
|
||||||
vim.log.levels.ERROR
|
|
||||||
)
|
|
||||||
if state.saved_interactive_session then
|
|
||||||
vim.cmd.source(state.saved_interactive_session)
|
|
||||||
vim.fn.delete(state.saved_interactive_session)
|
|
||||||
state.saved_interactive_session = nil
|
|
||||||
end
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local orchestrator = vim.fn.fnamemodify(utils.get_plugin_path() .. '/scripts/interact.py', ':p')
|
|
||||||
cmdline = table.concat({
|
|
||||||
'uv',
|
|
||||||
'run',
|
|
||||||
vim.fn.shellescape(orchestrator),
|
|
||||||
vim.fn.shellescape(interactor),
|
|
||||||
vim.fn.shellescape(binary),
|
|
||||||
}, ' ')
|
|
||||||
else
|
|
||||||
cmdline = vim.fn.shellescape(binary)
|
|
||||||
end
|
|
||||||
|
|
||||||
vim.cmd.terminal(cmdline)
|
|
||||||
local term_buf = vim.api.nvim_get_current_buf()
|
|
||||||
local term_win = vim.api.nvim_get_current_win()
|
|
||||||
|
|
||||||
local cleaned = false
|
|
||||||
local function cleanup()
|
|
||||||
if cleaned then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
cleaned = true
|
|
||||||
if term_buf and vim.api.nvim_buf_is_valid(term_buf) then
|
|
||||||
local job = vim.b[term_buf] and vim.b[term_buf].terminal_job_id or nil
|
|
||||||
if job then
|
|
||||||
pcall(vim.fn.jobstop, job)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if state.saved_interactive_session then
|
if state.saved_interactive_session then
|
||||||
vim.cmd.source(state.saved_interactive_session)
|
vim.cmd.source(state.saved_interactive_session)
|
||||||
vim.fn.delete(state.saved_interactive_session)
|
vim.fn.delete(state.saved_interactive_session)
|
||||||
state.saved_interactive_session = nil
|
state.saved_interactive_session = nil
|
||||||
end
|
end
|
||||||
state.interactive_buf = nil
|
|
||||||
state.interactive_win = nil
|
|
||||||
state.set_active_panel(nil)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.api.nvim_create_autocmd({ 'BufWipeout', 'BufUnload' }, {
|
execute.compile_problem(false, function(compile_result)
|
||||||
buffer = term_buf,
|
if not compile_result.success then
|
||||||
callback = cleanup,
|
run.handle_compilation_failure(compile_result.output)
|
||||||
})
|
restore_session()
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
vim.api.nvim_create_autocmd('WinClosed', {
|
local binary = state.get_binary_file()
|
||||||
callback = function()
|
if not binary or binary == '' then
|
||||||
|
logger.log('No binary produced.', vim.log.levels.ERROR)
|
||||||
|
restore_session()
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local cmdline
|
||||||
|
if interactor_cmd and interactor_cmd ~= '' then
|
||||||
|
local interactor = interactor_cmd
|
||||||
|
if not interactor:find('/') then
|
||||||
|
interactor = './' .. interactor
|
||||||
|
end
|
||||||
|
if vim.fn.executable(interactor) ~= 1 then
|
||||||
|
logger.log(
|
||||||
|
("Interactor '%s' is not executable."):format(interactor_cmd),
|
||||||
|
vim.log.levels.ERROR
|
||||||
|
)
|
||||||
|
restore_session()
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local orchestrator =
|
||||||
|
vim.fn.fnamemodify(utils.get_plugin_path() .. '/scripts/interact.py', ':p')
|
||||||
|
cmdline = table.concat({
|
||||||
|
'uv',
|
||||||
|
'run',
|
||||||
|
vim.fn.shellescape(orchestrator),
|
||||||
|
vim.fn.shellescape(interactor),
|
||||||
|
vim.fn.shellescape(binary),
|
||||||
|
}, ' ')
|
||||||
|
else
|
||||||
|
cmdline = vim.fn.shellescape(binary)
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.cmd.terminal(cmdline)
|
||||||
|
local term_buf = vim.api.nvim_get_current_buf()
|
||||||
|
local term_win = vim.api.nvim_get_current_win()
|
||||||
|
|
||||||
|
local cleaned = false
|
||||||
|
local function cleanup()
|
||||||
if cleaned then
|
if cleaned then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local any = false
|
cleaned = true
|
||||||
for _, win in ipairs(vim.api.nvim_list_wins()) do
|
if term_buf and vim.api.nvim_buf_is_valid(term_buf) then
|
||||||
if vim.api.nvim_win_is_valid(win) and vim.api.nvim_win_get_buf(win) == term_buf then
|
local job = vim.b[term_buf] and vim.b[term_buf].terminal_job_id or nil
|
||||||
any = true
|
if job then
|
||||||
break
|
pcall(vim.fn.jobstop, job)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if not any then
|
restore_session()
|
||||||
cleanup()
|
state.interactive_buf = nil
|
||||||
end
|
state.interactive_win = nil
|
||||||
end,
|
state.set_active_panel(nil)
|
||||||
})
|
end
|
||||||
|
|
||||||
vim.api.nvim_create_autocmd('TermClose', {
|
vim.api.nvim_create_autocmd({ 'BufWipeout', 'BufUnload' }, {
|
||||||
buffer = term_buf,
|
buffer = term_buf,
|
||||||
callback = function()
|
callback = cleanup,
|
||||||
vim.b[term_buf].cp_interactive_exited = true
|
})
|
||||||
end,
|
|
||||||
})
|
|
||||||
|
|
||||||
vim.keymap.set('t', '<c-q>', function()
|
vim.api.nvim_create_autocmd('WinClosed', {
|
||||||
cleanup()
|
callback = function()
|
||||||
end, { buffer = term_buf, silent = true })
|
if cleaned then
|
||||||
vim.keymap.set('n', '<c-q>', function()
|
return
|
||||||
cleanup()
|
end
|
||||||
end, { buffer = term_buf, silent = true })
|
local any = false
|
||||||
|
for _, win in ipairs(vim.api.nvim_list_wins()) do
|
||||||
|
if vim.api.nvim_win_is_valid(win) and vim.api.nvim_win_get_buf(win) == term_buf then
|
||||||
|
any = true
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if not any then
|
||||||
|
cleanup()
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
|
||||||
state.interactive_buf = term_buf
|
vim.api.nvim_create_autocmd('TermClose', {
|
||||||
state.interactive_win = term_win
|
buffer = term_buf,
|
||||||
state.set_active_panel('interactive')
|
callback = function()
|
||||||
|
vim.b[term_buf].cp_interactive_exited = true
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
|
||||||
|
vim.keymap.set('t', '<c-q>', function()
|
||||||
|
cleanup()
|
||||||
|
end, { buffer = term_buf, silent = true })
|
||||||
|
vim.keymap.set('n', '<c-q>', function()
|
||||||
|
cleanup()
|
||||||
|
end, { buffer = term_buf, silent = true })
|
||||||
|
|
||||||
|
state.interactive_buf = term_buf
|
||||||
|
state.interactive_win = term_win
|
||||||
|
state.set_active_panel('interactive')
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@return integer, integer
|
---@return integer, integer
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue