Repository navigation
Expand file tree
/
Copy pathmention.lua
More file actions
69 lines (54 loc) · 1.95 KB
/
Copy pathmention.lua
File metadata and controls
69 lines (54 loc) · 1.95 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
local M = {}
local mentions_namespace = vim.api.nvim_create_namespace('OpencodeMentions')
function M.highlight_all_mentions(buf)
-- Pattern for mentions
local mention_pattern = '@[%w_%-%./][%w_%-%./]*'
-- Clear existing extmarks
pcall(vim.api.nvim_buf_clear_namespace, buf, mentions_namespace, 0, -1)
-- Get all lines in buffer
local ok_lines, lines = pcall(vim.api.nvim_buf_get_lines, buf, 0, -1, false)
if not ok_lines then
return
end
for row, line in ipairs(lines) do
local start_idx = 1
-- Find all mentions in the line
while true do
local mention_start, mention_end = line:find(mention_pattern, start_idx)
if not mention_start then
break
end
-- Add extmark for this mention
vim.api.nvim_buf_set_extmark(buf, mentions_namespace, row - 1, mention_start - 1, {
end_col = mention_end,
hl_group = 'OpencodeMention',
})
-- Move to search for the next mention
start_idx = mention_end + 1
end
end
end
local function insert_mention(windows, row, col, name)
local current_line = vim.api.nvim_buf_get_lines(windows.input_buf, row - 1, row, false)[1]
local insert_name = '@' .. name .. ' '
local new_line = current_line:sub(1, col) .. insert_name .. current_line:sub(col + 2)
vim.api.nvim_buf_set_lines(windows.input_buf, row - 1, row, false, { new_line })
M.highlight_all_mentions(windows.input_buf)
vim.cmd('startinsert')
vim.api.nvim_set_current_win(windows.input_win)
vim.api.nvim_win_set_cursor(windows.input_win, { row, col + 1 + #insert_name + 1 })
end
function M.mention(get_name)
local windows = require('opencode.state').windows
get_name(function(name)
vim.schedule(function()
if not windows or not name then
return
end
local cursor_pos = vim.api.nvim_win_get_cursor(windows.input_win)
local row, col = cursor_pos[1], cursor_pos[2]
insert_mention(windows, row, col, name)
end)
end)
end
return M