diff --git a/lua/Polydev/c.lua b/lua/Polydev/c.lua index 07964cd..fa45991 100644 --- a/lua/Polydev/c.lua +++ b/lua/Polydev/c.lua @@ -4,17 +4,15 @@ M.c_build = nil M.c_run = nil M.new_c_file = nil M.new_c_header_file = nil -M.new_c_project = nil M.config = { project_root = "~/Projects/C", keybinds = { [""] = "CloseTerminal", - ["cb"] = "CBuild", - ["cr"] = "CRun", - ["cnf"] = "NewCFile", - ["cnp"] = "NewCProject", - ["cnh"] = "NewCHeaderFile", + ["pb"] = "CBuild", + ["pr"] = "CRun", + ["nf"] = "NewCFile", + ["nh"] = "NewCHeaderFile", }, terminal = { right_padding = 0, @@ -35,11 +33,9 @@ function M.setup(opts) if command == "CBuild" then M.c_build = key end if command == "CRun" then M.c_run = key end if command == "NewCFile" then M.new_c_file = key end - if command == "NewCProject" then M.new_c_project = key end if command == "NewCHeaderFile" then M.new_c_header_file = key end end - vim.api.nvim_create_user_command("NewCProject", M.create_project, {}) vim.api.nvim_create_user_command("NewCHeaderFile", M.create_new_header_file, {}) vim.api.nvim_create_user_command("NewCFile", M.create_new_file, {}) vim.api.nvim_create_user_command("CBuild", M.build, {}) @@ -48,7 +44,6 @@ function M.setup(opts) vim.keymap.set("n", M.c_build, ":CBuild", { silent = true }) vim.keymap.set("n", M.c_run, ":CRun", { silent = true }) vim.keymap.set("n", M.new_c_file, ":NewCFile", { silent = true }) - vim.keymap.set("n", M.new_c_project, ":NewCProject", { silent = true }) vim.keymap.set("n", M.new_c_header_file, ":NewCHeaderFile", { silent = true }) end diff --git a/lua/Polydev/init.lua b/lua/Polydev/init.lua index 24af6c1..d9ff8a0 100644 --- a/lua/Polydev/init.lua +++ b/lua/Polydev/init.lua @@ -10,9 +10,9 @@ function M.load_language_module(lang) local ok, lang_module = pcall(require, "Polydev." .. lang) if ok and lang_module.setup then - lang_module.setup() - M.loaded_languages[lang] = lang_module - return true + lang_module.setup() + M.loaded_languages[lang] = lang_module + return true end print("Error: Unsupported language '" .. lang .. "'") @@ -22,13 +22,13 @@ end -- Create a new project by specifying a language function M.create_project() vim.ui.input({ prompt = "Enter language for project: " }, function(lang) - if not lang or lang == "" then return print("Project creation canceled.") end + if not lang or lang == "" then return print("Project creation canceled.") end - if M.load_language_module(lang) and M.loaded_languages[lang].create_project then - M.loaded_languages[lang].create_project() - else - print("Error: No project creation method for " .. lang) - end + if M.load_language_module(lang) and M.loaded_languages[lang].create_project then + M.loaded_languages[lang].create_project() + else + print("Error: No project creation method for " .. lang) + end end) end @@ -37,11 +37,11 @@ function M.setup() vim.api.nvim_create_user_command("NewProject", M.create_project, {}) vim.keymap.set("n", "np", ":NewProject", { silent = true }) vim.api.nvim_create_autocmd("FileType", { - pattern = "*", - callback = function() - local filetype = vim.bo.filetype - M.load_language_module(filetype) - end, + pattern = "*", + callback = function() + local filetype = vim.bo.filetype + M.load_language_module(filetype) + end, }) end diff --git a/lua/Polydev/java.lua b/lua/Polydev/java.lua index 47a73a1..d461762 100644 --- a/lua/Polydev/java.lua +++ b/lua/Polydev/java.lua @@ -3,17 +3,14 @@ M.close_key = nil M.java_build = nil M.java_run = nil M.new_java_file = nil -M.new_java_project = nil --- Default config M.config = { project_root = "~/Projects/Java", keybinds = { [""] = "CloseTerminal", - ["jb"] = "JavaBuild", - ["jr"] = "JavaRun", - ["jnf"] = "NewJavaFile", - ["jnp"] = "NewJavaProject", + ["pb"] = "JavaBuild", + ["pr"] = "JavaRun", + ["nf"] = "NewJavaFile", }, terminal = { right_padding = 0, @@ -30,68 +27,31 @@ M.config = { function M.setup(opts) M.config = vim.tbl_deep_extend("force", M.config, opts or {}) - -- Apply keybindings manually for key, command in pairs(M.config.keybinds) do - if command == "CloseTerminal" then - M.close_key = key - end - if command == "JavaBuild" then - M.java_build = key - end - if command == "JavaRun" then - M.java_run = key - end - if command == "NewJavaFile" then - M.new_java_file = key - end - if command == "NewJavaProject" then - M.new_java_project = key - end + if command == "CloseTerminal" then M.close_key = key end + if command == "JavaBuild" then M.java_build = key end + if command == "JavaRun" then M.java_run = key end + if command == "NewJavaFile" then M.new_java_file = key end end - -- Register Keybinds vim.keymap.set("n", M.java_build, ":JavaBuild", { silent = true }) vim.keymap.set("n", M.java_run, ":JavaRun", { silent = true }) vim.keymap.set("n", M.new_java_file, ":NewJavaFile", { silent = true }) - vim.keymap.set("n", M.new_java_project, ":NewJavaProject", { silent = true }) - -- Register user commands - vim.api.nvim_create_user_command("NewJavaProject", M.create_project, {}) vim.api.nvim_create_user_command("NewJavaFile", M.create_new_file, {}) vim.api.nvim_create_user_command("JavaBuild", M.build, {}) vim.api.nvim_create_user_command("JavaRun", M.run, {}) - vim.api.nvim_create_user_command("Migrate", M.migrate, {}) end --- Open floating terminal function M.open_float_terminal(cmd) local ui = vim.api.nvim_list_uis()[1] local term_cfg = M.config.terminal - -- Default window size (90% of the editor) - local default_width = math.floor(ui.width * 0.9) - local default_height = math.floor(ui.height * 0.9) + local width = math.max(1, math.floor(ui.width * 0.9) - term_cfg.left_padding - term_cfg.right_padding) + local height = math.max(1, math.floor(ui.height * 0.9) - term_cfg.top_padding - (term_cfg.bottom_padding + 1)) + local row = math.max(1, math.floor((ui.height - height) / 2) + term_cfg.top_padding) + local col = math.max(1, math.floor((ui.width - width) / 2) + term_cfg.left_padding) - -- Default centered position - local default_row = math.floor((ui.height - default_height) / 2) - local default_col = math.floor((ui.width - default_width) / 2) - - -- Adjust size based on padding - local width = default_width - term_cfg.left_padding - term_cfg.right_padding - local height = default_height - term_cfg.top_padding - (term_cfg.bottom_padding + 1) - - -- Adjust position based on padding - local row = default_row + term_cfg.top_padding - local col = default_col + term_cfg.left_padding - - -- Ensure window size doesn't go negative - width = math.max(1, width) - height = math.max(1, height) - -- Ensure position doesn't move off-screen - row = math.max(0, row) - col = math.max(0, col) - - -- Create buffer and window local buf = vim.api.nvim_create_buf(false, true) local win = vim.api.nvim_open_win(buf, true, { relative = "editor", @@ -103,56 +63,24 @@ function M.open_float_terminal(cmd) border = term_cfg.border and "rounded" or "none", }) - -- Enable scrolling, relative line numbers, and prevent closing on click vim.api.nvim_win_set_option(win, "winblend", vim.o.pumblend) vim.api.nvim_win_set_option(win, "winhighlight", "Normal:Pmenu,FloatBorder:Pmenu") - if(M.config.terminal.number == true) then + vim.api.nvim_win_set_option(win, "cursorline", true) + if(term_cfg.number == true) then vim.api.nvim_win_set_option(win, "number", true) + if(term_cfg.relativenumber == true) then + vim.api.nvim_win_set_option(win, "relativenumber", true) + end end - if(M.config.terminal.number == true and M.config.terminal.relativenumber == true) then - vim.api.nvim_win_set_option(win, "relativenumber", true) - end - if(M.config.terminal.scroll == true) then + if(term_cfg.scroll == true) then vim.api.nvim_win_set_option(win, "scrolloff", 5) end - vim.api.nvim_win_set_option(win, "cursorline", true) vim.fn.termopen(cmd) - vim.api.nvim_buf_set_keymap(buf, "n", M.close_key, "i:q", { noremap = true, silent = true }) return buf, win end --- Migrate old folder to new method -function M.migrate() - vim.ui.input({ prompt = "Enter project name to migrate: "}, function(project_name) - if not project_name or project_name == "" then - print("Migration failed") - return - end - local project_root = vim.fn.expand(M.config.project_root) .. "/" .. project_name - local legacy_src_dir = project_root .. "/root/src" - local legacy_out_dir = project_root .. "/root/out" - local src_dir = project_root .. "/src" - local build_dir = project_root .. "/build" - - vim.fn.mkdir(src_dir, "p") - vim.fn.mkdir(build_dir, "p") - - local compile_command = "mv " .. legacy_src_dir .. "/* " .. src_dir .. - " && mv " .. legacy_out_dir .. "/* " .. build_dir .. - " && rm -rd " .. project_root .. "/" .. "root/" - local compile_status = vim.fn.system(compile_command) - - if vim.v.shell_error ~= 0 then - print("Error during migration:\n" .. compile_status) - else - print("Migration successful!") - end - end) -end - --- Create a new Java project function M.create_project() -- Use vim.ui.input for better handling of input vim.ui.input({ prompt = "Enter project name: " }, function(project_name)