feat(picker): picker support

This commit is contained in:
Barrett Ruth 2025-09-21 11:10:54 -04:00
parent ea9883895f
commit a33e66680b
11 changed files with 829 additions and 2 deletions

View file

@ -52,6 +52,7 @@
---@field filename? fun(contest: string, contest_id: string, problem_id?: string, config: cp.Config, language?: string): string
---@field run_panel RunPanelConfig
---@field diff DiffConfig
---@field picker "telescope"|"fzf-lua"|nil
---@class cp.UserConfig
---@field contests? table<string, PartialContestConfig>
@ -62,6 +63,7 @@
---@field filename? fun(contest: string, contest_id: string, problem_id?: string, config: cp.Config, language?: string): string
---@field run_panel? RunPanelConfig
---@field diff? DiffConfig
---@field picker? "telescope"|"fzf-lua"|nil
local M = {}
local constants = require('cp.constants')
@ -110,6 +112,7 @@ M.defaults = {
args = { 'diff', '--no-index', '--word-diff=plain', '--word-diff-regex=.', '--no-prefix' },
},
},
picker = nil,
}
---@param user_config cp.UserConfig|nil
@ -129,6 +132,7 @@ function M.setup(user_config)
filename = { user_config.filename, { 'function', 'nil' }, true },
run_panel = { user_config.run_panel, { 'table', 'nil' }, true },
diff = { user_config.diff, { 'table', 'nil' }, true },
picker = { user_config.picker, { 'string', 'nil' }, true },
})
if user_config.contests then
@ -164,6 +168,12 @@ function M.setup(user_config)
end
end
end
if user_config.picker then
if not vim.tbl_contains({ 'telescope', 'fzf-lua' }, user_config.picker) then
error(("Invalid picker '%s'. Must be 'telescope' or 'fzf-lua'"):format(user_config.picker))
end
end
end
local config = vim.tbl_deep_extend('force', M.defaults, user_config or {})

View file

@ -1,7 +1,13 @@
local M = {}
M.PLATFORMS = { 'atcoder', 'codeforces', 'cses' }
M.ACTIONS = { 'run', 'next', 'prev' }
M.ACTIONS = { 'run', 'next', 'prev', 'pick' }
M.PLATFORM_DISPLAY_NAMES = {
atcoder = 'AtCoder',
codeforces = 'CodeForces',
cses = 'CSES',
}
M.CPP = 'cpp'
M.PYTHON = 'python'

View file

@ -698,6 +698,48 @@ local function navigate_problem(delta, language)
setup_problem(state.contest_id, new_problem.id, language)
end
local function handle_pick_action()
if not config.picker then
logger.log(
'No picker configured. Set picker = "telescope" or picker = "fzf-lua" in config',
vim.log.levels.ERROR
)
return
end
if config.picker == 'telescope' then
local ok, telescope = pcall(require, 'telescope')
if not ok then
logger.log(
'Telescope not available. Install telescope.nvim or change picker config',
vim.log.levels.ERROR
)
return
end
local ok_cp, telescope_cp = pcall(require, 'cp.pickers.telescope')
if not ok_cp then
logger.log('Failed to load telescope integration', vim.log.levels.ERROR)
return
end
telescope_cp.platform_picker()
elseif config.picker == 'fzf-lua' then
local ok, _ = pcall(require, 'fzf-lua')
if not ok then
logger.log(
'fzf-lua not available. Install fzf-lua or change picker config',
vim.log.levels.ERROR
)
return
end
local ok_cp, fzf_cp = pcall(require, 'cp.pickers.fzf_lua')
if not ok_cp then
logger.log('Failed to load fzf-lua integration', vim.log.levels.ERROR)
return
end
fzf_cp.platform_picker()
end
end
local function restore_from_current_file()
local current_file = vim.fn.expand('%:p')
if current_file == '' then
@ -837,6 +879,8 @@ function M.handle_command(opts)
navigate_problem(1, cmd.language)
elseif cmd.action == 'prev' then
navigate_problem(-1, cmd.language)
elseif cmd.action == 'pick' then
handle_pick_action()
end
return
end

123
lua/cp/pickers/fzf_lua.lua Normal file
View file

