From 9a6eb90efc335bdcd3444d28aa5c0c3bfd8dea53 Mon Sep 17 00:00:00 2001 From: Andrew Haynes Date: Fri, 30 Oct 2026 13:24:38 -0400 Subject: [PATCH] refactor(configs): completely overhauled how projects and files are handled 1. Run is now handled by the global run function instead of language specific runs 2. Fixed some notifications, making them more unified and informational 3. Cleaned up c/c++ code that was broken from create_file changes 4. Create_file has new architecture. It still takes in a function but can be disabled to be skipped (this will most likely change later as its unoptimized) 5. Added rust support 6. Keybinds are now only setup for language specific 7. Project creation has error and nil checking allowing for skipping and more customizable templates and setups for future languages --- lua/vern-nvim/actions.lua | 198 ++++++++++++++++++ lua/vern-nvim/actions/c.lua | 79 +++---- lua/vern-nvim/actions/cpp.lua | 70 ++----- lua/vern-nvim/actions/init.lua | 150 ------------- lua/vern-nvim/actions/java.lua | 32 ++- lua/vern-nvim/actions/lua.lua | 19 +- lua/vern-nvim/actions/python.lua | 34 +-- lua/vern-nvim/actions/rust.lua | 15 ++ lua/vern-nvim/configs/c.lua | 40 ++-- lua/vern-nvim/configs/cpp.lua | 40 ++-- lua/vern-nvim/configs/init.lua | 13 +- lua/vern-nvim/configs/java.lua | 16 +- lua/vern-nvim/configs/lua.lua | 16 +- lua/vern-nvim/configs/python.lua | 29 ++- lua/vern-nvim/configs/rust.lua | 46 ++++ lua/vern-nvim/init.lua | 56 ++--- .../{templates/init.lua => templates.lua} | 0 lua/vern-nvim/templates/extras/gitignore.lua | 9 + lua/vern-nvim/templates/rust.lua | 9 + lua/vern-nvim/types.lua | 19 +- lua/vern-nvim/utils.lua | 74 ++++--- stylua.toml | 5 +- 22 files changed, 562 insertions(+), 407 deletions(-) create mode 100644 lua/vern-nvim/actions.lua delete mode 100644 lua/vern-nvim/actions/init.lua create mode 100644 lua/vern-nvim/actions/rust.lua create mode 100644 lua/vern-nvim/configs/rust.lua rename lua/vern-nvim/{templates/init.lua => templates.lua} (100%) create mode 100644 lua/vern-nvim/templates/rust.lua diff --git a/lua/vern-nvim/actions.lua b/lua/vern-nvim/actions.lua new file mode 100644 index 0000000..e5cf9cc --- /dev/null +++ b/lua/vern-nvim/actions.lua @@ -0,0 +1,198 @@ +local M = {} + +local utils = require("vern-nvim.utils") +local templates = require("vern-nvim.templates") + +---Imitate create_file so you can skip over and use for each filetype + +M.create_project = function(opts) + opts = opts or {} + vim.ui.input( + { prompt = "Project language (e.g. python): ", completion = "filetype" }, + 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, configs = pcall(require, "vern-nvim.configs." .. ft) + if not ok or type(configs) ~= "table" then return end + local args = vim.tbl_deep_extend("force", {}, configs, opts or {}) + if not args[ft] or type(args[ft]) ~= "table" then return end + 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 ctx = { + project_name = pname, + company_name = args.env.author, + file_name = "", + } + + vim.fn.mkdir(root, "p") + + local tpl = args[ft].templates or {} + local dirs = args[ft].dirs + if type(dirs) == "table" and next(dirs) then + for _, dir in pairs(dirs) do + local path = vim.fs.normalize( + vim.fs.joinpath(root, utils.fmt(dir, ctx)) + ) + vim.fn.mkdir(path, "p") + end + end + + local bin + local project = tpl.project + if type(project) == "table" and next(project) then + for tbl_name, tbl in pairs(project) do + local raw_path = vim.fs.joinpath(tbl.path, tbl.name) + local path = + utils.resolve_path(raw_path, args[ft].dirs, ctx) + if tbl_name == "primary" then + bin = vim.fs.joinpath(root, path) + end + if tbl.kind then + local abs_file_path = + vim.fs.normalize(vim.fs.joinpath(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, ctx) + ) + end + end + end + local extras = tpl.extras + if type(extras) == "table" and next(extras) then + for _, tbl in pairs(extras) do + if tbl.enabled ~= false and tbl.kind then + local raw_path = vim.fs.joinpath(tbl.path, tbl.name) + local path = + utils.resolve_path(raw_path, args[ft].dirs, ctx) + local abs_file_path = + vim.fs.normalize(vim.fs.joinpath(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, ctx) + ) + end + end + end + + if type(configs.create_project) == "function" then + configs.create_project(root) + end + utils.init_git(root, ft) + utils.init_readme(root) + if bin then vim.cmd.edit(vim.fn.fnameescape(bin)) end + vim.notify( + "Project: " .. pname .. " has been created!", + vim.log.levels.INFO, + { title = "Vern Project" } + ) + end) + end + ) +end + +M.create_file = function(opts) + opts = opts or {} + local filetype = vim.bo.filetype + + local ft_ok, config = pcall(require, "vern-nvim.configs." .. filetype) + ---@type VernCreateFile + if + not filetype + or filetype == "" + or not ft_ok + or type(config) ~= "table" + then + vim.notify("Unknown Filetype: " .. filetype, vim.log.levels.WARN, { + title = "Vern File", + }) + return + else + local args = vim.tbl_deep_extend("force", {}, config, opts or {}) + local cf = args.create_file + local function function_write(mod, fname) + local raw_path = vim.fs.joinpath(mod.path, mod.name) + local context = { + project_name = utils.proj_name(), + company_name = args.env.author, + file_name = fname, + } + local path = + utils.resolve_path(raw_path, args[filetype].dirs or {}, context) + local full_path = vim.fs.joinpath(utils.proj_root(), path) + utils.dump( + full_path, + templates.fetch(mod.kind_path, mod.kind, context) + ) + vim.cmd.edit(vim.fn.fnameescape(full_path)) + vim.notify( + fname .. " has been created!", + vim.log.levels.INFO, + { title = "Vern File" } + ) + 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 done = function(mod) + if not mod or vim.tbl_isempty(mod) then return end + function_write(mod, fname) + end + + if cf and type(cf.func) == "function" and cf.allowed then + cf.func(args, done) + else + done(args[filetype].templates.file.source) + end + end) + end +end + +M.run = function(opts) + opts = opts or {} + local filetype = vim.bo.filetype + + local ft_ok, action = pcall(require, "vern-nvim.actions." .. filetype) + if + not filetype + or filetype == "" + or not ft_ok + or type(action.run) ~= "function" + then + vim.notify("Unknown Filetype: " .. filetype, vim.log.levels.WARN, { + title = "Vern Run", + }) + return + else + action.run(opts) + end +end + +return M diff --git a/lua/vern-nvim/actions/c.lua b/lua/vern-nvim/actions/c.lua index 6f106e7..83b6c53 100644 --- a/lua/vern-nvim/actions/c.lua +++ b/lua/vern-nvim/actions/c.lua @@ -2,78 +2,49 @@ local M = {} local utils = require("vern-nvim.utils") ----@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() -end - ----@param opts VernCConfig -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 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) - 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(), - company_name = opts.env.author, - file_name = fname, - } - 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, context) - ) - vim.cmd.edit(vim.fn.expand(path)) - vim.notify(fname .. " has been created!", vim.log.levels.INFO, { title = "Vern File" }) - end) - end) -end - ---@param opts? VernCConfig Takes a table for the runtime function M.run(opts) opts = opts or {} - local root = utils.proj_root() or "." - local pname = vim.fs.basename(root) - local build_dir = root .. "/build" - local build_cmd = "cd " .. build_dir .. " && cmake --build ." + local build_cmd = + string.format("cd %s/build && cmake --build .", utils.proj_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_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) + local buf, _ = utils.term( + { "echo 'compilation failed'" }, + opts.terminal, + true + ) if buf and vim.api.nvim_buf_is_valid(buf) then - vim.api.nvim_set_option_value("modifiable", true, { buf = buf }) + vim.api.nvim_set_option_value( + "modifiable", + true, + { buf = buf } + ) vim.api.nvim_buf_set_lines(buf, 0, -1, false, error_lines) - vim.api.nvim_set_option_value("modifiable", false, { buf = buf }) + vim.api.nvim_set_option_value( + "modifiable", + false, + { buf = buf } + ) end end) else vim.schedule(function() - local run_cmd = "cd '" .. build_dir .. "' && ./" .. pname + local run_cmd = string.format( + "cd %s/build && ./%s", + utils.proj_root(), + utils.proj_name() + ) utils.term({ run_cmd }, opts.terminal, false) end) end diff --git a/lua/vern-nvim/actions/cpp.lua b/lua/vern-nvim/actions/cpp.lua index f9bb867..d80dfa0 100644 --- a/lua/vern-nvim/actions/cpp.lua +++ b/lua/vern-nvim/actions/cpp.lua @@ -2,78 +2,36 @@ local M = {} local utils = require("vern-nvim.utils") ----@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() -end - ----@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 mod = {} - if ft == "c" then - mod = opts.cpp.templates.file.source or {} - end - if ft == "h" then - 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(), - company_name = opts.env.author, - file_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, context) - ) - vim.cmd.edit(vim.fn.expand(path)) - vim.notify(fname .. " has been created!", vim.log.levels.INFO, { title = "Vern File" }) - end) - end) -end - ---@param opts? VernCPPConfig Takes a table for the runtime function M.run(opts) opts = opts or {} - local root = utils.proj_root() or "." - local pname = vim.fs.basename(root) - local build_dir = root .. "/build" + local build_dir = (utils.proj_root() or ".") .. "/build" local build_cmd = "cd " .. build_dir .. " && cmake --build ." 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_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) - if buf and vim.api.nvim_buf_is_valid(buf) then - vim.api.nvim_set_option_value("modifiable", true, { buf = buf }) - vim.api.nvim_buf_set_lines(buf, 0, -1, false, error_lines) - vim.api.nvim_set_option_value("modifiable", false, { buf = buf }) - end + local buf, _ = utils.term( + { "echo 'compilation failed'" }, + opts.terminal, + true + ) + utils.term_override(buf, error_lines) end) else vim.schedule(function() - local run_cmd = "cd '" .. build_dir .. "' && ./" .. pname + local run_cmd = "cd '" + .. build_dir + .. "' && ./" + .. utils.proj_name() utils.term({ run_cmd }, opts.terminal, false) end) end diff --git a/lua/vern-nvim/actions/init.lua b/lua/vern-nvim/actions/init.lua deleted file mode 100644 index aaa811a..0000000 --- a/lua/vern-nvim/actions/init.lua +++ /dev/null @@ -1,150 +0,0 @@ -local M = {} - ----TODO ----Split filetype only and just change a filetype variable and vairables both share and then run the duplicate code - -local utils = require("vern-nvim.utils") -local templates = require("vern-nvim.templates") - -M.create_project = function(opts) - opts = opts or {} - vim.ui.input( - { prompt = "Project language (e.g. python): ", completion = "filetype" }, - 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 {}) - 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 ctx = { - project_name = pname, - company_name = args.env.author, - file_name = "", - } - - vim.fn.mkdir(root, "p") - - for _, dir in pairs(args[ft].dirs) do - local path = vim.fs.normalize(vim.fs.joinpath(root, utils.fmt(dir, ctx))) - vim.fn.mkdir(path, "p") - end - - local bin - for tbl_name, tbl in pairs(args[ft].templates.project) do - local raw_path = vim.fs.joinpath(tbl.path, tbl.name) - local path = utils.resolve_path(raw_path, args[ft].dirs, ctx) - if tbl_name == "primary" then - bin = vim.fs.joinpath(root, path) - end - local abs_file_path = vim.fs.normalize(vim.fs.joinpath(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, ctx)) - end - for _, tbl in pairs(args[ft].templates.extras) do - if tbl.enabled then - local raw_path = vim.fs.joinpath(tbl.path, tbl.name) - local path = utils.resolve_path(raw_path, args[ft].dirs, ctx) - local abs_file_path = vim.fs.normalize(vim.fs.joinpath(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, ctx)) - end - end - - 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 - -M.create_file = function(opts) - opts = opts or {} - local filetype = vim.bo.filetype - - local function continue_filetype(ft) - filetype = ft - local ok, actions = pcall(require, "vern-nvim.actions." .. filetype) - if not ok then - vim.notify("Unknown Filetype: " .. filetype, vim.log.levels.WARN, { - title = "Vern File", - }) - return - end - local configs = require("vern-nvim.configs." .. filetype) - 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 mod - if ok and actions and type(actions.create_file) == "function" then - mod = actions.create_file(args[filetype]) - or args[filetype].templates.file.source - or {} - end - local raw_path = vim.fs.joinpath(mod.path, mod.name) - local context = { - project_name = utils.proj_name(), - company_name = opts.env.author, - file_name = fname, - } - local path = utils.resolve_path(raw_path, args[filetype].dirs or {}, context) - local root = utils.proj_root() - utils.dump( - vim.fs.joinpath(root, path), - templates.fetch(mod.kind_path, mod.kind, context) - ) - vim.cmd.edit(vim.fn.fnameescape(path)) - vim.notify(fname .. " has been created!", vim.log.levels.INFO, { title = "Vern File" }) - end) - end - - local ft_ok, ft_config = pcall(require, "vern-nvim.configs." .. filetype) - if not filetype or filetype == "" or not ft_ok or type(ft_config) ~= "table" then - vim.ui.input({ - prompt = "File language (e.g. python): ", - completion = "filetype", - }, function(ft) - ft = ft and vim.trim(ft) - if not ft or vim.trim(ft) == "" then - return - end - continue_filetype(ft) - end) - else - continue_filetype(filetype) - end -end - -return M diff --git a/lua/vern-nvim/actions/java.lua b/lua/vern-nvim/actions/java.lua index 4cd456b..490b751 100644 --- a/lua/vern-nvim/actions/java.lua +++ b/lua/vern-nvim/actions/java.lua @@ -2,10 +2,6 @@ local M = {} local utils = require("vern-nvim.utils") -M.create_project = function() end - -M.create_file = function() end - ---@param opts VernJavaConfig Takes a table for the runtime function M.run(opts) opts = opts or {} @@ -16,8 +12,10 @@ function M.run(opts) file_name = "", } - local src_root = - vim.fs.joinpath(root, utils.resolve_path(opts.java.dirs.src, opts.java.dirs, context)) + 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')", @@ -29,16 +27,30 @@ function M.run(opts) 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_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) + local buf, _ = utils.term( + { "echo 'compilation failed'" }, + opts.terminal, + true + ) if buf and vim.api.nvim_buf_is_valid(buf) then - vim.api.nvim_set_option_value("modifiable", true, { buf = buf }) + vim.api.nvim_set_option_value( + "modifiable", + true, + { buf = buf } + ) vim.api.nvim_buf_set_lines(buf, 0, -1, false, error_lines) - vim.api.nvim_set_option_value("modifiable", false, { buf = buf }) + vim.api.nvim_set_option_value( + "modifiable", + false, + { buf = buf } + ) end end) else diff --git a/lua/vern-nvim/actions/lua.lua b/lua/vern-nvim/actions/lua.lua index 686749d..21cda73 100644 --- a/lua/vern-nvim/actions/lua.lua +++ b/lua/vern-nvim/actions/lua.lua @@ -2,20 +2,17 @@ local M = {} local utils = require("vern-nvim.utils") -M.create_project = function() end - -M.create_file = function() end - ----@param opts VernCPPConfig Takes a table for the runtime +---@param opts VernLuaConfig Takes a table for the runtime function M.run(opts) opts = opts or {} - local root = utils.proj_root() or "." - local pname = vim.fs.basename(root) + local full_path = + vim.fs.joinpath(utils.proj_root(), "lua", utils.proj_name()) - 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) + utils.term( + { "lua " .. full_path .. "/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 index be43d52..ac6cda7 100644 --- a/lua/vern-nvim/actions/python.lua +++ b/lua/vern-nvim/actions/python.lua @@ -2,13 +2,6 @@ local M = {} local utils = require("vern-nvim.utils") ----@param root string -M.create_project = function(root) - vim.fn.system("python3 -m venv " .. root .. "/venv") -end - -M.create_file = function() end - ---@param opts VernPythonConfig Takes a table for the runtime function M.run(opts) opts = opts or {} @@ -20,7 +13,8 @@ function M.run(opts) } local bin = opts.python.templates.project.primary - local exe = vim.fs.joinpath(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, context) local full_path = vim.fs.joinpath(vim.fn.expand(root), exe_path) utils.term({ "python3 " .. full_path }, opts.terminal or {}, false) @@ -30,16 +24,32 @@ 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) + vim.notify( + "Pip Cancelled", + vim.log.levels.WARN, + { title = "Vern Python" } + ) return end - vim.notify("Installing " .. mod, vim.log.levels.INFO) + 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) + vim.notify( + "Pip failed: " .. obj.stderr, + vim.log.levels.WARN, + { title = "Vern Python" } + ) else - vim.notify("Pip installed " .. mod, vim.log.levels.INFO) + vim.notify( + "Pip installed " .. mod, + vim.log.levels.INFO, + { title = "Vern Python" } + ) end end) end) diff --git a/lua/vern-nvim/actions/rust.lua b/lua/vern-nvim/actions/rust.lua new file mode 100644 index 0000000..a21a762 --- /dev/null +++ b/lua/vern-nvim/actions/rust.lua @@ -0,0 +1,15 @@ +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 53325f2..0cc99ee 100644 --- a/lua/vern-nvim/configs/c.lua +++ b/lua/vern-nvim/configs/c.lua @@ -2,6 +2,33 @@ local M = {} M.attach = function() end +---@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() +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, +} + ---@type VernLanguageConfig M.c = { root = "C", @@ -9,18 +36,7 @@ M.c = { src = "src", include = "include/{project_name}", }, - 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, - }, - }, + keybinds = {}, templates = { project = { primary = { diff --git a/lua/vern-nvim/configs/cpp.lua b/lua/vern-nvim/configs/cpp.lua index 80339ab..ff09a44 100644 --- a/lua/vern-nvim/configs/cpp.lua +++ b/lua/vern-nvim/configs/cpp.lua @@ -2,6 +2,33 @@ local M = {} M.attach = function() end +---@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() +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, +} + ---@type VernLanguageConfig M.cpp = { root = "CPP", @@ -9,18 +36,7 @@ M.cpp = { src = "src", include = "include/{project_name}", }, - 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, - }, - }, + keybinds = {}, templates = { project = { primary = { diff --git a/lua/vern-nvim/configs/init.lua b/lua/vern-nvim/configs/init.lua index 4b5155c..2a8512e 100644 --- a/lua/vern-nvim/configs/init.lua +++ b/lua/vern-nvim/configs/init.lua @@ -15,13 +15,18 @@ return { }, ["pf"] = { user_command = "VernCreateFile", - desc = "Creates a new project file", + desc = "Creates a new project file based on current buffer filetype", action = actions.create_file, }, + ["pr"] = { + user_command = "VernRunProject", + desc = "Builds and runs project based on current buffer filetype", + action = actions.run, + }, }, terminal = { mode = { - type = "split", + type = "split", --- (float)|(split) win = { float = { padding = { @@ -36,8 +41,8 @@ return { }, }, split = { - direction = "horizontal", - anchor = "bottom", + direction = "horizontal", --- (vertical)|(horizontal) + anchor = "bottom", --- (right)|(bottom) size = "20", }, }, diff --git a/lua/vern-nvim/configs/java.lua b/lua/vern-nvim/configs/java.lua index 288922d..1df9171 100644 --- a/lua/vern-nvim/configs/java.lua +++ b/lua/vern-nvim/configs/java.lua @@ -2,6 +2,14 @@ local M = {} M.attach = function() end +M.create_project = function() end + +---@type VernCreateFile +M.create_file = { + allowed = false, + func = function(_, _) return nil end, +} + ---@type VernLanguageConfig M.java = { root = "Java", @@ -9,13 +17,7 @@ M.java = { src = "src/com/{company_name}/{project_name}", bin = "bin", }, - keybinds = { - ["pr"] = { - user_command = "JavaRun", - desc = "Executes java code", - action = require("vern-nvim.actions.java").run, - }, - }, + keybinds = {}, templates = { project = { primary = { diff --git a/lua/vern-nvim/configs/lua.lua b/lua/vern-nvim/configs/lua.lua index c0d049e..e559ed1 100644 --- a/lua/vern-nvim/configs/lua.lua +++ b/lua/vern-nvim/configs/lua.lua @@ -2,19 +2,21 @@ local M = {} M.attach = function() end +M.create_project = function() end + +---@type VernCreateFile +M.create_file = { + allowed = false, + func = function(_, _) return nil end, +} + ---@type VernLanguageConfig M.lua = { root = "Lua", dirs = { src = "lua/{project_name}", }, - keybinds = { - ["pr"] = { - user_command = "LuaRun", - desc = "Executes lua code", - action = require("vern-nvim.actions.lua").run, - }, - }, + keybinds = {}, templates = { project = { primary = { diff --git a/lua/vern-nvim/configs/python.lua b/lua/vern-nvim/configs/python.lua index 9ba202a..d779a3f 100644 --- a/lua/vern-nvim/configs/python.lua +++ b/lua/vern-nvim/configs/python.lua @@ -2,9 +2,7 @@ local M = {} M.attach = function() local root = require("vern-nvim.utils").proj_root() - if not root then - return - end + if not root then return end local venv_dir = nil if vim.uv.fs_stat(root .. "/venv/bin/python") then venv_dir = root .. "/venv" @@ -17,6 +15,24 @@ M.attach = function() end end +---@param root string +M.create_project = function(root) + local obj = vim.system({ "python3", "-m", "venv", root, "/venv" }):wait() + if obj.code ~= 0 then + vim.notify( + "Virtual environment failed to initialize " .. obj.stderr, + vim.log.levels.ERROR, + { title = "Vern Python" } + ) + end +end + +---@type VernCreateFile +M.create_file = { + allowed = false, + func = function(_, _) return nil end, +} + ---@type VernLanguageConfig M.python = { root = "Python", @@ -24,13 +40,8 @@ M.python = { src = "src/{project_name}", }, keybinds = { - ["pr"] = { - user_command = "Pythonrun", - desc = "Executes python code", - action = require("vern-nvim.actions.python").run, - }, ["pi"] = { - user_command = "Pythonpip", + user_command = "VernPythonPip", desc = "Install python packages for environment", action = require("vern-nvim.actions.python").pip_install, }, diff --git a/lua/vern-nvim/configs/rust.lua b/lua/vern-nvim/configs/rust.lua new file mode 100644 index 0000000..ec81b99 --- /dev/null +++ b/lua/vern-nvim/configs/rust.lua @@ -0,0 +1,46 @@ +local M = {} + +M.attach = function() end + +M.create_project = function(root) + local obj = vim.system({ + "cargo", + "init", + vim.fn.expand(root), + "--name", + vim.fs.basename(root), + }):wait() + + if obj.code ~= 0 then + vim.notify("Cargo init failed:" .. obj.stderr, vim.log.levels.ERROR) + end +end + +---@type VernCreateFile +M.create_file = { + allowed = false, + func = function(_, _) return nil end, +} + +---@type VernLanguageConfig +M.rust = { + root = "Rust", + dirs = nil, + keybinds = {}, + templates = { + project = { + primary = { path = "src", name = "main.rs" }, + }, + file = { + source = { + path = "src", + name = "{file_name}.rs", + kind = "source", + kind_path = "vern-nvim.templates.rust", + }, + }, + extras = {}, + }, +} + +return M diff --git a/lua/vern-nvim/init.lua b/lua/vern-nvim/init.lua index 2603c71..1e0084d 100644 --- a/lua/vern-nvim/init.lua +++ b/lua/vern-nvim/init.lua @@ -17,52 +17,54 @@ 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, { + 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, { silent = true, desc = def.desc, }) end end - local group = vim.api.nvim_create_augroup("VernAutocommands", { clear = true }) + local group = + vim.api.nvim_create_augroup("VernAutocommands", { clear = true }) vim.api.nvim_create_autocmd("FileType", { group = group, pattern = "*", callback = function(args) local ft = vim.bo[args.buf].filetype - if not ft or ft == "" then - return - end - local has_config, mod = pcall(require, "vern-nvim.configs." .. ft) - if not has_config then - return - end + if not ft or ft == "" then return end + local ok, mod = pcall(require, "vern-nvim.configs." .. ft) + 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 for key, def in pairs(opts[ft].keybinds or {}) do if type(def.action) == "function" then - vim.api.nvim_buf_create_user_command(args.buf, def.user_command, function() - def.action(opts) - end, { nargs = "*", desc = def.desc }) - vim.keymap.set("n", key, function() - def.action(opts) - end, { - buffer = args.buf, - silent = true, - desc = def.desc, - }) + vim.api.nvim_buf_create_user_command( + args.buf, + def.user_command, + function() def.action(opts) end, + { nargs = "*", desc = def.desc } + ) + vim.keymap.set( + "n", + key, + function() def.action(opts) end, + { + buffer = args.buf, + silent = true, + desc = def.desc, + } + ) end end end - local ok, attach_mod = pcall(require, "vern-nvim.configs." .. ft) - if ok and type(attach_mod.attach) == "function" then - attach_mod.attach(args.buf, opts) + if ok and type(mod.attach) == "function" then + mod.attach(args.buf, opts) end end, desc = "Attach languages to buffer", diff --git a/lua/vern-nvim/templates/init.lua b/lua/vern-nvim/templates.lua similarity index 100% rename from lua/vern-nvim/templates/init.lua rename to lua/vern-nvim/templates.lua diff --git a/lua/vern-nvim/templates/extras/gitignore.lua b/lua/vern-nvim/templates/extras/gitignore.lua index d134bcf..ab0a20b 100644 --- a/lua/vern-nvim/templates/extras/gitignore.lua +++ b/lua/vern-nvim/templates/extras/gitignore.lua @@ -42,6 +42,15 @@ bin/ *.class *.jar ]=]), + rust = vim.trim([=[ +debug +target +**/*.rs.bk +*.pdb +**/mutants.out*/ +rustc-ice-*.txt +#.idea/ + ]=]), } local extra = (lang and lang_block[lang]) or "" diff --git a/lua/vern-nvim/templates/rust.lua b/lua/vern-nvim/templates/rust.lua new file mode 100644 index 0000000..5ea2088 --- /dev/null +++ b/lua/vern-nvim/templates/rust.lua @@ -0,0 +1,9 @@ +return { + --[[ ===================================================================== + TEMPLATE: SOURCE (Standalone File) + File: {name}.rs + Target: src/{name}.rs + ===================================================================== --]] + source = vim.trim([=[ +]=]) .. "\n", +} diff --git a/lua/vern-nvim/types.lua b/lua/vern-nvim/types.lua index 651d516..352bd21 100644 --- a/lua/vern-nvim/types.lua +++ b/lua/vern-nvim/types.lua @@ -2,6 +2,10 @@ ---@alias SplitDirection "vertical" | "horizontal" ---@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? + ---@class VernKeybindItem ---@field user_command string ---@field desc string @@ -39,20 +43,20 @@ ---@class VernFileLayout ---@field path string ---@field name string ----@field kind string ----@field kind_path string +---@field kind? string +---@field kind_path? string ---@class VernExtrasLayout : VernFileLayout ---@field enabled boolean ---@class VernLanguageTemplates ----@field project table ----@field file table ----@field extras table +---@field project table|nil +---@field file table|nil +---@field extras table|nil ---@class VernLanguageConfig ---@field root string ----@field dirs table +---@field dirs table|nil ---@field keybinds table ---@field templates VernLanguageTemplates @@ -70,3 +74,6 @@ --- ---@class VernJavaConfig : VernCoreConfig ---@field java? VernLanguageConfig + +---@class VernRustConfig : VernCoreConfig +---@field rust? VernLanguageConfig diff --git a/lua/vern-nvim/utils.lua b/lua/vern-nvim/utils.lua index 02f6cbf..47e2e75 100644 --- a/lua/vern-nvim/utils.lua +++ b/lua/vern-nvim/utils.lua @@ -11,11 +11,10 @@ end ---@param str string String to replace ---@param ctx table Replacment characters ---@return string -function M.fmt(str, ctx) - return (str:gsub("{([^}]+)}", ctx)) -end +function M.fmt(str, ctx) return (str:gsub("{([^}]+)}", ctx)) end ---@param bufnr? integer Default is 0 (current buffer) +---@return string function M.proj_root(bufnr) bufnr = bufnr or 0 local root_markers = { @@ -26,42 +25,48 @@ function M.proj_root(bufnr) "CMakeLists", "Cargo.toml", } - return vim.fs.root(bufnr, root_markers) or vim.uv.cwd() + return vim.fs.root(bufnr, root_markers) or vim.uv.cwd() or "." end +---@return string +function M.proj_name() return vim.fs.basename(M.proj_root()) or "" end + ---@param raw_path string Combination of tbl.path and tbl.name to override ---@param dir_tbl table Strings to replace raw directories in {dirs.<>} with ---@param ctx table Strings to replace raw names like {project_name} with ---@return string path Fully replaced path function M.resolve_path(raw_path, dir_tbl, ctx) --- Replace the {dirs.} - raw_path = raw_path:gsub("{dirs%.([%w_]+)}", function(dir_key) - return dir_tbl and dir_tbl[dir_key] or dir_key - end) + raw_path = raw_path:gsub( + "{dirs%.([%w_]+)}", + function(dir_key) return dir_tbl and dir_tbl[dir_key] or dir_key end + ) --- Replace the {} - local path = raw_path:gsub("{([%w_]+)}", function(key) - return ctx and ctx[key] or ("{" .. key .. "}") - end) + local path = raw_path:gsub( + "{([%w_]+)}", + function(key) return ctx and ctx[key] or ("{" .. key .. "}") end + ) return path end ----@return string -function M.proj_name() - return vim.fn.fnamemodify(M.proj_root(), ":t") -end - ---@param path string ---@param ft string function M.init_git(path, ft) ft = ft and vim.trim(ft) - if not ft or ft == "" then - return - end + if not ft or ft == "" then return end - local gitignore = require("vern-nvim.templates.extras.gitignore").template(ft) - vim.fn.system({ "git", "-C", path, "init" }) + local gitignore = + require("vern-nvim.templates.extras.gitignore").template(ft) + local obj = vim.system({ "git", "-C", path, "init" }):wait() + if obj.code ~= 0 then + vim.notify( + "Git init failed " .. obj.stderr, + vim.log.levels.ERROR, + { title = "Vern Git" } + ) + end M.dump(vim.fs.joinpath(path, ".gitignore"), gitignore) end @@ -98,7 +103,12 @@ function M.term(cmd, opts, scratch) local pos = { row = math.floor( - (ui.height - (vim.o.laststatus == 3 and 1 or 0) - vim.o.cmdheight - dim.height) / 2 + ( + ui.height + - (vim.o.laststatus == 3 and 1 or 0) + - vim.o.cmdheight + - dim.height + ) / 2 ), col = math.floor((ui.width - dim.width) / 2), } @@ -111,11 +121,11 @@ function M.term(cmd, opts, scratch) row = pos.row, col = pos.col, style = "minimal", - border = opts.mode.win.float.border.enabled and opts.mode.win.float.border.type or nil, + border = opts.mode.win.float.border.enabled + and opts.mode.win.float.border.type + or nil, }) - if scratch then - return buf, win - end + if scratch then return buf, win end end if opts.mode.type == "split" then if opts.mode.win.split.direction == "vertical" then @@ -135,9 +145,7 @@ function M.term(cmd, opts, scratch) win = vim.api.nvim_get_current_win() vim.api.nvim_win_set_buf(0, buf) - if scratch then - return buf, win - end + if scratch then return buf, win end end local full_cmd = type(cmd) == "table" and table.concat(cmd, " ") or cmd vim.fn.jobstart(full_cmd, { term = true }) @@ -151,4 +159,14 @@ function M.term(cmd, opts, scratch) return buf, win end +---@param buf integer +---@param lines table +function M.term_override(buf, lines) + if buf and vim.api.nvim_buf_is_valid(buf) then + vim.api.nvim_set_option_value("modifiable", true, { buf = buf }) + vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines) + vim.api.nvim_set_option_value("modifiable", false, { buf = buf }) + end +end + return M diff --git a/stylua.toml b/stylua.toml index cf3739d..2a7c855 100644 --- a/stylua.toml +++ b/stylua.toml @@ -1,7 +1,7 @@ # stylua.toml # How many characters per line before wrapping -column_width = 100 +column_width = 80 # Indentation style: "Tabs" or "Spaces" line_endings = "Unix" @@ -17,4 +17,5 @@ quote_style = "AutoPreferDouble" call_parentheses = "Always" # Sorting and formatting behavior -collapse_simple_statement = "Never" +# collapse_simple_statement = "Never" +collapse_simple_statement = "Always"