wip
This commit is contained in:
+152
-149
@@ -8,187 +8,190 @@ local loaded_languages = {}
|
|||||||
---@param opts? table
|
---@param opts? table
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function M.load_language_module(lang, opts)
|
function M.load_language_module(lang, opts)
|
||||||
if loaded_languages[lang] then return true end
|
if loaded_languages[lang] then return true end
|
||||||
|
|
||||||
local ok, mod = pcall(require, "Polydev.languages")
|
local ok, mod = pcall(require, "Polydev.languages")
|
||||||
if not ok or type(mod.languages) ~= "table" then return false end
|
if not ok or type(mod.languages) ~= "table" then return false end
|
||||||
|
|
||||||
local lang_mod = mod.languages[lang]
|
local lang_mod = mod.languages[lang]
|
||||||
if type(lang_mod) ~= "table" or type(lang_mod.setup) ~= "function" then return false end
|
if type(lang_mod) ~= "table" or type(lang_mod.setup) ~= "function" then return false end
|
||||||
|
|
||||||
lang_mod:setup(opts and opts[lang] or {})
|
lang_mod:setup(opts and opts[lang] or {})
|
||||||
loaded_languages[lang] = lang_mod
|
loaded_languages[lang] = lang_mod
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
M.languages = {
|
M.languages = {
|
||||||
java = {
|
java = {
|
||||||
setup = function (self, user_opts)
|
setup = function (self, user_opts)
|
||||||
local opts = vim.tbl_deep_extend("force", {}, configs.get("java"), user_opts or {})
|
local opts = vim.tbl_deep_extend("force", {}, configs.get("java"), user_opts or {})
|
||||||
vim.api.nvim_create_user_command("JavaRun", function() self:run() end, {})
|
vim.api.nvim_create_user_command("JavaRun", function() self:run() end, {})
|
||||||
for key, _ in pairs(opts.keybinds) do
|
for key, _ in pairs(opts.keybinds) do
|
||||||
vim.keymap.set("n", key, ":JavaRun<CR>", { silent = true })
|
vim.keymap.set("n", key, ":JavaRun<CR>", { silent = true })
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
run = function ()
|
run = function ()
|
||||||
local root = utils.get_project_root()
|
local root = utils.get_project_root()
|
||||||
|
|
||||||
local cmd = string.format("javac -d %s %s", root .. "/build", 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)
|
||||||
|
|
||||||
if not vim.v.shell_error == 0 then
|
if not vim.v.shell_error == 0 then
|
||||||
local term_buf = utils.terminal({ cmd })
|
local term_buf = utils.terminal({ cmd })
|
||||||
vim.api.nvim_set_option_value("modifiable", true, { win = term_buf })
|
vim.api.nvim_set_option_value("modifiable", true, { win = term_buf })
|
||||||
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_set_option_value("modifiable", false, { win = term_buf })
|
vim.api.nvim_set_option_value("modifiable", false, { win = term_buf })
|
||||||
else
|
else
|
||||||
utils.terminal({ "java -cp " .. root, "build" .. " Main" })
|
utils.terminal({ "java -cp " .. root, "build" .. " Main" })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
c = {
|
c = {
|
||||||
setup = function (self, user_opts)
|
setup = function (self, user_opts)
|
||||||
local opts = vim.tbl_deep_extend("force", {}, configs.get("c"), user_opts or {})
|
local opts = vim.tbl_deep_extend("force", {}, configs.get("c"), user_opts or {})
|
||||||
vim.api.nvim_create_user_command("CRun", function () self:run() end, {})
|
vim.api.nvim_create_user_command("CRun", function () self:run() end, {})
|
||||||
for key, _ in pairs(opts.keybinds) do
|
for key, _ in pairs(opts.keybinds) do
|
||||||
vim.keymap.set("n", key, ":CRun<CR>", { silent = true })
|
vim.keymap.set("n", key, ":CRun<CR>", { silent = true })
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
run = function ()
|
run = function ()
|
||||||
local root = utils.get_project_root()
|
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 ."
|
||||||
local output = vim.fn.system(cmd)
|
local output = vim.fn.system(cmd)
|
||||||
local success = vim.v.shell_error == 0
|
local success = vim.v.shell_error == 0
|
||||||
|
|
||||||
if not success then
|
if not success then
|
||||||
local term_buf = utils.terminal({ cmd })
|
local term_buf = utils.terminal({ cmd })
|
||||||
vim.api.nvim_set_option_value("modifiable", true, { win = term_buf })
|
vim.api.nvim_set_option_value("modifiable", true, { win = term_buf })
|
||||||
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_set_option_value("modifiable", false, { win = term_buf })
|
vim.api.nvim_set_option_value("modifiable", false, { win = term_buf })
|
||||||
end
|
end
|
||||||
if success then
|
if success then
|
||||||
local run_cmd = "cd '" .. build_dir .. "' && ./" .. utils.get_project_name()
|
local run_cmd = "cd '" .. build_dir .. "' && ./" .. utils.get_project_name()
|
||||||
utils.terminal({ run_cmd })
|
utils.terminal({ run_cmd })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
cpp = {
|
cpp = {
|
||||||
setup = function (self, user_opts)
|
setup = function (self, user_opts)
|
||||||
local opts = vim.tbl_deep_extend("force", {}, configs.get("cpp"), user_opts or {})
|
local opts = vim.tbl_deep_extend("force", {}, configs.get("cpp"), user_opts or {})
|
||||||
vim.api.nvim_create_user_command("CppRun", function () self:run() end, {})
|
vim.api.nvim_create_user_command("CppRun", function () self:run() end, {})
|
||||||
for key, _ in pairs(opts.keybinds) do
|
for key, _ in pairs(opts.keybinds) do
|
||||||
vim.keymap.set("n", key, ":CppRun<CR>", { silent = true })
|
vim.keymap.set("n", key, ":CppRun<CR>", { silent = true })
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
run = function ()
|
run = function ()
|
||||||
local root = utils.get_project_root()
|
local root = utils.get_project_root()
|
||||||
local build_dir = root .. "/build"
|
local build_dir = root .. "/build"
|
||||||
|
if utils.exists(build_dir) == nil then
|
||||||
|
vim.fn.mkdir(build_dir, "p")
|
||||||
|
vim.fn.system(string.format("cd %s/build && cmake -S .. -B . && cmake --build . && cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .. && ln -sf compile_commands.json ../compile_commands.json", root))
|
||||||
|
end
|
||||||
|
|
||||||
local cmd = "cd '" .. build_dir .. "' && cmake --build ."
|
local cmd = "cd '" .. build_dir .. "' && cmake --build ."
|
||||||
local output = vim.fn.system(cmd)
|
local output = vim.fn.system(cmd)
|
||||||
local success = vim.v.shell_error == 0
|
local success = vim.v.shell_error == 0
|
||||||
|
|
||||||
if not success then
|
if not success then
|
||||||
local term_buf = utils.terminal({ cmd })
|
local term_buf = utils.terminal({ cmd })
|
||||||
|
|
||||||
vim.api.nvim_set_option_value("modifiable", true, { buf = term_buf })
|
vim.api.nvim_set_option_value("modifiable", true, { buf = term_buf })
|
||||||
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_set_option_value("modifiable", false, { buf = term_buf })
|
vim.api.nvim_set_option_value("modifiable", false, { buf = term_buf })
|
||||||
|
end
|
||||||
end
|
if success then
|
||||||
if success then
|
local run_cmd = "cd '" .. build_dir .. "' && ./" .. utils.get_project_name()
|
||||||
local run_cmd = "cd '" .. build_dir .. "' && ./" .. utils.get_project_name()
|
utils.terminal({ run_cmd })
|
||||||
utils.terminal({ run_cmd })
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
},
|
},
|
||||||
html = {
|
html = {
|
||||||
setup = function (user_opts)
|
setup = function (user_opts)
|
||||||
vim.tbl_deep_extend("force", {}, configs.get("html"), user_opts or {})
|
vim.tbl_deep_extend("force", {}, configs.get("html"), user_opts or {})
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
rust = {
|
rust = {
|
||||||
setup = function (self, user_opts)
|
setup = function (self, user_opts)
|
||||||
local opts = vim.tbl_deep_extend("force", {}, configs.get("rust"), user_opts or {})
|
local opts = vim.tbl_deep_extend("force", {}, configs.get("rust"), user_opts or {})
|
||||||
vim.api.nvim_create_user_command("RustRun", function () self:run() end, {})
|
vim.api.nvim_create_user_command("RustRun", function () self:run() end, {})
|
||||||
for key, _ in pairs(opts.keybinds) do
|
for key, _ in pairs(opts.keybinds) do
|
||||||
vim.keymap.set("n", key, ":RustRun<CR>", { silent = true })
|
vim.keymap.set("n", key, ":RustRun<CR>", { silent = true })
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
run = function ()
|
run = function ()
|
||||||
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(table.concat(cmd))
|
local output = vim.fn.system(table.concat(cmd))
|
||||||
|
|
||||||
if not vim.v.shell_error == 0 then
|
if not vim.v.shell_error == 0 then
|
||||||
local term_buf = utils.terminal(cmd)
|
local term_buf = utils.terminal(cmd)
|
||||||
vim.api.nvim_set_option_value("modifiable", true, { win = term_buf })
|
vim.api.nvim_set_option_value("modifiable", true, { win = term_buf })
|
||||||
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_set_option_value("modifiable", false, { win = term_buf })
|
vim.api.nvim_set_option_value("modifiable", false, { win = term_buf })
|
||||||
else
|
else
|
||||||
utils.terminal({ "cargo run" })
|
utils.terminal({ "cargo run" })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
python = {
|
python = {
|
||||||
setup = function(self, user_opts)
|
setup = function(self, user_opts)
|
||||||
local opts = vim.tbl_deep_extend("force", {}, configs.get("python"), user_opts or {})
|
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("PythonPip", function () self:install_dependency() end, {})
|
||||||
vim.api.nvim_create_user_command("PythonRun", function () self:run() end, {})
|
vim.api.nvim_create_user_command("PythonRun", function () self:run() end, {})
|
||||||
for key, command in pairs(opts.keybinds) do
|
for key, command in pairs(opts.keybinds) do
|
||||||
if command == "PythonRun" then
|
if command == "PythonRun" then
|
||||||
vim.keymap.set("n", key, ":PythonRun<CR>", { silent = true })
|
vim.keymap.set("n", key, ":PythonRun<CR>", { silent = true })
|
||||||
end
|
end
|
||||||
if command == "PythonPip" then
|
if command == "PythonPip" then
|
||||||
vim.keymap.set("n", key, ":PythonPip<CR>", { silent = true })
|
vim.keymap.set("n", key, ":PythonPip<CR>", { silent = true })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local root = utils.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
|
||||||
vim.env.VIRTUAL_ENV = venv_path
|
vim.env.VIRTUAL_ENV = venv_path
|
||||||
vim.env.PATH = root .. "/venv/bin:" .. vim.env.PATH
|
vim.env.PATH = root .. "/venv/bin:" .. vim.env.PATH
|
||||||
end
|
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
|
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 = {
|
lua = {
|
||||||
setup = function (self, user_opts)
|
setup = function (self, user_opts)
|
||||||
local opts = vim.tbl_deep_extend("force", {}, configs.get("lua"), user_opts or {})
|
local opts = vim.tbl_deep_extend("force", {}, configs.get("lua"), user_opts or {})
|
||||||
vim.api.nvim_create_user_command("LuaRun", function () self:run() end, {})
|
vim.api.nvim_create_user_command("LuaRun", function () self:run() end, {})
|
||||||
for key, _ in pairs(opts.keybinds) do
|
for key, _ in pairs(opts.keybinds) do
|
||||||
vim.keymap.set("n", key, ":LuaRun<CR>", { silent = true })
|
vim.keymap.set("n", key, ":LuaRun<CR>", { 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
|
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
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,15 @@
|
|||||||
local M = {}
|
|
||||||
M.opts = {}
|
M.opts = {}
|
||||||
|
|
||||||
function M.setup(opts)
|
function M.setup(opts)
|
||||||
M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("terminal"), opts or {})
|
M.opts = vim.tbl_deep_extend("force", {}, require("Polydev.configs").get("terminal"), opts or {})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function exists(path)
|
||||||
|
return (io.open(path,"r") ~= nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
---@return string
|
---@return string
|
||||||
function M.get_project_root()
|
function M.get_project_root()
|
||||||
local plugin_dir = vim.fn.fnamemodify(debug.getinfo(1, "S").source:sub(2), ":h:h:h")
|
local plugin_dir = vim.fn.fnamemodify(debug.getinfo(1, "S").source:sub(2), ":h:h:h")
|
||||||
|
|||||||
Reference in New Issue
Block a user