@ -0,0 +1,123 @@
local picker_utils = require('cp.pickers')
local function problem_picker(platform, contest_id)
local constants = require('cp.constants')
local platform_display_name = constants.PLATFORM_DISPLAY_NAMES[platform] or platform
local fzf = require('fzf-lua')
local problems = picker_utils.get_problems_for_contest(platform, contest_id)
if #problems == 0 then
vim.notify(
('No problems found for contest: %s %s'):format(platform_display_name, contest_id),
vim.log.levels.WARN
)
return
end
local entries = vim.tbl_map(function(problem)
return problem.display_name
end, problems)
return fzf.fzf_exec(entries, {
prompt = ('Select Problem (%s %s)> '):format(platform_display_name, contest_id),
actions = {
['default'] = function(selected)
if not selected or #selected == 0 then
return
end
local selected_name = selected[1]
local problem = nil
for _, p in ipairs(problems) do
if p.display_name == selected_name then
problem = p
break
end
end
if problem then
picker_utils.setup_problem(platform, contest_id, problem.id)
end
end,
},
})
end
local function contest_picker(platform)
local constants = require('cp.constants')
local platform_display_name = constants.PLATFORM_DISPLAY_NAMES[platform] or platform
local fzf = require('fzf-lua')
local contests = picker_utils.get_contests_for_platform(platform)
if #contests == 0 then
vim.notify(
('No contests found for platform: %s'):format(platform_display_name),
vim.log.levels.WARN
)
return
end
local entries = vim.tbl_map(function(contest)
return contest.display_name
end, contests)
return fzf.fzf_exec(entries, {
prompt = ('Select Contest (%s)> '):format(platform_display_name),
actions = {
['default'] = function(selected)
if not selected or #selected == 0 then
return
end
local selected_name = selected[1]
local contest = nil
for _, c in ipairs(contests) do
if c.display_name == selected_name then
contest = c
break
end
end
if contest then
problem_picker(platform, contest.id)
end
end,
},
})
end
local function platform_picker()
local fzf = require('fzf-lua')
local platforms = picker_utils.get_platforms()
local entries = vim.tbl_map(function(platform)
return platform.display_name
end, platforms)
return fzf.fzf_exec(entries, {
prompt = 'Select Platform> ',
actions = {
['default'] = function(selected)
if not selected or #selected == 0 then
return
end
local selected_name = selected[1]
local platform = nil
for _, p in ipairs(platforms) do
if p.display_name == selected_name then
platform = p
break
end
end
if platform then
contest_picker(platform.id)
end
end,
},
})
end
return {
platform_picker = platform_picker,
}

145
lua/cp/pickers/init.lua Normal file
View file

@ -0,0 +1,145 @@
local M = {}
local cache = require('cp.cache')
local logger = require('cp.log')
local scrape = require('cp.scrape')
---@class cp.PlatformItem
---@field id string Platform identifier (e.g. "codeforces", "atcoder", "cses")
---@field display_name string Human-readable platform name (e.g. "Codeforces", "AtCoder", "CSES")
---@class cp.ContestItem
---@field id string Contest identifier (e.g. "1951", "abc324", "sorting")
---@field name string Full contest name (e.g. "Educational Codeforces Round 168")
---@field display_name string Formatted display name for picker
---@class cp.ProblemItem
---@field id string Problem identifier (e.g. "a", "b", "c")
---@field name string Problem name (e.g. "Two Permutations", "Painting Walls")
---@field display_name string Formatted display name for picker
---Get list of available competitive programming platforms
---@return cp.PlatformItem[]
local function get_platforms()
local constants = require('cp.constants')
return vim.tbl_map(function(platform)
return {
id = platform,
display_name = constants.PLATFORM_DISPLAY_NAMES[platform] or platform,
}
end, constants.PLATFORMS)
end
---Get list of contests for a specific platform
---@param platform string Platform identifier (e.g. "codeforces", "atcoder")
---@return cp.ContestItem[]
local function get_contests_for_platform(platform)
local contests = {}
local function get_plugin_path()
local plugin_path = debug.getinfo(1, 'S').source:sub(2)
return vim.fn.fnamemodify(plugin_path, ':h:h:h')
end
local plugin_path = get_plugin_path()
local cmd = {
'uv',
'run',
'--directory',
plugin_path,
'-m',
'scrapers.' .. platform,
'contests',
}
local result = vim
.system(cmd, {
cwd = plugin_path,
text = true,
timeout = 30000,
})
:wait()
if result.code ~= 0 then
logger.log(
('Failed to get contests for %s: %s'):format(platform, result.stderr or 'unknown error'),
vim.log.levels.ERROR
)
return contests
end
local ok, data = pcall(vim.json.decode, result.stdout)
if not ok or not data.success then
logger.log(('Failed to parse contest data for %s'):format(platform), vim.log.levels.ERROR)
return contests
end
for _, contest in ipairs(data.contests or {}) do
table.insert(contests, {
id = contest.id,
name = contest.name,
display_name = contest.display_name,
})
end
return contests
end
---Get list of problems for a specific contest
---@param platform string Platform identifier
---@param contest_id string Contest identifier
---@return cp.ProblemItem[]
local function get_problems_for_contest(platform, contest_id)
local problems = {}
cache.load()
local contest_data = cache.get_contest_data(platform, contest_id)
if contest_data and contest_data.problems then
for _, problem in ipairs(contest_data.problems) do
table.insert(problems, {
id = problem.id,
name = problem.name,
display_name = problem.name,
})
end
return problems
end
local metadata_result = scrape.scrape_contest_metadata(platform, contest_id)
if not metadata_result.success then
logger.log(
('Failed to get problems for %s %s: %s'):format(
platform,
contest_id,
metadata_result.error or 'unknown error'
),
vim.log.levels.ERROR
)
return problems
end
for _, problem in ipairs(metadata_result.problems or {}) do
table.insert(problems, {
id = problem.id,
name = problem.name,
display_name = problem.name,
})
end
return problems
end
---Set up a specific problem by calling the main CP handler
---@param platform string Platform identifier
---@param contest_id string Contest identifier
---@param problem_id string Problem identifier
local function setup_problem(platform, contest_id, problem_id)
local cp = require('cp')
cp.handle_command({ fargs = { platform, contest_id, problem_id } })
end
M.get_platforms = get_platforms
M.get_contests_for_platform = get_contests_for_platform
M.get_problems_for_contest = get_problems_for_contest
M.setup_problem = setup_problem
return M

