Skip to content

Commit 1b3b7c1

Browse files
committed
feat(ui/reference_picker): deduplicate refs from tool parts
1 parent 306c33c commit 1b3b7c1

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

lua/opencode/ui/reference_picker.lua

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ local function make_ref(path, line_str, col_str, abs_start, abs_end)
7070
}
7171
end
7272

73+
local function picker_ref_key(path, line)
74+
return make_absolute_path(path) .. ':' .. (line or 0)
75+
end
76+
7377
---@param text string
7478
---@param message_id string
7579
---@return CodeReference[]
@@ -164,7 +168,7 @@ local function collect_picker_refs()
164168
local c = cache[message_id]
165169
if c then
166170
for _, ref in ipairs(c.refs) do
167-
local key = ref.file_path .. ':' .. (ref.line or 0)
171+
local key = picker_ref_key(ref.file_path, ref.line)
168172
if not seen[key] then
169173
seen[key] = true
170174
table.insert(refs, ref)
@@ -177,10 +181,10 @@ local function collect_picker_refs()
177181
if part.type == 'tool' then
178182
local file_path = vim.tbl_get(part, 'state', 'input', 'filePath')
179183
if file_path and vim.fn.filereadable(file_path) == 1 then
180-
local key = file_path .. ':0'
184+
local rel = vim.fn.fnamemodify(file_path, ':~:.')
185+
local key = picker_ref_key(rel, nil)
181186
if not seen[key] then
182187
seen[key] = true
183-
local rel = vim.fn.fnamemodify(file_path, ':~:.')
184188
table.insert(refs, make_ref(rel, '', '', 0, 0))
185189
end
186190
end

tests/unit/reference_picker_spec.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,25 @@ describe('opencode.ui.reference_picker', function()
556556
assert.equal('src/file.lua', items[1].file_path)
557557
end)
558558

559+
it('deduplicates matching text refs and tool-part refs', function()
560+
local items = pick_items({
561+
{
562+
id = 'msg1',
563+
text = 'Check `src/file.lua` for details.',
564+
parts = {
565+
{
566+
type = 'tool',
567+
state = { input = { filePath = '/test/project/src/file.lua' } },
568+
},
569+
},
570+
},
571+
})
572+
573+
assert.is_not_nil(items)
574+
assert.equal(1, #items)
575+
assert.equal('src/file.lua', items[1].file_path)
576+
end)
577+
559578
it('ignores non-existent files in tool parts', function()
560579
local notify_calls = {}
561580
local original_notify = vim.notify

0 commit comments

Comments
 (0)