From 3046a3ce366277f369295ba5bf2f9c33f63605e5 Mon Sep 17 00:00:00 2001 From: Andrew Haynes Date: Mon, 26 Oct 2026 21:11:55 -0400 Subject: [PATCH] refactor: continuation of compressing languages to be agnostic --- lua/vern-nvim/actions/c.lua | 51 +++++++++++++------------------- lua/vern-nvim/actions/cpp.lua | 31 ++++++++++++++----- lua/vern-nvim/actions/init.lua | 32 ++++++-------------- lua/vern-nvim/attach/c.lua | 10 ++----- lua/vern-nvim/attach/cpp.lua | 10 ++----- lua/vern-nvim/attach/lua.lua | 10 ++----- lua/vern-nvim/attach/python.lua | 16 ++-------- lua/vern-nvim/configs/c.lua | 2 ++ lua/vern-nvim/configs/cpp.lua | 6 ++-- lua/vern-nvim/configs/init.lua | 2 ++ lua/vern-nvim/configs/lua.lua | 1 + lua/vern-nvim/configs/python.lua | 4 ++- lua/vern-nvim/init.lua | 16 ++-------- lua/vern-nvim/types.lua | 1 + 14 files changed, 81 insertions(+), 111 deletions(-) diff --git a/lua/vern-nvim/actions/c.lua b/lua/vern-nvim/actions/c.lua index 0a67c5b..86193ae 100644 --- a/lua/vern-nvim/actions/c.lua +++ b/lua/vern-nvim/actions/c.lua @@ -1,9 +1,6 @@ local M = {} local utils = require("vern-nvim.utils") -local templates = require("vern-nvim.templates") - -local join = vim.fs.joinpath ---@param root string M.create_project = function(root) @@ -12,42 +9,36 @@ M.create_project = function(root) end ---@param opts VernCConfig -function M.create_file(opts) +M.create_file = function(opts) + opts = opts or {} vim.ui.input({ prompt = "Enter type (c/h): " }, function(ft) ft = ft and vim.trim(ft) if not ft or ft == "" then return end - local configs = require("vern-nvim.configs.c") - ---@type VernCConfig - local cfg = vim.tbl_deep_extend("force", {}, configs, opts or {}) + local mod = {} + if ft == "c" then + mod = opts.c.templates.file.source or {} + end + if ft == "h" then + mod = opts.c.templates.file.header or {} + end vim.ui.input({ prompt = "Enter file name: " }, function(fname) - local root = join(vim.fn.expand(cfg.workspace_root), cfg.c.root, utils.proj_name()) - local input_txt = fname - ft = ft and vim.trim(ft) - if not ft or ft == "" then + fname = fname and vim.trim(fname) + if not fname or fname == "" then return end - local mod - if ft == "c" then - mod = cfg.c.templates.file.source or {} - end - if ft == "h" then - mod = cfg.c.templates.file.header or {} - input_txt = fname:upper() - end - local raw_path = join(mod.path, mod.name) + + local raw_path = vim.fs.joinpath(mod.path, mod.name) local context = { project_name = utils.proj_name(), name = fname } - local path = utils.resolve_path(raw_path, cfg.c.dirs or {}, context) - local abs_file_path = vim.fs.normalize(join(root, path)) - if ft == "h" then - local abs_dir_path = vim.fs.dirname(abs_file_path) - if abs_dir_path ~= root and not vim.startswith(root, abs_dir_path) then - vim.fn.mkdir(abs_dir_path, "p") - end - end - utils.dump(abs_file_path, templates.fetch(mod.kind_path, mod.kind, input_txt)) - vim.cmd.edit(vim.fn.expand(abs_file_path)) + local path = utils.resolve_path(raw_path, opts.c.dirs or {}, context) + local root = utils.proj_root() + utils.dump( + vim.fs.joinpath(root, path), + require("vern-nvim.templates").fetch(mod.kind_path, mod.kind, fname) + ) + vim.cmd.edit(vim.fn.expand(path)) + vim.notify(fname .. " has been created!", vim.log.levels.INFO, { title = "Vern File" }) end) end) end diff --git a/lua/vern-nvim/actions/cpp.lua b/lua/vern-nvim/actions/cpp.lua index 3ea0e1d..a495146 100644 --- a/lua/vern-nvim/actions/cpp.lua +++ b/lua/vern-nvim/actions/cpp.lua @@ -8,23 +8,38 @@ M.create_project = function(root) local _ = vim.system({ "sh", "-c", build_cmd }):wait() end ----@param opts VernLanguageTemplates -M.create_file = function(opts, callback) +---@param opts VernCPPConfig +M.create_file = function(opts) + opts = opts or {} vim.ui.input({ prompt = "Enter type (c/h): " }, function(ft) ft = ft and vim.trim(ft) if not ft or ft == "" then return end - local template = {} + local mod = {} if ft == "c" then - template = opts.file.source or {} + mod = opts.cpp.templates.file.source or {} end if ft == "h" then - template = opts.file.header or {} - end - if callback then - callback(template) + mod = opts.cpp.templates.file.header or {} end + vim.ui.input({ prompt = "Enter file name: " }, function(fname) + fname = fname and vim.trim(fname) + if not fname or fname == "" then + return + end + + local raw_path = vim.fs.joinpath(mod.path, mod.name) + local context = { project_name = utils.proj_name(), name = fname } + local path = utils.resolve_path(raw_path, opts.cpp.dirs or {}, context) + local root = utils.proj_root() + utils.dump( + vim.fs.joinpath(root, path), + require("vern-nvim.templates").fetch(mod.kind_path, mod.kind, fname) + ) + vim.cmd.edit(vim.fn.expand(path)) + vim.notify(fname .. " has been created!", vim.log.levels.INFO, { title = "Vern File" }) + end) end) end diff --git a/lua/vern-nvim/actions/init.lua b/lua/vern-nvim/actions/init.lua index e7218bb..95dd0c5 100644 --- a/lua/vern-nvim/actions/init.lua +++ b/lua/vern-nvim/actions/init.lua @@ -69,31 +69,17 @@ M.create_file = function(opts) return end - local function continue_creation(mod) - mod = mod or args[ft].templates.file.source or {} - if vim.tbl_isempty(mod) then - return - end - local raw_path = vim.fs.joinpath(mod.path, mod.name) - local context = { project_name = utils.proj_name(), name = fname } - local path = utils.resolve_path(raw_path, args[ft].dirs or {}, context) - local root = utils.proj_root() - utils.dump( - vim.fs.joinpath(root, path), - templates.fetch(mod.kind_path, mod.kind, fname) - ) - vim.cmd.edit(vim.fn.expand(path)) - vim.notify( - fname .. " has been created!", - vim.log.levels.INFO, - { title = "Vern File" } - ) - end + local mod if ok and actions and type(actions.create_file) == "function" then - actions.create_file(args[ft].templates, continue_creation) - else - continue_creation(nil) + mod = actions.create_file(args[ft]) or args[ft].templates.file.source or {} end + local raw_path = vim.fs.joinpath(mod.path, mod.name) + local context = { project_name = utils.proj_name(), name = fname } + local path = utils.resolve_path(raw_path, args[ft].dirs or {}, context) + local root = utils.proj_root() + utils.dump(vim.fs.joinpath(root, path), templates.fetch(mod.kind_path, mod.kind, fname)) + vim.cmd.edit(vim.fn.expand(path)) + vim.notify(fname .. " has been created!", vim.log.levels.INFO, { title = "Vern File" }) end) end) end diff --git a/lua/vern-nvim/attach/c.lua b/lua/vern-nvim/attach/c.lua index 29adfd5..1395634 100644 --- a/lua/vern-nvim/attach/c.lua +++ b/lua/vern-nvim/attach/c.lua @@ -1,4 +1,3 @@ -local actions = require("vern-nvim.actions.c") local config = require("vern-nvim.configs.c") ---@param bufnr integer Buffer to activate on : defaults to 0 @@ -6,14 +5,11 @@ local config = require("vern-nvim.configs.c") return function(bufnr, user_opts) ---@type VernCConfig local opts = vim.tbl_deep_extend("force", {}, config, user_opts or {}) - vim.api.nvim_buf_create_user_command(bufnr, "CRun", function() - if type(actions.run) == "function" then - actions.run(opts) - end - end, { desc = "Run c project" }) - for key, def in pairs(opts.c.keybinds or {}) do if type(def.action) == "function" then + vim.api.nvim_buf_create_user_command(bufnr, def.user_command, function() + def.action(opts) + end, { nargs = "*", desc = def.desc }) vim.keymap.set("n", key, function() def.action(opts) end, { diff --git a/lua/vern-nvim/attach/cpp.lua b/lua/vern-nvim/attach/cpp.lua index 115de7a..fa69c78 100644 --- a/lua/vern-nvim/attach/cpp.lua +++ b/lua/vern-nvim/attach/cpp.lua @@ -1,4 +1,3 @@ -local actions = require("vern-nvim.actions.cpp") local config = require("vern-nvim.configs.cpp") ---@param bufnr integer Buffer to activate on : defaults to 0 @@ -6,14 +5,11 @@ local config = require("vern-nvim.configs.cpp") return function(bufnr, user_opts) ---@type VernCPPConfig local opts = vim.tbl_deep_extend("force", {}, config, user_opts or {}) - vim.api.nvim_buf_create_user_command(bufnr, "CPPRun", function() - if type(actions.run) == "function" then - actions.run(opts) - end - end, { desc = "Run cpp project" }) - for key, def in pairs(opts.cpp.keybinds or {}) do if type(def.action) == "function" then + vim.api.nvim_buf_create_user_command(bufnr, def.user_command, function() + def.action(opts) + end, { nargs = "*", desc = def.desc }) vim.keymap.set("n", key, function() def.action(opts) end, { diff --git a/lua/vern-nvim/attach/lua.lua b/lua/vern-nvim/attach/lua.lua index a33abd5..765fa11 100644 --- a/lua/vern-nvim/attach/lua.lua +++ b/lua/vern-nvim/attach/lua.lua @@ -1,4 +1,3 @@ -local actions = require("vern-nvim.actions.lua") local config = require("vern-nvim.configs.lua") ---@param bufnr integer Buffer to activate on : defaults to 0 @@ -6,14 +5,11 @@ local config = require("vern-nvim.configs.lua") return function(bufnr, user_opts) ---@type VernLuaConfig local opts = vim.tbl_deep_extend("force", {}, config, user_opts or {}) - vim.api.nvim_buf_create_user_command(bufnr, "LuaRun", function() - if type(actions.run) == "function" then - actions.run(opts) - end - end, { desc = "Run lua project" }) - for key, def in pairs(opts.lua.keybinds or {}) do if type(def.action) == "function" then + vim.api.nvim_buf_create_user_command(bufnr, def.user_command, function() + def.action(opts) + end, { nargs = "*", desc = def.desc }) vim.keymap.set("n", key, function() def.action(opts) end, { diff --git a/lua/vern-nvim/attach/python.lua b/lua/vern-nvim/attach/python.lua index 60a4cb3..994fc42 100644 --- a/lua/vern-nvim/attach/python.lua +++ b/lua/vern-nvim/attach/python.lua @@ -1,4 +1,3 @@ -local actions = require("vern-nvim.actions.python") local config = require("vern-nvim.configs.python") ---@param bufnr integer Buffer to activate on : defaults to 0 @@ -6,20 +5,11 @@ local config = require("vern-nvim.configs.python") return function(bufnr, user_opts) ---@type VernPythonConfig local opts = vim.tbl_deep_extend("force", {}, config, user_opts or {}) - vim.api.nvim_buf_create_user_command(bufnr, "Pythonrun", function() - if type(actions.run) == "function" then - actions.run(opts) - end - end, { desc = "Run python project" }) - - vim.api.nvim_buf_create_user_command(bufnr, "Pythonpip", function() - if actions.pip_install then - actions.pip_install() - end - end, { nargs = "*", desc = "Install python packages" }) - for key, def in pairs(opts.python.keybinds or {}) do if type(def.action) == "function" then + vim.api.nvim_buf_create_user_command(bufnr, def.user_command, function() + def.action(opts) + end, { nargs = "*", desc = def.desc }) vim.keymap.set("n", key, function() def.action(opts) end, { diff --git a/lua/vern-nvim/configs/c.lua b/lua/vern-nvim/configs/c.lua index 9191ab3..e53f226 100644 --- a/lua/vern-nvim/configs/c.lua +++ b/lua/vern-nvim/configs/c.lua @@ -9,10 +9,12 @@ M.c = { }, keybinds = { ["pr"] = { + user_command = "CRun", desc = "Executes c code", action = require("vern-nvim.actions.c").run, }, ["pf"] = { + user_command = "CCreateFile", desc = "Creates source/header files", action = require("vern-nvim.actions.c").create_file, }, diff --git a/lua/vern-nvim/configs/cpp.lua b/lua/vern-nvim/configs/cpp.lua index 36f56c0..77ad8a2 100644 --- a/lua/vern-nvim/configs/cpp.lua +++ b/lua/vern-nvim/configs/cpp.lua @@ -9,10 +9,12 @@ M.cpp = { }, keybinds = { ["pr"] = { + user_command = "CPPRun", desc = "Executes cpp code", action = require("vern-nvim.actions.cpp").run, }, ["pf"] = { + user_command = "CPPCreateFile", desc = "Creates source/header files", action = require("vern-nvim.actions.cpp").create_file, }, @@ -47,7 +49,7 @@ M.cpp = { }, source = { path = "{dirs.src}", - name = "{name}.c", + name = "{name}.cpp", kind = "source", kind_path = "vern-nvim.templates.cpp", }, @@ -57,7 +59,7 @@ M.cpp = { path = ".", name = ".clang-tidy", kind = "linter", - kind_path = "vern-nvim.templates.c", + kind_path = "vern-nvim.templates.cpp", enabled = false, }, }, diff --git a/lua/vern-nvim/configs/init.lua b/lua/vern-nvim/configs/init.lua index 88487ec..3ecc6bc 100644 --- a/lua/vern-nvim/configs/init.lua +++ b/lua/vern-nvim/configs/init.lua @@ -5,10 +5,12 @@ return { workspace_root = "~/personal/Projects", keybinds = { ["pc"] = { + user_command = "VernCreateProject", desc = "Creates a new project", action = actions.create_project, }, ["pf"] = { + user_command = "VernCreateFile", desc = "Creates a new project file", action = actions.create_file, }, diff --git a/lua/vern-nvim/configs/lua.lua b/lua/vern-nvim/configs/lua.lua index 6d08786..85d8bf2 100644 --- a/lua/vern-nvim/configs/lua.lua +++ b/lua/vern-nvim/configs/lua.lua @@ -8,6 +8,7 @@ M.lua = { }, keybinds = { ["pr"] = { + user_command = "LuaRun", desc = "Executes lua code", action = require("vern-nvim.actions.lua").run, }, diff --git a/lua/vern-nvim/configs/python.lua b/lua/vern-nvim/configs/python.lua index a8122ba..9925258 100644 --- a/lua/vern-nvim/configs/python.lua +++ b/lua/vern-nvim/configs/python.lua @@ -8,11 +8,13 @@ M.python = { }, keybinds = { ["pr"] = { + user_command = "Pythonrun", desc = "Executes python code", action = require("vern-nvim.actions.python").run, }, ["pi"] = { - desc = "Asks for what python package to install", + user_command = "Pythonpip", + desc = "Install python packages for environment", action = require("vern-nvim.actions.python").pip_install, }, }, diff --git a/lua/vern-nvim/init.lua b/lua/vern-nvim/init.lua index 6182e2b..edf09e2 100644 --- a/lua/vern-nvim/init.lua +++ b/lua/vern-nvim/init.lua @@ -1,7 +1,6 @@ local M = {} local configs = require("vern-nvim.configs") -local actions = require("vern-nvim.actions") ---@type VernCoreConfig M.opts = vim.deepcopy(configs) @@ -18,6 +17,9 @@ function M.setup(user_opts) for key, def in pairs(M.opts.keybinds or {}) do if type(def.action) == "function" then + vim.api.nvim_create_user_command(def.user_command, function() + def.action(M.opts) + end, { nargs = "*", desc = def.desc }) vim.keymap.set("n", key, function() def.action(M.opts) end, { @@ -27,18 +29,6 @@ function M.setup(user_opts) end end - vim.api.nvim_create_user_command("VernCreateProject", function() - if actions.create_project then - actions.create_project(M.opts) - end - end, { nargs = "*", desc = "Create a new project" }) - - vim.api.nvim_create_user_command("VernCreateFile", function() - if type(actions.create_file) == "function" then - actions.create_file(M.opts) - end - end, { nargs = "*", desc = "Creates a new file for project" }) - local group = vim.api.nvim_create_augroup("VernAutocommands", { clear = true }) vim.api.nvim_create_autocmd("FileType", { group = group, diff --git a/lua/vern-nvim/types.lua b/lua/vern-nvim/types.lua index f1549b0..6b26c61 100644 --- a/lua/vern-nvim/types.lua +++ b/lua/vern-nvim/types.lua @@ -3,6 +3,7 @@ ---@alias SplitAnchorType "right" | "bottom" ---@class VernKeybindItem +---@field user_command string ---@field desc string ---@field action function