Skip to content

Commit 75a4559

Browse files
committed
feat(mention): highlight mention in user prompt
1 parent d3fbdcd commit 75a4559

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

lua/opencode/types.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@
238238
---@field display_line number Line number to display the action
239239
---@field range? { from: number, to: number } Optional range for the action
240240

241-
---@alias OutputExtmark vim.api.keyset.set_extmark|fun():vim.api.keyset.set_extmark
241+
---@alias OutputExtmarkType vim.api.keyset.set_extmark & {start_col:0}
242+
---@alias OutputExtmark OutputExtmarkType|fun():OutputExtmarkType
242243

243244
---@class OpencodeMessage
244245
---@field info MessageInfo Metadata about the message

lua/opencode/ui/formatter.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ function M._format_user_prompt(output, text)
320320
output:add_lines(vim.split(text, '\n'))
321321

322322
local end_line = output:get_line_count()
323-
323+
require('opencode.ui.mention').highlight_mention(output)
324324
M._add_vertical_border(output, start_line, end_line, 'OpencodeMessageRoleUser', -3)
325325
end
326326

lua/opencode/ui/mention.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@ function M.highlight_all_mentions(buf, callback)
4040
end
4141
end
4242

43+
---@param output Output
44+
function M.highlight_mention(output)
45+
local mention_pattern = '@[%w_%-%./][%w_%-%./]*'
46+
for i, line in pairs(output:get_lines()) do
47+
for mention in line:gmatch(mention_pattern) do
48+
local col_start, col_end = line:find(mention, 1)
49+
if col_start and col_end then
50+
output:add_extmark(i, {
51+
start_col = col_start - 1,
52+
end_col = col_end,
53+
hl_group = 'OpencodeMention',
54+
priority = 1000,
55+
})
56+
end
57+
end
58+
end
59+
end
60+
4361
local function insert_mention(windows, row, col, name)
4462
local current_line = vim.api.nvim_buf_get_lines(windows.input_buf, row - 1, row, false)[1]
4563

lua/opencode/ui/output_window.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,11 @@ function M.set_extmarks(extmarks, line_offset)
123123
if actual_mark.end_row then
124124
actual_mark.end_row = actual_mark.end_row + line_offset
125125
end
126-
pcall(vim.api.nvim_buf_set_extmark, output_buf, M.namespace, target_line, 0, actual_mark)
126+
local start_col = actual_mark.start_col
127+
if actual_mark.start_col then
128+
actual_mark.start_col = nil
129+
end
130+
pcall(vim.api.nvim_buf_set_extmark, output_buf, M.namespace, target_line, start_col or 0, actual_mark)
127131
end
128132
end
129133
end

0 commit comments

Comments
 (0)