Skip to content

Commit a2cc678

Browse files
committed
refactor(context-completion): cleanup the menu creation
1 parent 96dd3bc commit a2cc678

File tree

2 files changed

+87
-68
lines changed

2 files changed

+87
-68
lines changed

lua/opencode/context.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local state = require('opencode.state')
66

77
local M = {}
88

9+
---@type OpencodeContext
910
M.context = {
1011
-- current file
1112
current_file = nil,

lua/opencode/ui/completion/context.lua

Lines changed: 86 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ local function format_selections(selections)
6363
return table.concat(content, '\n')
6464
end
6565

66-
---@param cursor_data OpencodeContextCursorData
66+
---@param cursor_data? OpencodeContextCursorData
6767
---@return string
6868
local function format_cursor_data(cursor_data)
6969
if context.is_context_enabled('cursor_data') == false then
@@ -83,6 +83,83 @@ local function format_cursor_data(cursor_data)
8383
return table.concat(parts, '\n')
8484
end
8585

86+
---@param ctx OpencodeContext
87+
---@return CompletionItem
88+
local function add_current_file_item(ctx)
89+
local current_file_available = context.is_context_enabled('current_file')
90+
return create_context_item(
91+
'Current File',
92+
'current_file',
93+
current_file_available,
94+
string.format('Current file: %s', ctx.current_file and vim.fn.fnamemodify(ctx.current_file.path, ':~:.'))
95+
)
96+
end
97+
98+
---@param ctx OpencodeContext
99+
---@return CompletionItem[]
100+
local function add_mentioned_files_items(ctx)
101+
local items = {}
102+
if context.is_context_enabled('files') and ctx.mentioned_files and #ctx.mentioned_files > 0 then
103+
for _, file in ipairs(ctx.mentioned_files) do
104+
local filename = vim.fn.fnamemodify(file, ':~:.')
105+
table.insert(
106+
items,
107+
create_context_item(filename, 'mentioned_file', true, 'Select to remove file ' .. filename, icons.get('file'))
108+
)
109+
end
110+
end
111+
return items
112+
end
113+
114+
---@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+
)
123+
end
124+
125+
---@param ctx OpencodeContext
126+
---@return CompletionItem[]
127+
local function add_subagents_items(ctx)
128+
if not (context.is_context_enabled('agents') or ctx.mentioned_subagents) then
129+
return {}
130+
end
131+
local items = {}
132+
for _, agent in ipairs(ctx.mentioned_subagents or {}) do
133+
table.insert(
134+
items,
135+
create_context_item(agent .. ' (agent)', 'subagent', true, 'Select to remove agent ' .. agent, icons.get('agent'))
136+
)
137+
end
138+
return items
139+
end
140+
141+
---@param ctx OpencodeContext
142+
---@return CompletionItem
143+
local function add_diagnostics_item(ctx)
144+
return create_context_item(
145+
'Diagnostics',
146+
'diagnostics',
147+
context.is_context_enabled('diagnostics'),
148+
format_diagnostics(ctx.linter_errors)
149+
)
150+
end
151+
152+
---@param ctx OpencodeContext
153+
---@return CompletionItem
154+
local function add_cursor_data_item(ctx)
155+
return create_context_item(
156+
'Cursor Data',
157+
'cursor_data',
158+
context.is_context_enabled('cursor_data'),
159+
format_cursor_data(ctx.cursor_data)
160+
)
161+
end
162+
86163
---@type CompletionSource
87164
local context_source = {
88165
name = 'context',
@@ -95,75 +172,16 @@ local context_source = {
95172
return {}
96173
end
97174

98-
local items = {}
99175
local ctx = context.delta_context()
100176

101-
local current_file_available = context.is_context_enabled('current_file')
102-
103-
table.insert(
104-
items,
105-
create_context_item(
106-
'Current File',
107-
'current_file',
108-
current_file_available,
109-
string.format('Current file: %s', ctx.current_file and vim.fn.fnamemodify(ctx.current_file.path, ':~:.'))
110-
)
111-
)
112-
113-
if context.is_context_enabled('files') and ctx.mentioned_files and #ctx.mentioned_files > 0 then
114-
for _, file in ipairs(ctx.mentioned_files) do
115-
local filename = vim.fn.fnamemodify(file, ':~:.')
116-
table.insert(
117-
items,
118-
create_context_item(filename, 'mentioned_file', true, 'Select to remove file ' .. filename, icons.get('file'))
119-
)
120-
end
121-
end
122-
123-
table.insert(
124-
items,
125-
create_context_item(
126-
'Selection',
127-
'selection',
128-
context.is_context_enabled('selection'),
129-
format_selections(ctx.selections or {})
130-
)
131-
)
132-
133-
if context.is_context_enabled('agents') and ctx.mentioned_subagents then
134-
for _, subagent in ipairs(ctx.mentioned_subagents) do
135-
table.insert(
136-
items,
137-
create_context_item(
138-
subagent .. ' (agent)',
139-
'subagent',
140-
true,
141-
'Select to remove agent ' .. subagent,
142-
icons.get('agent')
143-
)
144-
)
145-
end
146-
end
147-
148-
table.insert(
149-
items,
150-
create_context_item(
151-
'Diagnostics',
152-
'diagnostics',
153-
context.is_context_enabled('diagnostics'),
154-
format_diagnostics(ctx.linter_errors)
155-
)
156-
)
157-
158-
table.insert(
159-
items,
160-
create_context_item(
161-
'Cursor Data',
162-
'cursor_data',
163-
context.is_context_enabled('cursor_data'),
164-
format_cursor_data(ctx.cursor_data)
165-
)
166-
)
177+
local items = {
178+
add_current_file_item(ctx),
179+
add_selection_item(ctx),
180+
add_diagnostics_item(ctx),
181+
add_cursor_data_item(ctx),
182+
}
183+
vim.list_extend(items, add_mentioned_files_items(ctx))
184+
vim.list_extend(items, add_subagents_items(ctx))
167185

168186
if #input > 0 then
169187
items = vim.tbl_filter(function(item)

0 commit comments

Comments
 (0)