diff --git a/lua/cp/setup.lua b/lua/cp/setup.lua index e473563..d2c9f43 100644 --- a/lua/cp/setup.lua +++ b/lua/cp/setup.lua @@ -165,6 +165,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) return end diff --git a/lua/cp/ui/panel.lua b/lua/cp/ui/panel.lua index 4e7962d..d3fccc2 100644 --- a/lua/cp/ui/panel.lua +++ b/lua/cp/ui/panel.lua @@ -70,37 +70,24 @@ function M.toggle_interactive(interactor_cmd) return end - local platform, contest_id = state.get_platform(), state.get_contest_id() - if not platform then - logger.log('No platform configured.', vim.log.levels.ERROR) - return - end - if not contest_id then + local platform, contest_id, problem_id = + state.get_platform(), state.get_contest_id(), state.get_problem_id() + if not platform or not contest_id or not problem_id then logger.log( - ("No contest %s configured for platform '%s'."):format( - contest_id, - constants.PLATFORM_DISPLAY_NAMES[platform] - ), + 'No platform/contest/problem configured. Use :CP [...] first.', vim.log.levels.ERROR ) return end - local problem_id = state.get_problem_id() - if not problem_id then - logger.log('No problem is active.', vim.log.levels.ERROR) - return - end - cache.load() local contest_data = cache.get_contest_data(platform, contest_id) if not contest_data or not contest_data.index_map - or not contest_data.problems[contest_data.index_map[problem_id]] or not contest_data.problems[contest_data.index_map[problem_id]].interactive then - logger.log('This problem is not interactive. Use :CP run.', vim.log.levels.ERROR) + logger.log('This problem is not interactive. Use :CP {run,panel}.', vim.log.levels.ERROR) return end @@ -223,13 +210,13 @@ function M.toggle_interactive(interactor_cmd) end function M.ensure_io_view() - local platform, contest_id = state.get_platform(), state.get_contest_id() - if not platform or not contest_id then - return - end - - local problem_id = state.get_problem_id() - if not problem_id then + local platform, contest_id, problem_id = + state.get_platform(), state.get_contest_id(), state.get_problem_id() + if not platform or not contest_id or not problem_id then + logger.log( + 'No platform/contest/problem configured. Use :CP [...] first.', + vim.log.levels.ERROR + ) return end @@ -238,9 +225,9 @@ function M.ensure_io_view() if contest_data and contest_data.index_map - and contest_data.problems[contest_data.index_map[problem_id]] and contest_data.problems[contest_data.index_map[problem_id]].interactive then + logger.log('No platform configured.', vim.log.levels.ERROR) return end @@ -285,8 +272,8 @@ function M.ensure_io_view() end end - utils.update_buffer_content(input_buf, {}, nil, nil) - utils.update_buffer_content(output_buf, {}, nil, nil) + utils.update_buffer_content(input_buf, {}) + utils.update_buffer_content(output_buf, {}) vim.api.nvim_set_current_win(solution_win) @@ -294,40 +281,23 @@ function M.ensure_io_view() end function M.run_io_view(test_index) - local platform, contest_id = state.get_platform(), state.get_contest_id() - if not platform then + local platform, contest_id, problem_id = + state.get_platform(), state.get_contest_id(), state.get_problem_id() + if not platform or not contest_id or not problem_id then logger.log( - 'No platform configured. Use :CP [...] first.', + 'No platform/contest/problem configured. Use :CP [...] first.', vim.log.levels.ERROR ) - return - end - - if not contest_id then - logger.log( - ("No contest configured for platform '%s'."):format( - constants.PLATFORM_DISPLAY_NAMES[platform] - ), - vim.log.levels.ERROR - ) - return - end - - local problem_id = state.get_problem_id() - if not problem_id then - logger.log('No problem is active.', vim.log.levels.ERROR) - return end cache.load() local contest_data = cache.get_contest_data(platform, contest_id) if - contest_data - and contest_data.index_map - and contest_data.problems[contest_data.index_map[problem_id]] - and contest_data.problems[contest_data.index_map[problem_id]].interactive + not contest_data + or not contest_data.index_map + or not contest_data.problems[contest_data.index_map[problem_id]].interactive then - logger.log('This is an interactive problem. Use :CP interact instead.', vim.log.levels.WARN) + logger.log('This problem is not interactive. Use :CP {run,panel}.', vim.log.levels.ERROR) return end @@ -480,20 +450,9 @@ function M.toggle_panel(panel_opts) local platform, contest_id = state.get_platform(), state.get_contest_id() - if not platform then + if not platform or not contest_id then logger.log( - 'No platform configured. Use :CP [...] first.', - vim.log.levels.ERROR - ) - return - end - - if not contest_id then - logger.log( - ("No contest '%s' configured for platform '%s'."):format( - contest_id, - constants.PLATFORM_DISPLAY_NAMES[platform] - ), + 'No platform/contest/problem configured. Use :CP [...] first.', vim.log.levels.ERROR ) return diff --git a/lua/cp/utils.lua b/lua/cp/utils.lua index 6ce2311..e9bba54 100644 --- a/lua/cp/utils.lua +++ b/lua/cp/utils.lua @@ -125,6 +125,10 @@ function M.create_buffer_with_options(filetype) return buf end +---@param bufnr integer +---@param lines string[] +---@param highlights? Highlight[] +---@param namespace? integer function M.update_buffer_content(bufnr, lines, highlights, namespace) local was_readonly = vim.api.nvim_get_option_value('readonly', { buf = bufnr })