View file

@ -0,0 +1,130 @@
local finders = require('telescope.finders')
local pickers = require('telescope.pickers')
local telescope = require('telescope')
local conf = require('telescope.config').values
local action_state = require('telescope.actions.state')
local actions = require('telescope.actions')
local picker_utils = require('cp.pickers')
local function platform_picker(opts)
opts = opts or {}
local platforms = picker_utils.get_platforms()
pickers
.new(opts, {
prompt_title = 'Select Platform',
finder = finders.new_table({
results = platforms,
entry_maker = function(entry)
return {
value = entry,
display = entry.display_name,
ordinal = entry.display_name,
}
end,
}),
sorter = conf.generic_sorter(opts),
attach_mappings = function(prompt_bufnr, map)
actions.select_default:replace(function()
local selection = action_state.get_selected_entry()
actions.close(prompt_bufnr)
if selection then
contest_picker(opts, selection.value.id)
end
end)
return true
end,
})
:find()
end
local function contest_picker(opts, platform)
local constants = require('cp.constants')
local platform_display_name = constants.PLATFORM_DISPLAY_NAMES[platform] or platform
local contests = picker_utils.get_contests_for_platform(platform)
if #contests == 0 then
vim.notify(
('No contests found for platform: %s'):format(platform_display_name),
vim.log.levels.WARN
)
return
end
pickers
.new(opts, {
prompt_title = ('Select Contest (%s)'):format(platform_display_name),
finder = finders.new_table({
results = contests,
entry_maker = function(entry)
return {
value = entry,
display = entry.display_name,
ordinal = entry.display_name,
}
end,
}),
sorter = conf.generic_sorter(opts),
attach_mappings = function(prompt_bufnr, map)
actions.select_default:replace(function()
local selection = action_state.get_selected_entry()
actions.close(prompt_bufnr)
if selection then
problem_picker(opts, platform, selection.value.id)
end
end)
return true
end,
})
:find()
end
local function problem_picker(opts, platform, contest_id)
local constants = require('cp.constants')
local platform_display_name = constants.PLATFORM_DISPLAY_NAMES[platform] or platform
local problems = picker_utils.get_problems_for_contest(platform, contest_id)
if #problems == 0 then
vim.notify(
('No problems found for contest: %s %s'):format(platform_display_name, contest_id),
vim.log.levels.WARN
)
return
end
pickers
.new(opts, {
prompt_title = ('Select Problem (%s %s)'):format(platform_display_name, contest_id),
finder = finders.new_table({
results = problems,
entry_maker = function(entry)
return {
value = entry,
display = entry.display_name,
ordinal = entry.display_name,
}
end,
}),
sorter = conf.generic_sorter(opts),
attach_mappings = function(prompt_bufnr, map)
actions.select_default:replace(function()
local selection = action_state.get_selected_entry()
actions.close(prompt_bufnr)
if selection then
picker_utils.setup_problem(platform, contest_id, selection.value.id)
end
end)
return true
end,
})
:find()
end
return {
platform_picker = platform_picker,
}