From ae0df7fe288e2503ddb9785c98406048b6e75c74 Mon Sep 17 00:00:00 2001 From: Andrew Haynes Date: Sat, 31 Oct 2026 01:34:14 -0400 Subject: [PATCH] refactor(actions): deleted actions files for language types and fixed them --- lua/vern-nvim/actions.lua | 14 +++-- lua/vern-nvim/actions/c.lua | 45 --------------- lua/vern-nvim/actions/cpp.lua | 45 --------------- lua/vern-nvim/actions/java.lua | 58 ------------------- lua/vern-nvim/actions/lua.lua | 19 ------- lua/vern-nvim/actions/python.lua | 60 -------------------- lua/vern-nvim/actions/rust.lua | 15 ----- lua/vern-nvim/configs/c.lua | 49 +++++++++------- lua/vern-nvim/configs/cpp.lua | 49 +++++++++------- lua/vern-nvim/configs/java.lua | 47 ++++++++++++--- lua/vern-nvim/configs/lua.lua | 29 +++++++--- lua/vern-nvim/configs/python.lua | 66 ++++++++++++++++++++-- lua/vern-nvim/configs/rust.lua | 23 +++++--- lua/vern-nvim/init.lua | 2 +- lua/vern-nvim/languages/shared/compile.lua | 31 ++++++++++ lua/vern-nvim/types.lua | 5 +- 16 files changed, 240 insertions(+), 317 deletions(-) delete mode 100644 lua/vern-nvim/actions/c.lua delete mode 100644 lua/vern-nvim/actions/cpp.lua delete mode 100644 lua/vern-nvim/actions/java.lua delete mode 100644 lua/vern-nvim/actions/lua.lua delete mode 100644 lua/vern-nvim/actions/python.lua delete mode 100644 lua/vern-nvim/actions/rust.lua create mode 100644 lua/vern-nvim/languages/shared/compile.lua diff --git a/lua/vern-nvim/actions.lua b/lua/vern-nvim/actions.lua index e5cf9cc..cff2a2a 100644 --- a/lua/vern-nvim/actions.lua +++ b/lua/vern-nvim/actions.lua @@ -166,8 +166,8 @@ M.create_file = function(opts) function_write(mod, fname) end - if cf and type(cf.func) == "function" and cf.allowed then - cf.func(args, done) + if cf and type(cf) == "function" then + cf(args, done) else done(args[filetype].templates.file.source) end @@ -179,7 +179,7 @@ M.run = function(opts) opts = opts or {} local filetype = vim.bo.filetype - local ft_ok, action = pcall(require, "vern-nvim.actions." .. filetype) + local ft_ok, action = pcall(require, "vern-nvim.configs." .. filetype) if not filetype or filetype == "" @@ -191,7 +191,13 @@ M.run = function(opts) }) return else - action.run(opts) + local args = vim.tbl_deep_extend( + "force", + {}, + opts, + require("vern-nvim.configs." .. filetype) + ) + action.run(args) end end diff --git a/lua/vern-nvim/actions/c.lua b/lua/vern-nvim/actions/c.lua deleted file mode 100644 index 12f658c..0000000 --- a/lua/vern-nvim/actions/c.lua +++ /dev/null @@ -1,45 +0,0 @@ -local M = {} - -local utils = require("vern-nvim.utils") - ----@param opts? VernCConfig Takes a table for the runtime -function M.run(opts) - opts = opts or {} - - local build_dir = vim.fn.fnameescape((utils.proj_root() or ".") .. "/build") - - vim.system( - { "sh", "-c", string.format("cd %s && cmake --build .", build_dir) }, - { text = true }, - function(obj) - if obj.code ~= 0 then - vim.schedule(function() - local error_obj = (obj.stderr ~= "" and obj.stderr) - or obj.stdout - or "" - local error_lines = vim.list_extend( - { "Error During compilation" }, - vim.split(error_obj, "\n", { trimempty = true }) - ) - local buf, _ = utils.term( - { "echo 'compilation failed'" }, - opts.terminal, - true - ) - utils.term_override(buf, error_lines) - end) - else - vim.schedule(function() - local run_cmd = string.format( - "cd %s && ./%s", - build_dir, - utils.proj_name() - ) - utils.term({ run_cmd }, opts.terminal, false) - end) - end - end - ) -end - -return M diff --git a/lua/vern-nvim/actions/cpp.lua b/lua/vern-nvim/actions/cpp.lua deleted file mode 100644 index 96daf4e..0000000 --- a/lua/vern-nvim/actions/cpp.lua +++ /dev/null @@ -1,45 +0,0 @@ -local M = {} - -local utils = require("vern-nvim.utils") - ----@param opts? VernCPPConfig Takes a table for the runtime -function M.run(opts) - opts = opts or {} - - local build_dir = vim.fn.fnameescape((utils.proj_root() or ".") .. "/build") - - vim.system( - { "sh", "-c", string.format("cd %s && cmake --build .", build_dir) }, - { text = true }, - function(obj) - if obj.code ~= 0 then - vim.schedule(function() - local error_obj = (obj.stderr ~= "" and obj.stderr) - or obj.stdout - or "" - local error_lines = vim.list_extend( - { "Error During compilation" }, - vim.split(error_obj, "\n", { trimempty = true }) - ) - local buf, _ = utils.term( - { "echo 'compilation failed'" }, - opts.terminal, - true - ) - utils.term_override(buf, error_lines) - end) - else - vim.schedule(function() - local run_cmd = string.format( - "cd %s && ./%s", - build_dir, - utils.proj_name() - ) - utils.term({ run_cmd }, opts.terminal, false) - end) - end - end - ) -end - -return M diff --git a/lua/vern-nvim/actions/java.lua b/lua/vern-nvim/actions/java.lua deleted file mode 100644 index 25119b8..0000000 --- a/lua/vern-nvim/actions/java.lua +++ /dev/null @@ -1,58 +0,0 @@ -local M = {} - -local utils = require("vern-nvim.utils") - ----@param opts VernJavaConfig Takes a table for the runtime -function M.run(opts) - opts = opts or {} - local root = utils.proj_root() or "." - local context = { - project_name = utils.proj_name(), - company_name = opts.env.author, - file_name = "", - } - - local src_root = vim.fs.joinpath( - root, - utils.resolve_path(opts.java.dirs.src, opts.java.dirs, context) - ) - local build_dir = root .. "/bin" - local build_cmd = string.format( - "javac -d %s -sourcepath %s $(find %s -name '*java')", - build_dir, - src_root, - src_root - ) - - vim.system({ "sh", "-c", build_cmd }, { text = true }, function(obj) - if obj.code ~= 0 then - vim.schedule(function() - local error_obj = (obj.stderr ~= "" and obj.stderr) - or obj.stdout - or "" - local error_lines = vim.list_extend( - { "Error During compilation" }, - vim.split(error_obj, "\n", { trimempty = true }) - ) - local buf, _ = utils.term( - { "echo 'compilation failed'" }, - opts.terminal, - true - ) - utils.term_override(buf, error_lines) - end) - else - vim.schedule(function() - local run_cmd = string.format( - "java -cp %s com.%s.%s.Main", - build_dir, - context.company_name, - context.project_name - ) - utils.term({ run_cmd }, opts.terminal, false) - end) - end - end) -end - -return M diff --git a/lua/vern-nvim/actions/lua.lua b/lua/vern-nvim/actions/lua.lua deleted file mode 100644 index 55a1b87..0000000 --- a/lua/vern-nvim/actions/lua.lua +++ /dev/null @@ -1,19 +0,0 @@ -local M = {} - -local utils = require("vern-nvim.utils") - ----@param opts VernLuaConfig Takes a table for the runtime -function M.run(opts) - opts = opts or {} - - utils.term({ - "lua " .. vim.fs.joinpath( - utils.proj_root(), - "lua", - utils.proj_name(), - "/init.lua" - ), - }, opts.terminal or {}, false) -end - -return M diff --git a/lua/vern-nvim/actions/python.lua b/lua/vern-nvim/actions/python.lua deleted file mode 100644 index ce1a250..0000000 --- a/lua/vern-nvim/actions/python.lua +++ /dev/null @@ -1,60 +0,0 @@ -local M = {} - -local utils = require("vern-nvim.utils") - ----@param opts VernPythonConfig Takes a table for the runtime -function M.run(opts) - opts = opts or {} - local root = utils.proj_root() or "." - local context = { - project_name = utils.proj_name(), - company_name = opts.env.author, - file_name = "", - } - local bin = opts.python.templates.project.primary - - 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, context) - local full_path = vim.fs.joinpath(vim.fn.expand(root), exe_path) - - utils.term({ "python3 " .. full_path }, opts.terminal or {}, false) -end - -function M.pip_install() - vim.ui.input({ prompt = "Enter pip module: " }, function(mod) - mod = mod and vim.trim(mod) - if not mod or mod == "" then - vim.notify( - "Pip Cancelled", - vim.log.levels.WARN, - { title = "Vern Python" } - ) - return - end - vim.notify( - "Installing " .. mod, - vim.log.levels.INFO, - { title = "Vern Python" } - ) - vim.system({ "pip", "install", mod }, { text = true }, function(obj) - vim.schedule(function() - if obj.code ~= 0 then - vim.notify( - "Pip failed: " .. obj.stderr, - vim.log.levels.WARN, - { title = "Vern Python" } - ) - else - vim.notify( - "Pip installed " .. mod, - vim.log.levels.INFO, - { title = "Vern Python" } - ) - end - end) - end) - end) -end - -return M diff --git a/lua/vern-nvim/actions/rust.lua b/lua/vern-nvim/actions/rust.lua deleted file mode 100644 index a21a762..0000000 --- a/lua/vern-nvim/actions/rust.lua +++ /dev/null @@ -1,15 +0,0 @@ -local M = {} - -local utils = require("vern-nvim.utils") - ----@param opts VernRustConfig Takes a table for the runtime -function M.run(opts) - opts = opts or {} - utils.term( - { "cd " .. utils.proj_root() .. " && cargo run" }, - opts.terminal or {}, - false - ) -end - -return M diff --git a/lua/vern-nvim/configs/c.lua b/lua/vern-nvim/configs/c.lua index 0cc99ee..a755aac 100644 --- a/lua/vern-nvim/configs/c.lua +++ b/lua/vern-nvim/configs/c.lua @@ -1,6 +1,9 @@ +local utils = require("vern-nvim.utils") +local comp = require("vern-nvim.languages.shared.compile") + local M = {} -M.attach = function() end +M.attach = nil ---@param root string M.create_project = function(root) @@ -11,23 +14,31 @@ M.create_project = function(root) end ---@type VernCreateFile -M.create_file = { - allowed = true, - ---@param opts VernCConfig - func = function(opts, continue) - opts = opts or {} - local file = vim.tbl_get(opts, "c", "templates", "file") - local items = { "source", "header" } - vim.ui.select(items, { - prompt = "Select type:", - format_item = function(item) return item end, - }, function(choice, _) - if choice == items[1] then return continue(file.source) end - if choice == items[2] then return continue(file.header) end - continue(nil) - end) - end, -} +---@param opts VernCConfig +M.create_file = function(opts, continue) + opts = opts or {} + local file = vim.tbl_get(opts, "c", "templates", "file") + local items = { "source", "header" } + vim.ui.select(items, { + prompt = "Select type:", + format_item = function(item) return item end, + }, function(choice, _) + if choice == items[1] then return continue(file.source) end + if choice == items[2] then return continue(file.header) end + continue(nil) + end) +end + +---@param opts? VernCConfig Takes a table for the runtime +M.run = function(opts) + opts = opts or {} + + local build_dir = vim.fn.fnameescape((utils.proj_root() or ".") .. "/build") + local build_cmd = string.format("cd %s && cmake --build .", build_dir) + local run_cmd = string.format("cd %s && ./%s", build_dir, utils.proj_name()) + + comp.compile(opts, build_cmd, run_cmd) +end ---@type VernLanguageConfig M.c = { @@ -36,7 +47,7 @@ M.c = { src = "src", include = "include/{project_name}", }, - keybinds = {}, + keybinds = nil, templates = { project = { primary = { diff --git a/lua/vern-nvim/configs/cpp.lua b/lua/vern-nvim/configs/cpp.lua index ff09a44..c1ff207 100644 --- a/lua/vern-nvim/configs/cpp.lua +++ b/lua/vern-nvim/configs/cpp.lua @@ -1,6 +1,9 @@ +local utils = require("vern-nvim.utils") +local comp = require("vern-nvim.languages.shared.compile") + local M = {} -M.attach = function() end +M.attach = nil ---@param root string M.create_project = function(root) @@ -11,23 +14,31 @@ M.create_project = function(root) end ---@type VernCreateFile -M.create_file = { - allowed = true, - ---@param opts VernCPPConfig - func = function(opts, continue) - opts = opts or {} - local file = vim.tbl_get(opts, "cpp", "templates", "file") - local items = { "source", "header" } - vim.ui.select(items, { - prompt = "Select type:", - format_item = function(item) return item end, - }, function(choice, _) - if choice == items[1] then return continue(file.source) end - if choice == items[2] then return continue(file.header) end - continue(nil) - end) - end, -} +---@param opts VernCPPConfig +M.create_file = function(opts, continue) + opts = opts or {} + local file = vim.tbl_get(opts, "cpp", "templates", "file") + local items = { "source", "header" } + vim.ui.select(items, { + prompt = "Select type:", + format_item = function(item) return item end, + }, function(choice, _) + if choice == items[1] then return continue(file.source) end + if choice == items[2] then return continue(file.header) end + continue(nil) + end) +end +--- +---@param opts? VernCPPConfig Takes a table for the runtime +M.run = function(opts) + opts = opts or {} + + local build_dir = vim.fn.fnameescape((utils.proj_root() or ".") .. "/build") + local build_cmd = string.format("cd %s && cmake --build .", build_dir) + local run_cmd = string.format("cd %s && ./%s", build_dir, utils.proj_name()) + + comp.compile(opts, build_cmd, run_cmd) +end ---@type VernLanguageConfig M.cpp = { @@ -36,7 +47,7 @@ M.cpp = { src = "src", include = "include/{project_name}", }, - keybinds = {}, + keybinds = nil, templates = { project = { primary = { diff --git a/lua/vern-nvim/configs/java.lua b/lua/vern-nvim/configs/java.lua index 1df9171..48e44b2 100644 --- a/lua/vern-nvim/configs/java.lua +++ b/lua/vern-nvim/configs/java.lua @@ -1,14 +1,45 @@ +local utils = require("vern-nvim.utils") +local comp = require("vern-nvim.languages.shared.compile") + local M = {} -M.attach = function() end +M.attach = nil -M.create_project = function() end +M.create_project = nil ---@type VernCreateFile -M.create_file = { - allowed = false, - func = function(_, _) return nil end, -} +M.create_file = nil + +---@param opts VernJavaConfig Takes a table for the runtime +M.run = function(opts) + opts = opts or {} + local root = utils.proj_root() + local context = { + project_name = utils.proj_name(), + company_name = opts.env.author, + file_name = "", + } + + local src_root = vim.fs.joinpath( + root, + utils.resolve_path(opts.java.dirs.src, opts.java.dirs, context) + ) + local build_dir = root .. "/bin" + local build_cmd = string.format( + "javac -d %s -sourcepath %s $(find %s -name '*java')", + build_dir, + src_root, + src_root + ) + local run_cmd = string.format( + "java -cp %s com.%s.%s.Main", + build_dir, + context.company_name, + context.project_name + ) + + comp.compile(opts, build_cmd, run_cmd) +end ---@type VernLanguageConfig M.java = { @@ -17,7 +48,7 @@ M.java = { src = "src/com/{company_name}/{project_name}", bin = "bin", }, - keybinds = {}, + keybinds = nil, templates = { project = { primary = { @@ -41,7 +72,7 @@ M.java = { kind_path = "vern-nvim.templates.java", }, }, - extras = {}, + extras = nil, }, } return M diff --git a/lua/vern-nvim/configs/lua.lua b/lua/vern-nvim/configs/lua.lua index e559ed1..c8bb7f4 100644 --- a/lua/vern-nvim/configs/lua.lua +++ b/lua/vern-nvim/configs/lua.lua @@ -1,14 +1,27 @@ +local utils = require("vern-nvim.utils") + local M = {} -M.attach = function() end +M.attach = nil -M.create_project = function() end +M.create_project = nil ---@type VernCreateFile -M.create_file = { - allowed = false, - func = function(_, _) return nil end, -} +M.create_file = nil + +---@param opts VernLuaConfig Takes a table for the runtime +M.run = function(opts) + opts = opts or {} + + utils.term({ + "lua " .. vim.fs.joinpath( + utils.proj_root(), + "lua", + utils.proj_name(), + "/init.lua" + ), + }, opts.terminal or {}, false) +end ---@type VernLanguageConfig M.lua = { @@ -16,7 +29,7 @@ M.lua = { dirs = { src = "lua/{project_name}", }, - keybinds = {}, + keybinds = nil, templates = { project = { primary = { @@ -40,7 +53,7 @@ M.lua = { kind_path = "vern-nvim.templates.lua", }, }, - extras = {}, + extras = nil, }, } diff --git a/lua/vern-nvim/configs/python.lua b/lua/vern-nvim/configs/python.lua index d779a3f..7b0ce31 100644 --- a/lua/vern-nvim/configs/python.lua +++ b/lua/vern-nvim/configs/python.lua @@ -1,3 +1,5 @@ +local utils = require("vern-nvim.utils") + local M = {} M.attach = function() @@ -28,10 +30,62 @@ M.create_project = function(root) end ---@type VernCreateFile -M.create_file = { - allowed = false, - func = function(_, _) return nil end, -} +M.create_file = nil + +---@param opts VernPythonConfig Takes a table for the runtime +M.run = function(opts) + opts = opts or {} + local root = utils.proj_root() or "." + local context = { + project_name = utils.proj_name(), + company_name = opts.env.author, + file_name = "", + } + local bin = opts.python.templates.project.primary + + 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, context) + local full_path = vim.fs.joinpath(vim.fn.expand(root), exe_path) + + utils.term({ "python3 " .. full_path }, opts.terminal or {}, false) +end + +M.pip_install = function() + vim.ui.input({ prompt = "Enter pip module: " }, function(mod) + mod = mod and vim.trim(mod) + if not mod or mod == "" then + vim.notify( + "Pip Cancelled", + vim.log.levels.WARN, + { title = "Vern Python" } + ) + return + end + vim.notify( + "Installing " .. mod, + vim.log.levels.INFO, + { title = "Vern Python" } + ) + vim.system({ "pip", "install", mod }, { text = true }, function(obj) + vim.schedule(function() + if obj.code ~= 0 then + vim.notify( + "Pip failed: " .. obj.stderr, + vim.log.levels.WARN, + { title = "Vern Python" } + ) + else + vim.notify( + "Pip installed " .. mod, + vim.log.levels.INFO, + { title = "Vern Python" } + ) + end + end) + end) + end) +end ---@type VernLanguageConfig M.python = { @@ -43,7 +97,7 @@ M.python = { ["pi"] = { user_command = "VernPythonPip", desc = "Install python packages for environment", - action = require("vern-nvim.actions.python").pip_install, + action = M.pip_install, }, }, templates = { @@ -75,7 +129,7 @@ M.python = { kind_path = "vern-nvim.templates.python", }, }, - extras = {}, + extras = nil, }, } return M diff --git a/lua/vern-nvim/configs/rust.lua b/lua/vern-nvim/configs/rust.lua index ec81b99..a448d2c 100644 --- a/lua/vern-nvim/configs/rust.lua +++ b/lua/vern-nvim/configs/rust.lua @@ -1,6 +1,8 @@ +local utils = require("vern-nvim.utils") + local M = {} -M.attach = function() end +M.attach = nil M.create_project = function(root) local obj = vim.system({ @@ -17,16 +19,23 @@ M.create_project = function(root) end ---@type VernCreateFile -M.create_file = { - allowed = false, - func = function(_, _) return nil end, -} +M.create_file = nil + +---@param opts VernRustConfig Takes a table for the runtime +M.run = function(opts) + opts = opts or {} + utils.term( + { "cd " .. utils.proj_root() .. " && cargo run" }, + opts.terminal or {}, + false + ) +end ---@type VernLanguageConfig M.rust = { root = "Rust", dirs = nil, - keybinds = {}, + keybinds = nil, templates = { project = { primary = { path = "src", name = "main.rs" }, @@ -39,7 +48,7 @@ M.rust = { kind_path = "vern-nvim.templates.rust", }, }, - extras = {}, + extras = nil, }, } diff --git a/lua/vern-nvim/init.lua b/lua/vern-nvim/init.lua index 1e0084d..825591b 100644 --- a/lua/vern-nvim/init.lua +++ b/lua/vern-nvim/init.lua @@ -41,7 +41,7 @@ function M.setup(user_opts) if not ok then return end local opts = vim.tbl_deep_extend("force", {}, mod, M.opts or {}) - if opts[ft] and opts[ft].keybinds then + if opts[ft] and opts[ft].keybinds ~= nil then for key, def in pairs(opts[ft].keybinds or {}) do if type(def.action) == "function" then vim.api.nvim_buf_create_user_command( diff --git a/lua/vern-nvim/languages/shared/compile.lua b/lua/vern-nvim/languages/shared/compile.lua new file mode 100644 index 0000000..fd7650a --- /dev/null +++ b/lua/vern-nvim/languages/shared/compile.lua @@ -0,0 +1,31 @@ +local M = {} + +local utils = require("vern-nvim.utils") + +M.compile = function(opts, build_cmd, run_cmd) + vim.system({ "sh", "-c", build_cmd }, { text = true }, function(obj) + if obj.code ~= 0 then + vim.schedule(function() + local error_obj = (obj.stderr ~= "" and obj.stderr) + or obj.stdout + or "" + local error_lines = vim.list_extend( + { "Error During compilation" }, + vim.split(error_obj, "\n", { trimempty = true }) + ) + local buf, _ = utils.term( + { "echo 'compilation failed'" }, + opts.terminal, + true + ) + utils.term_override(buf, error_lines) + end) + else + vim.schedule( + function() utils.term({ run_cmd }, opts.terminal, false) end + ) + end + end) +end + +return M diff --git a/lua/vern-nvim/types.lua b/lua/vern-nvim/types.lua index 352bd21..83ad6aa 100644 --- a/lua/vern-nvim/types.lua +++ b/lua/vern-nvim/types.lua @@ -3,8 +3,7 @@ ---@alias SplitAnchorType "right" | "bottom" ---@class VernCreateFile ----@field allowed boolean Determines whether actions/init.lua uses the function ----@field func fun(opts: any, continue: fun(mod: VernFileLayout|nil)): any? +---@field func? fun(opts: any, continue: fun(mod: VernFileLayout|nil)): any? ---@class VernKeybindItem ---@field user_command string @@ -57,7 +56,7 @@ ---@class VernLanguageConfig ---@field root string ---@field dirs table|nil ----@field keybinds table +---@field keybinds? table ---@field templates VernLanguageTemplates ---@class VernPythonConfig : VernCoreConfig