From b6d46a0ceffcb9aadc837c6d8e1eb42e5342ee69 Mon Sep 17 00:00:00 2001 From: Andrew Haynes Date: Thu, 22 Oct 2026 14:36:19 -0400 Subject: [PATCH] Chore: moved similar functions to a utils for actions so they dont repeat and copy costantly. Going to have to think of a better way that consolidates it all as a lot of it overlaps --- lua/vern/actions/c.lua | 33 ++------------------------ lua/vern/actions/python.lua | 32 +++----------------------- lua/vern/actions/utils.lua | 46 +++++++++++++++++++++++++++++++++++++ lua/vern/attach/c.lua | 21 ----------------- lua/vern/init.lua | 7 +++++- 5 files changed, 57 insertions(+), 82 deletions(-) create mode 100644 lua/vern/actions/utils.lua diff --git a/lua/vern/actions/c.lua b/lua/vern/actions/c.lua index a14771d..ff6bf3e 100644 --- a/lua/vern/actions/c.lua +++ b/lua/vern/actions/c.lua @@ -2,6 +2,7 @@ local M = {} local utils = require("vern.utils") local templates = require("vern.templates") +local action_utils = require("vern.actions.utils") local join = vim.fs.joinpath @@ -17,38 +18,8 @@ function M.create_project(opts) ---@type VernCConfig local cfg = vim.tbl_deep_extend("force", {}, configs, configs, opts or {}) - local ctx = { project_name = pname } - local root = join(vim.fn.expand(cfg.workspace_root), cfg.c.root, pname) - vim.fn.mkdir(root, "p") - local bin - for tbl_name, tbl in pairs(cfg.c.templates.project) do - local raw_path = join(tbl.path, tbl.name) - local path = utils.resolve_path(raw_path, cfg.c.dirs, ctx) - if tbl_name == "primary" then - bin = join(root, path) - end - local abs_file_path = vim.fs.normalize(join(root, path)) - print("File Path: " .. abs_file_path) - 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 - utils.dump(abs_file_path, templates.fetch(tbl.kind_path, tbl.kind, pname)) - end - for _, tbl in pairs(cfg.c.templates.extras) do - if tbl.enabled then - local raw_path = join(tbl.path, tbl.name) - local path = utils.resolve_path(raw_path, cfg.c.dirs, ctx) - local abs_file_path = vim.fs.normalize(join(root, path)) - 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 - utils.dump(abs_file_path, templates.fetch(tbl.kind_path, tbl.kind, pname)) - end - end - + local bin = action_utils.create(cfg.c, pname, root) utils.init_git(root, "c") vim.cmd.edit(vim.fn.fnameescape(bin)) diff --git a/lua/vern/actions/python.lua b/lua/vern/actions/python.lua index 2b611c6..5eff52c 100644 --- a/lua/vern/actions/python.lua +++ b/lua/vern/actions/python.lua @@ -2,6 +2,7 @@ local M = {} local utils = require("vern.utils") local templates = require("vern.templates") +local action_utils = require("vern.actions.utils") local join = vim.fs.joinpath @@ -14,38 +15,11 @@ function M.create_project(opts) end local configs = require("vern.configs.python") + ---@type VernPythonConfig local cfg = vim.tbl_deep_extend("force", {}, configs, opts or {}) - local ctx = { project_name = pname } - local root = join(vim.fn.expand(cfg.workspace_root), cfg.python.root, pname) - vim.fn.mkdir(root, "p") - local bin - for tbl_name, tbl in pairs(cfg.python.templates.project) do - local raw_path = join(tbl.path, tbl.name) - local path = utils.resolve_path(raw_path, cfg.python.dirs, ctx) - if tbl_name == "primary" then - bin = join(root, path) - end - local abs_file_path = join(root, path) - 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 - utils.dump(abs_file_path, templates.fetch(tbl.kind_path, tbl.kind, pname)) - end - for _, tbl in pairs(cfg.python.templates.extras) do - if tbl.enabled then - local raw_path = join(tbl.path, tbl.name) - local path = utils.resolve_path(raw_path, cfg.python.dirs, ctx) - local abs_file_path = join(root, path) - 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 - utils.dump(abs_file_path, templates.fetch(tbl.kind_path, tbl.kind, pname)) - end - end + local bin = action_utils.create(cfg.python, pname, root) utils.init_git(root, "python") diff --git a/lua/vern/actions/utils.lua b/lua/vern/actions/utils.lua new file mode 100644 index 0000000..39b24a3 --- /dev/null +++ b/lua/vern/actions/utils.lua @@ -0,0 +1,46 @@ +local M = {} + +local utils = require("vern.utils") +local templates = require("vern.templates") + +local join = vim.fs.joinpath + +---@param cfg table Language config +---@param pname string Name of project +---@param root string Project location +---@return string bin Location of executable +function M.create(cfg, pname, root) + local ctx = { project_name = pname } + + vim.fn.mkdir(root, "p") + local bin + for tbl_name, tbl in pairs(cfg.templates.project) do + local raw_path = join(tbl.path, tbl.name) + local path = utils.resolve_path(raw_path, cfg.dirs, ctx) + if tbl_name == "primary" then + bin = join(root, path) + end + local abs_file_path = vim.fs.normalize(join(root, path)) + 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 + utils.dump(abs_file_path, templates.fetch(tbl.kind_path, tbl.kind, pname)) + end + for _, tbl in pairs(cfg.templates.extras) do + if tbl.enabled then + local raw_path = join(tbl.path, tbl.name) + local path = utils.resolve_path(raw_path, cfg.dirs, ctx) + local abs_file_path = vim.fs.normalize(join(root, path)) + 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 + utils.dump(abs_file_path, templates.fetch(tbl.kind_path, tbl.kind, pname)) + end + end + + return bin +end + +return M diff --git a/lua/vern/attach/c.lua b/lua/vern/attach/c.lua index 10a42d2..97b3d55 100644 --- a/lua/vern/attach/c.lua +++ b/lua/vern/attach/c.lua @@ -12,12 +12,6 @@ return function(bufnr, user_opts) end end, { desc = "Run c project" }) - -- vim.api.nvim_buf_create_user_command(bufnr, "Pythonpip", function(cmd_args) - -- 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.keymap.set("n", key, function() @@ -29,19 +23,4 @@ return function(bufnr, user_opts) }) end end - - local root = require("vern.utils").proj_root() - if not root then - return - end - local venv_dir = nil - if vim.uv.fs_stat(root .. "/venv/bin/python") then - venv_dir = root .. "/venv" - elseif vim.uv.fs_stat(root .. "/.venv/bin/python") then - venv_dir = root .. "/.venv" - end - if venv_dir then - vim.env.VIRTUAL_ENV = venv_dir - vim.env.PATH = venv_dir .. "/bin:" .. vim.env.PATH - end end diff --git a/lua/vern/init.lua b/lua/vern/init.lua index e1324b5..52c7747 100644 --- a/lua/vern/init.lua +++ b/lua/vern/init.lua @@ -6,7 +6,12 @@ local actions = require("vern.actions") ---@type VernCoreConfig M.opts = vim.deepcopy(configs) ----@param user_opts? VernCoreConfig | table +---@class VernUserConfig +---@field workspace_root? string +---@field keybinds? table +---@field terminal? VernTerminalConfig | table + +---@param user_opts? VernUserConfig function M.setup(user_opts) ---@type VernCoreConfig M.opts = vim.tbl_deep_extend("force", {}, configs, user_opts or {})