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
This commit is contained in:
DarthMooMancer
2025-05-12 01:27:46 -04:00
parent 8584ed4bbf
commit c6ea4f4f89
8 changed files with 40 additions and 153 deletions
+3 -18
View File
@@ -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
+3 -43
View File
@@ -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<CR>", { silent = true })
end
if M.keybinds.NewCppHeaderFile then
vim.keymap.set("n", M.keybinds.NewCppHeaderFile, ":NewCppHeaderFile<CR>", { 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 <iostream>
// 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
+1 -5
View File
@@ -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
+5 -6
View File
@@ -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)
+2 -30
View File
@@ -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
+4 -33
View File
@@ -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
+3 -15
View File
@@ -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