Skip to content

Commit 708cfbf

Browse files
committed
fix(picker): use index-based item matching in fzf-lua
1 parent 4e4be32 commit 708cfbf

1 file changed

Lines changed: 12 additions & 11 deletions

File tree

lua/opencode/ui/base_picker.lua

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -234,19 +234,17 @@ local function fzf_ui(opts)
234234
fzf_opts = {
235235
['--prompt'] = opts.title .. ' > ',
236236
['--multi'] = has_multi_action and true or nil,
237+
['--with-nth'] = '2..', -- hide the index prefix from display
238+
['--delimiter'] = '\x01', -- use SOH as delimiter (invisible char)
237239
},
238240
_headers = { 'actions' },
239241
-- Enable builtin previewer for file preview support
240242
previewer = opts.preview == 'file' and 'builtin' or nil,
241243
fn_fzf_index = function(line)
242-
-- Strip the appended file:line:col info before matching
243-
-- fzf-lua uses nbsp (U+2002 EN SPACE) as separator
244-
local nbsp = '\xe2\x80\x82'
245-
local display_part = line:match('^([^' .. nbsp .. ']+)') or line
246-
for i, item in ipairs(opts.items) do
247-
if opts.format_fn(item):to_string() == display_part then
248-
return i
249-
end
244+
-- Extract the numeric index prefix before the SOH delimiter
245+
local idx_str = line:match('^(%d+)\x01')
246+
if idx_str then
247+
return tonumber(idx_str)
250248
end
251249
return nil
252250
end,
@@ -255,9 +253,12 @@ local function fzf_ui(opts)
255253

256254
local function create_finder()
257255
return function(fzf_cb)
258-
for _, item in ipairs(opts.items) do
256+
for idx, item in ipairs(opts.items) do
259257
local line_str = opts.format_fn(item):to_string()
260258

259+
-- Prepend index with SOH delimiter for reliable matching
260+
local indexed_line = tostring(idx) .. '\x01' .. line_str
261+
261262
-- For file preview support, append file:line:col format
262263
-- fzf-lua's builtin previewer automatically parses this format
263264
if opts.preview == 'file' and type(item) == 'table' then
@@ -278,11 +279,11 @@ local function fzf_ui(opts)
278279
-- Append position info after nbsp separator (fzf-lua standard)
279280
-- nbsp is U+2002 EN SPACE, not regular tab
280281
local nbsp = '\xe2\x80\x82'
281-
line_str = line_str .. nbsp .. pos_info
282+
indexed_line = indexed_line .. nbsp .. pos_info
282283
end
283284
end
284285

285-
fzf_cb(line_str)
286+
fzf_cb(indexed_line)
286287
end
287288
fzf_cb()
288289
end

0 commit comments

Comments
 (0)