From fa7851a787a197869e027beabc70cb2e8026754f Mon Sep 17 00:00:00 2001 From: DarthMooMancer <107283522+DarthMooMancer@users.noreply.github.com> Date: Wed, 14 May 2025 16:27:53 -0400 Subject: [PATCH] rework: top-level project folder is now changeable and organized templates --- lua/Polydev/configs.lua | 94 +++++++++++++++------------- lua/Polydev/init.lua | 7 +-- lua/Polydev/templates.lua | 128 +++++++++++++++----------------------- lua/Polydev/ui.lua | 39 +++++++----- lua/Polydev/utils.lua | 12 +--- 5 files changed, 129 insertions(+), 151 deletions(-) diff --git a/lua/Polydev/configs.lua b/lua/Polydev/configs.lua index 99bac19..1a61e5b 100644 --- a/lua/Polydev/configs.lua +++ b/lua/Polydev/configs.lua @@ -2,60 +2,66 @@ local M = {} M.defaults = { globals = { + project_root = "~/Projects", keybinds = { ["po"] = "PolydevManager", } }, - html = { - project_root = "~/Projects/Html", - }, - rust = { - project_root = "~/Projects/Rust", - keybinds = { - ["pr"] = "RustRun", - }, - }, - java = { - project_root = "~/Projects/Java", - keybinds = { - ["pr"] = "JavaRun", - }, - }, - python = { - project_root = "~/Projects/Python", - keybinds = { - ["pr"] = "PythonRun", - ["pb"] = "PythonPip", - }, - }, - lua = { - project_root = "~/Projects/Lua", - keybinds = { - ["pr"] = "LuaRun", - }, - }, - c = { - project_root = "~/Projects/C", - keybinds = { - ["pr"] = "CRun", - }, - build_attributes = "", - }, - cpp = { - project_root = "~/Projects/Cpp", - keybinds = { - ["pr"] = "CppRun", - }, - build_attributes = "", - }, terminal = { preset = "max", border = true, - number = true, - relativenumber = true, } } +M.defaults.rust = { + project_root = M.defaults.globals.project_root .. "/Rust", + keybinds = { + ["pr"] = "RustRun", + }, +} + +M.defaults.java = { + project_root = M.defaults.globals.project_root .. "/Java", + keybinds = { + ["pr"] = "JavaRun", + }, +} + +M.defaults.python = { + project_root = M.defaults.globals.project_root .. "/Python", + keybinds = { + ["pr"] = "PythonRun", + ["pb"] = "PythonPip", + }, +} + +M.defaults.lua = { + project_root = M.defaults.globals.project_root .. "/Lua", + keybinds = { + ["pr"] = "LuaRun", + }, +} + +M.defaults.c = { + project_root = M.defaults.globals.project_root .. "/C", + keybinds = { + ["pr"] = "CRun", + }, + build_attributes = "", +} + +M.defaults.cpp = { + project_root = M.defaults.globals.project_root .. "/CPP", + keybinds = { + ["pr"] = "CppRun", + }, + build_attributes = "", +} + +M.defaults.html = { + project_root = M.defaults.globals.project_root .. "/Html", +} + M.user_config = {} function M.setup(user_opts) diff --git a/lua/Polydev/init.lua b/lua/Polydev/init.lua index 7fc6025..2140876 100644 --- a/lua/Polydev/init.lua +++ b/lua/Polydev/init.lua @@ -9,12 +9,11 @@ function M.setup(opts) config.user_config[lang] = vim.tbl_deep_extend("force", config.defaults[lang] or {}, user_opts) end - vim.api.nvim_create_user_command("PolydevOpen", ui.manager, {}) + vim.api.nvim_create_user_command("PolydevOpen", function() ui.manager(config.get("globals").project_root) end, {}) - local keys = vim.tbl_deep_extend("force", {}, config.get("globals").keybinds, opts or {}) + local keys = vim.tbl_deep_extend("force", {}, config.get("globals").keybinds, opts.keybinds or {}) for key, _ in pairs(keys) do - vim.keymap.set("n", key, ":PolydevOpen", { silent = true }) - break + vim.keymap.set("n", key, ":PolydevOpen") end vim.api.nvim_create_autocmd("FileType", { diff --git a/lua/Polydev/templates.lua b/lua/Polydev/templates.lua index 7e8c519..5df692f 100644 --- a/lua/Polydev/templates.lua +++ b/lua/Polydev/templates.lua @@ -2,7 +2,35 @@ local utils = require("Polydev.utils") local M = {} +local function aux_file(guard, file_type, file_name) + if not file_name then return print("Header file creation canceled") end + local guard_macro = file_name:upper():gsub("[^A-Z0-9]", "_") .. guard + local content = string.format([[ +#ifndef %s +#define %s + +// Add code here + +#endif +]], guard_macro, guard_macro) + utils.write_file({ utils.get_project_root(), "include", file_name .. file_type }, content) +end + M.projects = { + lua = { + run = function(project_name, project_root) + local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name + vim.fn.mkdir(full_project_root .. "/lua/" .. project_name, "p") + utils.write_file({ full_project_root, "lua", project_name, "init.lua" }, [[ +local M = {} + +return M +]]) + local gitignore = { "" } + utils.init_git(full_project_root, gitignore) + vim.cmd("edit " .. full_project_root .. "/lua/" .. project_name .. "/init.lua") + end + }, java = { run = function(project_name, project_root) local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name @@ -58,20 +86,6 @@ setup( print("Virtual environment activated for " .. project_name) end }, - lua = { - run = function(project_name, project_root) - local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name - vim.fn.mkdir(full_project_root .. "/lua/" .. project_name, "p") - utils.write_file({ full_project_root, "lua", project_name, "init.lua" }, [[ -local M = {} - -return M -]]) - local gitignore = { "" } - utils.init_git(full_project_root, gitignore) - vim.cmd("edit " .. full_project_root .. "/lua/" .. project_name .. "/init.lua") - end - }, c = { run = function(project_name, project_root) local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name @@ -147,6 +161,9 @@ set(CMAKE_C_STANDARD_REQUIRED ON) html = { run = function(project_name, project_root) local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name + if vim.fn.isdirectory(full_project_root) == 0 then + vim.fn.mkdir(full_project_root, "p") -- Create directory with parent directories + end local main_html_content = [[ @@ -167,10 +184,14 @@ set(CMAKE_C_STANDARD_REQUIRED ON) } M.files = { + lua = { + run = function (file_name) + utils.write_file({ utils.get_project_root(), "lua", utils.get_project_name(), file_name .. ".lua"}, "") + end + }, java = { run = function (file_name) - local root_dir = utils.get_project_root() - utils.write_file({ root_dir, "src", file_name .. ".java" }, string.format([[ + utils.write_file({ utils.get_project_root(), "src", file_name .. ".java" }, string.format([[ public class %s { // New File } @@ -179,33 +200,23 @@ public class %s { }, python = { run = function (file_name) - local root_dir = utils.get_project_root() - utils.write_file({ root_dir, "include", file_name .. ".py" }, "") - end - }, - lua = { - run = function (file_name) - local root = utils.get_project_root() - utils.write_file({ root, "lua", utils.get_project_name(), file_name .. ".lua"}, "") + utils.write_file({ utils.get_project_root(), "include", file_name .. ".py" }, "") end }, c = { run = function (file_name) - local root_dir = utils.get_project_root() - utils.write_file({ root_dir, "src", file_name .. ".c" }, "") + utils.write_file({ utils.get_project_root(), "src", file_name .. ".c" }, "") end }, cpp = { run = function (file_name) - local root_dir = utils.get_project_root() - utils.write_file({ root_dir, "src", file_name .. ".cpp" }, "") + utils.write_file({ utils.get_project_root(), "src", file_name .. ".cpp" }, "") end }, rust = { run = function (file_name) - local root_dir = utils.get_project_root() - utils.write_file({ root_dir, "src", file_name .. ".rs" }, "") + utils.write_file({ utils.get_project_root(), "src", file_name .. ".rs" }, "") end }, html = { @@ -229,57 +240,16 @@ public class %s { } M.aux_files = { - c = { - run = function (file_name) - if not file_name then return print("Header file creation canceled") end - local guard_macro = file_name:upper():gsub("[^A-Z0-9]", "_") .. "_H" - local content = string.format([[ -#ifndef %s -#define %s - -// Add code here - -#endif -]], guard_macro, guard_macro) - utils.write_file({ utils.get_project_root(), "include", file_name .. ".h" }, content) + run = function (lang, file_name) + if lang == "c" then + aux_file("_H", ".h", file_name) + print("Successful creation of " .. file_name .. ".h") end - }, - cpp = { - run = function(file_name) - if not file_name then return print("Header file creation canceled") end - local guard_macro = file_name:upper():gsub("[^A-Z0-9]", "_") .. "_HPP" - local content = string.format([[ -#ifndef %s -#define %s - -// Add code here - -#endif -]], guard_macro, guard_macro) - utils.write_file({ utils.get_project_root(), "include", file_name .. ".hpp" }, content) + if lang == "cpp" then + aux_file("_HPP", ".hpp", file_name) + print("Successful creation of " .. file_name .. ".hpp") end - } + end } -function M.create_project(lang, project_root) - vim.ui.input({ prompt = "Enter project name: " }, function(project_name) - if not project_name or project_name == "" then return print("Project creation canceled") end - M.projects[lang].run(project_name, project_root) - end) -end - -function M.create_new_file(lang) - vim.ui.input({ prompt = "Enter file name: " }, function(file_name) - if not file_name or file_name == "" then return print("File creation canceled") end - M.files[lang].run(file_name) - end) -end - -function M.create_aux_file(lang) - vim.ui.input({ promt = "Enter file name: "}, function(file_name) - if not file_name or file_name == "" then return print("File creation canceled") end - M.aux_files[lang].run(file_name) - end) -end - return M diff --git a/lua/Polydev/ui.lua b/lua/Polydev/ui.lua index 28980fc..d022409 100644 --- a/lua/Polydev/ui.lua +++ b/lua/Polydev/ui.lua @@ -62,7 +62,7 @@ function M.load_language_module(lang, opts) return true end -function M.manager() +function M.manager(project_root) local templates = require("Polydev.templates") local Popup = require("nui.popup") local Layout = require("nui.layout") @@ -118,16 +118,16 @@ function M.manager() position = "50%", size = { width = "70%", height = "70%" }, }, - Layout.Box({ - Layout.Box(search, { size = "10%" }), Layout.Box({ + Layout.Box(search, { size = "10%" }), Layout.Box({ - Layout.Box(popup, { size = "55%" }), - Layout.Box(keybinds, {size = "45%" }), - }, { dir = "col", size = "50%" }), - Layout.Box(preview, { size = "50%" }), - }, { dir = "row", size = "90%" }), - }, { dir = "col" }) + Layout.Box({ + Layout.Box(popup, { size = "55%" }), + Layout.Box(keybinds, {size = "45%" }), + }, { dir = "col", size = "50%" }), + Layout.Box(preview, { size = "50%" }), + }, { dir = "row", size = "90%" }), + }, { dir = "col" }) ) layout:mount() @@ -181,7 +181,7 @@ function M.manager() local function refresh() entries = get_entries(cwd) - local root = vim.fn.expand("~/Projects") + local root = vim.fn.expand(project_root) -- If we're not in the Projects folder, redirect to Projects if not cwd:find(root, 1, true) then @@ -275,7 +275,7 @@ function M.manager() popup:map("n", "D", function() local entry = get_selected_entry() if not entry.is_up then - vim.ui.input({ prompt = "(y/N) Defaults to N: " .. entry.name }, function(input) + vim.ui.input({ prompt = "Deletion of " .. entry.name .. ": (y/N) Defaults to N: " }, function(input) if input == "y" or input == "Y" then vim.fn.delete(entry.full_path, "rf") refresh() @@ -292,7 +292,10 @@ function M.manager() if M.load_language_module(lang) and templates.files[lang] and templates.files[lang].run then popup:unmount() vim.schedule(function() - templates.create_new_file(lang) + vim.ui.input({ prompt = "Enter file name: " }, function(file_name) + if not file_name or file_name == "" then return print("File creation canceled") end + templates.files[lang].run(file_name) + end) end) else print("Error: No File creation method for " .. lang) @@ -303,10 +306,13 @@ function M.manager() popup:map("n", "x", function() vim.ui.input({ prompt = "Enter language for new auxilary file: "}, function(lang) if not lang or lang == "" then return print("File create canceled") end - if M.load_language_module(lang) and templates.aux_files[lang] and templates.aux_files[lang].run then + if M.load_language_module(lang) and templates.aux_files.run then popup:unmount() vim.schedule(function() - templates.create_aux_file(lang) + vim.ui.input({ prompt = "Enter file name: "}, function(file_name) + if not file_name or file_name == "" then return print("File creation canceled") end + templates.aux_files.run(lang, file_name) + end) end) else print("Error: No File creation method for " .. lang) @@ -323,7 +329,10 @@ function M.manager() if M.load_language_module(lang) and templates.projects[lang] and templates.projects[lang].run then popup:unmount() vim.schedule(function() - templates.create_project(lang, opts.project_root) + vim.ui.input({ prompt = "Enter project name: " }, function(project_name) + if not project_name or project_name == "" then return print("Project creation canceled") end + templates.projects[lang].run(project_name, opts.project_root) + end) end) else print("Error: No project creation method for " .. lang) diff --git a/lua/Polydev/utils.lua b/lua/Polydev/utils.lua index b5c07e2..62933d5 100644 --- a/lua/Polydev/utils.lua +++ b/lua/Polydev/utils.lua @@ -43,9 +43,9 @@ function M.init_git(path, gitignore_lines) vim.fn.system({ "git", "-C", path, "init" }) if gitignore_lines and #gitignore_lines > 0 then - local gitignore_path = { path, ".gitignore" } - local contents = table.concat(gitignore_lines, "\n") - M.write_file(gitignore_path, contents) + local gitignore_path = { path, ".gitignore" } + local contents = table.concat(gitignore_lines, "\n") + M.write_file(gitignore_path, contents) end end @@ -74,12 +74,6 @@ function M.terminal(cmd) vim.api.nvim_set_option_value("winhighlight", "Normal:Pmenu,FloatBorder:Pmenu", { win = win }) vim.api.nvim_set_option_value("cursorline", true, { win = win }) vim.api.nvim_set_option_value("scrolloff", 5, { win = win }) - if(M.opts.number) then - vim.api.nvim_set_option_value("number", true, { win = win }) - if(M.opts.relativenumber) then - vim.api.nvim_set_option_value("relativenumber", true, { win = win }) - end - end vim.fn.termopen(table.concat(cmd, "/")) vim.api.nvim_buf_set_keymap(buf, "n", "", "i:q", { noremap = true, silent = true })