Skip to content

Commit 516e91a

Browse files
committed
feat(ui): enhance file URI parsing to support line ranges
- add support for line range format: file://path:start-end - modify regex pattern to accommodate optional end line numbers - update reference structure to include optional end_pos for highlighting
1 parent 9885d95 commit 516e91a

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

lua/opencode/ui/reference_picker.lua

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ end
2727
---@field match_end number End position in original text
2828
---@field file string Absolute file path (for Snacks picker preview)
2929
---@field pos number[]|nil Position as {line, col} for Snacks picker preview
30+
---@field end_pos number[]|nil End position as {line, col} for Snacks picker range highlighting
3031

3132
---Parse file:// URI references from text
3233
---@param text string The text to parse
@@ -35,21 +36,31 @@ end
3536
function M.parse_references(text, message_id)
3637
local references = {}
3738

38-
-- Match file:// URIs with optional line and column numbers
39-
-- Formats: file://path/to/file or file://path/to/file:line or file://path/to/file:line:column
40-
local pattern = 'file://([%w_./%-]+):?(%d*):?(%d*)'
39+
-- Match file:// URIs with optional line and column numbers or line ranges
40+
-- Formats: file://path/to/file or file://path/to/file:line or file://path/to/file:line:column or file://path/to/file:line-endline
41+
local pattern = 'file://([%w_./%-]+):?(%d*):?(%d*)-?(%d*)'
4142
local search_start = 1
4243

4344
while search_start <= #text do
44-
local match_start, match_end, path, line_str, col_str = text:find(pattern, search_start)
45+
local match_start, match_end, path, line_str, col_or_end_str, end_line_str = text:find(pattern, search_start)
4546
if not match_start then
4647
break
4748
end
4849

4950
-- Only add if file exists
5051
if file_exists(path) then
5152
local line = line_str ~= '' and tonumber(line_str) or nil
52-
local column = col_str ~= '' and tonumber(col_str) or nil
53+
local column = nil
54+
local end_line = nil
55+
56+
-- Determine if we have a range or a column
57+
if end_line_str ~= '' then
58+
-- Range format: file://path:start-end
59+
end_line = tonumber(end_line_str)
60+
elseif col_or_end_str ~= '' then
61+
-- Column format: file://path:line:col
62+
column = tonumber(col_or_end_str)
63+
end
5364

5465
-- Create absolute path for Snacks preview
5566
local abs_path = path
@@ -66,6 +77,7 @@ function M.parse_references(text, message_id)
6677
match_end = match_end,
6778
file = abs_path,
6879
pos = line and { line, (column or 1) - 1 } or nil,
80+
end_pos = end_line and { end_line, 0 } or nil,
6981
})
7082
end
7183

0 commit comments

Comments
 (0)