forked from sudo-tee/opencode.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreference_picker.lua
More file actions
317 lines (268 loc) · 9.29 KB
/
reference_picker.lua
File metadata and controls
317 lines (268 loc) · 9.29 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
-- Code reference picker for navigating to file references in LLM responses
local state = require('opencode.state')
local config = require('opencode.config')
local base_picker = require('opencode.ui.base_picker')
local icons = require('opencode.ui.icons')
local M = {}
---Check if a file reference is valid
---@param path string File path
---@param context string Surrounding text
---@return boolean
local function is_valid_file_reference(path, context)
-- Reject URLs (but allow file paths that merely contain these substrings)
local lower = context:lower()
-- Match http/https URLs at word boundaries or www.-style URLs
if lower:match('%f[%w]https?://%S+') or lower:match('%f[%w]www%.[%w%-_]+') then
return false
end
return (path:match('%.[%w]+$') and vim.fn.filereadable(path) == 1) or false
end
---@class CodeReference
---@field file_path string Relative or absolute file path
---@field line number|nil Line number (1-indexed)
---@field column number|nil Column number (optional)
---@field message_id string ID of the message containing this reference
---@field match_start number Start position in original text
---@field match_end number End position in original text
---@field file string Absolute file path (for Snacks picker preview)
---@field pos number[]|nil Position as {line, col} for Snacks picker preview
---@field end_pos number[]|nil End position as {line, col} for Snacks picker range highlighting
---Create absolute path from relative path
---@param path string
---@return string
local function make_absolute_path(path)
if not vim.startswith(path, '/') then
return vim.fn.getcwd() .. '/' .. path
end
return path
end
---Create a CodeReference object from parsed components
---@param path string
---@param line number|nil
---@param column number|nil
---@param end_line number|nil
---@param message_id string
---@param match_start number
---@param match_end number
---@return CodeReference
local function create_code_reference(path, line, column, end_line, message_id, match_start, match_end)
local abs_path = make_absolute_path(path)
return {
file_path = path,
line = line,
column = column,
message_id = message_id,
match_start = match_start,
match_end = match_end,
file = abs_path,
pos = line and { line, (column or 1) - 1 } or nil,
end_pos = end_line and { end_line, 0 } or nil,
}
end
---Parse line, column, and range information from pattern captures
---@param line_str string
---@param col_or_end_str string
---@param end_line_str string
---@return number|nil line, number|nil column, number|nil end_line
local function parse_position_info(line_str, col_or_end_str, end_line_str)
local line = line_str ~= '' and tonumber(line_str) or nil
local column = nil
local end_line = nil
if end_line_str ~= '' then
end_line = tonumber(end_line_str)
elseif col_or_end_str ~= '' then
column = tonumber(col_or_end_str)
end
return line, column, end_line
end
---Parse file references using a pattern
---@param text string The text to parse
---@param pattern string Lua pattern to match
---@param message_id string The message ID for tracking
---@return CodeReference[]
local function parse_references_with_pattern(text, pattern, message_id)
local references = {}
local search_start = 1
while search_start <= #text do
local match_start, match_end, path, line_str, col_or_end_str, end_line_str = text:find(pattern, search_start)
if not match_start then
break
end
local context_start = math.max(1, match_start - 30)
local context = text:sub(context_start, match_end + 10)
if path and is_valid_file_reference(path, context) then
local line, column, end_line = parse_position_info(line_str, col_or_end_str, end_line_str)
local ref = create_code_reference(path, line, column, end_line, message_id, match_start, match_end)
table.insert(references, ref)
end
search_start = match_end + 1
end
return references
end
---Parse file references from text using multiple pattern strategies
---@param text string The text to parse
---@param message_id string The message ID for tracking
---@return CodeReference[]
function M.parse_references(text, message_id)
local all_refs = {}
local patterns = {
'`([^`\n]+%.%w+):?(%d*):?(%d*)-?(%d*)`', -- Backticks: `file.ext:line`
'file://([%S]+%.%w+):?(%d*):?(%d*)-?(%d*)', -- file:// URIs
'([%w_./%-]+/[%w_./%-]*%.%w+):?(%d*):?(%d*)-?(%d*)', -- Paths with /
'([%w_%-]+%.%w+):?(%d*):?(%d*)-?(%d*)', -- Top-level files
}
for _, pattern in ipairs(patterns) do
local refs = parse_references_with_pattern(text, pattern, message_id)
vim.list_extend(all_refs, refs)
end
-- Sort by position and deduplicate
table.sort(all_refs, function(a, b)
return a.match_start < b.match_start
end)
local deduplicated = {}
for _, ref in ipairs(all_refs) do
local last = deduplicated[#deduplicated]
if not last or ref.match_start > last.match_end then
table.insert(deduplicated, ref)
end
end
return deduplicated
end
---Collect all references from assistant messages in the current session
---Returns references in reverse order (most recent first)
---@return CodeReference[]
function M.collect_references()
local all_references = {}
if not state.messages then
return all_references
end
for i = #state.messages, 1, -1 do
local msg = state.messages[i]
if msg.info and msg.info.role == 'assistant' then
local refs = msg.references or M._parse_message_references(msg)
for _, ref in ipairs(refs) do
table.insert(all_references, ref)
end
end
end
-- Keep first occurrence which is most recent due to reverse iteration
local seen = {}
local deduplicated = {}
for _, ref in ipairs(all_references) do
local normalized_path = vim.fn.fnamemodify(ref.file_path, ':p')
local dedup_key = normalized_path .. ':' .. (ref.line or 0)
if not seen[dedup_key] then
seen[dedup_key] = true
table.insert(deduplicated, ref)
end
end
return deduplicated
end
---Parse references from a single message's text parts
---@param msg OpencodeMessage
---@return CodeReference[]
function M._parse_message_references(msg)
local refs = {}
if not msg.parts then
return refs
end
local message_id = msg.info and msg.info.id or ''
for _, part in ipairs(msg.parts) do
if part.type == 'text' and part.text then
local part_refs = M.parse_references(part.text, message_id)
for _, ref in ipairs(part_refs) do
table.insert(refs, ref)
end
end
if part.type == 'tool' then
local file_path = vim.tbl_get(part, 'state', 'input', 'filePath')
if file_path and vim.fn.filereadable(file_path) == 1 then
local relative_path = vim.fn.fnamemodify(file_path, ':~:.')
local ref = create_code_reference(relative_path, nil, nil, nil, message_id, 0, 0)
table.insert(refs, ref)
end
end
end
return refs
end
---Parse and cache references for all assistant messages in the current session
function M._parse_session_messages()
if not state.messages then
return
end
for _, msg in ipairs(state.messages) do
if msg.info and msg.info.role == 'assistant' and not msg.references then
msg.references = M._parse_message_references(msg)
end
end
end
---Setup reference picker event subscriptions
---Should be called once during plugin initialization
function M.setup()
if state.event_manager then
state.event_manager:subscribe('session.idle', function()
M._parse_session_messages()
end)
end
state.subscribe('messages', function()
M._parse_session_messages()
end)
end
---Format a reference for display in the picker
---@param ref CodeReference
---@param width number|nil
---@return PickerItem
local function format_reference_item(ref, width)
local icon = icons.get('file')
local location = ref.file_path
if ref.line then
location = location .. ':' .. ref.line
if ref.end_pos and ref.end_pos[1] then
location = location .. '-' .. ref.end_pos[1]
elseif ref.column then
location = location .. ':' .. ref.column
end
end
local display_text = icon .. ' ' .. location
return base_picker.create_time_picker_item(display_text, nil, nil, width)
end
---Open the reference picker
function M.pick()
local references = M.collect_references()
if #references == 0 then
vim.notify('No code references found in the conversation', vim.log.levels.INFO)
return
end
local callback = function(selected)
if selected then
M.navigate_to(selected)
end
end
return base_picker.pick({
items = references,
format_fn = format_reference_item,
actions = {},
callback = callback,
title = 'Code References (' .. #references .. ')',
width = config.ui.picker_width or 100,
preview = 'file',
layout_opts = config.ui.picker,
})
end
---Navigate to a code reference
---@param ref CodeReference
function M.navigate_to(ref)
local file_path = make_absolute_path(ref.file_path)
vim.cmd('tabedit ' .. vim.fn.fnameescape(file_path))
if ref.line then
local line = math.max(1, ref.line)
local col = ref.column and math.max(0, ref.column - 1) or 0
local line_count = vim.api.nvim_buf_line_count(0)
line = math.min(line, line_count)
vim.api.nvim_win_set_cursor(0, { line, col })
if config.ui.reference_picker_center_on_jump then
vim.cmd('normal! zz')
end
end
end
return M