stylua no special config;

This commit is contained in:
Barrett Ruth 2025-09-11 23:59:02 -05:00
parent 35f8727b85
commit 0636757839
7 changed files with 262 additions and 309 deletions

View file

@ -1,13 +1,13 @@
vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, { vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, {
pattern = '*/io/*.in', pattern = "*/io/*.in",
callback = function() callback = function()
vim.bo.filetype = 'cpinput' vim.bo.filetype = "cpinput"
end, end,
}) })
vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, { vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, {
pattern = '*/io/*.out', pattern = "*/io/*.out",
callback = function() callback = function()
vim.bo.filetype = 'cpoutput' vim.bo.filetype = "cpoutput"
end, end,
}) })

View file

@ -1,6 +1,6 @@
vim.opt_local.number = false vim.opt_local.number = false
vim.opt_local.relativenumber = false vim.opt_local.relativenumber = false
vim.opt_local.statuscolumn = '' vim.opt_local.statuscolumn = ""
vim.opt_local.signcolumn = 'no' vim.opt_local.signcolumn = "no"
vim.opt_local.wrap = true vim.opt_local.wrap = true
vim.opt_local.linebreak = true vim.opt_local.linebreak = true

View file

@ -1,6 +1,6 @@
vim.opt_local.number = false vim.opt_local.number = false
vim.opt_local.relativenumber = false vim.opt_local.relativenumber = false
vim.opt_local.statuscolumn = '' vim.opt_local.statuscolumn = ""
vim.opt_local.signcolumn = 'no' vim.opt_local.signcolumn = "no"
vim.opt_local.wrap = true vim.opt_local.wrap = true
vim.opt_local.linebreak = true vim.opt_local.linebreak = true

View file

@ -1,19 +1,19 @@
local M = {} local M = {}
M.defaults = { M.defaults = {
template_dir = nil, template_dir = nil,
contests = { contests = {
atcoder = { cpp_version = 23 }, atcoder = { cpp_version = 23 },
codeforces = { cpp_version = 23 }, codeforces = { cpp_version = 23 },
cses = { cpp_version = 20 }, cses = { cpp_version = 20 },
icpc = { cpp_version = 20 }, icpc = { cpp_version = 20 },
usaco = { cpp_version = 17 }, usaco = { cpp_version = 17 },
}, },
snippets = {}, snippets = {},
} }
function M.setup(user_config) function M.setup(user_config)
return vim.tbl_deep_extend('force', M.defaults, user_config or {}) return vim.tbl_deep_extend("force", M.defaults, user_config or {})
end end
return M return M

View file

