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:
@@ -17,19 +17,8 @@ function M.setup(opts)
|
|||||||
end
|
end
|
||||||
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()
|
function M.run()
|
||||||
local root = M.get_project_root()
|
local root = utils.get_project_root()
|
||||||
if not root then return print("Error: Project root not found.") end
|
|
||||||
local build_dir = root .. "/build"
|
local build_dir = root .. "/build"
|
||||||
|
|
||||||
local cmd = "cd " .. build_dir .. " && cmake --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.list_extend({ "Error during compilation:" }, vim.split(output, "\n", { trimempty = true }))
|
||||||
)
|
)
|
||||||
vim.api.nvim_buf_set_option(term_buf, "modifiable", false)
|
vim.api.nvim_buf_set_option(term_buf, "modifiable", false)
|
||||||
end
|
else
|
||||||
|
utils.open_float_terminal("cd " .. build_dir .. " && ./" .. utils.get_project_name())
|
||||||
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$"))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -6,9 +6,7 @@ M.opts = nil
|
|||||||
|
|
||||||
function M.setup(opts)
|
function M.setup(opts)
|
||||||
M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("cpp"), opts or {})
|
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, {})
|
vim.api.nvim_create_user_command("CppRun", M.run, {})
|
||||||
|
|
||||||
for key, command in pairs(M.opts.keybinds) do
|
for key, command in pairs(M.opts.keybinds) do
|
||||||
M.keybinds[command] = key
|
M.keybinds[command] = key
|
||||||
end
|
end
|
||||||
@@ -16,44 +14,10 @@ function M.setup(opts)
|
|||||||
if M.keybinds.CppRun then
|
if M.keybinds.CppRun then
|
||||||
vim.keymap.set("n", M.keybinds.CppRun, ":CppRun<CR>", { silent = true })
|
vim.keymap.set("n", M.keybinds.CppRun, ":CppRun<CR>", { silent = true })
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
function M.run()
|
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 build_dir = root .. "/build"
|
||||||
|
|
||||||
local cmd = "cd " .. build_dir .. " && cmake --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.list_extend({ "Error during compilation:" }, vim.split(output, "\n", { trimempty = true }))
|
||||||
)
|
)
|
||||||
vim.api.nvim_buf_set_option(term_buf, "modifiable", false)
|
vim.api.nvim_buf_set_option(term_buf, "modifiable", false)
|
||||||
end
|
else
|
||||||
|
utils.open_float_terminal("cd " .. build_dir .. " && ./" .. utils.get_project_name())
|
||||||
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$"))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,9 @@
|
|||||||
---@type table
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
---@type table|nil
|
M.opts = {}
|
||||||
M.opts = nil
|
|
||||||
|
|
||||||
---@param opts table
|
|
||||||
function M.setup(opts)
|
function M.setup(opts)
|
||||||
M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("html"), opts or {})
|
M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("html"), opts or {})
|
||||||
end
|
end
|
||||||
|
|
||||||
---@return table
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ local utils = require("Polydev.utils")
|
|||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
M.keybinds = {}
|
M.keybinds = {}
|
||||||
M.opts = nil
|
M.opts = {}
|
||||||
|
|
||||||
function M.setup(opts)
|
function M.setup(opts)
|
||||||
M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("java"), opts or {})
|
M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("java"), opts or {})
|
||||||
@@ -18,19 +18,18 @@ function M.setup(opts)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function M.run()
|
function M.run()
|
||||||
local project_root = vim.fn.expand("%:p:h"):match("(.*)/src")
|
local root = utils.get_project_root()
|
||||||
if not project_root then return print("Error: Must be inside the 'src' directory.") end
|
local out_dir = root .. "/build"
|
||||||
local out_dir = project_root .. "/build"
|
|
||||||
|
|
||||||
if not utils.is_dir(out_dir) then
|
if not utils.is_dir(out_dir) then
|
||||||
vim.fn.mkdir(out_dir, "p")
|
vim.fn.mkdir(out_dir, "p")
|
||||||
end
|
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)
|
local compile_status = vim.fn.system(compile_command)
|
||||||
|
|
||||||
if vim.v.shell_error == 0 then
|
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
|
else
|
||||||
print("Error during compilation. Opening terminal for details...")
|
print("Error during compilation. Opening terminal for details...")
|
||||||
local term_buf = utils.open_float_terminal(compile_command)
|
local term_buf = utils.open_float_terminal(compile_command)
|
||||||
|
|||||||
@@ -1,16 +1,9 @@
|
|||||||
---@module "Polydev.utils"
|
|
||||||
local utils = require("Polydev.utils")
|
local utils = require("Polydev.utils")
|
||||||
|
|
||||||
---@type table
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
---@type table
|
|
||||||
M.keybinds = {}
|
M.keybinds = {}
|
||||||
|
M.opts = {}
|
||||||
|
|
||||||
---@type table|nil
|
|
||||||
M.opts = nil
|
|
||||||
|
|
||||||
---@param opts table
|
|
||||||
function M.setup(opts)
|
function M.setup(opts)
|
||||||
M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("lua"), opts or {})
|
M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("lua"), opts or {})
|
||||||
for key, command in pairs(M.opts.keybinds) do
|
for key, command in pairs(M.opts.keybinds) do
|
||||||
@@ -25,31 +18,10 @@ function M.setup(opts)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function M.run()
|
function M.run()
|
||||||
---@return string|nil
|
local init_lua_path = utils.get_project_root() .. "/lua/" .. utils.get_project_name() .. "/init.lua"
|
||||||
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"
|
|
||||||
if vim.fn.filereadable(init_lua_path) == 1 then
|
if vim.fn.filereadable(init_lua_path) == 1 then
|
||||||
utils.open_float_terminal("lua " .. init_lua_path)
|
utils.open_float_terminal("lua " .. init_lua_path)
|
||||||
else
|
|
||||||
print("Error: " .. init_lua_path .. " not found in " .. get_project_root())
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@return table
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
@@ -1,26 +1,16 @@
|
|||||||
---@module "Polydev.utils"
|
|
||||||
local utils = require("Polydev.utils")
|
local utils = require("Polydev.utils")
|
||||||
|
|
||||||
---@module "Polydev.configs"
|
|
||||||
local config = require("Polydev.configs")
|
|
||||||
|
|
||||||
---@tyep table
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
---@tyep table
|
|
||||||
M.keybinds = {}
|
M.keybinds = {}
|
||||||
|
M.opts = {}
|
||||||
|
|
||||||
---@type nil|table
|
|
||||||
M.opts = nil
|
|
||||||
|
|
||||||
---@param opts table
|
|
||||||
function M.setup(opts)
|
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
|
for key, command in pairs(M.opts.keybinds) do
|
||||||
M.keybinds[command] = key
|
M.keybinds[command] = key
|
||||||
end
|
end
|
||||||
|
|
||||||
local root = M.get_project_root()
|
local root = utils.get_project_root()
|
||||||
if root then
|
if root then
|
||||||
local venv_path = root .. "/venv/bin/activate"
|
local venv_path = root .. "/venv/bin/activate"
|
||||||
if vim.fn.filereadable(venv_path) == 1 then
|
if vim.fn.filereadable(venv_path) == 1 then
|
||||||
@@ -40,19 +30,6 @@ function M.setup(opts)
|
|||||||
end
|
end
|
||||||
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()
|
function M.install_dependency()
|
||||||
vim.ui.input({ prompt = "Enter pip module: " }, function(pip_module)
|
vim.ui.input({ prompt = "Enter pip module: " }, function(pip_module)
|
||||||
if not pip_module or pip_module == "" then
|
if not pip_module or pip_module == "" then
|
||||||
@@ -64,15 +41,10 @@ function M.install_dependency()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function M.run()
|
function M.run()
|
||||||
local root = M.get_project_root()
|
local root = utils.get_project_root()
|
||||||
if not root then
|
|
||||||
---@return nil
|
|
||||||
return print("Error: Project root not found.")
|
|
||||||
end
|
|
||||||
|
|
||||||
local main_file = root .. "/main.py"
|
local main_file = root .. "/main.py"
|
||||||
if vim.fn.filereadable(main_file) == 0 then
|
if vim.fn.filereadable(main_file) == 0 then
|
||||||
---@return nil
|
|
||||||
return print("Error: main.py not found in project root.")
|
return print("Error: main.py not found in project root.")
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -91,5 +63,4 @@ function M.run()
|
|||||||
utils.open_float_terminal(venv_cmd .. "python3 " .. main_file)
|
utils.open_float_terminal(venv_cmd .. "python3 " .. main_file)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@return table
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ local utils = require("Polydev.utils")
|
|||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
M.keybinds = {}
|
M.keybinds = {}
|
||||||
M.opts = nil
|
M.opts = {}
|
||||||
|
|
||||||
function M.setup(opts)
|
function M.setup(opts)
|
||||||
M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("rust"), opts or {})
|
M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("rust"), opts or {})
|
||||||
@@ -17,18 +17,8 @@ function M.setup(opts)
|
|||||||
end
|
end
|
||||||
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()
|
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
|
if not root then return print("Error: Project root not found.") end
|
||||||
|
|
||||||
local cmd = "cargo build"
|
local cmd = "cargo build"
|
||||||
@@ -42,9 +32,7 @@ function M.run()
|
|||||||
vim.list_extend({ "Error during compilation:" }, vim.split(output, "\n", { trimempty = true }))
|
vim.list_extend({ "Error during compilation:" }, vim.split(output, "\n", { trimempty = true }))
|
||||||
)
|
)
|
||||||
vim.api.nvim_buf_set_option(term_buf, "modifiable", false)
|
vim.api.nvim_buf_set_option(term_buf, "modifiable", false)
|
||||||
end
|
else
|
||||||
|
|
||||||
if success then
|
|
||||||
utils.open_float_terminal("cargo run")
|
utils.open_float_terminal("cargo run")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -231,8 +231,8 @@ public class %s {
|
|||||||
M.aux_files = {
|
M.aux_files = {
|
||||||
c = {
|
c = {
|
||||||
run = function (file_name)
|
run = function (file_name)
|
||||||
if not file_name then return print("Header file creation canceled.") end
|
if not file_name then return print("Header file creation canceled") end
|
||||||
local root_dir = utils.get_project_root()
|
local root = utils.get_project_root()
|
||||||
local guard_macro = file_name:upper():gsub("[^A-Z0-9]", "_") .. "_H"
|
local guard_macro = file_name:upper():gsub("[^A-Z0-9]", "_") .. "_H"
|
||||||
local content = string.format([[
|
local content = string.format([[
|
||||||
#ifndef %s
|
#ifndef %s
|
||||||
@@ -242,7 +242,23 @@ M.aux_files = {
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
]], guard_macro, guard_macro)
|
]], 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
|
end
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user