Skip to content

Commit ebdb133

Browse files
committed
feat(ui): jump straight to the only match
Opening a reference that resolves to a single location still showed a picker with one entry in it. Jump there directly and keep the picker for the case where several locations match.
1 parent 13f6355 commit ebdb133

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

‎lua/opencode/ui/reference_picker.lua‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ function M.pick()
4343
return
4444
end
4545

46+
if #refs == 1 then
47+
M.navigate_to(refs[1])
48+
return
49+
end
50+
4651
return base_picker.pick({
4752
items = refs,
4853
format_fn = format_reference_item,

‎tests/unit/reference_picker_spec.lua‎

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,33 @@ describe('opencode.ui.reference_picker', function()
226226
vim.notify = original_notify
227227
end)
228228

229+
it('navigates directly when there is only one reference', function()
230+
local pick_calls = {}
231+
mock_base_picker.pick = function()
232+
table.insert(pick_calls, true)
233+
return {}
234+
end
235+
local cmd_calls = {}
236+
vim.cmd = function(cmd)
237+
table.insert(cmd_calls, cmd)
238+
end
239+
240+
rebuild_facts({
241+
{
242+
id = 'msg1',
243+
kind = 'assistant',
244+
session_id = 'ses_1',
245+
content = {
246+
{ kind = 'text', id = 'part1', text = 'Check `src/main.lua:10`.' },
247+
},
248+
},
249+
})
250+
reference_picker.pick()
251+
252+
assert.equal(0, #pick_calls)
253+
assert.equal('tabedit /test/project/src/main.lua', cmd_calls[1])
254+
end)
255+
229256
it('passes correct options to base_picker.pick', function()
230257
local pick_calls = {}
231258
mock_base_picker.pick = function(opts)
@@ -240,16 +267,17 @@ describe('opencode.ui.reference_picker', function()
240267
session_id = 'ses_1',
241268
content = {
242269
{ kind = 'text', id = 'part1', text = 'Check `src/main.lua:10`.' },
270+
{ kind = 'text', id = 'part2', text = 'Also `src/other.lua:3`.' },
243271
},
244272
},
245273
})
246274
reference_picker.pick()
247275

248276
assert.equal(1, #pick_calls)
249-
assert.equal(1, #pick_calls[1].items)
277+
assert.equal(2, #pick_calls[1].items)
250278
assert.is_function(pick_calls[1].format_fn)
251279
assert.is_function(pick_calls[1].callback)
252-
assert.equal('Code References (1)', pick_calls[1].title)
280+
assert.equal('Code References (2)', pick_calls[1].title)
253281
assert.equal(100, pick_calls[1].width)
254282
assert.equal('file', pick_calls[1].preview)
255283
end)
@@ -261,7 +289,7 @@ describe('opencode.ui.reference_picker', function()
261289
kind = 'assistant',
262290
session_id = 'ses_1',
263291
content = {
264-
{ kind = 'text', id = 'part1', text = 'Check `src/main.lua:10` for details.' },
292+
{ kind = 'text', id = 'part1', text = 'Check `src/main.lua:10` for details. And `src/util.lua:2` too.' },
265293
},
266294
},
267295
})
@@ -275,7 +303,7 @@ describe('opencode.ui.reference_picker', function()
275303
local items = captured and captured.items
276304

277305
assert.is_not_nil(items)
278-
assert.equal(1, #items)
306+
assert.equal(2, #items)
279307
assert.equal('src/main.lua', items[1].path)
280308
assert.equal(10, items[1].line)
281309
end)
@@ -289,6 +317,7 @@ describe('opencode.ui.reference_picker', function()
289317
content = {
290318
{ kind = 'text', id = 'part1', text = 'First `src/main.lua:10`.' },
291319
{ kind = 'text', id = 'part2', text = 'Second `src/main.lua:10`.' },
320+
{ kind = 'text', id = 'part3', text = 'Third `src/other.lua:1`.' },
292321
},
293322
},
294323
})
@@ -300,9 +329,9 @@ describe('opencode.ui.reference_picker', function()
300329

301330
reference_picker.pick()
302331

303-
assert.equal(1, #captured.items)
332+
assert.equal(2, #captured.items)
304333
assert.equal('part1', captured.items[1].part_id)
305-
assert.equal('Code References (1)', captured.title)
334+
assert.equal('Code References (2)', captured.title)
306335
end)
307336
end)
308337
end)

0 commit comments

Comments
 (0)