rework: top-level project folder is now changeable and organized templates
This commit is contained in:
+50
-44
@@ -2,60 +2,66 @@ local M = {}
|
||||
|
||||
M.defaults = {
|
||||
globals = {
|
||||
project_root = "~/Projects",
|
||||
keybinds = {
|
||||
["<leader>po"] = "PolydevManager",
|
||||
}
|
||||
},
|
||||
html = {
|
||||
project_root = "~/Projects/Html",
|
||||
},
|
||||
rust = {
|
||||
project_root = "~/Projects/Rust",
|
||||
keybinds = {
|
||||
["<leader>pr"] = "RustRun",
|
||||
},
|
||||
},
|
||||
java = {
|
||||
project_root = "~/Projects/Java",
|
||||
keybinds = {
|
||||
["<leader>pr"] = "JavaRun",
|
||||
},
|
||||
},
|
||||
python = {
|
||||
project_root = "~/Projects/Python",
|
||||
keybinds = {
|
||||
["<leader>pr"] = "PythonRun",
|
||||
["<leader>pb"] = "PythonPip",
|
||||
},
|
||||
},
|
||||
lua = {
|
||||
project_root = "~/Projects/Lua",
|
||||
keybinds = {
|
||||
["<leader>pr"] = "LuaRun",
|
||||
},
|
||||
},
|
||||
c = {
|
||||
project_root = "~/Projects/C",
|
||||
keybinds = {
|
||||
["<leader>pr"] = "CRun",
|
||||
},
|
||||
build_attributes = "",
|
||||
},
|
||||
cpp = {
|
||||
project_root = "~/Projects/Cpp",
|
||||
keybinds = {
|
||||
["<leader>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 = {
|
||||
["<leader>pr"] = "RustRun",
|
||||
},
|
||||
}
|
||||
|
||||
M.defaults.java = {
|
||||
project_root = M.defaults.globals.project_root .. "/Java",
|
||||
keybinds = {
|
||||
["<leader>pr"] = "JavaRun",
|
||||
},
|
||||
}
|
||||
|
||||
M.defaults.python = {
|
||||
project_root = M.defaults.globals.project_root .. "/Python",
|
||||
keybinds = {
|
||||
["<leader>pr"] = "PythonRun",
|
||||
["<leader>pb"] = "PythonPip",
|
||||
},
|
||||
}
|
||||
|
||||
M.defaults.lua = {
|
||||
project_root = M.defaults.globals.project_root .. "/Lua",
|
||||
keybinds = {
|
||||
["<leader>pr"] = "LuaRun",
|
||||
},
|
||||
}
|
||||
|
||||
M.defaults.c = {
|
||||
project_root = M.defaults.globals.project_root .. "/C",
|
||||
keybinds = {
|
||||
["<leader>pr"] = "CRun",
|
||||
},
|
||||
build_attributes = "",
|
||||
}
|
||||
|
||||
M.defaults.cpp = {
|
||||
project_root = M.defaults.globals.project_root .. "/CPP",
|
||||
keybinds = {
|
||||
["<leader>pr"] = "CppRun",
|
||||
},
|
||||
build_attributes = "",
|
||||
}
|
||||
|
||||
M.defaults.html = {
|
||||
project_root = M.defaults.globals.project_root .. "/Html",
|
||||
}
|
||||
|
||||
M.user_config = {}
|
||||
|
||||
function M.setup(user_opts)
|
||||
|
||||
@@ -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<CR>", { silent = true })
|
||||
break
|
||||
vim.keymap.set("n", key, ":PolydevOpen<CR>")
|
||||
end
|
||||
|
||||
vim.api.nvim_create_autocmd("FileType", {
|
||||
|
||||
+49
-79
@@ -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 = [[
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
@@ -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
|
||||
|
||||
+24
-15
@@ -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)
|
||||
|
||||
@@ -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", "<ESC>", "i<C-\\><C-n>:q<CR>", { noremap = true, silent = true })
|
||||
|
||||
Reference in New Issue
Block a user