From 15e0947dfb155a9f3f004fd4ba3226a26a31c8f8 Mon Sep 17 00:00:00 2001 From: DarthMooMancer <107283522+DarthMooMancer@users.noreply.github.com> Date: Wed, 19 Feb 2025 10:50:00 -0500 Subject: [PATCH] Update init.lua --- lua/nvim-java/init.lua | 133 +++++++++++++++++------------------------ 1 file changed, 56 insertions(+), 77 deletions(-) diff --git a/lua/nvim-java/init.lua b/lua/nvim-java/init.lua index 8745a62..e6eeb70 100644 --- a/lua/nvim-java/init.lua +++ b/lua/nvim-java/init.lua @@ -1,39 +1,38 @@ local M = {} -local augroup = vim.api.nvim_create_augroup("nvim-java", { clear = true }) +-- Default config +M.config = { + project_root = "~/Projects", +} --- Helper function for creating directories -local function mkdir(path) - os.execute("mkdir -p " .. path) +-- Function to setup the plugin +function M.setup(opts) + M.config = vim.tbl_deep_extend("force", M.config, opts or {}) + + -- 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, {}) end --- Helper function for writing to a file -local function write_file(path, content) - local file = io.open(path, "w") - if file then - file:write(content) - file:close() - else - print("Error writing to file: " .. path) - end -end - --- Function to create a new Java project -local function create_java_project() +-- Create a new Java project +function M.create_project() vim.ui.input({ prompt = "Enter project name: " }, function(project_name) if not project_name or project_name == "" then print("Project creation canceled.") return end - local project_root = vim.fn.expand("~") .. "/Projects/" .. project_name + local project_root = vim.fn.expand(M.config.project_root) .. "/" .. project_name local src_dir = project_root .. "/root/src" local out_dir = project_root .. "/root/out" - mkdir(src_dir) - mkdir(out_dir) + -- Create directories + vim.fn.mkdir(src_dir, "p") + vim.fn.mkdir(out_dir, "p") - -- Create a Main.java file + -- Write Main.java local main_java_path = src_dir .. "/Main.java" local main_java_content = [[ public class Main { @@ -42,17 +41,21 @@ public class Main { } } ]] - write_file(main_java_path, main_java_content) - - -- Open Main.java in Neovim - vim.cmd("edit " .. main_java_path) - print("Java project '" .. project_name .. "' created at " .. project_root) + local file = io.open(main_java_path, "w") + if file then + file:write(main_java_content) + file:close() + vim.cmd("edit " .. main_java_path) + print("Project '" .. project_name .. "' created at " .. project_root) + else + print("Error creating Main.java") + end end) end --- Function to create a new Java file -local function create_new_java_file() - vim.ui.input({ prompt = "Enter class name for new Java file: " }, function(class_name) +-- Create a new Java file +function M.create_new_file() + vim.ui.input({ prompt = "Enter class name: " }, function(class_name) if not class_name or class_name == "" then print("Class creation canceled.") return @@ -65,36 +68,39 @@ local function create_new_java_file() return end - local src_dir = root_dir .. "/root/src" - local java_file_path = src_dir .. "/" .. class_name .. ".java" - + local java_file_path = root_dir .. "/root/src/" .. class_name .. ".java" local java_content = string.format([[ public class %s { - // New File + // New File } ]], class_name) - write_file(java_file_path, java_content) - vim.cmd("edit " .. java_file_path) - print(class_name .. ".java created successfully!") + local file = io.open(java_file_path, "w") + if file then + file:write(java_content) + file:close() + vim.cmd("edit " .. java_file_path) + print(class_name .. ".java created successfully!") + else + print("Error creating " .. class_name .. ".java") + end end) end --- Function to build Java project -local function javabuild() +-- Compile Java files +function M.build() local current_dir = vim.fn.expand("%:p:h") local project_root = current_dir:match("(.*)/root/src") - if not project_root then - print("Error: You must be inside the 'src' directory of a project.") + print("Error: Must be inside the 'src' directory.") return end local src_dir = project_root .. "/root/src" local out_dir = project_root .. "/root/out" - mkdir(out_dir) - local compile_command = "javac -d " .. out_dir .. " " .. src_dir .. "/*.java" + vim.fn.mkdir(out_dir, "p") + local compile_command = string.format("javac -d %s %s/*.java", out_dir, src_dir) local compile_status = vim.fn.system(compile_command) if vim.v.shell_error ~= 0 then @@ -104,23 +110,16 @@ local function javabuild() end end --- Floating terminal for Java execution +-- Open floating terminal local function open_float_terminal(cmd) local ui = vim.api.nvim_list_uis()[1] - local width = math.floor(ui.width * 0.9) - local height = math.floor(ui.height * 0.9) - local row = math.floor(ui.height * 0.05) - local col = math.floor(ui.width * 0.05) + local width, height = math.floor(ui.width * 0.9), math.floor(ui.height * 0.9) + local row, col = math.floor(ui.height * 0.05), math.floor(ui.width * 0.05) local buf = vim.api.nvim_create_buf(false, true) local win = vim.api.nvim_open_win(buf, true, { - relative = 'editor', - width = width - 10, - height = height - 10, - row = row + 5, - col = col + 5, - style = "minimal", - border = "none", + relative = "editor", width = width - 10, height = height - 10, + row = row + 5, col = col + 5, style = "minimal", border = "rounded" }) vim.api.nvim_win_set_option(win, "winblend", vim.o.pumblend) @@ -129,21 +128,16 @@ local function open_float_terminal(cmd) vim.fn.termopen(cmd) vim.cmd("startinsert") - -- Keymaps to close the terminal vim.api.nvim_buf_set_keymap(buf, "t", "", ":q", { noremap = true, silent = true }) - vim.api.nvim_buf_set_keymap(buf, "n", "", ":q", { noremap = true, silent = true }) - vim.api.nvim_buf_set_keymap(buf, 'i', '', ':q!', { noremap = true, silent = true }) - return buf, win end --- Function to run Java project in floating terminal -local function javarun_float() +-- Run Java program in floating terminal +function M.run() local current_dir = vim.fn.expand("%:p:h") local project_root = current_dir:match("(.*)/root/src") - if not project_root then - print("Error: You must be inside the 'src' directory of a project.") + print("Error: Must be inside the 'src' directory.") return end @@ -151,19 +145,4 @@ local function javarun_float() open_float_terminal("java -cp " .. out_dir .. " Main") end --- Setup function to register commands -function M.setup() - vim.api.nvim_create_autocmd("VimEnter", { - group = augroup, - desc = "Register Java project commands", - once = true, - callback = function() - vim.api.nvim_create_user_command("NewJavaProject", create_java_project, {}) - vim.api.nvim_create_user_command("JavaBuild", javabuild, {}) - vim.api.nvim_create_user_command("NewJavaFile", create_new_java_file, {}) - vim.api.nvim_create_user_command("JavaRun", javarun_float, {}) - end, - }) -end - return M