Merge pull request #173 from barrett-ruth/feat/lang

fix language-based problem navigation
This commit is contained in:
Barrett Ruth 2025-10-24 14:27:18 -04:00 committed by GitHub
commit c857b66998
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 68 additions and 13 deletions

View file

@ -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?

View file

@ -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,21 +334,30 @@ 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
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
M.setup_contest(platform, contest_id, problems[new_index].id, lang)
end

View file

@ -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)

View file

@ -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')