-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathdispatch.lua
More file actions
194 lines (162 loc) · 4.78 KB
/
dispatch.lua
File metadata and controls
194 lines (162 loc) · 4.78 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
local handlers = require('opencode.commands.handlers')
local config = require('opencode.config')
local registry = require('opencode.registry')
local state = require('opencode.state')
local M = {}
local lifecycle_hook_keys = {
before = 'on_command_before',
after = 'on_command_after',
error = 'on_command_error',
finally = 'on_command_finally',
}
local lifecycle_event_names = {
before = 'custom.command.before',
after = 'custom.command.after',
error = 'custom.command.error',
finally = 'custom.command.finally',
}
---@param event_name string
---@param payload table
local function emit_lifecycle_event(event_name, payload)
local manager = state.event_manager
if manager and type(manager.emit) == 'function' then
pcall(manager.emit, manager, event_name, payload)
end
end
---@param stage OpencodeCommandLifecycleStage
---@param hook_id string
---@param hook_fn OpencodeCommandDispatchHook
---@param ctx OpencodeCommandDispatchContext
---@return OpencodeCommandDispatchContext
local function run_hook(stage, hook_id, hook_fn, ctx)
local ok, next_ctx_or_err = pcall(hook_fn, ctx)
if not ok then
emit_lifecycle_event('custom.command.hook_error', {
stage = stage,
hook_id = hook_id,
error = tostring(next_ctx_or_err),
context = ctx,
})
return ctx
end
if type(next_ctx_or_err) == 'table' then
return next_ctx_or_err
end
return ctx
end
---@param stage OpencodeCommandLifecycleStage
---@return { id: string, fn: OpencodeCommandDispatchHook }[]
local function collect_registry_stage_hooks(stage)
local hooks = registry.get_hooks()
local hook_names = vim.tbl_keys(hooks)
table.sort(hook_names)
local stage_hooks = {}
for _, hook_name in ipairs(hook_names) do
local hook_spec = hooks[hook_name]
local hook_fn
if type(hook_spec) == 'table' then
hook_fn = hook_spec[stage]
if type(hook_fn) ~= 'function' then
hook_fn = hook_spec[lifecycle_hook_keys[stage]]
end
elseif type(hook_spec) == 'function' and (hook_name == stage or hook_name == lifecycle_hook_keys[stage]) then
hook_fn = hook_spec
end
if type(hook_fn) == 'function' then
stage_hooks[#stage_hooks + 1] = {
id = 'registry:' .. hook_name,
fn = hook_fn,
}
end
end
return stage_hooks
end
---@param stage OpencodeCommandLifecycleStage
---@param ctx OpencodeCommandDispatchContext
---@return OpencodeCommandDispatchContext
local function run_hook_pipeline(stage, ctx)
local next_ctx = ctx
for _, hook in ipairs(collect_registry_stage_hooks(stage)) do
next_ctx = run_hook(stage, hook.id, hook.fn, next_ctx)
end
local hooks = config.hooks
if hooks then
local config_hook_name = lifecycle_hook_keys[stage]
local config_hook = hooks[config_hook_name]
if type(config_hook) == 'function' then
next_ctx = run_hook(stage, 'config:' .. config_hook_name, config_hook, next_ctx)
end
end
emit_lifecycle_event(lifecycle_event_names[stage], next_ctx)
return next_ctx
end
---@param parsed OpencodeCommandParseResult
---@param api OpencodeCommandApi
---@return OpencodeCommandDispatchResult
function M.command(parsed, api)
---@type OpencodeCommandDispatchContext
local ctx = {
parsed = parsed,
intent = parsed.intent,
args = parsed.intent and parsed.intent.args or nil,
range = parsed.intent and parsed.intent.range or nil,
}
if not parsed.ok then
ctx.error = parsed.error
ctx = run_hook_pipeline('error', ctx)
ctx = run_hook_pipeline('finally', ctx)
return {
ok = false,
error = ctx.error,
}
end
ctx = run_hook_pipeline('before', ctx)
local intent = ctx.intent or parsed.intent
local args = ctx.args
if args == nil and intent then
args = intent.args
end
local range = ctx.range
if range == nil and intent then
range = intent.range
end
if intent then
intent.args = args or {}
intent.range = range
ctx.intent = intent
end
local ok, result, handler_error = handlers.execute(intent.handler_id, api, intent.args, intent.range)
if not ok then
ctx.error = {
code = 'unknown_handler',
message = 'Unknown command handler: ' .. intent.handler_id,
handler_id = intent.handler_id,
}
ctx = run_hook_pipeline('error', ctx)
ctx = run_hook_pipeline('finally', ctx)
return {
ok = false,
intent = ctx.intent,
error = ctx.error,
}
end
if handler_error then
ctx.error = handler_error
ctx = run_hook_pipeline('error', ctx)
ctx = run_hook_pipeline('finally', ctx)
return {
ok = false,
intent = ctx.intent,
error = ctx.error,
}
end
ctx.result = result
ctx = run_hook_pipeline('after', ctx)
ctx = run_hook_pipeline('finally', ctx)
return {
ok = true,
result = ctx.result,
intent = ctx.intent,
}
end
return M