Skip to content

Commit fc26e93

Browse files
committed
refactor(ui): add type annotations and clarifying comments
Add Lua annotations (@param, @return) and short descriptive comments across many UI and renderer modules (base_picker, formatter, grep tool, highlight, output_window, question_window, renderer/buffer, renderer/ctx, renderer/events, renderer/flush, ui). These changes improve readability and editor tooling support. No behavioral changes.
1 parent b6b2072 commit fc26e93

11 files changed

Lines changed: 220 additions & 15 deletions

File tree

lua/opencode/ui/base_picker.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ local function telescope_ui(opts)
8484
local entry_display = require('telescope.pickers.entry_display')
8585

8686
-- Create displayer dynamically based on number of parts
87+
---@param picker_item PickerItem
88+
---@return table
8789
local function create_displayer(picker_item)
8890
local items = {}
8991
for _ in ipairs(picker_item.parts) do
@@ -128,6 +130,7 @@ local function telescope_ui(opts)
128130
return entry
129131
end
130132

133+
---@return unknown
131134
local function refresh_picker()
132135
return current_picker
133136
and current_picker:refresh(
@@ -222,6 +225,7 @@ end
222225
local function fzf_ui(opts)
223226
local fzf_lua = require('fzf-lua')
224227

228+
---@return table
225229
local function create_fzf_config()
226230
local has_multi_action = util.some(opts.actions, function(action)
227231
return action.multi_selection
@@ -251,6 +255,7 @@ local function fzf_ui(opts)
251255
}
252256
end
253257

258+
---@return fun(fzf_cb: fun(line?: string))
254259
local function create_finder()
255260
return function(fzf_cb)
256261
for idx, item in ipairs(opts.items) do
@@ -289,6 +294,7 @@ local function fzf_ui(opts)
289294
end
290295
end
291296

297+
---Reopen fzf-lua to reflect updated picker items.
292298
local function refresh_fzf()
293299
vim.schedule(function()
294300
fzf_ui(opts)
@@ -584,6 +590,7 @@ function M.create_picker_item(parts)
584590
parts = parts,
585591
}
586592

593+
---@return string
587594
function item:to_string()
588595
local texts = {}
589596
for _, part in ipairs(self.parts) do
@@ -592,6 +599,7 @@ function M.create_picker_item(parts)
592599
return table.concat(texts, ' ')
593600
end
594601

602+
---@return table
595603
function item:to_formatted_text()
596604
local formatted = {}
597605
for _, part in ipairs(self.parts) do

lua/opencode/ui/formatter.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ function M._format_revert_message(session_data, start_idx)
9999
return output
100100
end
101101

102+
---@param output Output
103+
---@param text string
104+
---@param action_type string
105+
---@param args any[]
106+
---@param key? string
107+
---@param line? integer
102108
local function add_action(output, text, action_type, args, key, line)
103109
-- actions use api-indexing (e.g. 0 indexed)
104110
line = (line or output:get_line_count()) - 1
@@ -384,6 +390,8 @@ function M._format_diagnostics_context(output, part)
384390
M.add_vertical_border(output, start_line, end_line, 'OpencodeMessageRoleUser', -3)
385391
end
386392

393+
---@param part OpencodeMessagePart|nil
394+
---@return string|nil
387395
local function get_visible_user_part_kind(part)
388396
if not part then
389397
return nil
@@ -417,6 +425,10 @@ local function get_visible_user_part_kind(part)
417425
return nil
418426
end
419427

428+
---@param message OpencodeMessage|nil
429+
---@param part OpencodeMessagePart|nil
430+
---@return string|nil previous_kind
431+
---@return string|nil next_kind
420432
local function get_user_part_neighbors(message, part)
421433
if not message or not message.parts or not part or not part.id then
422434
return nil, nil

lua/opencode/ui/formatter/tools/grep.lua

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
local icons = require('opencode.ui.icons')
22
local M = {}
33

4-
---@param input GrepToolInput|nil
4+
---@param value any
55
---@return string
6-
local function resolve_grep_string(input)
7-
local function normalize_part(value)
8-
if value == nil or value == vim.NIL then
9-
return ''
10-
end
11-
12-
local value_type = type(value)
13-
if value_type == 'string' then
14-
return value
15-
end
16-
if value_type == 'number' or value_type == 'boolean' then
17-
return tostring(value)
18-
end
19-
6+
local function normalize_part(value)
7+
if value == nil or value == vim.NIL then
208
return ''
219
end
2210

11+
local value_type = type(value)
12+
if value_type == 'string' then
13+
return value
14+
end
15+
if value_type == 'number' or value_type == 'boolean' then
16+
return tostring(value)
17+
end
18+
19+
return ''
20+
end
21+
22+
---@param input GrepToolInput|nil
23+
---@return string
24+
local function resolve_grep_string(input)
2325
if not input then
2426
return ''
2527
end

lua/opencode/ui/highlight.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local M = {}
22

3+
---Define the plugin highlight groups for the current background.
34
function M.setup()
45
local is_light = vim.o.background == 'light'
56

lua/opencode/ui/output_window.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ function M.end_update()
4040
end
4141
end
4242

43+
---@return integer
4344
function M.create_buf()
4445
local output_buf = vim.api.nvim_create_buf(false, true)
4546
local filetype = config.ui.output.filetype or 'opencode_output'
@@ -53,6 +54,7 @@ function M.create_buf()
5354
return output_buf
5455
end
5556

57+
---@return vim.api.keyset.win_config
5658
function M._build_output_win_config()
5759
return {
5860
relative = 'editor',
@@ -180,6 +182,7 @@ function M.sync_cursor_with_viewport(win)
180182
M._was_at_bottom_by_win[win] = visible_bottom >= line_count
181183
end
182184

185+
---@param windows OpencodeWindowState
183186
function M.setup(windows)
184187
window_options.set_window_option(
185188
'winhighlight',
@@ -215,6 +218,7 @@ function M.setup(windows)
215218
M.setup_keymaps(windows)
216219
end
217220

221+
---@param windows OpencodeWindowState?
218222
function M.update_dimensions(windows)
219223
if config.ui.position == 'current' then
220224
return
@@ -250,6 +254,7 @@ function M.update_dimensions(windows)
250254
pcall(vim.api.nvim_win_set_config, windows.output_win, { width = width })
251255
end
252256

257+
---@return integer
253258
function M.get_buf_line_count()
254259
local windows = state.windows
255260
if not windows or not windows.output_buf or not vim.api.nvim_buf_is_valid(windows.output_buf) then
@@ -399,6 +404,7 @@ function M.highlight_changed_lines(start_line, end_line)
399404
end, config.debug.highlight_changed_lines_timeout_ms or 120)
400405
end
401406

407+
---@param should_stop_insert? boolean
402408
function M.focus_output(should_stop_insert)
403409
if not M.mounted() then
404410
return
@@ -411,6 +417,7 @@ function M.focus_output(should_stop_insert)
411417
vim.api.nvim_set_current_win(state.windows.output_win)
412418
end
413419

420+
---Close and delete the output window and buffer.
414421
function M.close()
415422
if not M.mounted() then
416423
return
@@ -422,11 +429,14 @@ function M.close()
422429
pcall(vim.api.nvim_buf_delete, state.windows.output_buf, { force = true })
423430
end
424431

432+
---@param windows OpencodeWindowState
425433
function M.setup_keymaps(windows)
426434
local keymap = require('opencode.keymap')
427435
keymap.setup_window_keymaps(config.keymap.output_window, windows.output_buf)
428436
end
429437

438+
---@param windows OpencodeWindowState
439+
---@param group integer
430440
function M.setup_autocmds(windows, group)
431441
vim.api.nvim_create_autocmd('WinEnter', {
432442
group = group,
@@ -472,6 +482,7 @@ function M.setup_autocmds(windows, group)
472482
})
473483
end
474484

485+
---Clear the output buffer and all namespaces.
475486
function M.clear()
476487
M.set_lines({})
477488
-- clear extmarks in all namespaces as I've seen RenderMarkdown leave some

lua/opencode/ui/question_window.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@ function M.belongs_to_active_session(question_request)
5151
return false
5252
end
5353

54+
---Request the renderer to show the current question display.
5455
local function render_question()
5556
require('opencode.ui.renderer.events').render_question_display()
5657
end
5758

59+
---Request the renderer to remove the current question display.
5860
local function clear_question()
5961
require('opencode.ui.renderer.events').clear_question_display()
6062
end
@@ -111,6 +113,7 @@ function M.restore_pending_question(session_id)
111113
end)
112114
end
113115

116+
---Reset the current question state and remove any dialog UI.
114117
function M.clear_question()
115118
M._clear_dialog()
116119
M._current_question = nil
@@ -161,6 +164,8 @@ local function answer_current_question(answer_value)
161164
end)
162165
end
163166

