Repository navigation
Expand file tree
/
Copy pathreference_picker.lua
More file actions
79 lines (70 loc) · 2.03 KB
/
Copy pathreference_picker.lua
File metadata and controls
79 lines (70 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
local config = require('opencode.config')
local base_picker = require('opencode.ui.base_picker')
local icons = require('opencode.ui.icons')
local M = {}
local function make_absolute_path(path)
if not vim.startswith(path, '/') then
return vim.fn.getcwd() .. '/' .. path
end
return path
end
local function format_reference_item(ref, width)
local icon = icons.get('file')
local location = ref.path
if ref.line then
location = location .. ':' .. ref.line
if ref.col then
location = location .. ':' .. ref.col
end
end
return base_picker.create_time_picker_item(icon .. ' ' .. location, nil, nil, width)
end
local function display_refs(refs)
local items = {}
local seen = {}
for _, ref in ipairs(refs or {}) do
local key = make_absolute_path(ref.path) .. ':' .. (ref.line or 0)
if not seen[key] then
seen[key] = true
items[#items + 1] = ref
end
end
return items
end
function M.pick()
local refs = display_refs(require('opencode.ui.reference_facts').current_refs())
if #refs == 0 then
vim.notify('No code references found in the conversation', vim.log.levels.INFO)
return
end
return base_picker.pick({
items = refs,
format_fn = format_reference_item,
actions = {},
callback = function(selected)
if selected then
M.navigate_to(selected)
end
end,
title = 'Code References (' .. #refs .. ')',
width = config.ui.picker_width,
preview = 'file',
layout_opts = config.ui.picker,
})
end
function M.navigate_to(ref)
local file_path = make_absolute_path(ref.path)
if vim.fn.filereadable(file_path) ~= 1 then
vim.notify('File not found: ' .. file_path, vim.log.levels.WARN)
return
end
vim.cmd('tabedit ' .. vim.fn.fnameescape(file_path))
if ref.line then
local line = math.max(1, ref.line)
local col = ref.col and math.max(0, ref.col - 1) or 0
local line_count = vim.api.nvim_buf_line_count(0)
vim.api.nvim_win_set_cursor(0, { math.min(line, line_count), col })
vim.cmd('normal! zz')
end
end
return M