@@ -14,6 +14,9 @@ local terminal = require 'neocrush.terminal'
1414--- @type neocrush.Config | nil
1515local config
1616
17+ --- @type fun (): boolean | nil Callback to check auto_focus state from init module
18+ local auto_focus_check = nil
19+
1720local ns = vim .api .nvim_create_namespace ' neocrush-highlight'
1821local original_handler = nil
1922local handler_installed = false
@@ -55,7 +58,18 @@ function M.flash_range(bufnr, start_line, end_line)
5558 end , config .highlight_duration )
5659end
5760
61+ --- Check if auto-focus is currently enabled.
62+ --- Uses the callback from init module to get live state, falling back to config.
63+ --- @return boolean
64+ local function is_auto_focus_enabled ()
65+ if auto_focus_check then
66+ return auto_focus_check ()
67+ end
68+ return config and config .auto_focus or true
69+ end
70+
5871--- Ensure a buffer is visible in some window (for highlighting).
72+ --- When auto_focus is disabled, only returns a window if the buffer is already visible.
5973--- @param bufnr integer Buffer handle
6074--- @return integer | nil win Window handle where buffer is displayed , or nil
6175local function ensure_buffer_visible (bufnr )
@@ -71,6 +85,11 @@ local function ensure_buffer_visible(bufnr)
7185 return wins [1 ]
7286 end
7387
88+ -- When auto-focus is off, don't open new windows for edited files
89+ if not is_auto_focus_enabled () then
90+ return nil
91+ end
92+
7493 local target_win = terminal .find_edit_target_window ()
7594 if target_win then
7695 local ok = pcall (vim .api .nvim_win_set_buf , target_win , bufnr )
@@ -147,7 +166,7 @@ function M.apply_edit_handler(err, result, ctx, conf)
147166 local new_lines = vim .split (edit .newText or ' ' , ' \n ' , { plain = true })
148167 local actual_end = start_line + # new_lines
149168
150- if win and vim .api .nvim_win_is_valid (win ) then
169+ if win and vim .api .nvim_win_is_valid (win ) and is_auto_focus_enabled () then
151170 vim .api .nvim_win_set_cursor (win , { start_line + 1 , 0 })
152171 vim .api .nvim_win_call (win , function ()
153172 vim .cmd ' normal! zz'
196215---- ---------------------------------------------------------------------------
197216
198217--- @param cfg neocrush.Config
199- function M .setup (cfg )
218+ --- @param opts ? { auto_focus_check ?: fun (): boolean }
219+ function M .setup (cfg , opts )
200220 config = cfg
221+ if opts and opts .auto_focus_check then
222+ auto_focus_check = opts .auto_focus_check
223+ end
201224 M .install ()
202225end
203226
0 commit comments