From 98bd619cfa2759ab4aac19317403b13bcdd28b15 Mon Sep 17 00:00:00 2001 From: Andrew Haynes Date: Thu, 15 Oct 2026 23:32:26 -0400 Subject: [PATCH] REFACTOR: Merged templates into a per language file, deleted init.lua as it was just a middle man and wasnt needed (in fact it is better without it) FT: Changes to python --- lua/vern/actions/init.lua | 16 ++++++++ lua/vern/actions/python.lua | 3 ++ lua/vern/attach/init.lua | 25 ------------ lua/vern/configs/default.lua | 11 +++++ lua/vern/configs/python.lua | 10 ++--- lua/vern/init.lua | 41 +++++++++++++++++-- lua/vern/templates/extras/gitignore.lua | 12 +++--- lua/vern/templates/python/__init__.lua | 13 ------ lua/vern/templates/python/init.lua | 53 +++++++++++++++++++++++++ lua/vern/templates/python/main.lua | 15 ------- lua/vern/templates/python/module.lua | 13 ------ lua/vern/templates/python/pyproject.lua | 25 ------------ 12 files changed, 131 insertions(+), 106 deletions(-) create mode 100644 lua/vern/actions/init.lua delete mode 100644 lua/vern/attach/init.lua delete mode 100644 lua/vern/templates/python/__init__.lua create mode 100644 lua/vern/templates/python/init.lua delete mode 100644 lua/vern/templates/python/main.lua delete mode 100644 lua/vern/templates/python/module.lua delete mode 100644 lua/vern/templates/python/pyproject.lua diff --git a/lua/vern/actions/init.lua b/lua/vern/actions/init.lua new file mode 100644 index 0000000..1fb5c2e --- /dev/null +++ b/lua/vern/actions/init.lua @@ -0,0 +1,16 @@ +local M = {} + +M.create_project = function(opts, args) + opts = opts or {} + local test = nil + if opts and opts.terminal then + test = opts.terminal.mode + end + if args and args.args and args ~= "" then + print(string.format("Opts: %s, Args: %s", test, args.args)) + return + end +end +M.create_file = function(opts, args) end + +return M diff --git a/lua/vern/actions/python.lua b/lua/vern/actions/python.lua index 33bc89d..ec52308 100644 --- a/lua/vern/actions/python.lua +++ b/lua/vern/actions/python.lua @@ -3,6 +3,9 @@ local M = {} local utils = require("vern.utils") M.default_target_file = "main.py" +function M.create_project(opts) end +function M.create_file(opts) end + --- Goal is to add providers for uv and venv so its available to more users ---@param opts? table Takes a table for the runtime function M.run(opts) diff --git a/lua/vern/attach/init.lua b/lua/vern/attach/init.lua deleted file mode 100644 index a6bdcd6..0000000 --- a/lua/vern/attach/init.lua +++ /dev/null @@ -1,25 +0,0 @@ -local M = {} - -function M.buf(bufnr, user_opts) - bufnr = (bufnr == nil or bufnr == 0) and vim.api.nvim_get_current_buf() - or bufnr - local ft = vim.bo[bufnr].filetype - if not ft or ft == "" then - return - end - - user_opts = user_opts or {} - - local default = require("vern.configs.default") - - local lang_opts = (user_opts or {})[ft] or {} - local final_opts = - vim.tbl_deep_extend("force", {}, default, user_opts, lang_opts) - final_opts[ft] = nil - local ok, lang_attach = pcall(require, "vern.attach." .. ft) --- like a try / catch block in c++ - if ok and type(lang_attach) == "function" then - lang_attach(bufnr, final_opts) - end -end - -return M diff --git a/lua/vern/configs/default.lua b/lua/vern/configs/default.lua index 8bd048c..cdb1fe7 100644 --- a/lua/vern/configs/default.lua +++ b/lua/vern/configs/default.lua @@ -1,5 +1,16 @@ +local actions = require("vern.actions") return { workspace_root = "~/personal/Projects", + keybinds = { + ["pc"] = { + desc = "Creates a new project", + action = actions.create_project, + }, + ["pf"] = { + desc = "Creates a new project file", + action = actions.create_file, + }, + }, terminal = { mode = "split", win = { diff --git a/lua/vern/configs/python.lua b/lua/vern/configs/python.lua index 1d64bc9..ead7035 100644 --- a/lua/vern/configs/python.lua +++ b/lua/vern/configs/python.lua @@ -15,27 +15,27 @@ return { templates = { project = { ["executable"] = { - template = "vern.templates.python.main", + template = { "vern.templates.python", "main" }, target = "src/%s/main.py", }, ["init"] = { - template = "vern.templates.python.__init__", + template = { "vern.templates.python", "__init__" }, target = "src/%s/__init__.py", }, ["manifest"] = { - template = "vern.templates.python.pyproject", + template = { "vern.templates.python", "pyproject" }, target = "pyproject.toml", }, }, file = { ["module"] = { - template = "vern.templates.python.module", + template = { "vern.templates.python", "module" }, target = "src/%s.py", }, }, extras = { ["gitignore"] = { - template = "vern.templates.extras.gitignore", + template = "vern.templates.extras.gitignore", --- needs to call the function in file target = ".gitignore", }, }, diff --git a/lua/vern/init.lua b/lua/vern/init.lua index 6614192..2f8ef16 100644 --- a/lua/vern/init.lua +++ b/lua/vern/init.lua @@ -1,9 +1,38 @@ local M = {} -M.user_opts = {} +local global_opts = require("vern.configs.default") +local actions = require("vern.actions") +M.opts = {} -function M.setup(opts) - M.user_opts = opts or {} +function M.setup(user_opts) + M.opts = vim.tbl_deep_extend("force", {}, global_opts, user_opts or {}) + + --- Bind global keybinds + for key, def in pairs(M.opts.keybinds or {}) do + if type(def.action) == "function" then + vim.keymap.set("n", key, function() + def.action(M.opts) + end, { + silent = true, + desc = def.desc, + }) + end + end + + --- Load project cmdline commands + vim.api.nvim_create_user_command("VernCreateProject", function(args) + if actions.create_project then + actions.create_project(M.opts, args) + end + end, { nargs = "*", desc = "Create a new project" }) + + vim.api.nvim_create_user_command("VernCreateFile", function(args) + if type(actions.create_file) == "function" then + actions.create_file(M.opts, args) + end + end, { nargs = "*", desc = "Creates a new file for project" }) + + --- Create group and attach based on ft local group = vim.api.nvim_create_augroup("VernAutocommands", { clear = true }) @@ -11,7 +40,11 @@ function M.setup(opts) group = group, pattern = "*", callback = function(args) - require("vern.attach").buf(args.buf, M.user_opts) + local ft = vim.bo[args.buf].filetype + local ok, attach = pcall(require, "vern.attach." .. ft) + if ok and type(attach) == "function" then + attach(args.buf, M.user_opts) + end end, desc = "Attach languages to buffer", }) diff --git a/lua/vern/templates/extras/gitignore.lua b/lua/vern/templates/extras/gitignore.lua index c71d152..e1bd85f 100644 --- a/lua/vern/templates/extras/gitignore.lua +++ b/lua/vern/templates/extras/gitignore.lua @@ -2,7 +2,7 @@ local M = {} ---@param opts table M.template = function(opts) - local base = [=[ + local base = [=[ .DS_Store .vscode/ @@ -12,8 +12,8 @@ M.template = function(opts) .env.* ]=] - local lang_block = { - python = [=[ + local lang_block = { + python = [=[ __pycache__/ build/ dist/ @@ -22,10 +22,10 @@ env/ venv/ .pytest_cache/ ]=], - } + } - local extra = (opts and opts and lang_block[opts.lang]) or "" - return base .. extra + local extra = (opts and opts and lang_block[opts.lang]) or "" + return base .. extra end return M diff --git a/lua/vern/templates/python/__init__.lua b/lua/vern/templates/python/__init__.lua deleted file mode 100644 index b9eac27..0000000 --- a/lua/vern/templates/python/__init__.lua +++ /dev/null @@ -1,13 +0,0 @@ -local M = {} - ----@param opts table -M.template = function(opts) - local template = [=[ -# %s - -__all__: list[str] = [] -]=] - return string.format(template, opts.project_name) -end - -return M diff --git a/lua/vern/templates/python/init.lua b/lua/vern/templates/python/init.lua new file mode 100644 index 0000000..8d8dd67 --- /dev/null +++ b/lua/vern/templates/python/init.lua @@ -0,0 +1,53 @@ +local M = {} + +M.main = function(opts) + local template = [=[ +def main() -> None: + printf("Hello from %s!") + +if __name__ == "__main__": + main() +]=] + return string.format(template, opts.project_name) +end + +M.__init__ = function(opts) + local template = [=[ +# %s + +__all__: list[str] = [] +]=] + return string.format(template, opts.project_name) +end + +M.pyproject = function(opts) + local n = opts.project_name + local template = [=[ +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[project] +name = "%s" +version = "0.1.0" +description = "Project %s" +readme = "README.md" +requires-python = ">=3.10" +dependencies = [] + +[project.scripts] +%s = "%s.main:main" +]=] + return string.format(template, n, n, n, n) +end + +M.module = function(opts) + local template = [=[ +""" +%s module +""" +]=] + return string.format(template, opts.project_name) +end + +return M diff --git a/lua/vern/templates/python/main.lua b/lua/vern/templates/python/main.lua deleted file mode 100644 index 41c2d19..0000000 --- a/lua/vern/templates/python/main.lua +++ /dev/null @@ -1,15 +0,0 @@ -local M = {} - ----@param opts table -M.template = function(opts) - local template = [=[ -def main() -> None: - printf("Hello from %s!") - -if __name__ == "__main__": - main() -]=] - return string.format(template, opts.project_name) -end - -return M diff --git a/lua/vern/templates/python/module.lua b/lua/vern/templates/python/module.lua deleted file mode 100644 index 752af9c..0000000 --- a/lua/vern/templates/python/module.lua +++ /dev/null @@ -1,13 +0,0 @@ -local M = {} - ----@param opts table -M.template = function(opts) - local template = [=[ -""" -%s module -""" -]=] - return string.format(template, opts.project_name) -end - -return M diff --git a/lua/vern/templates/python/pyproject.lua b/lua/vern/templates/python/pyproject.lua deleted file mode 100644 index 20a3f44..0000000 --- a/lua/vern/templates/python/pyproject.lua +++ /dev/null @@ -1,25 +0,0 @@ -local M = {} - ----@param opts table -M.template = function(opts) - local n = opts.project_name - local template = [=[ -[build-system] -requires = ["hatchling"] -build-backend = "hatchling.build" - -[project] -name = "%s" -version = "0.1.0" -description = "Project %s" -readme = "README.md" -requires-python = ">=3.10" -dependencies = [] - -[project.scripts] -%s = "%s.main:main" -]=] - return string.format(template, n, n, n, n) -end - -return M