From 372afcdd4feb5fd9a5361814831091755b3b1989 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Sun, 12 Apr 2026 15:11:24 -0600 Subject: [PATCH] fix: focus source window before dap jumps on event_stopped MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When stepping into a library file (e.g., Rust stdlib fmt.rs) or any file not already open, dap uses its switchbuf logic to decide which window to load the new source into. The 'uselast' variant targets the current window if it looks like source (empty buftype or matching filetype); otherwise it falls back to `winnr('#')`. Problem: if the user pressed `s` / `d` / `r` while focus was on a dapui pane (which is common — clicking into Scopes or Watches to inspect, then stepping), or if the event fires while focus is on our bar window, dap's current-window check fails and it dumps the new source into whatever `winnr('#')` resolves to. Combined with bufferline.nvim's tabline, this produces the chrome-breaking layout shown in the screenshot. Fix: register `dap.listeners.before.event_stopped` that shifts focus to a known source window (via the existing source_window() helper) BEFORE dap's jump handler runs. Dap's uselast then sees a real source window as current and lands the new file there. Chrome stays intact. Co-Authored-By: Claude Opus 4.6 (1M context) --- lua/turbo-debug/mode.lua | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lua/turbo-debug/mode.lua b/lua/turbo-debug/mode.lua index 73dfb58..f7e2793 100644 --- a/lua/turbo-debug/mode.lua +++ b/lua/turbo-debug/mode.lua @@ -714,6 +714,21 @@ local function ensure_dapui() dap.listeners.after.event_terminated["turbo-debug-bar"] = refresh_bars dap.listeners.after.event_exited["turbo-debug-bar"] = refresh_bars + -- When a stopped event fires, dap will jump to the source frame using + -- its switchbuf logic. `uselast` (the common default) targets the + -- CURRENT window — but if focus happens to be on a dapui pane or one + -- of our bars, dap falls back to `winnr('#')` which can land anywhere + -- (including bufferline's tabline area or a wrong split). By shifting + -- focus to a real source window BEFORE dap processes the jump, we + -- guarantee new source files (stepped-into library/stdlib files) open + -- in the intended source window and the chrome stays intact. + dap.listeners.before.event_stopped["turbo-debug-focus-source"] = function() + local w = source_window() + if w and w ~= vim.api.nvim_get_current_win() then + pcall(vim.api.nvim_set_current_win, w) + end + end + dap.listeners.after.event_stopped["turbo-debug-ip"] = function(session, body) vim.defer_fn(function() clear_ip()