rename and simplify things

This commit is contained in:
Barrett Ruth 2025-10-05 11:59:24 -04:00
parent 794426402a
commit b68ecbbe96
16 changed files with 43 additions and 297 deletions

View file

@ -10,7 +10,6 @@ local actions = constants.ACTIONS
---@class ParsedCommand
---@field type string
---@field error string?
---@field debug? boolean
---@field action? string
---@field message? string
---@field contest? string
@ -26,22 +25,16 @@ local function parse_command(args)
}
end
local debug = vim.tbl_contains(args, '--debug')
local filtered_args = vim.tbl_filter(function(arg)
return arg ~= '--debug'
end, args)
local first = filtered_args[1]
local first = args[1]
if vim.tbl_contains(actions, first) then
if first == 'cache' then
local subcommand = filtered_args[2]
local subcommand = args[2]
if not subcommand then
return { type = 'error', message = 'cache command requires subcommand: clear' }
end
if vim.tbl_contains({ 'clear', 'read' }, subcommand) then
local platform = filtered_args[3]
local platform = args[3]
return {
type = 'cache',
subcommand = subcommand,
@ -51,26 +44,26 @@ local function parse_command(args)
return { type = 'error', message = 'unknown cache subcommand: ' .. subcommand }
end
else
return { type = 'action', action = first, debug = debug }
return { type = 'action', action = first }
end
end
if vim.tbl_contains(platforms, first) then
if #filtered_args == 1 then
if #args == 1 then
return {
type = 'error',
message = 'Too few arguments - specify a contest.',
}
elseif #filtered_args == 2 then
elseif #args == 2 then
return {
type = 'contest_setup',
platform = first,
contest = filtered_args[2],
contest = args[2],
}
elseif #filtered_args == 3 then
elseif #args == 3 then
return {
type = 'error',
message = 'Setup contests with :CP <platform> <contest_id>',
message = 'Setup contests with :CP <platform> <contest_id>.',
}
else
return { type = 'error', message = 'Too many arguments' }
@ -109,7 +102,9 @@ function M.handle_command(opts)
if cmd.action == 'interact' then
ui.toggle_interactive()
elseif cmd.action == 'run' then
ui.toggle_run_panel(cmd.debug)
ui.toggle_run_panel()
elseif cmd.action == 'debug' then
ui.toggle_run_panel({ debug = true })
elseif cmd.action == 'next' then
setup.navigate_problem(1)
elseif cmd.action == 'prev' then

View file

@ -42,7 +42,6 @@
---@field languages table<string, CpLanguage>
---@field platforms table<string, CpPlatform>
---@field hooks Hooks
---@field snippets any[]
---@field debug boolean
---@field scrapers string[]
---@field filename? fun(contest: string, contest_id: string, problem_id?: string, config: cp.Config, language?: string): string
@ -100,7 +99,6 @@ M.defaults = {
default_language = 'cpp',
},
},
snippets = {},
hooks = { before_run = nil, before_debug = nil, setup_code = nil },
debug = false,
scrapers = constants.PLATFORMS,

View file

@ -2,7 +2,7 @@ local M = {}
local utils = require('cp.utils')
local function check_required()
local function check()
vim.health.start('cp.nvim [required] ~')
if vim.fn.has('nvim-0.10.0') == 1 then
@ -49,24 +49,12 @@ local function check_required()
end
end
local function check_optional()
vim.health.start('cp.nvim [optional] ~')
local has_luasnip = pcall(require, 'luasnip')
if has_luasnip then
vim.health.ok('LuaSnip integration available')
else
vim.health.info('LuaSnip not available (templates optional)')
end
end
function M.check()
local version = require('cp.version')
vim.health.start('cp.nvim health check ~')
vim.health.info('Version: ' .. version.version)
check_required()
check_optional()
check()
end
return M

View file

@ -2,7 +2,6 @@ local M = {}
local config_module = require('cp.config')
local logger = require('cp.log')
local snippets = require('cp.snippets')
if vim.fn.has('nvim-0.10.0') == 0 then
logger.log('Requires nvim-0.10.0+', vim.log.levels.ERROR)
@ -11,7 +10,6 @@ end
local user_config = {}
local config = nil
local snippets_initialized = false
local initialized = false
--- Root handler for all `:CP ...` commands
@ -27,10 +25,6 @@ function M.setup(opts)
config = config_module.setup(user_config)
config_module.set_current_config(config)
if not snippets_initialized then
snippets.setup(config)
snippets_initialized = true
end
initialized = true
end

