@@ -436,4 +436,47 @@ T["client()"]["disabled copilot does not spam warnings on buffer enter"] = funct
436436 MiniTest .expect .equality (count , 0 )
437437end
438438
439+ T [" client()" ][" on_buf_enter skips filetype check for non-buflisted buffers" ] = function ()
440+ child .configure_copilot ()
441+
442+ -- Create a non-buflisted buffer with a filetype mismatch and trigger BufEnter.
443+ -- This simulates floating windows / preview buffers that should not trigger
444+ -- the filetype-change detach+re-attach cycle.
445+ child .lua ([[
446+ local util = require("copilot.util")
447+
448+ local buf = vim.api.nvim_create_buf(false, true) -- nobuflisted, scratch
449+ vim.api.nvim_set_option_value("filetype", "lua", { buf = buf })
450+
451+ -- Manually set previous_ft to simulate a buffer that was previously attached.
452+ -- buf_attach won't succeed for non-buflisted buffers (should_attach rejects them),
453+ -- so previous_ft wouldn't normally be set. In the real bug scenario, this state
454+ -- can be reached through various pathways.
455+ util.set_buffer_previous_ft(buf, "lua")
456+
457+ -- Change filetype on same non-buflisted buffer
458+ vim.api.nvim_set_option_value("filetype", "python", { buf = buf })
459+
460+ -- Trigger BufEnter (simulating entering the buffer)
461+ vim.api.nvim_exec_autocmds("BufEnter", { buffer = buf })
462+
463+ -- Wait for scheduled callbacks to execute
464+ vim.wait(200, function() return false end, 10)
465+ ]] )
466+
467+ -- The filetype change should NOT cause detach+re-attach for non-buflisted buffers
468+ local detach_was_called = child .lua ([[
469+ local log_content = ""
470+ local logfile = io.open("./tests/logs/test_client.log", "r")
471+ if logfile then
472+ log_content = logfile:read("*a")
473+ logfile:close()
474+ end
475+ -- If the guard works, we should NOT see "filetype changed" for non-buflisted buffers
476+ return log_content:find("filetype changed, detaching and re%-attaching") ~= nil
477+ ]] )
478+
479+ MiniTest .expect .equality (detach_was_called , false )
480+ end
481+
439482return T
0 commit comments