@ -1,344 +1,297 @@
local config_module = require('cp.config') local config_module = require("cp.config")
local snippets = require('cp.snippets') local snippets = require("cp.snippets")
local M = {} local M = {}
local config = {} local config = {}
local function log(msg, level) local function log(msg, level)
vim.notify(('[cp.nvim]: %s'):format(msg), level or vim.log.levels.INFO) vim.notify(("[cp.nvim]: %s"):format(msg), level or vim.log.levels.INFO)
end end
local function clearcol() local function clearcol()
vim.api.nvim_set_option_value('number', false, { scope = 'local' }) vim.api.nvim_set_option_value("number", false, { scope = "local" })
vim.api.nvim_set_option_value('relativenumber', false, { scope = 'local' }) vim.api.nvim_set_option_value("relativenumber", false, { scope = "local" })
vim.api.nvim_set_option_value('statuscolumn', '', { scope = 'local' }) vim.api.nvim_set_option_value("statuscolumn", "", { scope = "local" })
vim.api.nvim_set_option_value('signcolumn', 'no', { scope = 'local' }) vim.api.nvim_set_option_value("signcolumn", "no", { scope = "local" })
vim.api.nvim_set_option_value('equalalways', false, { scope = 'global' }) vim.api.nvim_set_option_value("equalalways", false, { scope = "global" })
end end
local function get_plugin_path() local function get_plugin_path()
local plugin_path = debug.getinfo(1, 'S').source:sub(2) local plugin_path = debug.getinfo(1, "S").source:sub(2)
return vim.fn.fnamemodify(plugin_path, ':h:h:h') return vim.fn.fnamemodify(plugin_path, ":h:h:h")
end end
local function setup_python_env() local function setup_python_env()
local plugin_path = get_plugin_path() local plugin_path = get_plugin_path()
local venv_dir = plugin_path .. '/.venv' local venv_dir = plugin_path .. "/.venv"
if vim.fn.executable('uv') == 0 then if vim.fn.executable("uv") == 0 then
log( log(
'uv is not installed. Install it to enable problem scraping: https://docs.astral.sh/uv/', "uv is not installed. Install it to enable problem scraping: https://docs.astral.sh/uv/",
vim.log.levels.WARN vim.log.levels.WARN
) )
return false return false
end end
if vim.fn.isdirectory(venv_dir) == 0 then if vim.fn.isdirectory(venv_dir) == 0 then
log('setting up Python environment for scrapers...') log("setting up Python environment for scrapers...")
local result = vim.fn.system( local result = vim.fn.system(("cd %s && uv sync"):format(vim.fn.shellescape(plugin_path)))
('cd %s && uv sync'):format(vim.fn.shellescape(plugin_path)) if vim.v.shell_error ~= 0 then
) log("failed to setup Python environment: " .. result, vim.log.levels.ERROR)
if vim.v.shell_error ~= 0 then return false
log( end
'failed to setup Python environment: ' .. result, log("python environment setup complete")
vim.log.levels.ERROR end
)
return false
end
log('python environment setup complete')
end
return true return true
end end
local competition_types = { 'atcoder', 'codeforces', 'cses', 'icpc' } local competition_types = { "atcoder", "codeforces", "cses", "icpc" }
local function setup_contest(contest_type) local function setup_contest(contest_type)
if not vim.tbl_contains(competition_types, contest_type) then if not vim.tbl_contains(competition_types, contest_type) then
log( log(
('unknown contest type. Available: [%s]'):format( ("unknown contest type. Available: [%s]"):format(table.concat(competition_types, ", ")),
table.concat(competition_types, ', ') vim.log.levels.ERROR
), )
vim.log.levels.ERROR return
) end
return
end
vim.g.cp_contest = contest_type vim.g.cp_contest = contest_type
vim.fn.system(('cp -fr %s/* .'):format(config.template_dir)) vim.fn.system(("cp -fr %s/* ."):format(config.template_dir))
vim.fn.system( vim.fn.system(("make setup VERSION=%s"):format(config.contests[contest_type].cpp_version))
('make setup VERSION=%s'):format( log(("set up %s contest environment"):format(contest_type))
config.contests[contest_type].cpp_version
)
)
log(('set up %s contest environment'):format(contest_type))
end end
local function setup_problem(problem_id, problem_letter) local function setup_problem(problem_id, problem_letter)
if not vim.g.cp_contest then if not vim.g.cp_contest then
log( log("no contest mode set. run :CP <contest> first", vim.log.levels.ERROR)
'no contest mode set. run :CP <contest> first', return
vim.log.levels.ERROR end
)
return
end
if vim.g.cp_diff_mode then if vim.g.cp_diff_mode then
vim.cmd.diffoff() vim.cmd.diffoff()
if vim.g.cp_saved_session then if vim.g.cp_saved_session then
vim.fn.delete(vim.g.cp_saved_session) vim.fn.delete(vim.g.cp_saved_session)
vim.g.cp_saved_session = nil vim.g.cp_saved_session = nil
end end
if vim.g.cp_temp_output then if vim.g.cp_temp_output then
vim.fn.delete(vim.g.cp_temp_output) vim.fn.delete(vim.g.cp_temp_output)
vim.g.cp_temp_output = nil vim.g.cp_temp_output = nil
end end
vim.g.cp_diff_mode = false vim.g.cp_diff_mode = false
end end
vim.cmd.only() vim.cmd.only()
local filename, full_problem_id local filename, full_problem_id
if if (vim.g.cp_contest == "atcoder" or vim.g.cp_contest == "codeforces") and problem_letter then
(vim.g.cp_contest == 'atcoder' or vim.g.cp_contest == 'codeforces') full_problem_id = problem_id .. problem_letter
and problem_letter filename = full_problem_id .. ".cc"
then vim.fn.system(("make scrape %s %s %s"):format(vim.g.cp_contest, problem_id, problem_letter))
full_problem_id = problem_id .. problem_letter else
filename = full_problem_id .. '.cc' full_problem_id = problem_id
vim.fn.system( filename = problem_id .. ".cc"
('make scrape %s %s %s'):format( vim.fn.system(("make scrape %s %s"):format(vim.g.cp_contest, problem_id))
vim.g.cp_contest, end
problem_id,
problem_letter
)
)
else
full_problem_id = problem_id
filename = problem_id .. '.cc'
vim.fn.system(
('make scrape %s %s'):format(vim.g.cp_contest, problem_id)
)
end
vim.cmd.e(filename) vim.cmd.e(filename)
if vim.api.nvim_buf_get_lines(0, 0, -1, true)[1] == '' then if vim.api.nvim_buf_get_lines(0, 0, -1, true)[1] == "" then
vim.api.nvim_input(('i%s<c-space><esc>'):format(vim.g.cp_contest)) vim.api.nvim_input(("i%s<c-space><esc>"):format(vim.g.cp_contest))
end end
vim.api.nvim_set_option_value('winbar', '', { scope = 'local' }) vim.api.nvim_set_option_value("winbar", "", { scope = "local" })
vim.api.nvim_set_option_value('foldlevel', 0, { scope = 'local' }) vim.api.nvim_set_option_value("foldlevel", 0, { scope = "local" })
vim.api.nvim_set_option_value('foldmethod', 'marker', { scope = 'local' }) vim.api.nvim_set_option_value("foldmethod", "marker", { scope = "local" })
vim.api.nvim_set_option_value('foldmarker', '{{{,}}}', { scope = 'local' }) vim.api.nvim_set_option_value("foldmarker", "{{{,}}}", { scope = "local" })
vim.api.nvim_set_option_value('foldtext', '', { scope = 'local' }) vim.api.nvim_set_option_value("foldtext", "", { scope = "local" })
vim.diagnostic.enable(false) vim.diagnostic.enable(false)
local base_fp = vim.fn.fnamemodify(filename, ':p:h') local base_fp = vim.fn.fnamemodify(filename, ":p:h")
local input = ('%s/io/%s.in'):format(base_fp, full_problem_id) local input = ("%s/io/%s.in"):format(base_fp, full_problem_id)
local output = ('%s/io/%s.out'):format(base_fp, full_problem_id) local output = ("%s/io/%s.out"):format(base_fp, full_problem_id)
vim.cmd.vsplit(output) vim.cmd.vsplit(output)
vim.cmd.w() vim.cmd.w()
clearcol() clearcol()
vim.cmd(('vertical resize %d'):format(math.floor(vim.o.columns * 0.3))) vim.cmd(("vertical resize %d"):format(math.floor(vim.o.columns * 0.3)))
vim.cmd.split(input) vim.cmd.split(input)
vim.cmd.w() vim.cmd.w()
clearcol() clearcol()
vim.cmd.wincmd('h') vim.cmd.wincmd("h")
log(('switched to problem %s'):format(full_problem_id)) log(("switched to problem %s"):format(full_problem_id))
end end
local function get_current_problem() local function get_current_problem()
local filename = vim.fn.expand('%:t:r') local filename = vim.fn.expand("%:t:r")
if filename == '' then if filename == "" then
log('no file open', vim.log.levels.ERROR) log("no file open", vim.log.levels.ERROR)
return nil return nil
end end
return filename return filename
end end
local function run_problem() local function run_problem()
local problem_id = get_current_problem() local problem_id = get_current_problem()
if not problem_id then if not problem_id then
return return
end end
local has_lsp, lsp = pcall(require, 'lsp') local has_lsp, lsp = pcall(require, "lsp")
if has_lsp and lsp.lsp_format then if has_lsp and lsp.lsp_format then
lsp.lsp_format({ async = true }) lsp.lsp_format({ async = true })
end end
vim.system({ 'make', 'run', vim.fn.expand('%:t') }, {}, function() vim.system({ "make", "run", vim.fn.expand("%:t") }, {}, function()
vim.schedule(function() vim.schedule(function()
vim.cmd.checktime() vim.cmd.checktime()
end) end)
end) end)
end end
local function debug_problem() local function debug_problem()
local problem_id = get_current_problem() local problem_id = get_current_problem()
if not problem_id then if not problem_id then
return return
end end
local has_lsp, lsp = pcall(require, 'lsp') local has_lsp, lsp = pcall(require, "lsp")
if has_lsp and lsp.lsp_format then if has_lsp and lsp.lsp_format then
lsp.lsp_format({ async = true }) lsp.lsp_format({ async = true })
end end
vim.system({ 'make', 'debug', vim.fn.expand('%:t') }, {}, function() vim.system({ "make", "debug", vim.fn.expand("%:t") }, {}, function()
vim.schedule(function() vim.schedule(function()
vim.cmd.checktime() vim.cmd.checktime()
end) end)
end) end)
end end
local function diff_problem() local function diff_problem()
if vim.g.cp_diff_mode then if vim.g.cp_diff_mode then
vim.cmd.diffoff() vim.cmd.diffoff()
if vim.g.cp_saved_session then if vim.g.cp_saved_session then
vim.cmd(('silent! source %s'):format(vim.g.cp_saved_session)) vim.cmd(("silent! source %s"):format(vim.g.cp_saved_session))
vim.fn.delete(vim.g.cp_saved_session) vim.fn.delete(vim.g.cp_saved_session)
vim.g.cp_saved_session = nil vim.g.cp_saved_session = nil
end end
vim.g.cp_diff_mode = false vim.g.cp_diff_mode = false
log('exited diff mode') log("exited diff mode")
else else
local problem_id = get_current_problem() local problem_id = get_current_problem()
if not problem_id then if not problem_id then
return return
end end
local base_fp = vim.fn.getcwd() local base_fp = vim.fn.getcwd()
local output = ('%s/io/%s.out'):format(base_fp, problem_id) local output = ("%s/io/%s.out"):format(base_fp, problem_id)
local expected = ('%s/io/%s.expected'):format(base_fp, problem_id) local expected = ("%s/io/%s.expected"):format(base_fp, problem_id)
local input = ('%s/io/%s.in'):format(base_fp, problem_id) local input = ("%s/io/%s.in"):format(base_fp, problem_id)
if vim.fn.filereadable(expected) == 0 then if vim.fn.filereadable(expected) == 0 then
log( log(("No expected output file found: %s"):format(expected), vim.log.levels.ERROR)
('No expected output file found: %s'):format(expected), return
vim.log.levels.ERROR end
)
return
end
local temp_output = vim.fn.tempname() local temp_output = vim.fn.tempname()
vim.fn.system( vim.fn.system(("awk '/^\\[[^]]*\\]:/ {exit} {print}' %s > %s"):format(vim.fn.shellescape(output), temp_output))
("awk '/^\\[[^]]*\\]:/ {exit} {print}' %s > %s"):format(
vim.fn.shellescape(output),
temp_output
)
)
local session_file = vim.fn.tempname() .. '.vim' local session_file = vim.fn.tempname() .. ".vim"
vim.cmd(('silent! mksession! %s'):format(session_file)) vim.cmd(("silent! mksession! %s"):format(session_file))
vim.g.cp_saved_session = session_file vim.g.cp_saved_session = session_file
vim.cmd.diffoff() vim.cmd.diffoff()
vim.cmd.only() vim.cmd.only()
vim.cmd.edit(temp_output) vim.cmd.edit(temp_output)
vim.cmd.diffthis() vim.cmd.diffthis()
clearcol() clearcol()
vim.cmd.vsplit(expected) vim.cmd.vsplit(expected)
vim.cmd.diffthis() vim.cmd.diffthis()
clearcol() clearcol()
vim.cmd(('botright split %s'):format(input)) vim.cmd(("botright split %s"):format(input))
clearcol() clearcol()
vim.cmd.wincmd('k') vim.cmd.wincmd("k")
vim.g.cp_diff_mode = true vim.g.cp_diff_mode = true
vim.g.cp_temp_output = temp_output vim.g.cp_temp_output = temp_output
log('entered diff mode') log("entered diff mode")
end end
end end
local initialized = false local initialized = false
function M.setup(user_config) function M.setup(user_config)
if initialized and not user_config then if initialized and not user_config then
return return
end end
config = config_module.setup(user_config) config = config_module.setup(user_config)
local plugin_path = get_plugin_path() local plugin_path = get_plugin_path()
config.template_dir = plugin_path .. '/templates' config.template_dir = plugin_path .. "/templates"
config.snippets.path = plugin_path .. '/templates/snippets' config.snippets.path = plugin_path .. "/templates/snippets"
snippets.setup(config) snippets.setup(config)
if initialized then if initialized then
return return
end end
initialized = true initialized = true
setup_python_env() setup_python_env()
vim.api.nvim_create_user_command('CP', function(opts) vim.api.nvim_create_user_command("CP", function(opts)
local args = opts.fargs local args = opts.fargs
if #args == 0 then if #args == 0 then
log( log("Usage: :CP <contest|problem_id|run|debug|diff>", vim.log.levels.ERROR)
'Usage: :CP <contest|problem_id|run|debug|diff>', return
vim.log.levels.ERROR end
)
return
end
local cmd = args[1] local cmd = args[1]
if vim.tbl_contains(competition_types, cmd) then if vim.tbl_contains(competition_types, cmd) then
if args[2] then if args[2] then
setup_contest(cmd) setup_contest(cmd)
if (cmd == 'atcoder' or cmd == 'codeforces') and args[3] then if (cmd == "atcoder" or cmd == "codeforces") and args[3] then
setup_problem(args[2], args[3]) setup_problem(args[2], args[3])
else else
setup_problem(args[2]) setup_problem(args[2])
end end
else else
setup_contest(cmd) setup_contest(cmd)
end end
elseif cmd == 'run' then elseif cmd == "run" then
run_problem() run_problem()
elseif cmd == 'debug' then elseif cmd == "debug" then
debug_problem() debug_problem()
elseif cmd == 'diff' then elseif cmd == "diff" then
diff_problem() diff_problem()
else else
if vim.g.cp_contest then if vim.g.cp_contest then
if if (vim.g.cp_contest == "atcoder" or vim.g.cp_contest == "codeforces") and args[2] then
( setup_problem(cmd, args[2])
vim.g.cp_contest == 'atcoder' else
or vim.g.cp_contest == 'codeforces' setup_problem(cmd)
) and args[2] end
then else
setup_problem(cmd, args[2]) log("no contest mode set. run :CP <contest> first or use full command", vim.log.levels.ERROR)
else end
setup_problem(cmd) end
end end, {
else nargs = "*",
log( complete = function(ArgLead, CmdLine, ...)
'no contest mode set. run :CP <contest> first or use full command', local commands = vim.list_extend(vim.deepcopy(competition_types), { "run", "debug", "diff" })
vim.log.levels.ERROR return vim.tbl_filter(function(cmd)
) return cmd:find(ArgLead, 1, true) == 1
end end, commands)
end end,
end, { })
nargs = '*',
complete = function(ArgLead, CmdLine, ...)
local commands = vim.list_extend(
vim.deepcopy(competition_types),
{ 'run', 'debug', 'diff' }
)
return vim.tbl_filter(function(cmd)
return cmd:find(ArgLead, 1, true) == 1
end, commands)
end,
})
end end
return M return M

View file

@ -1,24 +1,24 @@
local M = {} local M = {}
function M.setup(config) function M.setup(config)
local has_luasnip, luasnip = pcall(require, 'luasnip') local has_luasnip, luasnip = pcall(require, "luasnip")
if not has_luasnip then if not has_luasnip then
return return
end end
local snippets = {} local snippets = {}
for name, snippet in pairs(config.snippets or {}) do for name, snippet in pairs(config.snippets or {}) do
if type(snippet) == 'table' and snippet.trig then if type(snippet) == "table" and snippet.trig then
table.insert(snippets, snippet) table.insert(snippets, snippet)
else else
table.insert(snippets, snippet) table.insert(snippets, snippet)
end end
end end
if #snippets > 0 then if #snippets > 0 then
luasnip.add_snippets('cpp', snippets) luasnip.add_snippets("cpp", snippets)
end end
end end
return M return M

View file

@ -1,6 +1,6 @@
if vim.g.loaded_cp then if vim.g.loaded_cp then
return return
end end
vim.g.loaded_cp = 1 vim.g.loaded_cp = 1
require('cp').setup() require("cp").setup()