feat: nest all config in vim.g

This commit is contained in:
Barrett Ruth 2025-09-14 00:02:31 -05:00
parent b5b55afece
commit 2214c510a6
3 changed files with 33 additions and 66 deletions

View file

@ -64,18 +64,11 @@ Navigation Commands ~
CONFIGURATION *cp-config* CONFIGURATION *cp-config*
cp.nvim is automatically lazy-loaded - no config/setup is required. cp.nvim works out of the box. No setup required.
Provide extra options via a setup() function with your package manager. For Optional: >
example, with lazy.nvim (https://github.com/folke/lazy.nvim): vim.g.cp = {
config = {
{
'barrett-ruth/cp.nvim',
config = function()
local ls = require('luasnip')
local s = ls.snippet
require('cp').setup({
debug = false, debug = false,
contests = { contests = {
default = { default = {
@ -84,42 +77,20 @@ example, with lazy.nvim (https://github.com/folke/lazy.nvim):
debug_flags = { "-g3", "-fsanitize=address,undefined", "-DLOCAL" }, debug_flags = { "-g3", "-fsanitize=address,undefined", "-DLOCAL" },
timeout_ms = 2000, timeout_ms = 2000,
}, },
atcoder = { atcoder = { cpp_version = 23 },
cpp_version = 23,
},
},
snippets = {
cses = {
s("cses", "#include <iostream>\nusing namespace std;\n\nint main() {\n\t$0\n}")
},
}, },
hooks = { hooks = {
before_run = function(problem_id) before_run = function(problem_id) vim.cmd.w() end,
vim.cmd.w()
vim.lsp.buf.format()
end,
before_debug = function(problem_id)
...
end
}, },
tile = function(source_buf, input_buf, output_buf) tile = function(source_buf, input_buf, output_buf)
vim.api.nvim_set_current_buf(source_buf) -- custom window layout
vim.cmd.vsplit()
vim.api.nvim_set_current_buf(output_buf)
vim.cmd.vsplit()
vim.api.nvim_set_current_buf(input_buf)
vim.cmd('wincmd h | wincmd h')
end, end,
filename = function(contest, problem_id, problem_letter) filename = function(contest, problem_id, problem_letter)
if contest == "atcoder" then -- custom filename generation
return problem_id:lower() .. (problem_letter or "") .. ".cpp"
else
return problem_id:lower() .. (problem_letter or "") .. ".cc"
end
end, end,
})
end
} }
}
<
Configuration options: Configuration options:

View file

@ -263,21 +263,15 @@ local function navigate_problem(delta)
end end
end end
local initialized = false local function ensure_initialized()
if config then
function M.is_initialized()
return initialized
end
function M.setup(user_config)
if initialized and not user_config then
return return
end end
config = config_module.setup(user_config) vim.g.cp = vim.g.cp or {}
config = config_module.setup(vim.g.cp.config)
logger.set_config(config) logger.set_config(config)
snippets.setup(config) snippets.setup(config)
initialized = true
end end
local function parse_command(args) local function parse_command(args)
@ -311,6 +305,7 @@ local function parse_command(args)
end end
function M.handle_command(opts) function M.handle_command(opts)
ensure_initialized()
local cmd = parse_command(opts.fargs) local cmd = parse_command(opts.fargs)
if cmd.type == "error" then if cmd.type == "error" then
@ -374,4 +369,8 @@ function M.handle_command(opts)
end end
end end
function M.is_initialized()
return config ~= nil
end
return M return M

View file

@ -8,9 +8,6 @@ local actions = { "run", "debug", "diff", "next", "prev" }
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")
if not cp.is_initialized() then
cp.setup()
end
cp.handle_command(opts) cp.handle_command(opts)
end, { end, {
nargs = "*", nargs = "*",