From 718ce9036f807353dcd4fde140771b15b59ccdf0 Mon Sep 17 00:00:00 2001 From: newbieQQ Date: Wed, 17 Jun 2026 15:21:53 +0800 Subject: [PATCH] fix: detect buffer mismatch on toggle (e.g. :e opened file over terminal) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- lua/QQdock/init.lua | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/lua/QQdock/init.lua b/lua/QQdock/init.lua index 99627f5..8d873af 100644 --- a/lua/QQdock/init.lua +++ b/lua/QQdock/init.lua @@ -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 -- 需要打开 → 基于当前窗口计算布局