Merge pull request #173 from barrett-ruth/feat/lang
fix language-based problem navigation
This commit is contained in:
commit
c857b66998
4 changed files with 68 additions and 13 deletions
|
|
@ -276,10 +276,13 @@ local function data_row(c, idx, tc, is_current, test_state)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param test_state PanelState
|
---@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)
|
function M.render_test_list(test_state)
|
||||||
local lines, highlights = {}, {}
|
local lines, highlights = {}, {}
|
||||||
local c = compute_cols(test_state)
|
local c = compute_cols(test_state)
|
||||||
|
local current_test_line = nil
|
||||||
|
|
||||||
table.insert(lines, top_border(c))
|
table.insert(lines, top_border(c))
|
||||||
table.insert(lines, header_line(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 is_current = (i == test_state.current_index)
|
||||||
local row, hi = data_row(c, i, tc, is_current, test_state)
|
local row, hi = data_row(c, i, tc, is_current, test_state)
|
||||||
table.insert(lines, row)
|
table.insert(lines, row)
|
||||||
|
|
||||||
|
if is_current then
|
||||||
|
current_test_line = #lines
|
||||||
|
end
|
||||||
|
|
||||||
if hi then
|
if hi then
|
||||||
hi.line = #lines - 1
|
hi.line = #lines - 1
|
||||||
table.insert(highlights, hi)
|
table.insert(highlights, hi)
|
||||||
|
|
@ -327,7 +335,7 @@ function M.render_test_list(test_state)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return lines, highlights
|
return lines, highlights, current_test_line or 1
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param ran_test_case RanTestCase?
|
---@param ran_test_case RanTestCase?
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ local scraper = require('cp.scraper')
|
||||||
local state = require('cp.state')
|
local state = require('cp.state')
|
||||||
|
|
||||||
---Get the language of the current file from cache
|
---Get the language of the current file from cache
|
||||||
---@return string|nil
|
---@return string?
|
||||||
local function get_current_file_language()
|
local function get_current_file_language()
|
||||||
local current_file = vim.fn.expand('%:p')
|
local current_file = vim.fn.expand('%:p')
|
||||||
if current_file == '' then
|
if current_file == '' then
|
||||||
|
|
@ -20,6 +20,34 @@ local function get_current_file_language()
|
||||||
return file_state and file_state.language or nil
|
return file_state and file_state.language or nil
|
||||||
end
|
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
|
---@class TestCaseLite
|
||||||
---@field input string
|
---@field input string
|
||||||
---@field expected string
|
---@field expected string
|
||||||
|
|
@ -306,19 +334,28 @@ function M.navigate_problem(direction, language)
|
||||||
require('cp.ui.views').disable()
|
require('cp.ui.views').disable()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local lang = nil
|
||||||
|
|
||||||
if language then
|
if language then
|
||||||
local lang_result = config_module.get_language_for_platform(platform, language)
|
local lang_result = config_module.get_language_for_platform(platform, language)
|
||||||
if not lang_result.valid then
|
if not lang_result.valid then
|
||||||
logger.log(lang_result.error, vim.log.levels.ERROR)
|
logger.log(lang_result.error, vim.log.levels.ERROR)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
end
|
lang = language
|
||||||
|
else
|
||||||
local lang = language or get_current_file_language()
|
local existing_lang =
|
||||||
if lang and not language then
|
get_existing_problem_language(platform, contest_id, problems[new_index].id)
|
||||||
local lang_result = config_module.get_language_for_platform(platform, lang)
|
if existing_lang then
|
||||||
if not lang_result.valid then
|
lang = existing_lang
|
||||||
lang = nil
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -574,7 +574,7 @@ function M.toggle_panel(panel_opts)
|
||||||
end
|
end
|
||||||
run_render.setup_highlights()
|
run_render.setup_highlights()
|
||||||
local test_state = run.get_panel_state()
|
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(
|
utils.update_buffer_content(
|
||||||
test_buffers.tab_buf,
|
test_buffers.tab_buf,
|
||||||
tab_lines,
|
tab_lines,
|
||||||
|
|
@ -582,6 +582,17 @@ function M.toggle_panel(panel_opts)
|
||||||
test_list_namespace
|
test_list_namespace
|
||||||
)
|
)
|
||||||
update_diff_panes()
|
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
|
end
|
||||||
|
|
||||||
local function navigate_test_case(delta)
|
local function navigate_test_case(delta)
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,6 @@ if vim.g.loaded_cp then
|
||||||
end
|
end
|
||||||
vim.g.loaded_cp = 1
|
vim.g.loaded_cp = 1
|
||||||
|
|
||||||
local utils = require('cp.utils')
|
|
||||||
|
|
||||||
vim.api.nvim_create_user_command('CP', function(opts)
|
vim.api.nvim_create_user_command('CP', function(opts)
|
||||||
local cp = require('cp')
|
local cp = require('cp')
|
||||||
cp.handle_command(opts)
|
cp.handle_command(opts)
|
||||||
|
|
@ -69,6 +67,7 @@ end, {
|
||||||
elseif args[2] == 'cache' then
|
elseif args[2] == 'cache' then
|
||||||
return filter_candidates({ 'clear', 'read' })
|
return filter_candidates({ 'clear', 'read' })
|
||||||
elseif args[2] == 'interact' then
|
elseif args[2] == 'interact' then
|
||||||
|
local utils = require('cp.utils')
|
||||||
return filter_candidates(utils.cwd_executables())
|
return filter_candidates(utils.cwd_executables())
|
||||||
elseif args[2] == 'run' or args[2] == 'panel' then
|
elseif args[2] == 'run' or args[2] == 'panel' then
|
||||||
local state = require('cp.state')
|
local state = require('cp.state')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue