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
19 changes: 8 additions & 11 deletions lua/opencode/ui/base_picker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -515,17 +515,6 @@ local function fzf_ui(opts)
local fzf_config = create_fzf_config()
fzf_config.actions = actions_config

-- When a preview pane is active, fzf-lua splits the window (default
-- right:60% preview, left:40% list). Narrow opts.width so that the
-- format function produces entries sized for the list pane, not the
-- full window. The format closure reads opts.width on each call.
local has_preview = opts.preview and opts.preview ~= 'none' and opts.preview ~= false
if has_preview and opts.width then
local window_cols = opts.width + 8
-- list pane ≈ 40% of the window, minus a small border/padding allowance
opts.width = math.floor(window_cols * 0.4) - 4
end

fzf_lua.fzf_exec(create_finder(), fzf_config)
end

Expand Down Expand Up @@ -890,6 +879,14 @@ function M.pick(opts)
end
end

local has_preview = opts.preview and opts.preview ~= 'none' and opts.preview ~= false
if picker_type == 'fzf' and has_preview and format_width then
local window_cols = format_width + 8
-- Match fzf-lua's default right:60% preview split so item formatting
-- targets the visible list pane instead of the full window width.
format_width = math.floor(window_cols * 0.4) - 4
end

local original_format_fn = opts.format_fn
opts.format_fn = function(item)
return original_format_fn(item, format_width)
Expand Down
33 changes: 32 additions & 1 deletion tests/unit/base_picker_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ end)

describe('opencode.ui.base_picker fzf-lua preview', function()
local base_picker
local captured_fzf_finder
local captured_fzf_opts
local original_schedule
local saved_modules
Expand Down Expand Up @@ -290,10 +291,12 @@ describe('opencode.ui.base_picker fzf-lua preview', function()
end,
}

captured_fzf_finder = nil
captured_fzf_opts = nil

package.loaded['fzf-lua'] = {
fzf_exec = function(_, opts)
fzf_exec = function(finder, opts)
captured_fzf_finder = finder
captured_fzf_opts = opts
end,
}
Expand Down Expand Up @@ -386,4 +389,32 @@ describe('opencode.ui.base_picker fzf-lua preview', function()

vim.api.nvim_buf_delete(next_preview_buf, { force = true })
end)

it('formats entries to the visible list width when preview is active', function()
local observed_width

base_picker.pick({
title = 'Test',
items = { { name = 'session' } },
format_fn = function(item, width)
observed_width = width
return base_picker.create_picker_item({ { text = item.name } })
end,
actions = {},
callback = function() end,
preview = 'custom',
preview_fn = function() end,
})

local emitted_lines = {}
captured_fzf_finder(function(line)
if line then
table.insert(emitted_lines, line)
end
end)

assert.equal(31, observed_width)
assert.are.same({ '1\001session' }, emitted_lines)
assert.equal(88, captured_fzf_opts.winopts.width)
end)
end)
Loading