forked from sudo-tee/opencode.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.lua
More file actions
293 lines (271 loc) · 7.89 KB
/
state.lua
File metadata and controls
293 lines (271 loc) · 7.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
---@class OpencodeWindowState
---@field input_win integer|nil
---@field output_win integer|nil
---@field footer_win integer|nil
---@field footer_buf integer|nil
---@field input_buf integer|nil
---@field output_buf integer|nil
---@class OpencodeState
---@field windows OpencodeWindowState|nil
---@field is_opening boolean
---@field input_content table
---@field is_opencode_focused boolean
---@field last_focused_opencode_window string|nil
---@field last_input_window_position integer[]|nil
---@field last_output_window_position integer[]|nil
---@field last_code_win_before_opencode integer|nil
---@field current_code_buf number|nil
---@field saved_window_options table|nil
---@field display_route any|nil
---@field current_mode string
---@field last_output number
---@field last_sent_context OpencodeContext|nil
---@field current_context_config OpencodeContextConfig|nil
---@field context_updated_at number|nil
---@field active_session Session|nil
---@field restore_points RestorePoint[]
---@field current_model string|nil
---@field user_mode_model_map table<string, string>
---@field current_model_info table|nil
---@field current_variant string|nil
---@field messages OpencodeMessage[]|nil
---@field current_message OpencodeMessage|nil
---@field last_user_message OpencodeMessage|nil
---@field pending_permissions OpencodePermission[]
---@field cost number
---@field tokens_count number
---@field job_count number
---@field user_message_count table<string, number>
---@field opencode_server OpencodeServer|nil
---@field api_client OpencodeApiClient
---@field event_manager EventManager|nil
---@field pre_zoom_width integer|nil
---@field required_version string
---@field opencode_cli_version string|nil
---@field append fun( key:string, value:any)
---@field remove fun( key:string, idx:number)
---@field subscribe fun( key:string|nil, cb:fun(key:string, new_val:any, old_val:any))
---@field subscribe fun( key:string|string[]|nil, cb:fun(key:string, new_val:any, old_val:any))
---@field unsubscribe fun( key:string|nil, cb:fun(key:string, new_val:any, old_val:any))
---@field is_running fun():boolean
local M = {}
-- Internal raw state table
local _state = {
-- ui
windows = nil, ---@type OpencodeWindowState|nil
is_opening = false,
input_content = {},
is_opencode_focused = false,
last_focused_opencode_window = nil,
last_input_window_position = nil,
last_output_window_position = nil,
last_code_win_before_opencode = nil,
current_code_buf = nil,
saved_window_options = nil,
display_route = nil,
current_mode = nil,
last_output = 0,
pre_zoom_width = nil,
-- context
last_sent_context = nil,
current_context_config = {},
context_updated_at = nil,
-- session
active_session = nil,
restore_points = {},
current_model = nil,
user_mode_model_map = {},
current_model_info = nil,
current_variant = nil,
-- messages
messages = nil,
current_message = nil,
last_user_message = nil,
pending_permissions = {},
cost = 0,
tokens_count = 0,
-- job
job_count = 0,
user_message_count = {},
opencode_server = nil,
api_client = nil,
event_manager = nil,
-- versions
required_version = '0.6.3',
opencode_cli_version = nil,
}
-- Listener registry: { [key] = {cb1, cb2, ...}, ['*'] = {cb1, ...} }
local _listeners = {}
--- Subscribe to changes for a key (or all keys with '*').
---@param key string|string[]|nil If nil or '*', listens to all keys
---@param cb fun(key:string, new_val:any, old_val:any)
---@usage
--- state.subscribe('foo', function(key, new, old) ... end)
--- state.subscribe('*', function(key, new, old) ... end)
function M.subscribe(key, cb)
if type(key) == 'table' then
for _, k in ipairs(key) do
M.subscribe(k, cb)
end
return
end
key = key or '*'
if not _listeners[key] then
_listeners[key] = {}
end
table.insert(_listeners[key], cb)
end
--- Unsubscribe a callback for a key (or all keys)
---@param key string|nil
---@param cb fun(key:string, new_val:any, old_val:any)
function M.unsubscribe(key, cb)
key = key or '*'
local list = _listeners[key]
if not list then
return
end
for i, fn in ipairs(list) do
if fn == cb then
table.remove(list, i)
break
end
end
end
-- Notify listeners
local function _notify(key, new_val, old_val)
-- schedule notification to make sure we're not in a fast event
-- context
vim.schedule(function()
if _listeners[key] then
for _, cb in ipairs(_listeners[key]) do
local ok, err = pcall(cb, key, new_val, old_val)
if not ok then
vim.notify(err --[[@as string]])
end
end
end
if _listeners['*'] then
for _, cb in ipairs(_listeners['*']) do
pcall(cb, key, new_val, old_val)
end
end
end)
end
function M.append(key, value)
if type(value) ~= 'table' then
error('Value must be a table to append')
end
if not _state[key] then
_state[key] = {}
end
if type(_state[key]) ~= 'table' then
error('State key is not a table: ' .. key)
end
local old = vim.deepcopy(_state[key] --[[@as table]])
table.insert(_state[key] --[[@as table]], value)
_notify(key, _state[key], old)
end
function M.remove(key, idx)
if not _state[key] then
return
end
if type(_state[key]) ~= 'table' then
error('State key is not a table: ' .. key)
end
local old = vim.deepcopy(_state[key] --[[@as table]])
table.remove(_state[key] --[[@as table]], idx)
_notify(key, _state[key], old)
end
---
--- Returns true if any job (run or server) is running
---
function M.is_running()
return M.job_count > 0
end
---@return 'visible'|'hidden'|'closed'
function M.get_window_status()
if M.windows and M.windows.output_win and vim.api.nvim_win_is_valid(M.windows.output_win) then
return 'visible'
end
return 'closed'
end
---@param pos any
---@return integer[]|nil
local function normalize_cursor(pos)
if type(pos) ~= 'table' or #pos < 2 then
return nil
end
local line = tonumber(pos[1])
local col = tonumber(pos[2])
if not line or not col then
return nil
end
return { math.max(1, math.floor(line)), math.max(0, math.floor(col)) }
end
---Save cursor position from a window
---@param win_type 'input'|'output'
---@param win_id integer|nil
---@return integer[]|nil
function M.save_cursor_position(win_type, win_id)
if not win_id or not vim.api.nvim_win_is_valid(win_id) then
return nil
end
local ok, pos = pcall(vim.api.nvim_win_get_cursor, win_id)
if not ok then
return nil
end
local normalized = normalize_cursor(pos)
if not normalized then
return nil
end
M.set_cursor_position(win_type, normalized)
return normalized
end
---Set saved cursor position
---@param win_type 'input'|'output'
---@param pos integer[]|nil
function M.set_cursor_position(win_type, pos)
local normalized = normalize_cursor(pos)
if win_type == 'input' then
_state.last_input_window_position = normalized
elseif win_type == 'output' then
_state.last_output_window_position = normalized
end
end
---Get saved cursor position
---@param win_type 'input'|'output'
---@return integer[]|nil
function M.get_cursor_position(win_type)
if win_type == 'input' then
return normalize_cursor(_state.last_input_window_position)
end
if win_type == 'output' then
return normalize_cursor(_state.last_output_window_position)
end
return nil
end
--- Observable state proxy. All reads/writes go through this table.
--- Use `state.subscribe(key, cb)` to listen for changes.
--- Use `state.unsubscribe(key, cb)` to remove listeners.
---
--- Example:
--- state.subscribe('foo', function(key, new, old) print(key, new, old) end)
--- state.foo = 42 -- triggers callback
return setmetatable(M, {
__index = function(_, k)
return _state[k]
end,
__newindex = function(_, k, v)
local old = _state[k]
_state[k] = v
if not vim.deep_equal(old, v) then
_notify(k, v, old)
end
end,
__pairs = function()
return pairs(_state)
end,
__ipairs = function()
return ipairs(_state)
end,
}) --[[@as OpencodeState]]