Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions lua/neocrush/highlight.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ local terminal = require 'neocrush.terminal'
---@type neocrush.Config|nil
local config

---@type fun(): boolean|nil Callback to check auto_focus state from init module
local auto_focus_check = nil

local ns = vim.api.nvim_create_namespace 'neocrush-highlight'
local original_handler = nil
local handler_installed = false
Expand Down Expand Up @@ -55,7 +58,18 @@ function M.flash_range(bufnr, start_line, end_line)
end, config.highlight_duration)
end

--- Check if auto-focus is currently enabled.
--- Uses the callback from init module to get live state, falling back to config.
---@return boolean
local function is_auto_focus_enabled()
if auto_focus_check then
return auto_focus_check()
end
return config and config.auto_focus or true
end

--- Ensure a buffer is visible in some window (for highlighting).
--- When auto_focus is disabled, only returns a window if the buffer is already visible.
---@param bufnr integer Buffer handle
---@return integer|nil win Window handle where buffer is displayed, or nil
local function ensure_buffer_visible(bufnr)
Expand All @@ -71,6 +85,11 @@ local function ensure_buffer_visible(bufnr)
return wins[1]
end

-- When auto-focus is off, don't open new windows for edited files
if not is_auto_focus_enabled() then
return nil
end

local target_win = terminal.find_edit_target_window()
if target_win then
local ok = pcall(vim.api.nvim_win_set_buf, target_win, bufnr)
Expand Down Expand Up @@ -147,7 +166,7 @@ function M.apply_edit_handler(err, result, ctx, conf)
local new_lines = vim.split(edit.newText or '', '\n', { plain = true })
local actual_end = start_line + #new_lines

if win and vim.api.nvim_win_is_valid(win) then
if win and vim.api.nvim_win_is_valid(win) and is_auto_focus_enabled() then
vim.api.nvim_win_set_cursor(win, { start_line + 1, 0 })
vim.api.nvim_win_call(win, function()
vim.cmd 'normal! zz'
Expand Down Expand Up @@ -196,8 +215,12 @@ end
-------------------------------------------------------------------------------

---@param cfg neocrush.Config
function M.setup(cfg)
---@param opts? { auto_focus_check?: fun(): boolean }
function M.setup(cfg, opts)
config = cfg
if opts and opts.auto_focus_check then
auto_focus_check = opts.auto_focus_check
end
M.install()
end

Expand Down
6 changes: 5 additions & 1 deletion lua/neocrush/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ function M.setup(opts)
config = vim.tbl_deep_extend('force', default_config, opts or {})

require('neocrush.terminal').setup(config)
require('neocrush.lsp').setup(config)
require('neocrush.lsp').setup(config, {
auto_focus_check = function()
return config.auto_focus
end,
})
require('neocrush.commands').create(M)
require('neocrush.cvm').setup(config.cvm)

Expand Down
5 changes: 3 additions & 2 deletions lua/neocrush/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ end

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

vim.api.nvim_create_autocmd('LspAttach', {
group = vim.api.nvim_create_augroup('NeocrushLspAttach', { clear = true }),
Expand Down
31 changes: 31 additions & 0 deletions tests/highlight_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,35 @@ describe('neocrush.highlight', function()
assert.is_true(highlight.is_installed())
end)
end)

describe('auto_focus_check callback', function()
it('should accept auto_focus_check option in setup', function()
local focus_enabled = true
assert.has_no.errors(function()
highlight.setup({
highlight_group = 'IncSearch',
highlight_duration = 100,
auto_focus = true,
terminal_width = 80,
terminal_cmd = 'crush',
}, {
auto_focus_check = function()
return focus_enabled
end,
})
end)
end)

it('should work without auto_focus_check option', function()
assert.has_no.errors(function()
highlight.setup {
highlight_group = 'IncSearch',
highlight_duration = 100,
auto_focus = true,
terminal_width = 80,
terminal_cmd = 'crush',
}
end)
end)
end)
end)
11 changes: 11 additions & 0 deletions tests/neocrush_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,16 @@ describe('neocrush', function()
neocrush.disable_auto_focus()
assert.is_false(neocrush.is_auto_focus_enabled())
end)

it('should propagate auto_focus state to highlight module', function()
-- The highlight module should see toggled state via callback
neocrush.setup { auto_focus = true }
assert.is_true(neocrush.is_auto_focus_enabled())
neocrush.toggle_auto_focus()
assert.is_false(neocrush.is_auto_focus_enabled())
-- Toggle back
neocrush.toggle_auto_focus()
assert.is_true(neocrush.is_auto_focus_enabled())
end)
end)
end)
Loading