rework: top-level project folder is now changeable and organized templates

This commit is contained in:
DarthMooMancer
2025-05-14 16:27:53 -04:00
parent 632add5f4a
commit fa7851a787
5 changed files with 129 additions and 151 deletions
+32 -26
View File
@@ -2,58 +2,64 @@ local M = {}
M.defaults = { M.defaults = {
globals = { globals = {
project_root = "~/Projects",
keybinds = { keybinds = {
["<leader>po"] = "PolydevManager", ["<leader>po"] = "PolydevManager",
} }
}, },
html = { terminal = {
project_root = "~/Projects/Html", preset = "max",
}, border = true,
rust = { }
project_root = "~/Projects/Rust", }
M.defaults.rust = {
project_root = M.defaults.globals.project_root .. "/Rust",
keybinds = { keybinds = {
["<leader>pr"] = "RustRun", ["<leader>pr"] = "RustRun",
}, },
}, }
java = {
project_root = "~/Projects/Java", M.defaults.java = {
project_root = M.defaults.globals.project_root .. "/Java",
keybinds = { keybinds = {
["<leader>pr"] = "JavaRun", ["<leader>pr"] = "JavaRun",
}, },
}, }
python = {
project_root = "~/Projects/Python", M.defaults.python = {
project_root = M.defaults.globals.project_root .. "/Python",
keybinds = { keybinds = {
["<leader>pr"] = "PythonRun", ["<leader>pr"] = "PythonRun",
["<leader>pb"] = "PythonPip", ["<leader>pb"] = "PythonPip",
}, },
}, }
lua = {
project_root = "~/Projects/Lua", M.defaults.lua = {
project_root = M.defaults.globals.project_root .. "/Lua",
keybinds = { keybinds = {
["<leader>pr"] = "LuaRun", ["<leader>pr"] = "LuaRun",
}, },
}, }
c = {
project_root = "~/Projects/C", M.defaults.c = {
project_root = M.defaults.globals.project_root .. "/C",
keybinds = { keybinds = {
["<leader>pr"] = "CRun", ["<leader>pr"] = "CRun",
}, },
build_attributes = "", build_attributes = "",
}, }
cpp = {
project_root = "~/Projects/Cpp", M.defaults.cpp = {
project_root = M.defaults.globals.project_root .. "/CPP",
keybinds = { keybinds = {
["<leader>pr"] = "CppRun", ["<leader>pr"] = "CppRun",
}, },
build_attributes = "", build_attributes = "",
},
terminal = {
preset = "max",
border = true,
number = true,
relativenumber = true,
} }
M.defaults.html = {
project_root = M.defaults.globals.project_root .. "/Html",
} }
M.user_config = {} M.user_config = {}
+3 -4
View File
@@ -9,12 +9,11 @@ function M.setup(opts)
config.user_config[lang] = vim.tbl_deep_extend("force", config.defaults[lang] or {}, user_opts) config.user_config[lang] = vim.tbl_deep_extend("force", config.defaults[lang] or {}, user_opts)
end 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 for key, _ in pairs(keys) do
vim.keymap.set("n", key, ":PolydevOpen<CR>", { silent = true }) vim.keymap.set("n", key, ":PolydevOpen<CR>")
break
end end
vim.api.nvim_create_autocmd("FileType", { vim.api.nvim_create_autocmd("FileType", {
+49 -79
View File
@@ -2,7 +2,35 @@ local utils = require("Polydev.utils")
local M = {} 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 = { 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 = { java = {
run = function(project_name, project_root) run = function(project_name, project_root)
local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name
@@ -58,20 +86,6 @@ setup(
print("Virtual environment activated for " .. project_name) print("Virtual environment activated for " .. project_name)
end 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 = { c = {
run = function(project_name, project_root) run = function(project_name, project_root)
local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name
@@ -147,6 +161,9 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
html = { html = {
run = function(project_name, project_root) run = function(project_name, project_root)
local full_project_root = vim.fn.expand(project_root) .. "/" .. project_name 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 = [[ local main_html_content = [[
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
@@ -167,10 +184,14 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
} }
M.files = { M.files = {
lua = {
run = function (file_name)
utils.write_file({ utils.get_project_root(), "lua", utils.get_project_name(), file_name .. ".lua"}, "")
end
},
java = { java = {
run = function (file_name) run = function (file_name)
local root_dir = utils.get_project_root() utils.write_file({ utils.get_project_root(), "src", file_name .. ".java" }, string.format([[
utils.write_file({ root_dir, "src", file_name .. ".java" }, string.format([[
public class %s { public class %s {
// New File // New File
} }
@@ -179,33 +200,23 @@ public class %s {
}, },
python = { python = {
run = function (file_name) run = function (file_name)
local root_dir = utils.get_project_root() utils.write_file({ utils.get_project_root(), "include", file_name .. ".py" }, "")
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"}, "")
end end
}, },
c = { c = {
run = function (file_name) run = function (file_name)
local root_dir = utils.get_project_root() utils.write_file({ utils.get_project_root(), "src", file_name .. ".c" }, "")
utils.write_file({ root_dir, "src", file_name .. ".c" }, "")
end end
}, },
cpp = { cpp = {
run = function (file_name) run = function (file_name)
local root_dir = utils.get_project_root() utils.write_file({ utils.get_project_root(), "src", file_name .. ".cpp" }, "")
utils.write_file({ root_dir, "src", file_name .. ".cpp" }, "")
end end
}, },
rust = { rust = {
run = function (file_name) run = function (file_name)
local root_dir = utils.get_project_root() utils.write_file({ utils.get_project_root(), "src", file_name .. ".rs" }, "")
utils.write_file({ root_dir, "src", file_name .. ".rs" }, "")
end end
}, },
html = { html = {
@@ -229,57 +240,16 @@ public class %s {
} }
M.aux_files = { M.aux_files = {
c = { run = function (lang, file_name)
run = function (file_name) if lang == "c" then
if not file_name then return print("Header file creation canceled") end aux_file("_H", ".h", file_name)
local guard_macro = file_name:upper():gsub("[^A-Z0-9]", "_") .. "_H" print("Successful creation of " .. file_name .. ".h")
local content = string.format([[ end
#ifndef %s if lang == "cpp" then
#define %s aux_file("_HPP", ".hpp", file_name)
print("Successful creation of " .. file_name .. ".hpp")
// Add code here
#endif
]], guard_macro, guard_macro)
utils.write_file({ utils.get_project_root(), "include", file_name .. ".h" }, content)
end 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)
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 return M
+16 -7
View File
@@ -62,7 +62,7 @@ function M.load_language_module(lang, opts)
return true return true
end end
function M.manager() function M.manager(project_root)
local templates = require("Polydev.templates") local templates = require("Polydev.templates")
local Popup = require("nui.popup") local Popup = require("nui.popup")
local Layout = require("nui.layout") local Layout = require("nui.layout")
@@ -181,7 +181,7 @@ function M.manager()
local function refresh() local function refresh()
entries = get_entries(cwd) 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 we're not in the Projects folder, redirect to Projects
if not cwd:find(root, 1, true) then if not cwd:find(root, 1, true) then
@@ -275,7 +275,7 @@ function M.manager()
popup:map("n", "D", function() popup:map("n", "D", function()
local entry = get_selected_entry() local entry = get_selected_entry()
if not entry.is_up then 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 if input == "y" or input == "Y" then
vim.fn.delete(entry.full_path, "rf") vim.fn.delete(entry.full_path, "rf")
refresh() refresh()
@@ -292,7 +292,10 @@ function M.manager()
if M.load_language_module(lang) and templates.files[lang] and templates.files[lang].run then if M.load_language_module(lang) and templates.files[lang] and templates.files[lang].run then
popup:unmount() popup:unmount()
vim.schedule(function() 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) end)
else else
print("Error: No File creation method for " .. lang) print("Error: No File creation method for " .. lang)
@@ -303,10 +306,13 @@ function M.manager()
popup:map("n", "x", function() popup:map("n", "x", function()
vim.ui.input({ prompt = "Enter language for new auxilary file: "}, function(lang) 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 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() popup:unmount()
vim.schedule(function() 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) end)
else else
print("Error: No File creation method for " .. lang) 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 if M.load_language_module(lang) and templates.projects[lang] and templates.projects[lang].run then
popup:unmount() popup:unmount()
vim.schedule(function() 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) end)
else else
print("Error: No project creation method for " .. lang) print("Error: No project creation method for " .. lang)
-6
View File
@@ -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("winhighlight", "Normal:Pmenu,FloatBorder:Pmenu", { win = win })
vim.api.nvim_set_option_value("cursorline", true, { win = win }) vim.api.nvim_set_option_value("cursorline", true, { win = win })
vim.api.nvim_set_option_value("scrolloff", 5, { 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.fn.termopen(table.concat(cmd, "/"))
vim.api.nvim_buf_set_keymap(buf, "n", "<ESC>", "i<C-\\><C-n>:q<CR>", { noremap = true, silent = true }) vim.api.nvim_buf_set_keymap(buf, "n", "<ESC>", "i<C-\\><C-n>:q<CR>", { noremap = true, silent = true })