refactor(logger): table-based LogOpts; add sync, on_done to test stream
Problem: `logger.log` positional args were hard to extend, and adding `sync` support for pre-block notifications required a clean API. Test stream completion had no user-visible signal. `setup_contest` could silently overwrite files when a user's `filename` config returned colliding paths. Solution: Replace `(msg, level, override)` with `(msg, LogOpts?)` where `LogOpts` carries `level`, `override`, and `sync`. Sync path calls `vim.notify` directly; async path uses `vim.schedule` as before. Add `on_done` callback to `scrape_all_tests`, fired via `on_exit` and surfaced as a "Loaded N tests." notification. Detect filename collisions in `proceed()` before touching the filesystem. Migrate all call sites.
This commit is contained in:
parent
127089c57f
commit
29af2df858
18 changed files with 126 additions and 117 deletions
|
|
@ -16,7 +16,7 @@ local function apply_template(bufnr, lang_id, platform)
|
|||
end
|
||||
local path = vim.fn.expand(eff.template)
|
||||
if vim.fn.filereadable(path) ~= 1 then
|
||||
logger.log(('[cp.nvim] template not readable: %s'):format(path), vim.log.levels.WARN)
|
||||
logger.log(('[cp.nvim] template not readable: %s'):format(path), { level = vim.log.levels.WARN })
|
||||
return
|
||||
end
|
||||
local lines = vim.fn.readfile(path)
|
||||
|
|
@ -112,11 +112,12 @@ local function start_tests(platform, contest_id, problems)
|
|||
return not vim.tbl_isempty(cache.get_test_cases(platform, contest_id, p.id))
|
||||
end, problems)
|
||||
if cached_len ~= #problems then
|
||||
local to_fetch = #problems - cached_len
|
||||
logger.log(('Fetching %s/%s problem tests...'):format(cached_len, #problems))
|
||||
scraper.scrape_all_tests(platform, contest_id, function(ev)
|
||||
local cached_tests = {}
|
||||
if not ev.interactive and vim.tbl_isempty(ev.tests) then
|
||||
logger.log(("No tests found for problem '%s'."):format(ev.problem_id), vim.log.levels.WARN)
|
||||
logger.log(("No tests found for problem '%s'."):format(ev.problem_id), { level = vim.log.levels.WARN })
|
||||
end
|
||||
for i, t in ipairs(ev.tests) do
|
||||
cached_tests[i] = { index = i, input = t.input, expected = t.expected }
|
||||
|
|
@ -142,6 +143,8 @@ local function start_tests(platform, contest_id, problems)
|
|||
require('cp.utils').update_buffer_content(io_state.input_buf, input_lines, nil, nil)
|
||||
end
|
||||
end
|
||||
end, function()
|
||||
logger.log(('Loaded %d test%s.'):format(to_fetch, to_fetch == 1 and '' or 's'), { level = vim.log.levels.INFO, override = true })
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
|
@ -160,7 +163,7 @@ function M.setup_contest(platform, contest_id, problem_id, language)
|
|||
if language then
|
||||
local lang_result = config_module.get_language_for_platform(platform, language)
|
||||
if not lang_result.valid then
|
||||
logger.log(lang_result.error, vim.log.levels.ERROR)
|
||||
logger.log(lang_result.error, { level = vim.log.levels.ERROR })
|
||||
return
|
||||
end
|
||||
end
|
||||
|
|
@ -206,7 +209,7 @@ function M.setup_contest(platform, contest_id, problem_id, language)
|
|||
token = vim.uv.hrtime(),
|
||||
})
|
||||
|
||||
logger.log('Fetching contests problems...', vim.log.levels.INFO, true)
|
||||
logger.log('Fetching contests problems...', { level = vim.log.levels.INFO, override = true })
|
||||
scraper.scrape_contest_metadata(
|
||||
platform,
|
||||
contest_id,
|
||||
|
|
@ -242,7 +245,7 @@ end
|
|||
function M.setup_problem(problem_id, language)
|
||||
local platform = state.get_platform()
|
||||
if not platform then
|
||||
logger.log('No platform/contest/problem configured.', vim.log.levels.ERROR)
|
||||
logger.log('No platform/contest/problem configured.', { level = vim.log.levels.ERROR })
|
||||
return
|
||||
end
|
||||
|
||||
|
|
@ -263,7 +266,7 @@ function M.setup_problem(problem_id, language)
|
|||
if language then
|
||||
local lang_result = config_module.get_language_for_platform(platform, language)
|
||||
if not lang_result.valid then
|
||||
logger.log(lang_result.error, vim.log.levels.ERROR)
|
||||
logger.log(lang_result.error, { level = vim.log.levels.ERROR })
|
||||
return
|
||||
end
|
||||
end
|
||||
|
|
@ -397,7 +400,7 @@ function M.navigate_problem(direction, language)
|
|||
local contest_id = state.get_contest_id()
|
||||
local current_problem_id = state.get_problem_id()
|
||||
if not platform or not contest_id or not current_problem_id then
|
||||
logger.log('No platform configured.', vim.log.levels.ERROR)
|
||||
logger.log('No platform configured.', { level = vim.log.levels.ERROR })
|
||||
return
|
||||
end
|
||||
|
||||
|
|
@ -409,7 +412,7 @@ function M.navigate_problem(direction, language)
|
|||
constants.PLATFORM_DISPLAY_NAMES[platform],
|
||||
contest_id
|
||||
),
|
||||
vim.log.levels.ERROR
|
||||
{ level = vim.log.levels.ERROR }
|
||||
)
|
||||
return
|
||||
end
|
||||
|
|
@ -433,7 +436,7 @@ function M.navigate_problem(direction, language)
|
|||
if language then
|
||||
local lang_result = config_module.get_language_for_platform(platform, language)
|
||||
if not lang_result.valid then
|
||||
logger.log(lang_result.error, vim.log.levels.ERROR)
|
||||
logger.log(lang_result.error, { level = vim.log.levels.ERROR })
|
||||
return
|
||||
end
|
||||
lang = language
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue