Skip to content

Commit 549e2b2

Browse files
committed
chore: emmylua_ls type cleanup
1 parent da73b02 commit 549e2b2

File tree

11 files changed

+73
-60
lines changed

11 files changed

+73
-60
lines changed

lua/opencode/api_client.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ function OpencodeApiClient:_ensure_base_url()
3737
end
3838
end
3939

40+
if not state.opencode_server.url then
41+
return false
42+
end
43+
4044
self.base_url = state.opencode_server.url:gsub('/$', '')
4145
return true
4246
end
@@ -49,7 +53,7 @@ end
4953
--- @return Promise<any> promise
5054
function OpencodeApiClient:_call(endpoint, method, body, query)
5155
if not self:_ensure_base_url() then
52-
return nil
56+
return require('opencode.promise').new():reject('No server base url')
5357
end
5458
local url = self.base_url .. endpoint
5559

@@ -393,7 +397,7 @@ function OpencodeApiClient:subscribe_to_events(directory, on_event)
393397
chunk = chunk:gsub('^data:%s*', '')
394398
local ok, event = pcall(vim.json.decode, vim.trim(chunk))
395399
if ok and event then
396-
on_event(event)
400+
on_event(event --[[@as table]])
397401
end
398402
end)
399403
end

lua/opencode/health.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ local function check_configuration()
111111
return
112112
end
113113

114+
---@cast config OpencodeConfig
115+
114116
local valid_positions = { 'left', 'right', 'top', 'bottom' }
115117
if not vim.tbl_contains(valid_positions, config.ui.position) then
116118
health.warn(

lua/opencode/keymap.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ end
3434

3535
-- Setup window-specific keymaps (shared helper for input/output windows)
3636
---@param keymap_config table Window keymap configuration
37-
---@param buf_id number Buffer ID to set keymaps for
37+
---@param buf_id integer Buffer ID to set keymaps for
3838
function M.setup_window_keymaps(keymap_config, buf_id)
3939
if not vim.api.nvim_buf_is_valid(buf_id) then
4040
return

lua/opencode/state.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
---@field input_content table
1212
---@field is_opencode_focused boolean
1313
---@field last_focused_opencode_window string|nil
14-
---@field last_input_window_position number|nil
15-
---@field last_output_window_position number|nil
16-
---@field last_code_win_before_opencode number|nil
14+
---@field last_input_window_position integer[]|nil
15+
---@field last_output_window_position integer[]|nil
16+
---@field last_code_win_before_opencode integer|nil
1717
---@field current_code_buf number|nil
1818
---@field display_route any|nil
1919
---@field current_mode string

lua/opencode/throttling_emitter.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local M = {}
44
--- @field queue table[] Queue of pending items to be processed
55
--- @field drain_scheduled boolean Whether a drain is already scheduled
66
--- @field process_fn fun(table): nil Function to process the queue of events
7-
--- @field drain_interval_ms number Interval between drains in milliseconds
7+
--- @field drain_interval_ms integer Interval between drains in milliseconds
88
--- @field enqueue fun(self: ThrottlingEmitter, item: any) Enqueue an item for batch processing
99
--- @field clear fun(self: ThrottlingEmitter) Clear the queue and cancel any pending drain
1010
local ThrottlingEmitter = {}

lua/opencode/ui/autocmds.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function M.setup_resize_handler(windows)
5959
vim.api.nvim_create_autocmd('WinResized', {
6060
group = resize_group,
6161
callback = function(args)
62-
local win = tonumber(args.match)
62+
local win = tonumber(args.match) --[[@as integer]]
6363
if not win or not vim.api.nvim_win_is_valid(win) or not output_window.mounted() then
6464
return
6565
end

lua/opencode/ui/footer.lua

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ local loading_animation = require('opencode.ui.loading_animation')
1010
local M = {}
1111

1212
function M.render()
13-
local windows = state.windows
14-
if not output_window.mounted(windows) or not M.mounted(windows) then
13+
if not output_window.mounted() or not M.mounted() then
1514
return
1615
end
16+
---@cast state.windows { output_win: integer }
1717

1818
local segments = {}
1919

@@ -46,23 +46,22 @@ function M.render()
4646
append_to_footer(restore_point_text)
4747
end
4848

49-
---@diagnostic disable-next-line: need-check-nil
50-
local win_width = vim.api.nvim_win_get_width(windows.output_win)
49+
local win_width = vim.api.nvim_win_get_width(state.windows.output_win)
5150
local footer_text = table.concat(segments, ' | ') .. ' '
5251
footer_text = string.rep(' ', win_width - #footer_text) .. footer_text
5352

5453
M.set_content({ footer_text })
5554
end
5655

57-
---@param windows OpencodeWindowState
58-
function M._build_footer_win_config(windows)
56+
---@param output_win integer
57+
function M._build_footer_win_config(output_win)
5958
return {
6059
relative = 'win',
61-
win = windows.output_win,
60+
win = output_win,
6261
anchor = 'SW',
63-
width = vim.api.nvim_win_get_width(windows.output_win),
62+
width = vim.api.nvim_win_get_width(output_win),
6463
height = 1,
65-
row = vim.api.nvim_win_get_height(windows.output_win) - 1,
64+
row = vim.api.nvim_win_get_height(output_win) - 1,
6665
col = 0,
6766
focusable = false,
6867
style = 'minimal',
@@ -82,7 +81,11 @@ local function on_job_count_changed(_, new, old)
8281
end
8382

8483
function M.setup(windows)
85-
windows.footer_win = vim.api.nvim_open_win(windows.footer_buf, false, M._build_footer_win_config(windows))
84+
if not windows.output_win then
85+
return false
86+
end
87+
88+
windows.footer_win = vim.api.nvim_open_win(windows.footer_buf, false, M._build_footer_win_config(windows.output_win))
8689
vim.api.nvim_set_option_value('winhl', 'Normal:OpenCodeHint', { win = windows.footer_win })
8790

8891
-- for stats changes
@@ -95,7 +98,9 @@ function M.setup(windows)
9598
end
9699

97100
function M.close()
98-
if state.windows then
101+
if M.mounted() then
102+
---@cast state.windows { footer_win: integer, footer_buf: integer }
103+
99104
pcall(vim.api.nvim_win_close, state.windows.footer_win, true)
100105
pcall(vim.api.nvim_buf_delete, state.windows.footer_buf, { force = true })
101106
end
@@ -112,15 +117,17 @@ function M.mounted(windows)
112117
return windows
113118
and windows.footer_win
114119
and vim.api.nvim_win_is_valid(windows.footer_win)
120+
and windows.output_win
115121
and vim.api.nvim_win_is_valid(windows.output_win)
122+
and windows.footer_buf
116123
end
117124

118125
function M.update_window(windows)
119126
if not M.mounted(windows) then
120127
return
121128
end
122129

123-
vim.api.nvim_win_set_config(windows.footer_win, M._build_footer_win_config(windows))
130+
vim.api.nvim_win_set_config(windows.footer_win, M._build_footer_win_config(windows.output_win))
124131
M.render()
125132
end
126133

@@ -132,29 +139,26 @@ function M.create_buf()
132139
end
133140

134141
function M.clear()
135-
local windows = state.windows
136-
if not M.mounted() or not windows then
142+
if not M.mounted() then
137143
return
138144
end
145+
---@cast state.windows { footer_buf: integer }
139146

140147
local foot_ns_id = vim.api.nvim_create_namespace('opencode_footer')
141-
vim.api.nvim_buf_clear_namespace(windows.footer_buf, foot_ns_id, 0, -1)
148+
vim.api.nvim_buf_clear_namespace(state.windows.footer_buf, foot_ns_id, 0, -1)
142149

143150
M.set_content({})
144-
--
145-
-- state.tokens_count = 0
146-
-- state.cost = 0
147151
end
148152

149153
function M.set_content(lines)
150-
local windows = state.windows
151-
if not M.mounted() or not windows then
154+
if not M.mounted() then
152155
return
153156
end
157+
---@cast state.windows { footer_buf: integer }
154158

155-
vim.api.nvim_set_option_value('modifiable', true, { buf = windows.footer_buf })
156-
vim.api.nvim_buf_set_lines(windows.footer_buf, 0, -1, false, lines)
157-
vim.api.nvim_set_option_value('modifiable', false, { buf = windows.footer_buf })
159+
vim.api.nvim_set_option_value('modifiable', true, { buf = state.windows.footer_buf })
160+
vim.api.nvim_buf_set_lines(state.windows.footer_buf, 0, -1, false, lines)
161+
vim.api.nvim_set_option_value('modifiable', false, { buf = state.windows.footer_buf })
158162
end
159163

160164
return M

lua/opencode/ui/mention.lua

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
local config = require('opencode.config')
2-
31
local M = {}
42

53
local mentions_namespace = vim.api.nvim_create_namespace('OpencodeMentions')
@@ -25,6 +23,7 @@ function M.highlight_all_mentions(buf, callback)
2523
if not mention_start then
2624
break
2725
end
26+
---@cast mention_start integer
2827

2928
if callback then
3029
callback(line:sub(mention_start + 1, mention_end), row, mention_start, mention_end)
@@ -82,7 +81,7 @@ function M.highlight_mentions_in_output(output, text, mentions, start_line)
8281
end_col = col_end,
8382
hl_group = 'OpencodeMention',
8483
priority = 1000,
85-
})
84+
} --[[@as OutputExtmark]])
8685
break
8786
end
8887

@@ -91,9 +90,14 @@ function M.highlight_mentions_in_output(output, text, mentions, start_line)
9190
end
9291
end
9392
end
93+
9494
local function insert_mention(windows, row, col, name)
9595
local current_line = vim.api.nvim_buf_get_lines(windows.input_buf, row - 1, row, false)[1]
9696

97+
if not current_line then
98+
return
99+
end
100+
97101
local insert_name = '@' .. name .. ' '
98102

99103
local new_line = current_line:sub(1, col) .. insert_name .. current_line:sub(col + 2)
@@ -111,7 +115,7 @@ function M.mention(get_name)
111115

112116
get_name(function(name)
113117
vim.schedule(function()
114-
if not windows or not name then
118+
if not windows or not windows.input_win or not name then
115119
return
116120
end
117121
local cursor_pos = vim.api.nvim_win_get_cursor(windows.input_win)

lua/opencode/ui/output_window.lua

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,7 @@ end
2424

2525
function M.mounted(windows)
2626
windows = windows or state.windows
27-
if
28-
not state.windows
29-
or not state.windows.output_buf
30-
or not state.windows.output_win
31-
or not vim.api.nvim_win_is_valid(windows.output_win)
32-
then
33-
return false
34-
end
35-
36-
return true
27+
return windows and windows.output_buf and windows.output_win and vim.api.nvim_win_is_valid(windows.output_win)
3728
end
3829

3930
function M.setup(windows)
@@ -66,6 +57,7 @@ function M.get_buf_line_count()
6657
if not M.mounted() then
6758
return 0
6859
end
60+
---@cast state.windows { output_buf: integer }
6961

7062
return vim.api.nvim_buf_line_count(state.windows.output_buf)
7163
end
@@ -78,28 +70,25 @@ function M.set_lines(lines, start_line, end_line)
7870
if not M.mounted() then
7971
return
8072
end
73+
---@cast state.windows { output_buf: integer }
8174

8275
start_line = start_line or 0
8376
end_line = end_line or -1
8477

85-
local windows = state.windows
86-
if not windows or not windows.output_buf then
87-
return
88-
end
89-
90-
vim.api.nvim_set_option_value('modifiable', true, { buf = windows.output_buf })
91-
vim.api.nvim_buf_set_lines(windows.output_buf, start_line, end_line, false, lines)
92-
vim.api.nvim_set_option_value('modifiable', false, { buf = windows.output_buf })
78+
vim.api.nvim_set_option_value('modifiable', true, { buf = state.windows.output_buf })
79+
vim.api.nvim_buf_set_lines(state.windows.output_buf, start_line, end_line, false, lines)
80+
vim.api.nvim_set_option_value('modifiable', false, { buf = state.windows.output_buf })
9381
end
9482

9583
---Clear output buf extmarks
9684
---@param start_line? integer Line to start clearing, defaults 0
9785
---@param end_line? integer Line to clear until, defaults to -1
9886
---@param clear_all? boolean If true, clears all extmarks in the buffer
9987
function M.clear_extmarks(start_line, end_line, clear_all)
100-
if not M.mounted() or not state.windows.output_buf then
88+
if not M.mounted() then
10189
return
10290
end
91+
---@cast state.windows { output_buf: integer }
10392

10493
start_line = start_line or 0
10594
end_line = end_line or -1
@@ -108,12 +97,13 @@ function M.clear_extmarks(start_line, end_line, clear_all)
10897
end
10998

11099
---Apply extmarks to the output buffer
111-
---@param extmarks table<number, OutputExtmark> Extmarks indexed by line
100+
---@param extmarks table<number, OutputExtmark[]> Extmarks indexed by line
112101
---@param line_offset? integer Line offset to apply to extmarks, defaults to 0
113102
function M.set_extmarks(extmarks, line_offset)
114103
if not M.mounted() or not extmarks or type(extmarks) ~= 'table' then
115104
return
116105
end
106+
---@cast state.windows { output_buf: integer }
117107

118108
line_offset = line_offset or 0
119109

@@ -122,20 +112,26 @@ function M.set_extmarks(extmarks, line_offset)
122112
for line_idx, marks in pairs(extmarks) do
123113
for _, mark in ipairs(marks) do
124114
local actual_mark = type(mark) == 'function' and mark() or mark
125-
local target_line = line_offset + line_idx
115+
local target_line = line_offset + line_idx --[[@as integer]]
126116
if actual_mark.end_row then
127117
actual_mark.end_row = actual_mark.end_row + line_offset
128118
end
129119
local start_col = actual_mark.start_col
130120
if actual_mark.start_col then
131121
actual_mark.start_col = nil
132122
end
123+
---@cast actual_mark vim.api.keyset.set_extmark
133124
pcall(vim.api.nvim_buf_set_extmark, output_buf, M.namespace, target_line, start_col or 0, actual_mark)
134125
end
135126
end
136127
end
137128

138129
function M.focus_output(should_stop_insert)
130+
if not M.mounted() then
131+
return
132+
end
133+
---@cast state.windows { output_win: integer }
134+
139135
if should_stop_insert then
140136
vim.cmd('stopinsert')
141137
end
@@ -146,6 +142,8 @@ function M.close()
146142
if M.mounted() then
147143
return
148144
end
145+
---@cast state.windows { output_win: integer, output_buf: integer }
146+
149147
pcall(vim.api.nvim_win_close, state.windows.output_win, true)
150148
pcall(vim.api.nvim_buf_delete, state.windows.output_buf, { force = true })
151149
end

lua/opencode/ui/renderer.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ local trigger_on_data_rendered = require('opencode.util').debounce(function()
2222
return
2323
end
2424

25-
if not state.windows then
25+
if not state.windows or not state.windows.output_buf or not state.windows.output_win then
2626
return
2727
end
2828

0 commit comments

Comments
 (0)