Repository navigation
Expand file tree
/
Copy pathskill_picker.lua
More file actions
90 lines (77 loc) · 2.28 KB
/
Copy pathskill_picker.lua
File metadata and controls
90 lines (77 loc) · 2.28 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
local base_picker = require('opencode.ui.base_picker')
local Promise = require('opencode.promise')
local server_job = require('opencode.server_job')
local M = {}
---@param skill OpencodeSkill
---@param width number
---@return PickerItem
local function format_skill_item(skill, width)
width = width or vim.api.nvim_win_get_width(0)
local desc_width = math.max(20, width - 30)
local desc = skill.description or ''
if #desc > desc_width then
desc = desc:sub(1, desc_width - 3) .. '...'
end
return base_picker.create_picker_item({
{ text = base_picker.align(skill.name, 20, { truncate = true }), highlight = 'OpencodeFile' },
{ text = desc, highlight = 'OpencodeHint' },
})
end
---@param skill OpencodeSkill
---@param target PickerPreviewTarget
local function preview_skill(skill, target)
if not skill or not skill.content then
return
end
local lines = vim.split(skill.content, '\n')
target:set_lines(lines)
local bufnr = target:get_bufnr()
if bufnr then
vim.bo[bufnr].filetype = 'markdown'
end
end
---Show skills picker
M.pick = Promise.async(function()
local state = require('opencode.state')
local ui = require('opencode.ui.ui')
local input_window = require('opencode.ui.input_window')
local ok, skills = pcall(function()
local connection = server_job.ensure_server():await()
local util = require('opencode.util')
return connection.operations
.list_skills(
connection,
{ directory = state.current_cwd or vim.fn.getcwd() },
util.apply_path_map,
util.apply_reverse_path_map
)
:await()
end)
if not ok or not skills then
vim.notify('Failed to fetch skills: ' .. tostring(skills), vim.log.levels.ERROR)
return
end
if #skills == 0 then
vim.notify('No skills available', vim.log.levels.WARN)
return
end
local callback = function(selected)
if not selected then
return
end
ui.focus_input()
input_window.set_content('/' .. selected.name .. ' ')
vim.api.nvim_win_set_cursor(state.windows.input_win, { 1, #selected.name + 2 })
end
base_picker.pick({
items = skills,
format_fn = format_skill_item,
actions = {},
callback = callback,
title = 'Skills',
width = 65,
preview = 'custom',
preview_fn = preview_skill,
})
end)
return M