Skip to content

Commit 837a11d

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 30ea5db commit 837a11d

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
@@ -85,6 +85,8 @@ local function telescope_ui(opts)
8585
local entry_display = require('telescope.pickers.entry_display')
8686

8787
-- Create displayer dynamically based on number of parts
88+
---@param picker_item PickerItem
89+
---@return table
8890
local function create_displayer(picker_item)
8991
local items = {}
9092
for _ in ipairs(picker_item.parts) do
@@ -129,6 +131,7 @@ local function telescope_ui(opts)
129131
return entry
130132
end
131133

134+
---@return unknown
132135
local function refresh_picker()
133136
return current_picker
134137
and current_picker:refresh(
@@ -232,6 +235,7 @@ end
232235
local function fzf_ui(opts)
233236
local fzf_lua = require('fzf-lua')
234237

238+
---@return table
235239
local function create_fzf_config()
236240
local has_multi_action = util.some(opts.actions, function(action)
237241
return action.multi_selection
@@ -261,6 +265,7 @@ local function fzf_ui(opts)
261265
}
262266
end
263267

268+
---@return fun(fzf_cb: fun(line?: string))
264269
local function create_finder()
265270
return function(fzf_cb)
266271
for idx, item in ipairs(opts.items) do
@@ -299,6 +304,7 @@ local function fzf_ui(opts)
299304
end
300305
end
301306

307+
---Reopen fzf-lua to reflect updated picker items.
302308
local function refresh_fzf()
303309
vim.schedule(function()
304310
fzf_ui(opts)
@@ -644,6 +650,7 @@ function M.create_picker_item(parts)
644650
parts = parts,
645651
}
646652

653+
---@return string
647654
function item:to_string()
648655
local texts = {}
649656
for _, part in ipairs(self.parts) do
@@ -652,6 +659,7 @@ function M.create_picker_item(parts)
652659
return table.concat(texts, ' ')
653660
end
654661

662+
---@return table
655663
function item:to_formatted_text()
656664
local formatted = {}
657665
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)