feat(picker): picker support
This commit is contained in:
parent
ea9883895f
commit
a33e66680b
11 changed files with 829 additions and 2 deletions
123
lua/cp/pickers/fzf_lua.lua
Normal file
123
lua/cp/pickers/fzf_lua.lua
Normal 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
145
lua/cp/pickers/init.lua
Normal 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
|
||||
130
lua/cp/pickers/telescope.lua
Normal file
130
lua/cp/pickers/telescope.lua
Normal 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,
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue