refactor(hooks): replace flat hooks API with setup/on namespaces
Problem: the hooks API conflated distinct lifecycle scopes under a flat
table with inconsistent naming (setup_code, before_run, setup_io_input),
making it hard to reason about when each hook fires.
Solution: introduce two namespaces — hooks.setup.{contest,code,io} for
one-time initialization and hooks.on.{enter,run,debug} for recurring
events. hooks.setup.contest fires once when a contest dir is newly
created; hooks.on.enter is registered as a buffer-scoped BufEnter
autocmd and fires immediately after setup.code. The provisional buffer
setup_code callsite is removed as it ran on an unresolved temp buffer.
This commit is contained in:
parent
6a395af98f
commit
add022af8c
3 changed files with 95 additions and 46 deletions
|
|
@ -196,13 +196,6 @@ function M.setup_contest(platform, contest_id, problem_id, language)
|
|||
|
||||
state.set_language(lang)
|
||||
|
||||
if cfg.hooks and cfg.hooks.setup_code and not vim.b[bufnr].cp_setup_done then
|
||||
local ok = pcall(cfg.hooks.setup_code, state)
|
||||
if ok then
|
||||
vim.b[bufnr].cp_setup_done = true
|
||||
end
|
||||
end
|
||||
|
||||
state.set_provisional({
|
||||
bufnr = bufnr,
|
||||
platform = platform,
|
||||
|
|
@ -281,7 +274,15 @@ function M.setup_problem(problem_id, language)
|
|||
return
|
||||
end
|
||||
|
||||
vim.fn.mkdir(vim.fn.fnamemodify(source_file, ':h'), 'p')
|
||||
local contest_dir = vim.fn.fnamemodify(source_file, ':h')
|
||||
local is_new_dir = vim.fn.isdirectory(contest_dir) == 0
|
||||
vim.fn.mkdir(contest_dir, 'p')
|
||||
if is_new_dir then
|
||||
local s = config.hooks and config.hooks.setup
|
||||
if s and s.contest then
|
||||
pcall(s.contest, state)
|
||||
end
|
||||
end
|
||||
|
||||
local prov = state.get_provisional()
|
||||
if prov and prov.platform == platform and prov.contest_id == (state.get_contest_id() or '') then
|
||||
|
|
@ -302,15 +303,23 @@ function M.setup_problem(problem_id, language)
|
|||
state.set_solution_win(vim.api.nvim_get_current_win())
|
||||
if not vim.b[prov.bufnr].cp_setup_done then
|
||||
apply_template(prov.bufnr, lang, platform)
|
||||
if config.hooks and config.hooks.setup_code then
|
||||
local ok = pcall(config.hooks.setup_code, state)
|
||||
if ok then
|
||||
vim.b[prov.bufnr].cp_setup_done = true
|
||||
end
|
||||
local s = config.hooks and config.hooks.setup
|
||||
if s and s.code then
|
||||
local ok = pcall(s.code, state)
|
||||
if ok then vim.b[prov.bufnr].cp_setup_done = true end
|
||||
else
|
||||
helpers.clearcol(prov.bufnr)
|
||||
vim.b[prov.bufnr].cp_setup_done = true
|
||||
end
|
||||
local o = config.hooks and config.hooks.on
|
||||
if o and o.enter then
|
||||
local bufnr = prov.bufnr
|
||||
vim.api.nvim_create_autocmd('BufEnter', {
|
||||
buffer = bufnr,
|
||||
callback = function() pcall(o.enter, state) end,
|
||||
})
|
||||
pcall(o.enter, state)
|
||||
end
|
||||
end
|
||||
cache.set_file_state(
|
||||
vim.fn.fnamemodify(source_file, ':p'),
|
||||
|
|
@ -339,15 +348,22 @@ function M.setup_problem(problem_id, language)
|
|||
if is_new then
|
||||
apply_template(bufnr, lang, platform)
|
||||
end
|
||||
if config.hooks and config.hooks.setup_code then
|
||||
local ok = pcall(config.hooks.setup_code, state)
|
||||
if ok then
|
||||
vim.b[bufnr].cp_setup_done = true
|
||||
end
|
||||
local s = config.hooks and config.hooks.setup
|
||||
if s and s.code then
|
||||
local ok = pcall(s.code, state)
|
||||
if ok then vim.b[bufnr].cp_setup_done = true end
|
||||
else
|
||||
helpers.clearcol(bufnr)
|
||||
vim.b[bufnr].cp_setup_done = true
|
||||
end
|
||||
local o = config.hooks and config.hooks.on
|
||||
if o and o.enter then
|
||||
vim.api.nvim_create_autocmd('BufEnter', {
|
||||
buffer = bufnr,
|
||||
callback = function() pcall(o.enter, state) end,
|
||||
})
|
||||
pcall(o.enter, state)
|
||||
end
|
||||
end
|
||||
cache.set_file_state(
|
||||
vim.fn.expand('%:p'),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue