From 20b058f034dad4ae6ec62501344bbd74d7f39651 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Mon, 15 Sep 2025 12:22:07 -0400 Subject: [PATCH] better snippets, language management --- lua/cp/execute.lua | 10 +---- lua/cp/languages.lua | 19 +++++++++ lua/cp/snippets.lua | 95 ++------------------------------------------ plugin/cp.lua | 10 +---- 4 files changed, 26 insertions(+), 108 deletions(-) create mode 100644 lua/cp/languages.lua diff --git a/lua/cp/execute.lua b/lua/cp/execute.lua index 14724db..a0874f1 100644 --- a/lua/cp/execute.lua +++ b/lua/cp/execute.lua @@ -8,14 +8,8 @@ local M = {} local logger = require("cp.log") -local filetype_to_language = { - cpp = "cpp", - cxx = "cpp", - cc = "cpp", - c = "cpp", - py = "python", - py3 = "python", -} +local languages = require("cp.languages") +local filetype_to_language = languages.filetype_to_language ---@param source_file string ---@param contest_config table diff --git a/lua/cp/languages.lua b/lua/cp/languages.lua new file mode 100644 index 0000000..e811211 --- /dev/null +++ b/lua/cp/languages.lua @@ -0,0 +1,19 @@ +local M = {} + +M.CPP = "cpp" +M.PYTHON = "python" + +---@type table +M.filetype_to_language = { + cc = M.CPP, + cxx = M.CPP, + cpp = M.CPP, + c = M.CPP, + py = M.PYTHON, + py3 = M.PYTHON, +} + +---@type string[] +M.all = { M.CPP, M.PYTHON } + +return M \ No newline at end of file diff --git a/lua/cp/snippets.lua b/lua/cp/snippets.lua index 34b056e..8158a9a 100644 --- a/lua/cp/snippets.lua +++ b/lua/cp/snippets.lua @@ -10,109 +10,20 @@ function M.setup(config) local s, i, fmt = ls.snippet, ls.insert_node, require("luasnip.extras.fmt").fmt - local filetype_to_language = { - cc = "cpp", - c = "cpp", - py = "python", - py3 = "python", - } + local languages = require("cp.languages") + local filetype_to_language = languages.filetype_to_language local language_to_filetype = {} for ext, lang in pairs(filetype_to_language) do language_to_filetype[lang] = ext end - local template_definitions = { - cpp = { - codeforces = [[#include - -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 - -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 - -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 = [[{}]], - }, - } for language, filetype in pairs(language_to_filetype) do local snippets = {} - for contest, template in pairs(template_definitions[language] or {}) do - table.insert(snippets, s(contest, fmt(template, { i(1) }))) - end - for _, snippet in ipairs(config.snippets or {}) do - if snippet.filetype == filetype then - table.insert(snippets, snippet) - end + table.insert(snippets, snippet) end ls.add_snippets(filetype, snippets) diff --git a/plugin/cp.lua b/plugin/cp.lua index c272a27..a252fbf 100644 --- a/plugin/cp.lua +++ b/plugin/cp.lua @@ -13,14 +13,8 @@ end, { nargs = "*", desc = "Competitive programming helper", complete = function(ArgLead, CmdLine, _) - local filetype_to_language = { - cc = "cpp", - c = "cpp", - py = "python", - py3 = "python", - } - - local languages = vim.tbl_keys(vim.tbl_add_reverse_lookup(filetype_to_language)) + local languages_module = require("cp.languages") + local languages = languages_module.all if ArgLead:match("^--lang=") then local lang_completions = {}