From 9bf343846697e4cf55255e8321863a70e2ecf114 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Fri, 24 Oct 2025 11:49:48 -0400 Subject: [PATCH 1/3] fix: defer to previous problem language --- lua/cp/setup.lua | 53 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/lua/cp/setup.lua b/lua/cp/setup.lua index e6f9d21..991242e 100644 --- a/lua/cp/setup.lua +++ b/lua/cp/setup.lua @@ -9,7 +9,7 @@ local scraper = require('cp.scraper') local state = require('cp.state') ---Get the language of the current file from cache ----@return string|nil +---@return string? local function get_current_file_language() local current_file = vim.fn.expand('%:p') if current_file == '' then @@ -20,6 +20,34 @@ local function get_current_file_language() return file_state and file_state.language or nil end +---Check if a problem file exists for any enabled language +---@param platform string +---@param contest_id string +---@param problem_id string +---@return string? +local function get_existing_problem_language(platform, contest_id, problem_id) + local config = config_module.get_config() + local platform_config = config.platforms[platform] + if not platform_config then + return nil + end + + for _, lang_id in ipairs(platform_config.enabled_languages) do + local effective = config.runtime.effective[platform][lang_id] + if effective and effective.extension then + local basename = config.filename + and config.filename(platform, contest_id, problem_id, config, lang_id) + or config_module.default_filename(contest_id, problem_id) + local filepath = basename .. '.' .. effective.extension + if vim.fn.filereadable(filepath) == 1 then + return lang_id + end + end + end + + return nil +end + ---@class TestCaseLite ---@field input string ---@field expected string @@ -306,19 +334,28 @@ function M.navigate_problem(direction, language) require('cp.ui.views').disable() end + local lang = nil + 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) return end - end - - local lang = language or get_current_file_language() - if lang and not language then - local lang_result = config_module.get_language_for_platform(platform, lang) - if not lang_result.valid then - lang = nil + lang = language + else + local existing_lang = + get_existing_problem_language(platform, contest_id, problems[new_index].id) + if existing_lang then + lang = existing_lang + else + lang = get_current_file_language() + if lang then + local lang_result = config_module.get_language_for_platform(platform, lang) + if not lang_result.valid then + lang = nil + end + end end end From 6ff0320531716aa67dfb01aafb03026482f5a49e Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Fri, 24 Oct 2025 13:48:56 -0400 Subject: [PATCH 2/3] cleanup --- plugin/cp.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugin/cp.lua b/plugin/cp.lua index b4954a4..61f3b3d 100644 --- a/plugin/cp.lua +++ b/plugin/cp.lua @@ -3,8 +3,6 @@ if vim.g.loaded_cp then end vim.g.loaded_cp = 1 -local utils = require('cp.utils') - vim.api.nvim_create_user_command('CP', function(opts) local cp = require('cp') cp.handle_command(opts) @@ -69,6 +67,7 @@ end, { elseif args[2] == 'cache' then return filter_candidates({ 'clear', 'read' }) elseif args[2] == 'interact' then + local utils = require('cp.utils') return filter_candidates(utils.cwd_executables()) elseif args[2] == 'run' or args[2] == 'panel' then local state = require('cp.state') From b3168ff3f04bcfa96a3ef723319a6ad3e51a7600 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Fri, 24 Oct 2025 14:03:00 -0400 Subject: [PATCH 3/3] feat: center the curso --- lua/cp/runner/run_render.lua | 12 ++++++++++-- lua/cp/ui/views.lua | 13 ++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lua/cp/runner/run_render.lua b/lua/cp/runner/run_render.lua index 0847793..714ecd3 100644 --- a/lua/cp/runner/run_render.lua +++ b/lua/cp/runner/run_render.lua @@ -276,10 +276,13 @@ local function data_row(c, idx, tc, is_current, test_state) end ---@param test_state PanelState ----@return string[], Highlight[] lines and highlight positions +---@return string[] lines +---@return Highlight[] highlights +---@return integer current_test_line function M.render_test_list(test_state) local lines, highlights = {}, {} local c = compute_cols(test_state) + local current_test_line = nil table.insert(lines, top_border(c)) table.insert(lines, header_line(c)) @@ -289,6 +292,11 @@ function M.render_test_list(test_state) local is_current = (i == test_state.current_index) local row, hi = data_row(c, i, tc, is_current, test_state) table.insert(lines, row) + + if is_current then + current_test_line = #lines + end + if hi then hi.line = #lines - 1 table.insert(highlights, hi) @@ -327,7 +335,7 @@ function M.render_test_list(test_state) end end - return lines, highlights + return lines, highlights, current_test_line or 1 end ---@param ran_test_case RanTestCase? diff --git a/lua/cp/ui/views.lua b/lua/cp/ui/views.lua index 130da3b..23e2fc6 100644 --- a/lua/cp/ui/views.lua +++ b/lua/cp/ui/views.lua @@ -574,7 +574,7 @@ function M.toggle_panel(panel_opts) end run_render.setup_highlights() local test_state = run.get_panel_state() - local tab_lines, tab_highlights = run_render.render_test_list(test_state) + local tab_lines, tab_highlights, current_line = run_render.render_test_list(test_state) utils.update_buffer_content( test_buffers.tab_buf, tab_lines, @@ -582,6 +582,17 @@ function M.toggle_panel(panel_opts) test_list_namespace ) update_diff_panes() + + if + current_line + and test_windows.tab_win + and vim.api.nvim_win_is_valid(test_windows.tab_win) + then + vim.api.nvim_win_set_cursor(test_windows.tab_win, { current_line, 0 }) + vim.api.nvim_win_call(test_windows.tab_win, function() + vim.cmd('normal! zz') + end) + end end local function navigate_test_case(delta)