@@ -5,13 +5,15 @@ local icons = require('opencode.ui.icons')
55
66local M = {}
77
8+ --- @generic T
89--- @param name string
910--- @param type string
1011--- @param available boolean
1112--- @param documentation string | nil
1213--- @param icon string | nil
14+ --- @param additional_data ? T
1315--- @return CompletionItem
14- local function create_context_item (name , type , available , documentation , icon )
16+ local function create_context_item (name , type , available , documentation , icon , additional_data )
1517 local label = name
1618
1719 return {
@@ -22,7 +24,7 @@ local function create_context_item(name, type, available, documentation, icon)
2224 documentation = documentation or (available and name or ' Enable ' .. name .. ' for this message' ),
2325 insert_text = ' ' ,
2426 source_name = ' context' ,
25- data = { type = type , name = name , available = available },
27+ data = { type = type , name = name , available = available , additional_data = additional_data },
2628 }
2729end
2830
@@ -52,15 +54,9 @@ local function format_diagnostics(diagnostics)
5254 return table.concat (parts , ' , ' )
5355end
5456
55- local function format_selections (selections )
56- local content = {}
57- for _ , sel in ipairs (selections or {}) do
58- local lang = sel .file and sel .file .extension or ' '
59- local text = string.format (' ```%s\n %s\n ```' , lang , sel .content )
60-
61- table.insert (content , text )
62- end
63- return table.concat (content , ' \n ' )
57+ local function format_selection (selection )
58+ local lang = selection .file and selection .file .extension or ' '
59+ return string.format (' ```%s\n %s\n ```' , lang , selection .content )
6460end
6561
6662--- @param cursor_data ? OpencodeContextCursorData
@@ -112,14 +108,24 @@ local function add_mentioned_files_items(ctx)
112108end
113109
114110--- @param ctx OpencodeContext
115- --- @return CompletionItem
116- local function add_selection_item (ctx )
117- return create_context_item (
118- ' Selection' ,
119- ' selection' ,
120- context .is_context_enabled (' selection' ),
121- format_selections (ctx .selections or {})
122- )
111+ --- @return CompletionItem[]
112+ local function add_selection_items (ctx )
113+ local items = {
114+ create_context_item (
115+ ' Selection' .. (ctx .selections and # ctx .selections > 0 and string.format (' (%d)' , # ctx .selections ) or ' ' ),
116+ ' selection' ,
117+ context .is_context_enabled (' selection' )
118+ ),
119+ }
120+
121+ for i , selection in ipairs (ctx .selections or {}) do
122+ local label = ' Selection ' .. (selection .file and vim .fn .fnamemodify (selection .file .path , ' :t' )) or i
123+ table.insert (
124+ items ,
125+ create_context_item (label , ' selection_item' , true , format_selection (selection ), icons .get (' selection' ), selection )
126+ )
127+ end
128+ return items
123129end
124130
125131--- @param ctx OpencodeContext
@@ -176,10 +182,10 @@ local context_source = {
176182
177183 local items = {
178184 add_current_file_item (ctx ),
179- add_selection_item (ctx ),
180185 add_diagnostics_item (ctx ),
181186 add_cursor_data_item (ctx ),
182187 }
188+ vim .list_extend (items , add_selection_items (ctx ))
183189 vim .list_extend (items , add_mentioned_files_items (ctx ))
184190 vim .list_extend (items , add_subagents_items (ctx ))
185191
@@ -224,6 +230,8 @@ local context_source = {
224230 local subagent_name = item .data .name :gsub (' %(agent%)$' , ' ' )
225231 context .remove_subagent (subagent_name )
226232 input_win .remove_mention (subagent_name )
233+ elseif type == ' selection_item' then
234+ context .remove_selection (item .data .additional_data --[[ @as OpencodeContextSelection]] )
227235 end
228236
229237 vim .schedule (function ()
0 commit comments