From c6ea4f4f8910cb4d37d9e719c4569d949b2ea0f1 Mon Sep 17 00:00:00 2001 From: DarthMooMancer <107283522+DarthMooMancer@users.noreply.github.com> Date: Mon, 12 May 2025 01:27:46 -0400 Subject: [PATCH] rework: removed old get functions from each language to use utils instead and changed run in each language to use new system to be simpler feat: added aux_files for c/c++ and reworked them to work with new update --- lua/Polydev/languages/c.lua | 21 +++------------ lua/Polydev/languages/cpp.lua | 46 +++----------------------------- lua/Polydev/languages/html.lua | 6 +---- lua/Polydev/languages/java.lua | 11 ++++---- lua/Polydev/languages/lua.lua | 32 ++-------------------- lua/Polydev/languages/python.lua | 37 +++---------------------- lua/Polydev/languages/rust.lua | 18 +++---------- lua/Polydev/templates.lua | 22 ++++++++++++--- 8 files changed, 40 insertions(+), 153 deletions(-) diff --git a/lua/Polydev/languages/c.lua b/lua/Polydev/languages/c.lua index 8034414..a46f5eb 100644 --- a/lua/Polydev/languages/c.lua +++ b/lua/Polydev/languages/c.lua @@ -17,19 +17,8 @@ function M.setup(opts) end end -function M.get_project_root() - local dir = vim.fn.expand("%:p:h") - while dir ~= "/" do - if vim.fn.isdirectory(dir .. "/src") == 1 or vim.fn.filereadable(dir .. "/CMakeLists.txt") == 1 then - return dir - end - dir = vim.fn.fnamemodify(dir, ":h") - end -end - function M.run() - local root = M.get_project_root() - if not root then return print("Error: Project root not found.") end + local root = utils.get_project_root() local build_dir = root .. "/build" local cmd = "cd " .. build_dir .. " && cmake --build ." @@ -43,12 +32,8 @@ function M.run() vim.list_extend({ "Error during compilation:" }, vim.split(output, "\n", { trimempty = true })) ) vim.api.nvim_buf_set_option(term_buf, "modifiable", false) - end - - if success then - local files = vim.fn.glob(build_dir .. "/*.polydev", true, true) - if #files == 0 then return print("Error: No .polydev file found in build directory.") end - utils.open_float_terminal("cd " .. build_dir .. " && ./" .. files[1]:match("([^/]+)%.polydev$")) + else + utils.open_float_terminal("cd " .. build_dir .. " && ./" .. utils.get_project_name()) end end diff --git a/lua/Polydev/languages/cpp.lua b/lua/Polydev/languages/cpp.lua index 125381a..20cd4e8 100644 --- a/lua/Polydev/languages/cpp.lua +++ b/lua/Polydev/languages/cpp.lua @@ -6,9 +6,7 @@ M.opts = nil function M.setup(opts) M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("cpp"), opts or {}) - vim.api.nvim_create_user_command("NewCppHeaderFile", M.create_new_header_file, {}) vim.api.nvim_create_user_command("CppRun", M.run, {}) - for key, command in pairs(M.opts.keybinds) do M.keybinds[command] = key end @@ -16,44 +14,10 @@ function M.setup(opts) if M.keybinds.CppRun then vim.keymap.set("n", M.keybinds.CppRun, ":CppRun", { silent = true }) end - if M.keybinds.NewCppHeaderFile then - vim.keymap.set("n", M.keybinds.NewCppHeaderFile, ":NewCppHeaderFile", { silent = true }) - end -end - -function M.get_project_root() - local result = vim.fs.find({ "src", "CMakeLists.txt" }, { - upward = true, - stop = vim.loop.os_homedir(), - path = vim.fs.dirname(vim.api.nvim_buf_get_name(0)), - }) - return result[1] and vim.fs.dirname(result[1]) or nil -end - -local root = M.get_project_root() - -function M.create_new_header_file() - vim.ui.input({ prompt = "Enter header file name: " }, function(header_name) - if not header_name then return print("Header file creation canceled.") end - if not root then return print("Error: Project root not found.") end - local guard_macro = header_name:upper():gsub("[^A-Z0-9]", "_") .. "_HPP" - local content = string.format([[ -#ifndef %s -#define %s - -#include - -// Function prototypes -void example_function(); - -#endif // %s -]], guard_macro, guard_macro, guard_macro) - utils.write_file(root .. "/include/" .. header_name .. ".hpp", content) - end) end function M.run() - if not root then return print("Error: Project root not found.") end + local root = utils.get_project_root() local build_dir = root .. "/build" local cmd = "cd " .. build_dir .. " && cmake --build ." @@ -67,12 +31,8 @@ function M.run() vim.list_extend({ "Error during compilation:" }, vim.split(output, "\n", { trimempty = true })) ) vim.api.nvim_buf_set_option(term_buf, "modifiable", false) - end - - if success then - local files = vim.fn.glob(build_dir .. "/*.polydev", true, true) - if #files == 0 then return print("Error: No .polydev file found in build directory.") end - utils.open_float_terminal("cd " .. build_dir .. " && ./" .. files[1]:match("([^/]+)%.polydev$")) + else + utils.open_float_terminal("cd " .. build_dir .. " && ./" .. utils.get_project_name()) end end diff --git a/lua/Polydev/languages/html.lua b/lua/Polydev/languages/html.lua index 40f526d..b230cf1 100644 --- a/lua/Polydev/languages/html.lua +++ b/lua/Polydev/languages/html.lua @@ -1,13 +1,9 @@ ----@type table local M = {} ----@type table|nil -M.opts = nil +M.opts = {} ----@param opts table function M.setup(opts) M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("html"), opts or {}) end ----@return table return M diff --git a/lua/Polydev/languages/java.lua b/lua/Polydev/languages/java.lua index 1b70a90..138bc13 100644 --- a/lua/Polydev/languages/java.lua +++ b/lua/Polydev/languages/java.lua @@ -2,7 +2,7 @@ local utils = require("Polydev.utils") local M = {} M.keybinds = {} -M.opts = nil +M.opts = {} function M.setup(opts) M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("java"), opts or {}) @@ -18,19 +18,18 @@ function M.setup(opts) end function M.run() - local project_root = vim.fn.expand("%:p:h"):match("(.*)/src") - if not project_root then return print("Error: Must be inside the 'src' directory.") end - local out_dir = project_root .. "/build" + local root = utils.get_project_root() + local out_dir = root .. "/build" if not utils.is_dir(out_dir) then vim.fn.mkdir(out_dir, "p") end - local compile_command = string.format("javac -d %s %s/*.java", out_dir, project_root .. "/src") + local compile_command = string.format("javac -d %s %s/*.java", out_dir, root .. "/src") local compile_status = vim.fn.system(compile_command) if vim.v.shell_error == 0 then - utils.open_float_terminal("java -cp " .. project_root .. "/build" .. " Main") + utils.open_float_terminal("java -cp " .. root .. "/build" .. " Main") else print("Error during compilation. Opening terminal for details...") local term_buf = utils.open_float_terminal(compile_command) diff --git a/lua/Polydev/languages/lua.lua b/lua/Polydev/languages/lua.lua index c86adc9..5da9518 100644 --- a/lua/Polydev/languages/lua.lua +++ b/lua/Polydev/languages/lua.lua @@ -1,16 +1,9 @@ ----@module "Polydev.utils" local utils = require("Polydev.utils") - ----@type table local M = {} ----@type table M.keybinds = {} +M.opts = {} ----@type table|nil -M.opts = nil - ----@param opts table function M.setup(opts) M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("lua"), opts or {}) for key, command in pairs(M.opts.keybinds) do @@ -25,31 +18,10 @@ function M.setup(opts) end function M.run() - ---@return string|nil - local function get_project_root() - local dir = vim.fn.expand("%:p:h") - while dir ~= "/" do - local polydev_file = vim.fn.glob(dir .. "/*.polydev") - if polydev_file ~= "" then return dir end - dir = vim.fn.fnamemodify(dir, ":h") - end - return nil - end - - ---@return string - local function get_project_name() - return vim.fn.glob((get_project_root() or "") .. "/*.polydev"):match("([^/]+)%.polydev$") - end - - ---@return string - if not get_project_root() or not get_project_name() then return print("Error: Project root not found.") end - local init_lua_path = get_project_root() .. "/lua/" .. get_project_name() .. "/init.lua" + local init_lua_path = utils.get_project_root() .. "/lua/" .. utils.get_project_name() .. "/init.lua" if vim.fn.filereadable(init_lua_path) == 1 then utils.open_float_terminal("lua " .. init_lua_path) - else - print("Error: " .. init_lua_path .. " not found in " .. get_project_root()) end end ----@return table return M diff --git a/lua/Polydev/languages/python.lua b/lua/Polydev/languages/python.lua index 87c28ff..b473dd4 100644 --- a/lua/Polydev/languages/python.lua +++ b/lua/Polydev/languages/python.lua @@ -1,26 +1,16 @@ ----@module "Polydev.utils" local utils = require("Polydev.utils") - ----@module "Polydev.configs" -local config = require("Polydev.configs") - ----@tyep table local M = {} ----@tyep table M.keybinds = {} +M.opts = {} ----@type nil|table -M.opts = nil - ----@param opts table function M.setup(opts) - M.opts = vim.tbl_deep_extend("force", {}, config.get("python"), opts or {}) + M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("python"), opts or {}) for key, command in pairs(M.opts.keybinds) do M.keybinds[command] = key end - local root = M.get_project_root() + local root = utils.get_project_root() if root then local venv_path = root .. "/venv/bin/activate" if vim.fn.filereadable(venv_path) == 1 then @@ -40,19 +30,6 @@ function M.setup(opts) end end ----@return string|nil -function M.get_project_root() - local dir = vim.fn.expand("%:p:h") - while dir ~= "/" do - if vim.fn.isdirectory(dir .. "/venv") == 1 or vim.fn.filereadable(dir .. "/main.py") == 1 then - return dir - end - dir = vim.fn.fnamemodify(dir, ":h") - end - return nil -end - ----@return string|nil function M.install_dependency() vim.ui.input({ prompt = "Enter pip module: " }, function(pip_module) if not pip_module or pip_module == "" then @@ -64,15 +41,10 @@ function M.install_dependency() end function M.run() - local root = M.get_project_root() - if not root then - ---@return nil - return print("Error: Project root not found.") - end + local root = utils.get_project_root() local main_file = root .. "/main.py" if vim.fn.filereadable(main_file) == 0 then - ---@return nil return print("Error: main.py not found in project root.") end @@ -91,5 +63,4 @@ function M.run() utils.open_float_terminal(venv_cmd .. "python3 " .. main_file) end ----@return table return M diff --git a/lua/Polydev/languages/rust.lua b/lua/Polydev/languages/rust.lua index a191487..51fc964 100644 --- a/lua/Polydev/languages/rust.lua +++ b/lua/Polydev/languages/rust.lua @@ -2,7 +2,7 @@ local utils = require("Polydev.utils") local M = {} M.keybinds = {} -M.opts = nil +M.opts = {} function M.setup(opts) M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("rust"), opts or {}) @@ -17,18 +17,8 @@ function M.setup(opts) end end -local function get_project_root() - local dir = vim.fn.expand("%:p:h") - while dir ~= "/" do - if vim.fn.isdirectory(dir .. "/src") == 1 then - return dir - end - dir = vim.fn.fnamemodify(dir, ":h") - end -end - function M.run() - local root = get_project_root() + local root = utils.get_project_root() if not root then return print("Error: Project root not found.") end local cmd = "cargo build" @@ -42,9 +32,7 @@ function M.run() vim.list_extend({ "Error during compilation:" }, vim.split(output, "\n", { trimempty = true })) ) vim.api.nvim_buf_set_option(term_buf, "modifiable", false) - end - - if success then + else utils.open_float_terminal("cargo run") end end diff --git a/lua/Polydev/templates.lua b/lua/Polydev/templates.lua index 610497a..a21ff66 100644 --- a/lua/Polydev/templates.lua +++ b/lua/Polydev/templates.lua @@ -231,8 +231,8 @@ public class %s { M.aux_files = { c = { run = function (file_name) - if not file_name then return print("Header file creation canceled.") end - local root_dir = utils.get_project_root() + if not file_name then return print("Header file creation canceled") end + local root = utils.get_project_root() local guard_macro = file_name:upper():gsub("[^A-Z0-9]", "_") .. "_H" local content = string.format([[ #ifndef %s @@ -242,7 +242,23 @@ M.aux_files = { #endif ]], guard_macro, guard_macro) - utils.write_file({ root_dir, "include", file_name .. ".h" }, content) + utils.write_file({ root, "include", file_name .. ".h" }, content) + end + }, + cpp = { + run = function(file_name) + if not file_name then return print("Header file creation canceled") end + local root = utils.get_project_root() + 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, guard_macro) + utils.write_file({ root, "include", file_name .. ".hpp" }, content) end } }