From a2c43661c41a6a5b09e652e854623ae7bba301bf Mon Sep 17 00:00:00 2001 From: DarthMooMancer <107283522+DarthMooMancer@users.noreply.github.com> Date: Mon, 12 May 2025 08:43:42 -0400 Subject: [PATCH] rework: open_float_terminal uses table instead of string fix: all languages now use proper terminal setup --- lua/Polydev/languages/c.lua | 26 +++++---------- lua/Polydev/languages/cpp.lua | 25 +++++--------- lua/Polydev/languages/html.lua | 6 ++-- lua/Polydev/languages/java.lua | 26 +++++---------- lua/Polydev/languages/lua.lua | 18 +++------- lua/Polydev/languages/python.lua | 56 ++++++++------------------------ lua/Polydev/languages/rust.lua | 25 +++++--------- lua/Polydev/utils.lua | 3 +- 8 files changed, 55 insertions(+), 130 deletions(-) diff --git a/lua/Polydev/languages/c.lua b/lua/Polydev/languages/c.lua index a08277b..e6d3b75 100644 --- a/lua/Polydev/languages/c.lua +++ b/lua/Polydev/languages/c.lua @@ -1,31 +1,21 @@ local utils = require("Polydev.utils") local M = {} -M.keybinds = {} -M.opts = {} - -function M.setup(opts) - M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("c"), opts or {}) - for key, command in pairs(M.opts.keybinds) do - M.keybinds[command] = key - end - +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, {}) - - if M.keybinds.CRun then - vim.keymap.set("n", M.keybinds.CRun, ":CRun", { silent = true }) + 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 + local cmd = { "cd " .. root, "build", " && cmake --build ." } + local output = vim.fn.system(table.concat(cmd, "/")) - if not success then + if not vim.v.shell_error == 0 then local term_buf = utils.open_float_terminal(cmd) vim.api.nvim_buf_set_option(term_buf, "modifiable", true) vim.api.nvim_buf_set_lines(term_buf, 0, -1, false, @@ -33,7 +23,7 @@ function M.run() ) vim.api.nvim_buf_set_option(term_buf, "modifiable", false) else - utils.open_float_terminal("cd " .. build_dir .. " && ./" .. utils.get_project_name()) + utils.open_float_terminal({ "cd " .. root, "build" .. " && .", utils.get_project_name() }) end end diff --git a/lua/Polydev/languages/cpp.lua b/lua/Polydev/languages/cpp.lua index c9222c3..13ed270 100644 --- a/lua/Polydev/languages/cpp.lua +++ b/lua/Polydev/languages/cpp.lua @@ -1,30 +1,21 @@ local utils = require("Polydev.utils") local M = {} -M.keybinds = {} -M.opts = {} - -function M.setup(opts) - M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("cpp"), opts or {}) +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, command in pairs(M.opts.keybinds) do - M.keybinds[command] = key - end - - if M.keybinds.CppRun then - vim.keymap.set("n", M.keybinds.CppRun, ":CppRun", { silent = true }) + 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 + local cmd = { "cd " .. root, "build", " && cmake --build ." } + local output = vim.fn.system(table.concat(cmd, "/")) - if not success then + if not vim.v.shell_error == 0 then local term_buf = utils.open_float_terminal(cmd) vim.api.nvim_buf_set_option(term_buf, "modifiable", true) vim.api.nvim_buf_set_lines(term_buf, 0, -1, false, @@ -32,7 +23,7 @@ function M.run() ) vim.api.nvim_buf_set_option(term_buf, "modifiable", false) else - utils.open_float_terminal("cd " .. build_dir .. " && ./" .. utils.get_project_name()) + utils.open_float_terminal({ "cd " .. root, "build" .. " && .", utils.get_project_name() }) end end diff --git a/lua/Polydev/languages/html.lua b/lua/Polydev/languages/html.lua index b230cf1..9c05c25 100644 --- a/lua/Polydev/languages/html.lua +++ b/lua/Polydev/languages/html.lua @@ -1,9 +1,7 @@ local M = {} -M.opts = {} - -function M.setup(opts) - M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("html"), opts or {}) +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 index 76f17c0..b314049 100644 --- a/lua/Polydev/languages/java.lua +++ b/lua/Polydev/languages/java.lua @@ -1,39 +1,29 @@ local utils = require("Polydev.utils") local M = {} -M.keybinds = {} -M.opts = {} - -function M.setup(opts) - M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("java"), opts or {}) - for key, command in pairs(M.opts.keybinds) do - M.keybinds[command] = key - end - +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, {}) - - if M.keybinds.JavaRun then - vim.keymap.set("n", M.keybinds.JavaRun, ":JavaRun", { silent = true }) + 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 out_dir = root .. "/build" - local cmd = string.format("javac -d %s %s", out_dir, root .. "/src/*.java") + local cmd = string.format("javac -d %s %s", root .. "/build", root .. "/src/*.java") local output = vim.fn.system(cmd) - local success = vim.v.shell_error == 0 - if not success then - local term_buf = utils.open_float_terminal(cmd) + if not vim.v.shell_error == 0 then + local term_buf = utils.open_float_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.open_float_terminal("java -cp " .. root .. "/build" .. " Main") + utils.open_float_terminal({ "java -cp " .. root, "build" .. " Main" }) end end diff --git a/lua/Polydev/languages/lua.lua b/lua/Polydev/languages/lua.lua index 5da9518..25e517a 100644 --- a/lua/Polydev/languages/lua.lua +++ b/lua/Polydev/languages/lua.lua @@ -1,26 +1,18 @@ local utils = require("Polydev.utils") local M = {} -M.keybinds = {} -M.opts = {} - -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 - M.keybinds[command] = key - end - +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, {}) - - if M.keybinds.LuaRun then - vim.keymap.set("n", M.keybinds.LuaRun, ":LuaRun", { silent = true }) + 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.open_float_terminal("lua " .. init_lua_path) + utils.open_float_terminal({ "lua " .. init_lua_path }) end end diff --git a/lua/Polydev/languages/python.lua b/lua/Polydev/languages/python.lua index b473dd4..61251b4 100644 --- a/lua/Polydev/languages/python.lua +++ b/lua/Polydev/languages/python.lua @@ -1,13 +1,17 @@ local utils = require("Polydev.utils") local M = {} -M.keybinds = {} -M.opts = {} - -function M.setup(opts) - 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 +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() @@ -18,49 +22,17 @@ function M.setup(opts) vim.env.PATH = root .. "/venv/bin:" .. vim.env.PATH end end - - vim.api.nvim_create_user_command("PythonPip", M.install_dependency, {}) - vim.api.nvim_create_user_command("PythonRun", M.run, {}) - - if M.keybinds.PythonRun then - vim.keymap.set("n", M.keybinds.PythonRun, ":PythonRun", { silent = true }) - end - if M.keybinds.PythonPip then - vim.keymap.set("n", M.keybinds.PythonPip, ":PythonPip", { silent = true }) - 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.open_float_terminal("pip install " .. pip_module) + if not pip_module or pip_module == "" then return print("Pip canceled") end + utils.open_float_terminal({ "pip install " .. pip_module }) end) end function M.run() - local root = utils.get_project_root() - - local main_file = root .. "/main.py" - if vim.fn.filereadable(main_file) == 0 then - return print("Error: main.py not found in project root.") - end - - -- Detect current shell - local shell = vim.fn.getenv("SHELL") or "" - local venv_cmd - - if shell:match("fish") then - local venv_fish = root .. "/venv/bin/activate.fish" - venv_cmd = vim.fn.filereadable(venv_fish) == 1 and "source " .. venv_fish .. " && " or "" - else - local venv_bash = root .. "/venv/bin/activate" - venv_cmd = vim.fn.filereadable(venv_bash) == 1 and "source " .. venv_bash .. " && " or "" - end - - utils.open_float_terminal(venv_cmd .. "python3 " .. main_file) + utils.open_float_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 index 51fc964..de382a5 100644 --- a/lua/Polydev/languages/rust.lua +++ b/lua/Polydev/languages/rust.lua @@ -1,19 +1,11 @@ local utils = require("Polydev.utils") local M = {} -M.keybinds = {} -M.opts = {} - -function M.setup(opts) - M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("rust"), opts or {}) - for key, command in pairs(M.opts.keybinds) do - M.keybinds[command] = key - end - +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, {}) - - if M.keybinds.RustRun then - vim.keymap.set("n", M.keybinds.RustRun, ":RustRun", { silent = true }) + for key, _ in pairs(opts.keybinds) do + vim.keymap.set("n", key, ":RustRun", { silent = true }) end end @@ -21,11 +13,10 @@ 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(cmd) - local success = vim.v.shell_error == 0 + local cmd = { "cargo build" } + local output = vim.fn.system(table.concat(cmd)) - if not success then + if not vim.v.shell_error == 0 then local term_buf = utils.open_float_terminal(cmd) vim.api.nvim_buf_set_option(term_buf, "modifiable", true) vim.api.nvim_buf_set_lines(term_buf, 0, -1, false, @@ -33,7 +24,7 @@ function M.run() ) vim.api.nvim_buf_set_option(term_buf, "modifiable", false) else - utils.open_float_terminal("cargo run") + utils.open_float_terminal({ "cargo run" }) end end diff --git a/lua/Polydev/utils.lua b/lua/Polydev/utils.lua index f41c72c..0056dfe 100644 --- a/lua/Polydev/utils.lua +++ b/lua/Polydev/utils.lua @@ -42,6 +42,7 @@ function M.init_git(path, gitignore_lines) end end +---@param cmd table function M.open_float_terminal(cmd) local presets = require("Polydev.presets") local presets_opts = presets.getPresets(M.opts.preset) @@ -73,7 +74,7 @@ function M.open_float_terminal(cmd) end end - vim.fn.termopen(cmd) + vim.fn.termopen(table.concat(cmd, "/")) vim.api.nvim_buf_set_keymap(buf, "n", "", "i:q", { noremap = true, silent = true }) return buf, win end