diff --git a/lua/Polydev/languages.lua b/lua/Polydev/languages.lua new file mode 100644 index 0000000..bb11965 --- /dev/null +++ b/lua/Polydev/languages.lua @@ -0,0 +1,174 @@ +local utils = require("Polydev.utils") +local configs = require("Polydev.configs") +local M = {} + +M.languages = { + java = { + setup = function (self, user_opts) + local opts = vim.tbl_deep_extend("force", {}, configs.get("java"), user_opts or {}) + vim.api.nvim_create_user_command("JavaRun", function() self:run() end, {}) + for key, _ in pairs(opts.keybinds) do + vim.keymap.set("n", key, ":JavaRun", { silent = true }) + end + end, + run = function () + local root = utils.get_project_root() + + local cmd = string.format("javac -d %s %s", root .. "/build", root .. "/src/*.java") + local output = vim.fn.system(cmd) + + if not vim.v.shell_error == 0 then + local term_buf = utils.terminal({ cmd }) + vim.api.nvim_buf_set_option(term_buf, "modifiable", true) + vim.api.nvim_buf_set_lines(term_buf, 0, -1, false, + vim.list_extend({ "Error during compilation:" }, vim.split(output, "\n", { trimempty = true })) + ) + vim.api.nvim_buf_set_option(term_buf, "modifiable", false) + else + utils.terminal({ "java -cp " .. root, "build" .. " Main" }) + end + end + }, + c = { + setup = function (self, user_opts) + local opts = vim.tbl_deep_extend("force", {}, configs.get("c"), user_opts or {}) + vim.api.nvim_create_user_command("CRun", function () self:run() end, {}) + for key, _ in pairs(opts.keybinds) do + vim.keymap.set("n", key, ":CRun", { silent = true }) + end + end, + run = function () + local root = utils.get_project_root() + local build_dir = root .. "/build" + + local cmd = "cd '" .. build_dir .. "' && cmake --build ." + local output = vim.fn.system(cmd) + local success = vim.v.shell_error == 0 + + if not success then + local term_buf = utils.terminal({ cmd }) + vim.api.nvim_buf_set_option(term_buf, "modifiable", true) + vim.api.nvim_buf_set_lines(term_buf, 0, -1, false, + 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 run_cmd = "cd '" .. build_dir .. "' && ./" .. utils.get_project_name() + utils.terminal({ run_cmd }) + end + end + }, + cpp = { + setup = function (self, user_opts) + local opts = vim.tbl_deep_extend("force", {}, configs.get("cpp"), user_opts or {}) + vim.api.nvim_create_user_command("CppRun", function () self:run() end, {}) + for key, _ in pairs(opts.keybinds) do + vim.keymap.set("n", key, ":CppRun", { silent = true }) + end + end, + run = function () + local root = utils.get_project_root() + local build_dir = root .. "/build" + + local cmd = "cd '" .. build_dir .. "' && cmake --build ." + local output = vim.fn.system(cmd) + local success = vim.v.shell_error == 0 + + if not success then + local term_buf = utils.terminal({ cmd }) + vim.api.nvim_buf_set_option(term_buf, "modifiable", true) + vim.api.nvim_buf_set_lines(term_buf, 0, -1, false, + 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 run_cmd = "cd '" .. build_dir .. "' && ./" .. utils.get_project_name() + utils.terminal({ run_cmd }) + end + end + }, + html = { + setup = function (user_opts) + vim.tbl_deep_extend("force", {}, configs.get("html"), user_opts or {}) + end + }, + rust = { + setup = function (self, user_opts) + local opts = vim.tbl_deep_extend("force", {}, configs.get("rust"), user_opts or {}) + vim.api.nvim_create_user_command("RustRun", function () self:run() end, {}) + for key, _ in pairs(opts.keybinds) do + vim.keymap.set("n", key, ":RustRun", { silent = true }) + end + end, + run = function () + local root = utils.get_project_root() + if not root then return print("Error: Project root not found.") end + + local cmd = { "cargo build" } + local output = vim.fn.system(table.concat(cmd)) + + if not vim.v.shell_error == 0 then + local term_buf = utils.terminal(cmd) + vim.api.nvim_buf_set_option(term_buf, "modifiable", true) + vim.api.nvim_buf_set_lines(term_buf, 0, -1, false, + vim.list_extend({ "Error during compilation:" }, vim.split(output, "\n", { trimempty = true })) + ) + vim.api.nvim_buf_set_option(term_buf, "modifiable", false) + else + utils.terminal({ "cargo run" }) + end + end + }, + python = { + setup = function(self, user_opts) + local opts = vim.tbl_deep_extend("force", {}, configs.get("python"), user_opts or {}) + vim.api.nvim_create_user_command("PythonPip", function () self:install_dependency() end, {}) + vim.api.nvim_create_user_command("PythonRun", function () self:run() end, {}) + for key, command in pairs(opts.keybinds) do + if command == "PythonRun" then + vim.keymap.set("n", key, ":PythonRun", { silent = true }) + end + if command == "PythonPip" then + vim.keymap.set("n", key, ":PythonPip", { silent = true }) + end + end + + local root = utils.get_project_root() + if root then + local venv_path = root .. "/venv/bin/activate" + if vim.fn.filereadable(venv_path) == 1 then + vim.env.VIRTUAL_ENV = venv_path + vim.env.PATH = root .. "/venv/bin:" .. vim.env.PATH + end + end + end, + install_dependency = function () + vim.ui.input({ prompt = "Enter pip module: " }, function(pip_module) + if not pip_module or pip_module == "" then return print("Pip canceled") end + utils.terminal({ "pip install " .. pip_module }) + end) + end, + run = function () + utils.terminal({ "python3 " .. utils.get_project_root(), "main.py" }) + end + }, + lua = { + setup = function (self, user_opts) + local opts = vim.tbl_deep_extend("force", {}, configs.get("lua"), user_opts or {}) + vim.api.nvim_create_user_command("LuaRun", function () self:run() end, {}) + for key, _ in pairs(opts.keybinds) do + vim.keymap.set("n", key, ":LuaRun", { silent = true }) + end + end, + run = function () + 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.terminal({ "lua " .. init_lua_path }) + end + end + }, +} + +return M diff --git a/lua/Polydev/languages/c.lua b/lua/Polydev/languages/c.lua deleted file mode 100644 index 42f3c1c..0000000 --- a/lua/Polydev/languages/c.lua +++ /dev/null @@ -1,34 +0,0 @@ -local utils = require("Polydev.utils") -local M = {} - -function M.setup(user_opts) - local opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("c"), user_opts or {}) - vim.api.nvim_create_user_command("CRun", M.run, {}) - for key, _ in pairs(opts.keybinds) do - vim.keymap.set("n", key, ":CRun", { silent = true }) - end -end - -function M.run() - local root = utils.get_project_root() - local build_dir = root .. "/build" - - local cmd = "cd " .. build_dir .. " && cmake --build ." - local output = vim.fn.system(cmd) - local success = vim.v.shell_error == 0 - - if not success then - local term_buf = utils.terminal(cmd) - vim.api.nvim_buf_set_option(term_buf, "modifiable", true) - vim.api.nvim_buf_set_lines(term_buf, 0, -1, false, - 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 run_cmd = "cd " .. build_dir .. " && ./" .. utils.get_project_name() - utils.terminal({ run_cmd }) - end -end - -return M diff --git a/lua/Polydev/languages/cpp.lua b/lua/Polydev/languages/cpp.lua deleted file mode 100644 index 242b151..0000000 --- a/lua/Polydev/languages/cpp.lua +++ /dev/null @@ -1,34 +0,0 @@ -local utils = require("Polydev.utils") -local M = {} - -function M.setup(user_opts) - local opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("cpp"), user_opts or {}) - vim.api.nvim_create_user_command("CppRun", M.run, {}) - for key, _ in pairs(opts.keybinds) do - vim.keymap.set("n", key, ":CppRun", { silent = true }) - end -end - -function M.run() - local root = utils.get_project_root() - local build_dir = root .. "/build" - - local cmd = "cd " .. build_dir .. " && cmake --build ." - local output = vim.fn.system(cmd) - local success = vim.v.shell_error == 0 - - if not success then - local term_buf = utils.terminal({ cmd }) - vim.api.nvim_buf_set_option(term_buf, "modifiable", true) - vim.api.nvim_buf_set_lines(term_buf, 0, -1, false, - 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 run_cmd = "cd " .. build_dir .. " && ./" .. utils.get_project_name() - utils.terminal({ run_cmd }) - end -end - -return M diff --git a/lua/Polydev/languages/html.lua b/lua/Polydev/languages/html.lua deleted file mode 100644 index 9c05c25..0000000 --- a/lua/Polydev/languages/html.lua +++ /dev/null @@ -1,7 +0,0 @@ -local M = {} - -function M.setup(user_opts) - local opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("html"), user_opts or {}) -end - -return M diff --git a/lua/Polydev/languages/java.lua b/lua/Polydev/languages/java.lua deleted file mode 100644 index 7785370..0000000 --- a/lua/Polydev/languages/java.lua +++ /dev/null @@ -1,30 +0,0 @@ -local utils = require("Polydev.utils") -local M = {} - -function M.setup(user_opts) - local opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("java"), user_opts or {}) - vim.api.nvim_create_user_command("JavaRun", M.run, {}) - for key, _ in pairs(opts.keybinds) do - vim.keymap.set("n", key, ":JavaRun", { silent = true }) - end -end - -function M.run() - local root = utils.get_project_root() - - local cmd = string.format("javac -d %s %s", root .. "/build", root .. "/src/*.java") - local output = vim.fn.system(cmd) - - if not vim.v.shell_error == 0 then - local term_buf = utils.terminal({ cmd }) - vim.api.nvim_buf_set_option(term_buf, "modifiable", true) - vim.api.nvim_buf_set_lines(term_buf, 0, -1, false, - vim.list_extend({ "Error during compilation:" }, vim.split(output, "\n", { trimempty = true })) - ) - vim.api.nvim_buf_set_option(term_buf, "modifiable", false) - else - utils.terminal({ "java -cp " .. root, "build" .. " Main" }) - end -end - -return M diff --git a/lua/Polydev/languages/lua.lua b/lua/Polydev/languages/lua.lua deleted file mode 100644 index 4cf81ec..0000000 --- a/lua/Polydev/languages/lua.lua +++ /dev/null @@ -1,19 +0,0 @@ -local utils = require("Polydev.utils") -local M = {} - -function M.setup(user_opts) - local opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("lua"), user_opts or {}) - vim.api.nvim_create_user_command("LuaRun", M.run, {}) - for key, _ in pairs(opts.keybinds) do - vim.keymap.set("n", key, ":LuaRun", { silent = true }) - end -end - -function M.run() - 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.terminal({ "lua " .. init_lua_path }) - end -end - -return M diff --git a/lua/Polydev/languages/python.lua b/lua/Polydev/languages/python.lua deleted file mode 100644 index 81b5fdd..0000000 --- a/lua/Polydev/languages/python.lua +++ /dev/null @@ -1,38 +0,0 @@ -local utils = require("Polydev.utils") -local M = {} - -function M.setup(user_opts) - local opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("python"), user_opts or {}) - vim.api.nvim_create_user_command("PythonPip", M.install_dependency, {}) - vim.api.nvim_create_user_command("PythonRun", M.run, {}) - for key, command in pairs(opts.keybinds) do - if command == "PythonRun" then - vim.keymap.set("n", key, ":PythonRun", { silent = true }) - end - if command == "PythonPip" then - vim.keymap.set("n", key, ":PythonPip", { silent = true }) - end - end - - local root = utils.get_project_root() - if root then - local venv_path = root .. "/venv/bin/activate" - if vim.fn.filereadable(venv_path) == 1 then - vim.env.VIRTUAL_ENV = venv_path - vim.env.PATH = root .. "/venv/bin:" .. vim.env.PATH - end - end -end - -function M.install_dependency() - vim.ui.input({ prompt = "Enter pip module: " }, function(pip_module) - if not pip_module or pip_module == "" then return print("Pip canceled") end - utils.terminal({ "pip install " .. pip_module }) - end) -end - -function M.run() - utils.terminal({ "python3 " .. utils.get_project_root(), "main.py" }) -end - -return M diff --git a/lua/Polydev/languages/rust.lua b/lua/Polydev/languages/rust.lua deleted file mode 100644 index 2e1b8bb..0000000 --- a/lua/Polydev/languages/rust.lua +++ /dev/null @@ -1,31 +0,0 @@ -local utils = require("Polydev.utils") -local M = {} - -function M.setup(user_opts) - local opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("rust"), user_opts or {}) - vim.api.nvim_create_user_command("RustRun", M.run, {}) - for key, _ in pairs(opts.keybinds) do - vim.keymap.set("n", key, ":RustRun", { silent = true }) - end -end - -function M.run() - local root = utils.get_project_root() - if not root then return print("Error: Project root not found.") end - - local cmd = { "cargo build" } - local output = vim.fn.system(table.concat(cmd)) - - if not vim.v.shell_error == 0 then - local term_buf = utils.terminal(cmd) - vim.api.nvim_buf_set_option(term_buf, "modifiable", true) - vim.api.nvim_buf_set_lines(term_buf, 0, -1, false, - vim.list_extend({ "Error during compilation:" }, vim.split(output, "\n", { trimempty = true })) - ) - vim.api.nvim_buf_set_option(term_buf, "modifiable", false) - else - utils.terminal({ "cargo run" }) - end -end - -return M diff --git a/lua/Polydev/ui.lua b/lua/Polydev/ui.lua index 8f86f92..4c9491e 100644 --- a/lua/Polydev/ui.lua +++ b/lua/Polydev/ui.lua @@ -63,11 +63,18 @@ local loaded_languages = {} function M.load_language_module(lang, opts) if loaded_languages[lang] then return true end - local ok, mod = pcall(require, "Polydev.languages." .. lang) - if not ok or type(mod.setup) ~= "function" then return false end + local ok, mod = pcall(require, "Polydev.languages") + if not ok or type(mod.languages) ~= "table" then + return false + end - mod.setup(opts and opts[lang] or {}) - loaded_languages[lang] = mod + local lang_mod = mod.languages[lang] + if type(lang_mod) ~= "table" or type(lang_mod.setup) ~= "function" then + return false + end + + lang_mod:setup(opts and opts[lang] or {}) + loaded_languages[lang] = lang_mod return true end @@ -308,6 +315,7 @@ function M.manager(project_root) if not lang or lang == "" then return print("Project creation canceled") end opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get(lang), opts or {}) if M.load_language_module(lang) and templates.projects[lang] and templates.projects[lang].run then + -- if templates.projects[lang] and templates.projects[lang].run then popup:unmount() vim.schedule(function() vim.ui.input({ prompt = "Enter project name: " }, function(project_name)