refactor: realized I was heavily unoptimized.

Each language had the same boilerplate code to create new projects and
new files with minor differences between each language making them
difficult to edit and navigate.
This commit is contained in:
2026-10-26 14:13:33 -04:00
parent 774f14809d
commit 5eaa7d10a7
7 changed files with 95 additions and 215 deletions
+3 -21
View File
@@ -2,34 +2,16 @@ local M = {}
local utils = require("vern-nvim.utils")
local templates = require("vern-nvim.templates")
local action_utils = require("vern-nvim.actions.utils")
local join = vim.fs.joinpath
---@param opts VernCConfig
function M.create_project(opts)
vim.ui.input({ prompt = "Enter project name: " }, function(pname)
pname = pname and vim.trim(pname)
if not pname or pname == "" then
return
end
local configs = require("vern-nvim.configs.c")
---@type VernCConfig
local cfg = vim.tbl_deep_extend("force", {}, configs, configs, opts or {})
local root = join(vim.fn.expand(cfg.workspace_root), cfg.c.root, pname)
local bin = action_utils.create(cfg.c, pname, root)
---@param root string
M.create_project = function(root)
local build_cmd = "cd " .. root .. " && cmake -S . -B build && cmake --build build"
local _ = vim.system({ "sh", "-c", build_cmd }):wait()
utils.init_git(root, "c")
vim.cmd.edit(vim.fn.fnameescape(bin))
end)
end
---@param opts VernCConfig
function M.create_file(opts)
vim.ui.input({ prompt = "Enter type (c/h): " }, function(ft)
ft = ft and vim.trim(ft)
+9 -52
View File
@@ -1,73 +1,30 @@
local M = {}
local utils = require("vern-nvim.utils")
local templates = require("vern-nvim.templates")
local action_utils = require("vern-nvim.actions.utils")
local join = vim.fs.joinpath
---@param opts VernCPPConfig
function M.create_project(opts)
vim.ui.input({ prompt = "Enter project name: " }, function(pname)
pname = pname and vim.trim(pname)
if not pname or pname == "" then
return
end
local configs = require("vern-nvim.configs.cpp")
---@type VernCPPConfig
local cfg = vim.tbl_deep_extend("force", {}, configs, configs, opts or {})
local root = join(vim.fn.expand(cfg.workspace_root), cfg.cpp.root, pname)
local bin = action_utils.create(cfg.cpp, pname, root)
---@param root string
M.create_project = function(root)
local build_cmd = "cd " .. root .. " && cmake -S . -B build && cmake --build build"
local _ = vim.system({ "sh", "-c", build_cmd }):wait()
utils.init_git(root, "cpp")
vim.cmd.edit(vim.fn.fnameescape(bin))
end)
end
---@param opts VernCPPConfig
function M.create_file(opts)
---@param opts VernLanguageTemplates
M.create_file = function(opts, callback)
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.cpp")
---@type VernCPPConfig
local cfg = vim.tbl_deep_extend("force", {}, configs, opts or {})
vim.ui.input({ prompt = "Enter file name: " }, function(fname)
local root = join(vim.fn.expand(cfg.workspace_root), cfg.cpp.root, utils.proj_name())
local input_txt = fname
ft = ft and vim.trim(ft)
if not ft or ft == "" then
return
end
local mod
local template = {}
if ft == "c" then
mod = cfg.cpp.templates.file.source or {}
template = opts.file.source or {}
end
if ft == "h" then
mod = cfg.cpp.templates.file.header or {}
input_txt = fname:upper()
template = opts.file.header or {}
end
local raw_path = join(mod.path, mod.name)
local context = { project_name = utils.proj_name(), name = fname }
local path = utils.resolve_path(raw_path, cfg.cpp.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")
if callback then
callback(template)
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))
end)
end)
end
+63 -14
View File
@@ -1,5 +1,9 @@
local M = {}
local action_utils = require("vern-nvim.actions.utils")
local utils = require("vern-nvim.utils")
local templates = require("vern-nvim.templates")
M.create_project = function(opts)
opts = opts or {}
vim.ui.input(
@@ -7,18 +11,34 @@ M.create_project = function(opts)
function(ft)
ft = ft and vim.trim(ft)
if not ft or vim.trim(ft) == "" then
vim.notify("Unknown Filetype: " .. ft, vim.log.levels.WARN, {
title = "Vern Project",
})
return
end
local ok, actions = pcall(require, "vern-nvim.actions." .. ft)
local configs = require("vern-nvim.configs." .. ft)
local args = vim.tbl_deep_extend("force", {}, configs, opts or {})
if ok and actions and type(actions.create_project) == "function" then
actions.create_project(args)
else
vim.notify("Unknown Filetype: " .. ft, vim.log.levels.WARN, {
title = "Vern Project",
})
vim.ui.input({ prompt = "Enter project name: " }, function(pname)
pname = pname and vim.trim(pname)
if not pname or pname == "" then
return
end
local root =
vim.fs.joinpath(vim.fn.expand(args.workspace_root), args[ft].root, pname)
local bin = action_utils.create(args[ft], pname, root)
utils.init_git(root, ft)
utils.init_readme(root)
if ok and actions and type(actions.create_project) == "function" then
actions.create_project(root)
end
vim.cmd.edit(vim.fn.fnameescape(bin))
vim.notify(
"Project: " .. pname .. " has been created!",
vim.log.levels.INFO,
{ title = "Vern Project" }
)
end)
end
)
end
@@ -34,18 +54,47 @@ M.create_file = function(opts)
return
end
local ok, actions = pcall(require, "vern-nvim.actions." .. ft)
local configs = require("vern-nvim.configs." .. ft)
local args = vim.tbl_deep_extend("force", {}, configs, opts or {})
if ok and actions and type(actions.create_file) == "function" then
actions.create_file(args)
else
if not ok then
vim.notify("Unknown Filetype: " .. ft, vim.log.levels.WARN, {
title = "Vern File",
})
vim.notify("Error: " .. actions, vim.log.levels.ERROR, {
title = "Vern File",
})
return
end
local configs = require("vern-nvim.configs." .. ft)
local args = vim.tbl_deep_extend("force", {}, configs, opts or {})
vim.ui.input({ prompt = "Enter file name: " }, function(fname)
fname = fname and vim.trim(fname)
if not fname or fname == "" then
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
if ok and actions and type(actions.create_file) == "function" then
actions.create_file(args[ft].templates, continue_creation)
else
continue_creation(nil)
end
end)
end)
end
+3 -47
View File
@@ -1,54 +1,10 @@
local M = {}
local utils = require("vern-nvim.utils")
local templates = require("vern-nvim.templates")
local action_utils = require("vern-nvim.actions.utils")
local join = vim.fs.joinpath
M.create_project = function() end
---@param opts VernLuaConfig
function M.create_project(opts)
vim.ui.input({ prompt = "Enter project name: " }, function(pname)
pname = pname and vim.trim(pname)
if not pname or pname == "" then
return
end
local configs = require("vern-nvim.configs.lua")
---@type VernLuaConfig
local cfg = vim.tbl_deep_extend("force", {}, configs, configs, opts or {})
local root = join(vim.fn.expand(cfg.workspace_root), cfg.lua.root, pname)
local bin = action_utils.create(cfg.lua, pname, root)
utils.init_git(root, "lua")
vim.cmd.edit(vim.fn.fnameescape(bin))
end)
end
---@param opts VernLuaConfig
function M.create_file(opts)
opts = opts or {}
vim.ui.input({ prompt = "Enter file name: " }, function(fname)
fname = fname and vim.trim(fname)
if not fname or fname == "" then
return
end
local configs = require("vern-nvim.configs.lua")
---@type VernLuaConfig
local cfg = vim.tbl_deep_extend("force", {}, configs, opts or {})
local mod = cfg.lua.templates.file.source or {}
local raw_path = join(mod.path, mod.name)
local context = { project_name = utils.proj_name(), name = fname }
local path = utils.resolve_path(raw_path, cfg.lua.dirs or {}, context)
utils.dump(path, templates.fetch(mod.kind_path, mod.kind, fname))
vim.notify("File: " .. fname .. " has been created!")
vim.cmd.edit(vim.fn.expand(path))
end)
end
M.create_file = function() end
---@param opts VernCPPConfig Takes a table for the runtime
function M.run(opts)
@@ -56,7 +12,7 @@ function M.run(opts)
local root = utils.proj_root() or "."
local pname = vim.fs.basename(root)
local full_path = join(root, pname, "lua", pname)
local full_path = vim.fs.joinpath(root, "lua", pname)
print(full_path .. "/init.lua")
utils.term({ "lua " .. full_path .. "/init.lua" }, opts.terminal or {}, false)
+6 -51
View File
@@ -1,58 +1,13 @@
local M = {}
local utils = require("vern-nvim.utils")
local templates = require("vern-nvim.templates")
local action_utils = require("vern-nvim.actions.utils")
local join = vim.fs.joinpath
---@param opts VernPythonConfig
function M.create_project(opts)
vim.ui.input({ prompt = "Enter project name: " }, function(pname)
pname = pname and vim.trim(pname)
if not pname or pname == "" then
return
end
local configs = require("vern-nvim.configs.python")
---@type VernPythonConfig
local cfg = vim.tbl_deep_extend("force", {}, configs, opts or {})
local root = join(vim.fn.expand(cfg.workspace_root), cfg.python.root, pname)
local bin = action_utils.create(cfg.python, pname, root)
utils.init_git(root, "python")
local venv_path = root .. "/venv"
vim.fn.system("python3 -m venv " .. venv_path)
vim.notify("Project: " .. pname .. " has been created!")
vim.cmd.edit(vim.fn.fnameescape(bin))
end)
---@param root string
M.create_project = function(root)
vim.fn.system("python3 -m venv " .. root .. "/venv")
end
---@param opts VernPythonConfig
function M.create_file(opts)
opts = opts or {}
vim.ui.input({ prompt = "Enter file name: " }, function(fname)
fname = fname and vim.trim(fname)
if not fname or fname == "" then
return
end
local configs = require("vern-nvim.configs.python")
---@type VernPythonConfig
local cfg = vim.tbl_deep_extend("force", {}, configs, opts or {})
local mod = cfg.python.templates.file.source or {}
local raw_path = join(mod.path, mod.name)
local context = { project_name = utils.proj_name(), name = fname }
local path = utils.resolve_path(raw_path, cfg.python.dirs or {}, context)
utils.dump(path, templates.fetch(mod.kind_path, mod.kind, fname))
vim.notify("File: " .. fname .. " has been created!")
vim.cmd.edit(vim.fn.expand(path))
end)
end
M.create_file = function() end
---@param opts VernPythonConfig Takes a table for the runtime
function M.run(opts)
@@ -61,9 +16,9 @@ function M.run(opts)
local pname = vim.fs.basename(root)
local bin = opts.python.templates.project.primary
local exe = join(bin.path, bin.name) or "src/{project_name}/main.py"
local exe = vim.fs.joinpath(bin.path, bin.name) or "src/{project_name}/main.py"
local exe_path = utils.resolve_path(exe, opts.python.dirs, { project_name = pname })
local full_path = join(vim.fn.expand(root), exe_path)
local full_path = vim.fs.joinpath(vim.fn.expand(root), exe_path)
utils.term({ "python3 " .. full_path }, opts.terminal or {}, false)
end
-20
View File
@@ -117,26 +117,6 @@ M.languages = {
end
end,
},
lua = {
setup = function(self, user_opts)
local opts = vim.tbl_deep_extend("force", {}, configs.get("lua"), user_opts or {})
vim.api.nvim_create_user_command("LuaRun", function()
self:run()
end, {})
for key, _ in pairs(opts.keybinds) do
vim.keymap.set("n", key, ":LuaRun<CR>", { silent = true })
end
end,
run = function()
local init_lua_path = utils.get_project_root()
.. "/lua/"
.. utils.get_project_name()
.. "/init.lua"
if vim.fn.filereadable(init_lua_path) == 1 then
utils.terminal({ "lua " .. init_lua_path })
end
end,
},
}
return M
@@ -0,0 +1 @@
return "# Hello from your fellow README.md"