fix: detect buffer mismatch on toggle (e.g. :e opened file over terminal)

When terminal occupies main window and user directly opens a file
(:e somefile) in that window, the window's buffer no longer matches
term.bufnr. Previously this caused the hide path to incorrectly swap
back original_bufnr, wiping the user's file. Now we detect mismatch,
clear winid/borrowed, and re-evaluate use_main — which will correctly
fall through to a split when a real file is present.
This commit is contained in:
QQ 2026-06-17 15:21:53 +08:00
parent 81d623c5c0
commit 718ce9036f
1 changed files with 16 additions and 10 deletions

View File

@ -110,18 +110,24 @@ function M.open(cmd)
-- 已打开 → 关闭/隐藏,终端进程继续
if term and term.winid and vim.api.nvim_win_is_valid(term.winid) then
if config.debug then
vim.notify('QQdock: hide [' .. name .. ']', vim.log.levels.INFO)
end
if term.borrowed then
-- 占了主窗口 → 换回原始空 buffer
vim.api.nvim_win_set_buf(term.winid, term.original_bufnr)
-- 窗口 buffer 已被替换(用户直接 :e 打开了文件)→ 当作已隐藏
if vim.api.nvim_win_get_buf(term.winid) ~= term.bufnr then
term.winid = nil
term.borrowed = nil
else
vim.api.nvim_win_close(term.winid, true)
if config.debug then
vim.notify('QQdock: hide [' .. name .. ']', vim.log.levels.INFO)
end
if term.borrowed then
-- 占了主窗口 → 换回原始空 buffer
vim.api.nvim_win_set_buf(term.winid, term.original_bufnr)
else
vim.api.nvim_win_close(term.winid, true)
end
term.winid = nil
term.borrowed = nil
return
end
term.winid = nil
term.borrowed = nil
return
end
-- 需要打开 → 基于当前窗口计算布局