View file

@ -107,26 +107,6 @@ function M.setup_problem(problem_id, language)
local lang = language or config.platforms[platform].default_language
local source_file = state.get_source_file(lang)
vim.cmd.e(source_file)
local source_buf = vim.api.nvim_get_current_buf()
if vim.api.nvim_buf_get_lines(source_buf, 0, -1, true)[1] == '' then
local ok, luasnip = pcall(require, 'luasnip')
if ok then
local trigger = ('cp.nvim/%s.%s'):format(platform, lang)
vim.api.nvim_buf_set_lines(0, 0, -1, false, { trigger })
vim.api.nvim_win_set_cursor(0, { 1, #trigger })
vim.cmd.startinsert({ bang = true })
vim.schedule(function()
if luasnip.expandable() then
luasnip.expand()
else
vim.api.nvim_buf_set_lines(0, 0, 1, false, { '' })
vim.api.nvim_win_set_cursor(0, { 1, 0 })
end
vim.cmd.stopinsert()
end)
end
end
if config.hooks and config.hooks.setup_code then
config.hooks.setup_code(state)

View file

@ -1,134 +0,0 @@
local M = {}
local logger = require('cp.log')
function M.setup(config)
local ok, ls = pcall(require, 'luasnip')
if not ok then
logger.log('LuaSnip not available - snippets are disabled.', vim.log.levels.INFO, true)
return
end
local s, i, fmt = ls.snippet, ls.insert_node, require('luasnip.extras.fmt').fmt
local constants = require('cp.constants')
local filetype_to_language = constants.filetype_to_language
local language_to_filetype = {}
for ext, lang in pairs(filetype_to_language) do
if not language_to_filetype[lang] then
language_to_filetype[lang] = ext
end
end
local template_definitions = {
cpp = {
codeforces = [[#include <bits/stdc++.h>
using namespace std;
void solve() {{
{}
}}
int main() {{
std::cin.tie(nullptr)->sync_with_stdio(false);
int tc = 1;
std::cin >> tc;
for (int t = 0; t < tc; ++t) {{
solve();
}}
return 0;
}}]],
atcoder = [[#include <bits/stdc++.h>
using namespace std;
void solve() {{
{}
}}
int main() {{
std::cin.tie(nullptr)->sync_with_stdio(false);
#ifdef LOCAL
int tc;
std::cin >> tc;
for (int t = 0; t < tc; ++t) {{
solve();
}}
#else
solve();
#endif
return 0;
}}]],
cses = [[#include <bits/stdc++.h>
using namespace std;
int main() {{
std::cin.tie(nullptr)->sync_with_stdio(false);
{}
return 0;
}}]],
},
python = {
codeforces = [[def solve():
{}
if __name__ == "__main__":
tc = int(input())
for _ in range(tc):
solve()]],
atcoder = [[def solve():
{}
if __name__ == "__main__":
solve()]],
cses = [[def solve():
{}
if __name__ == "__main__":
solve()]],
},
}
local user_overrides = {}
for _, snippet in ipairs(config.snippets or {}) do
user_overrides[snippet.trigger:lower()] = snippet
end
for language, template_set in pairs(template_definitions) do
local snippets = {}
local filetype = constants.canonical_filetypes[language]
for contest, template in pairs(template_set) do
local prefixed_trigger = ('cp.nvim/%s.%s'):format(contest:lower(), language)
if not user_overrides[prefixed_trigger:lower()] then
table.insert(snippets, s(prefixed_trigger, fmt(template, { i(1) })))
end
end
for trigger, snippet in pairs(user_overrides) do
local prefix_match = trigger:lower():match('^cp%.nvim/[^.]+%.(.+)$')
if prefix_match == language then
table.insert(snippets, snippet)
end
end
ls.add_snippets(filetype, snippets)
end
end
return M

View file

@ -16,8 +16,8 @@ local function create_none_diff_layout(parent_win, expected_content, actual_cont
local expected_win = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(expected_win, expected_buf)
vim.api.nvim_set_option_value('filetype', 'cptest', { buf = expected_buf })
vim.api.nvim_set_option_value('filetype', 'cptest', { buf = actual_buf })
vim.api.nvim_set_option_value('filetype', 'cp', { buf = expected_buf })
vim.api.nvim_set_option_value('filetype', 'cp', { buf = actual_buf })
vim.api.nvim_set_option_value('winbar', 'Expected', { win = expected_win })
vim.api.nvim_set_option_value('winbar', 'Actual', { win = actual_win })
@ -53,8 +53,8 @@ local function create_vim_diff_layout(parent_win, expected_content, actual_conte
local expected_win = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(expected_win, expected_buf)
vim.api.nvim_set_option_value('filetype', 'cptest', { buf = expected_buf })
vim.api.nvim_set_option_value('filetype', 'cptest', { buf = actual_buf })
vim.api.nvim_set_option_value('filetype', 'cp', { buf = expected_buf })
vim.api.nvim_set_option_value('filetype', 'cp', { buf = actual_buf })
vim.api.nvim_set_option_value('winbar', 'Expected', { win = expected_win })
vim.api.nvim_set_option_value('winbar', 'Actual', { win = actual_win })
@ -96,7 +96,7 @@ local function create_git_diff_layout(parent_win, expected_content, actual_conte
local diff_win = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(diff_win, diff_buf)
vim.api.nvim_set_option_value('filetype', 'cptest', { buf = diff_buf })
vim.api.nvim_set_option_value('filetype', 'cp', { buf = diff_buf })
vim.api.nvim_set_option_value('winbar', 'Expected vs Actual', { win = diff_win })
local diff_backend = require('cp.ui.diff')
@ -132,7 +132,7 @@ local function create_single_layout(parent_win, content)
vim.cmd('resize ' .. math.floor(vim.o.lines * 0.35))
local win = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(win, buf)
vim.api.nvim_set_option_value('filetype', 'cptest', { buf = buf })
vim.api.nvim_set_option_value('filetype', 'cp', { buf = buf })
return {
buffers = { buf },

View file

@ -1,5 +1,8 @@
local M = {}
---@class RunOpts
---@field debug? boolean
local config_module = require('cp.config')
local layouts = require('cp.ui.layouts')
local logger = require('cp.log')
@ -51,19 +54,13 @@ function M.toggle_interactive()
local platform, contest_id = state.get_platform(), state.get_contest_id()
if not platform then
logger.log(
'No platform configured. Use :CP <platform> <contest> [--{lang=<lang>,debug}] first.',
vim.log.levels.ERROR
)
logger.log('No platform configured.', vim.log.levels.ERROR)
return
end
if not contest_id then
logger.log(
('No contest %s configured for platform %s. Use :CP <platform> <contest> [--{lang=<lang>,debug}] to set up first.'):format(
contest_id,
platform
),
('No contest %s configured for platform %s.'):format(contest_id, platform),
vim.log.levels.ERROR
)
return
@ -118,8 +115,8 @@ function M.toggle_interactive()
state.set_active_panel('interactive')
end
---@param debug? boolean
function M.toggle_run_panel(debug)
---@param run_opts? RunOpts
function M.toggle_run_panel(run_opts)
if state.get_active_panel() == 'run' then
if current_diff_layout then
current_diff_layout.cleanup()
@ -152,10 +149,7 @@ function M.toggle_run_panel(debug)
if not contest_id then
logger.log(
('No contest %s configured for platform %s. Use :CP <platform> <contest> [--{lang=<lang>,debug}] to set up first.'):format(
contest_id,
platform
),
('No contest %s configured for platform %s.'):format(contest_id, platform),
vim.log.levels.ERROR
)
return
@ -187,13 +181,6 @@ function M.toggle_run_panel(debug)
)
local config = config_module.get_config()
if config.hooks and config.hooks.before_run then
config.hooks.before_run(state)
end
if debug and config.hooks and config.hooks.before_debug then
config.hooks.before_debug(state)
end
local run = require('cp.runner.run')
local input_file = state.get_input_file()
logger.log(('run panel: checking test cases for %s'):format(input_file or 'none'))
@ -210,7 +197,7 @@ function M.toggle_run_panel(debug)
local tab_buf = utils.create_buffer_with_options()
local main_win = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(main_win, tab_buf)
vim.api.nvim_set_option_value('filetype', 'cptest', { buf = tab_buf })
vim.api.nvim_set_option_value('filetype', 'cp', { buf = tab_buf })
local test_windows = { tab_win = main_win }
local test_buffers = { tab_buf = tab_buf }
@ -282,6 +269,13 @@ function M.toggle_run_panel(debug)
setup_keybindings_for_buffer(test_buffers.tab_buf)
if config.hooks and config.hooks.before_run then
config.hooks.before_run(state)
end
if run_opts.debug and config.hooks and config.hooks.before_debug then
config.hooks.before_debug(state)
end
local execute = require('cp.runner.execute')
local compile_result = execute.compile_problem()
if compile_result.success then