From a842886933d83ec5580c3efb2a06a5a7d9cc9c27 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Fri, 24 Oct 2025 14:47:12 -0400 Subject: [PATCH] feat(ui): auto-hide source buffer on close --- doc/cp.nvim.txt | 20 ++++++++++++-------- lua/cp/helpers.lua | 1 - lua/cp/ui/edit.lua | 7 ------- lua/cp/ui/views.lua | 17 +++++++++++++++++ 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/doc/cp.nvim.txt b/doc/cp.nvim.txt index 71d73d7..2956d4b 100644 --- a/doc/cp.nvim.txt +++ b/doc/cp.nvim.txt @@ -590,14 +590,18 @@ Input: |VerdictFormatData| table with test results Output: |VerdictFormatResult| table with formatted line and optional highlights *VerdictFormatData* - {index} (integer) Test case number - {status} (table) { text: string, highlight_group: string } - {time_ms} (number) Execution time in milliseconds - {time_limit_ms} (number) Time limit in milliseconds - {memory_mb} (number) Peak memory usage in megabytes - {memory_limit_mb} (number) Memory limit in megabytes - {exit_code} (integer) Process exit code - {signal} (string|nil) Signal name for crashes (e.g. "SIGSEGV") + {index} (integer) Test case number + {status} (table) { text: string, highlight_group: string } + {time_ms} (number) Execution time in milliseconds + {time_limit_ms} (number) Time limit in milliseconds + {memory_mb} (number) Peak memory usage in megabytes + {memory_limit_mb} (number) Memory limit in megabytes + {exit_code} (integer) Process exit code + {signal} (string|nil) Signal name for crashes (e.g. "SIGSEGV") + {time_actual_width} (integer|nil) Dynamic width for time value alignment + {time_limit_width} (integer|nil) Dynamic width for time limit alignment + {mem_actual_width} (integer|nil) Dynamic width for memory value alignment + {mem_limit_width} (integer|nil) Dynamic width for memory limit alignment *VerdictFormatResult* {line} (string) The formatted verdict line diff --git a/lua/cp/helpers.lua b/lua/cp/helpers.lua index 070193c..57f87ca 100644 --- a/lua/cp/helpers.lua +++ b/lua/cp/helpers.lua @@ -58,7 +58,6 @@ function M.default_verdict_formatter(data) local exit_str = data.signal and string.format('%d (%s)', data.exit_code, data.signal) or tostring(data.exit_code) - -- Use dynamic widths if provided, otherwise use reasonable defaults local time_actual_w = data.time_actual_width or 6 local time_limit_w = data.time_limit_width or 4 local mem_actual_w = data.mem_actual_width or 3 diff --git a/lua/cp/ui/edit.lua b/lua/cp/ui/edit.lua index 897c4c4..2616016 100644 --- a/lua/cp/ui/edit.lua +++ b/lua/cp/ui/edit.lua @@ -156,20 +156,15 @@ function M.toggle_edit(test_index) local test_buffers = {} local num_tests = #test_cases - -- Step 1: Create N columns (vsplit creates full-height columns) for i = 1, num_tests - 1 do vim.cmd('vsplit') end - -- Step 2: Go to leftmost window vim.cmd('1wincmd w') - -- Step 3: For each column, split horizontally into input (top) and expected (bottom) for col = 1, num_tests do - -- Split current window horizontally vim.cmd('split') - -- After split, cursor is in bottom window. Go up to input window. vim.cmd('wincmd k') local input_win = vim.api.nvim_get_current_win() local input_buf = utils.create_buffer_with_options() @@ -180,7 +175,6 @@ function M.toggle_edit(test_index) vim.bo[input_buf].buflisted = false helpers.clearcol(input_buf) - -- Go down to expected window vim.cmd('wincmd j') local expected_win = vim.api.nvim_get_current_win() local expected_buf = utils.create_buffer_with_options() @@ -198,7 +192,6 @@ function M.toggle_edit(test_index) expected_win = expected_win, } - -- Move to next column (go up to top, then right) vim.cmd('wincmd k') vim.cmd('wincmd l') end diff --git a/lua/cp/ui/views.lua b/lua/cp/ui/views.lua index 4aa877f..ab71d5b 100644 --- a/lua/cp/ui/views.lua +++ b/lua/cp/ui/views.lua @@ -247,6 +247,23 @@ function M.ensure_io_view() current_test_index = 1, }) + local source_buf = vim.api.nvim_win_get_buf(solution_win) + vim.api.nvim_create_autocmd('BufDelete', { + buffer = source_buf, + callback = function() + local io = state.get_io_view_state() + if io then + if io.output_buf and vim.api.nvim_buf_is_valid(io.output_buf) then + vim.api.nvim_buf_delete(io.output_buf, { force = true }) + end + if io.input_buf and vim.api.nvim_buf_is_valid(io.input_buf) then + vim.api.nvim_buf_delete(io.input_buf, { force = true }) + end + state.set_io_view_state(nil) + end + end, + }) + if cfg.hooks and cfg.hooks.setup_io_output then pcall(cfg.hooks.setup_io_output, output_buf, state) end