Merge pull request #211 from barrettruth/fix/disappearing-io-view

fix `:CP {prev,next}` race condition
This commit is contained in:
Barrett Ruth 2026-01-27 11:28:16 -06:00 committed by GitHub
commit b88e2ce746
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 31 additions and 5 deletions

9
.gitignore vendored
View file

@ -1,11 +1,16 @@
.venv/
.venv
venv
doc/tags
*.log
build
io
debug
venv/
create
.*cache*
CLAUDE.md
__pycache__
.claude/
node_modules/

View file

@ -25,6 +25,7 @@ repos:
hooks:
- id: prettier
name: prettier
files: \.(md|,toml,yaml,sh)$
- repo: local
hooks:

View file

@ -348,6 +348,8 @@ function M.navigate_problem(direction, language)
return
end
logger.log(('navigate_problem: %s -> %s'):format(current_problem_id, problems[new_index].id))
local active_panel = state.get_active_panel()
if active_panel == 'run' then
require('cp.ui.views').disable()

View file

@ -10,6 +10,7 @@
---@field output_buf integer
---@field input_buf integer
---@field current_test_index integer?
---@field source_buf integer?
---@class cp.State
---@field get_platform fun(): string?

View file

@ -194,14 +194,27 @@ end
---@return integer, integer
local function get_or_create_io_buffers()
local io_state = state.get_io_view_state()
local solution_win = state.get_solution_win()
local current_source_buf = vim.api.nvim_win_get_buf(solution_win)
if io_state then
local output_valid = io_state.output_buf and vim.api.nvim_buf_is_valid(io_state.output_buf)
local input_valid = io_state.input_buf and vim.api.nvim_buf_is_valid(io_state.input_buf)
local same_source = io_state.source_buf == current_source_buf
if output_valid and input_valid then
if output_valid and input_valid and same_source then
return io_state.output_buf, io_state.input_buf
end
if io_state.source_buf then
pcall(vim.api.nvim_del_augroup_by_name, 'cp_io_cleanup_buf' .. io_state.source_buf)
end
if output_valid then
pcall(vim.api.nvim_buf_delete, io_state.output_buf, { force = true })
end
if input_valid then
pcall(vim.api.nvim_buf_delete, io_state.input_buf, { force = true })
end
end
local output_buf = utils.create_buffer_with_options('cpout')
@ -211,10 +224,10 @@ local function get_or_create_io_buffers()
output_buf = output_buf,
input_buf = input_buf,
current_test_index = 1,
source_buf = current_source_buf,
})
local solution_win = state.get_solution_win()
local source_buf = vim.api.nvim_win_get_buf(solution_win)
local source_buf = current_source_buf
local group_name = 'cp_io_cleanup_buf' .. source_buf
vim.api.nvim_create_augroup(group_name, { clear = true })
@ -249,6 +262,10 @@ local function get_or_create_io_buffers()
return
end
if io.source_buf ~= source_buf then
return
end
local wins = vim.api.nvim_list_wins()
for _, win in ipairs(wins) do
if vim.api.nvim_win_get_buf(win) == source_buf then