rework: open_float_terminal uses table instead of string

fix: all languages now use proper terminal setup
This commit is contained in:
DarthMooMancer
2025-05-12 08:43:42 -04:00
parent 4ddbe07a30
commit a2c43661c4
8 changed files with 55 additions and 130 deletions
+8 -18
View File
@@ -1,31 +1,21 @@
local utils = require("Polydev.utils") local utils = require("Polydev.utils")
local M = {} local M = {}
M.keybinds = {} function M.setup(user_opts)
M.opts = {} local opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("c"), user_opts or {})
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
vim.api.nvim_create_user_command("CRun", M.run, {}) vim.api.nvim_create_user_command("CRun", M.run, {})
for key, _ in pairs(opts.keybinds) do
if M.keybinds.CRun then vim.keymap.set("n", key, ":CRun<CR>", { silent = true })
vim.keymap.set("n", M.keybinds.CRun, ":CRun<CR>", { silent = true })
end end
end end
function M.run() function M.run()
local root = utils.get_project_root() local root = utils.get_project_root()
local build_dir = root .. "/build"
local cmd = "cd " .. build_dir .. " && cmake --build ." local cmd = { "cd " .. root, "build", " && cmake --build ." }
local output = vim.fn.system(cmd) local output = vim.fn.system(table.concat(cmd, "/"))
local success = vim.v.shell_error == 0
if not success then if not vim.v.shell_error == 0 then
local term_buf = utils.open_float_terminal(cmd) local term_buf = utils.open_float_terminal(cmd)
vim.api.nvim_buf_set_option(term_buf, "modifiable", true) vim.api.nvim_buf_set_option(term_buf, "modifiable", true)
vim.api.nvim_buf_set_lines(term_buf, 0, -1, false, 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) vim.api.nvim_buf_set_option(term_buf, "modifiable", false)
else 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
end end
+8 -17
View File
@@ -1,30 +1,21 @@
local utils = require("Polydev.utils") local utils = require("Polydev.utils")
local M = {} local M = {}
M.keybinds = {} function M.setup(user_opts)
M.opts = {} local opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("cpp"), user_opts or {})
function M.setup(opts)
M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("cpp"), opts or {})
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, _ in pairs(opts.keybinds) do
M.keybinds[command] = key vim.keymap.set("n", key, ":CppRun<CR>", { silent = true })
end
if M.keybinds.CppRun then
vim.keymap.set("n", M.keybinds.CppRun, ":CppRun<CR>", { silent = true })
end end
end end
function M.run() function M.run()
local root = utils.get_project_root() local root = utils.get_project_root()
local build_dir = root .. "/build"
local cmd = "cd " .. build_dir .. " && cmake --build ." local cmd = { "cd " .. root, "build", " && cmake --build ." }
local output = vim.fn.system(cmd) local output = vim.fn.system(table.concat(cmd, "/"))
local success = vim.v.shell_error == 0
if not success then if not vim.v.shell_error == 0 then
local term_buf = utils.open_float_terminal(cmd) local term_buf = utils.open_float_terminal(cmd)
vim.api.nvim_buf_set_option(term_buf, "modifiable", true) vim.api.nvim_buf_set_option(term_buf, "modifiable", true)
vim.api.nvim_buf_set_lines(term_buf, 0, -1, false, 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) vim.api.nvim_buf_set_option(term_buf, "modifiable", false)
else 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
end end
+2 -4
View File
@@ -1,9 +1,7 @@
local M = {} local M = {}
M.opts = {} function M.setup(user_opts)
local opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("html"), user_opts or {})
function M.setup(opts)
M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("html"), opts or {})
end end
return M return M
+8 -18
View File
@@ -1,39 +1,29 @@
local utils = require("Polydev.utils") local utils = require("Polydev.utils")
local M = {} local M = {}
M.keybinds = {} function M.setup(user_opts)
M.opts = {} local opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("java"), user_opts or {})
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
vim.api.nvim_create_user_command("JavaRun", M.run, {}) vim.api.nvim_create_user_command("JavaRun", M.run, {})
for key, _ in pairs(opts.keybinds) do
if M.keybinds.JavaRun then vim.keymap.set("n", key, ":JavaRun<CR>", { silent = true })
vim.keymap.set("n", M.keybinds.JavaRun, ":JavaRun<CR>", { silent = true })
end end
end end
function M.run() function M.run()
local root = utils.get_project_root() 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 output = vim.fn.system(cmd)
local success = vim.v.shell_error == 0
if not success then if not vim.v.shell_error == 0 then
local term_buf = utils.open_float_terminal(cmd) local term_buf = utils.open_float_terminal({ cmd })
vim.api.nvim_buf_set_option(term_buf, "modifiable", true) vim.api.nvim_buf_set_option(term_buf, "modifiable", true)
vim.api.nvim_buf_set_lines(term_buf, 0, -1, false, vim.api.nvim_buf_set_lines(term_buf, 0, -1, false,
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)
else else
utils.open_float_terminal("java -cp " .. root .. "/build" .. " Main") utils.open_float_terminal({ "java -cp " .. root, "build" .. " Main" })
end end
end end
+5 -13
View File
@@ -1,26 +1,18 @@
local utils = require("Polydev.utils") local utils = require("Polydev.utils")
local M = {} local M = {}
M.keybinds = {} function M.setup(user_opts)
M.opts = {} local opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("lua"), user_opts or {})
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
vim.api.nvim_create_user_command("LuaRun", M.run, {}) vim.api.nvim_create_user_command("LuaRun", M.run, {})
for key, _ in pairs(opts.keybinds) do
if M.keybinds.LuaRun then vim.keymap.set("n", key, ":LuaRun<CR>", { silent = true })
vim.keymap.set("n", M.keybinds.LuaRun, ":LuaRun<CR>", { silent = true })
end end
end end
function M.run() function M.run()
local init_lua_path = utils.get_project_root() .. "/lua/" .. utils.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 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
end end
+14 -42
View File
@@ -1,13 +1,17 @@
local utils = require("Polydev.utils") local utils = require("Polydev.utils")
local M = {} local M = {}
M.keybinds = {} function M.setup(user_opts)
M.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, {})
function M.setup(opts) vim.api.nvim_create_user_command("PythonRun", M.run, {})
M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("python"), opts or {}) for key, command in pairs(opts.keybinds) do
for key, command in pairs(M.opts.keybinds) do if command == "PythonRun" then
M.keybinds[command] = key vim.keymap.set("n", key, ":PythonRun<CR>", { silent = true })
end
if command == "PythonPip" then
vim.keymap.set("n", key, ":PythonPip<CR>", { silent = true })
end
end end
local root = utils.get_project_root() local root = utils.get_project_root()
@@ -18,49 +22,17 @@ function M.setup(opts)
vim.env.PATH = root .. "/venv/bin:" .. vim.env.PATH vim.env.PATH = root .. "/venv/bin:" .. vim.env.PATH
end end
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<CR>", { silent = true })
end
if M.keybinds.PythonPip then
vim.keymap.set("n", M.keybinds.PythonPip, ":PythonPip<CR>", { silent = true })
end
end end
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 return print("Pip canceled") end
return print("Pip canceled") utils.open_float_terminal({ "pip install " .. pip_module })
end
utils.open_float_terminal("pip install " .. pip_module)
end) end)
end end
function M.run() function M.run()
local root = utils.get_project_root() utils.open_float_terminal({ "python3 " .. utils.get_project_root(), "main.py" })
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)
end end
return M return M
+8 -17
View File
@@ -1,19 +1,11 @@
local utils = require("Polydev.utils") local utils = require("Polydev.utils")
local M = {} local M = {}
M.keybinds = {} function M.setup(user_opts)
M.opts = {} local opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("rust"), user_opts or {})
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
vim.api.nvim_create_user_command("RustRun", M.run, {}) vim.api.nvim_create_user_command("RustRun", M.run, {})
for key, _ in pairs(opts.keybinds) do
if M.keybinds.RustRun then vim.keymap.set("n", key, ":RustRun<CR>", { silent = true })
vim.keymap.set("n", M.keybinds.RustRun, ":RustRun<CR>", { silent = true })
end end
end end
@@ -21,11 +13,10 @@ function M.run()
local root = utils.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" }
local output = vim.fn.system(cmd) local output = vim.fn.system(table.concat(cmd))
local success = vim.v.shell_error == 0
if not success then if not vim.v.shell_error == 0 then
local term_buf = utils.open_float_terminal(cmd) local term_buf = utils.open_float_terminal(cmd)
vim.api.nvim_buf_set_option(term_buf, "modifiable", true) vim.api.nvim_buf_set_option(term_buf, "modifiable", true)
vim.api.nvim_buf_set_lines(term_buf, 0, -1, false, 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) vim.api.nvim_buf_set_option(term_buf, "modifiable", false)
else else
utils.open_float_terminal("cargo run") utils.open_float_terminal({ "cargo run" })
end end
end end
+2 -1
View File
@@ -42,6 +42,7 @@ function M.init_git(path, gitignore_lines)
end end
end end
---@param cmd table
function M.open_float_terminal(cmd) function M.open_float_terminal(cmd)
local presets = require("Polydev.presets") local presets = require("Polydev.presets")
local presets_opts = presets.getPresets(M.opts.preset) local presets_opts = presets.getPresets(M.opts.preset)
@@ -73,7 +74,7 @@ function M.open_float_terminal(cmd)
end end
end end
vim.fn.termopen(cmd) vim.fn.termopen(table.concat(cmd, "/"))
vim.api.nvim_buf_set_keymap(buf, "n", "<ESC>", "i<C-\\><C-n>:q<CR>", { noremap = true, silent = true }) vim.api.nvim_buf_set_keymap(buf, "n", "<ESC>", "i<C-\\><C-n>:q<CR>", { noremap = true, silent = true })
return buf, win return buf, win
end end