167+
---@param options OpencodeQuestionOption[]
168+
---@return integer|nil
164169
local function find_other_option(options)
165170
for i, opt in ipairs(options) do
166171
if vim.startswith(opt.label:lower(), 'other') then
@@ -170,6 +175,8 @@ local function find_other_option(options)
170175
return nil
171176
end
172177

178+
---@param question_info OpencodeQuestionInfo
179+
---@return integer
173180
local function get_total_options(question_info)
174181
local has_other = find_other_option(question_info.options) ~= nil
175182
return has_other and #question_info.options or (#question_info.options + 1)
@@ -198,6 +205,7 @@ function M._answer_with_option(option_index)
198205
answer_current_question(question_info.options[option_index].label)
199206
end
200207

208+
---Prompt for a free-form answer to the active question.
201209
function M._answer_with_custom()
202210
vim.ui.input({ prompt = 'Enter your response: ' }, function(input)
203211
if input and input ~= '' then
@@ -210,6 +218,8 @@ function M._answer_with_custom()
210218
end)
211219
end
212220

221+
---@param options OpencodeQuestionOption[]
222+
---@return OpencodeQuestionOption[]
213223
local function add_other_if_missing(options)
214224
if find_other_option(options) ~= nil then
215225
return options
@@ -258,6 +268,7 @@ function M.format_display(output)
258268
})
259269
end
260270

