Skip to content

Commit a10abdc

Browse files
committed
fix(highlight): respect auto_focus toggle in workspace edit handler
The workspace/applyEdit handler always opened and scrolled to edited files regardless of the auto_focus setting. When the user toggled auto_focus off via :CrushFocusOff or the API, the handler ignored this because: 1. highlight.lua had its own config copy that was never updated when init.lua toggled auto_focus 2. ensure_buffer_visible always opened new windows for edited buffers Fix: pass a callback from init.lua to highlight.lua that reads the live config.auto_focus state. When auto_focus is off, edits are still applied and visible buffers still get flash highlights, but the handler no longer opens new windows or scrolls to the edit location.
1 parent c2dcac2 commit a10abdc

5 files changed

Lines changed: 75 additions & 5 deletions

File tree

lua/neocrush/highlight.lua

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ local terminal = require 'neocrush.terminal'
1414
---@type neocrush.Config|nil
1515
local config
1616

17+
---@type fun(): boolean|nil Callback to check auto_focus state from init module
18+
local auto_focus_check = nil
19+
1720
local ns = vim.api.nvim_create_namespace 'neocrush-highlight'
1821
local original_handler = nil
1922
local handler_installed = false
@@ -55,7 +58,18 @@ function M.flash_range(bufnr, start_line, end_line)
5558
end, config.highlight_duration)
5659
end
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
6175
local 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'
@@ -196,8 +215,12 @@ end
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()
202225
end
203226

lua/neocrush/init.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,11 @@ function M.setup(opts)
187187
config = vim.tbl_deep_extend('force', default_config, opts or {})
188188

189189
require('neocrush.terminal').setup(config)
190-
require('neocrush.lsp').setup(config)
190+
require('neocrush.lsp').setup(config, {
191+
auto_focus_check = function()
192+
return config.auto_focus
193+
end,
194+
})
191195
require('neocrush.commands').create(M)
192196
require('neocrush.cvm').setup(config.cvm)
193197

lua/neocrush/lsp.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,9 @@ end
162162

163163
--- Install handler and set up autocmds.
164164
---@param cfg neocrush.Config
165-
function M.setup(cfg)
166-
highlight.setup(cfg)
165+
---@param opts? { auto_focus_check?: fun(): boolean }
166+
function M.setup(cfg, opts)
167+
highlight.setup(cfg, opts)
167168

168169
vim.api.nvim_create_autocmd('LspAttach', {
169170
group = vim.api.nvim_create_augroup('NeocrushLspAttach', { clear = true }),

tests/highlight_spec.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,35 @@ describe('neocrush.highlight', function()
7070
assert.is_true(highlight.is_installed())
7171
end)
7272
end)
73+
74+
describe('auto_focus_check callback', function()
75+
it('should accept auto_focus_check option in setup', function()
76+
local focus_enabled = true
77+
assert.has_no.errors(function()
78+
highlight.setup({
79+
highlight_group = 'IncSearch',
80+
highlight_duration = 100,
81+
auto_focus = true,
82+
terminal_width = 80,
83+
terminal_cmd = 'crush',
84+
}, {
85+
auto_focus_check = function()
86+
return focus_enabled
87+
end,
88+
})
89+
end)
90+
end)
91+
92+
it('should work without auto_focus_check option', function()
93+
assert.has_no.errors(function()
94+
highlight.setup {
95+
highlight_group = 'IncSearch',
96+
highlight_duration = 100,
97+
auto_focus = true,
98+
terminal_width = 80,
99+
terminal_cmd = 'crush',
100+
}
101+
end)
102+
end)
103+
end)
73104
end)

tests/neocrush_spec.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,16 @@ describe('neocrush', function()
5858
neocrush.disable_auto_focus()
5959
assert.is_false(neocrush.is_auto_focus_enabled())
6060
end)
61+
62+
it('should propagate auto_focus state to highlight module', function()
63+
-- The highlight module should see toggled state via callback
64+
neocrush.setup { auto_focus = true }
65+
assert.is_true(neocrush.is_auto_focus_enabled())
66+
neocrush.toggle_auto_focus()
67+
assert.is_false(neocrush.is_auto_focus_enabled())
68+
-- Toggle back
69+
neocrush.toggle_auto_focus()
70+
assert.is_true(neocrush.is_auto_focus_enabled())
71+
end)
6172
end)
6273
end)

0 commit comments

Comments
 (0)