271+
---Create the in-buffer dialog used to answer the active question.
261272
function M._setup_dialog()
262273
if not M.has_question() then
263274
return
@@ -272,11 +283,13 @@ function M._setup_dialog()
272283

273284
local buf = state.windows.output_buf
274285

286+
---@return boolean
275287
local function check_focused()
276288
local ui = require('opencode.ui.ui')
277289
return ui.is_opencode_focused() and M.has_question()
278290
end
279291

292+
---@param index integer
280293
local function on_select(index)
281294
if not check_focused() then
282295
return
@@ -289,6 +302,7 @@ function M._setup_dialog()
289302
end, 100)
290303
end
291304

305+
---Reject the current question if the dialog is dismissed.
292306
local function on_dismiss()
293307
if not check_focused() then
294308
return
@@ -298,10 +312,12 @@ function M._setup_dialog()
298312
render_question()
299313
end
300314

315+
---Refresh the rendered question state after navigation changes.
301316
local function on_navigate()
302317
render_question()
303318
end
304319

320+
---@return integer
305321
local function get_option_count()
306322
local question_info = M.get_current_question_info()
307323
return question_info and get_total_options(question_info) or 0
@@ -320,6 +336,7 @@ function M._setup_dialog()
320336
M._dialog:setup()
321337
end
322338

339+
---Tear down the active question dialog, if any.
323340
function M._clear_dialog()
324341
if M._dialog then
325342
M._dialog:teardown()

0 commit comments

Comments
 (0)