Update nvim-java.lua

This commit is contained in:
DarthMooMancer
2025-02-19 10:31:59 -05:00
parent 78e9a50e5f
commit f0817a8b2a
+57 -95
View File
@@ -1,25 +1,39 @@
local M = {}
local augroup = vim.api.nvim_create_augroup("nvim-java", { clear = true }) local augroup = vim.api.nvim_create_augroup("nvim-java", { clear = true })
-- Helper function for creating directories
local function mkdir(path)
os.execute("mkdir -p " .. path)
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() local function create_java_project()
-- Get project name from user input
vim.ui.input({ prompt = "Enter project name: " }, function(project_name) vim.ui.input({ prompt = "Enter project name: " }, function(project_name)
if not project_name or project_name == "" then if not project_name or project_name == "" then
print("Project creation canceled.") print("Project creation canceled.")
return return
end end
-- Define the project root path within ~/Projects
local project_root = vim.fn.expand("~") .. "/Projects/" .. project_name local project_root = vim.fn.expand("~") .. "/Projects/" .. project_name
local src_dir = project_root .. "/root/src"
local out_dir = project_root .. "/root/out"
-- Define the directory paths mkdir(src_dir)
local root_dir = project_root .. "/root" mkdir(out_dir)
local src_dir = root_dir .. "/src"
local out_dir = root_dir .. "/out"
-- Create the directories if they don't exist -- Create a Main.java file
os.execute("mkdir -p " .. src_dir)
os.execute("mkdir -p " .. out_dir)
-- Create Main.java in src/
local main_java_path = src_dir .. "/Main.java" local main_java_path = src_dir .. "/Main.java"
local main_java_content = [[ local main_java_content = [[
public class Main { public class Main {
@@ -28,76 +42,47 @@ public class Main {
} }
} }
]] ]]
-- Write Main.java file write_file(main_java_path, main_java_content)
local file = io.open(main_java_path, "w")
if file then
file:write(main_java_content)
file:close()
else
print("Error creating Main.java")
return
end
-- Open Main.java in Neovim -- Open Main.java in Neovim
vim.cmd("edit " .. main_java_path) vim.cmd("edit " .. main_java_path)
print("Java project '" .. project_name .. "' created successfully at " .. project_root) print("Java project '" .. project_name .. "' created at " .. project_root)
end) end)
end end
-- Function to create a new Java file
local function create_new_java_file() local function create_new_java_file()
-- Get the class name for the new Java file
vim.ui.input({ prompt = "Enter class name for new Java file: " }, function(class_name) vim.ui.input({ prompt = "Enter class name for new Java file: " }, function(class_name)
if not class_name or class_name == "" then if not class_name or class_name == "" then
print("Class creation canceled.") print("Class creation canceled.")
return return
end end
-- Get the current working directory
local current_dir = vim.fn.expand("%:p:h") local current_dir = vim.fn.expand("%:p:h")
local root_dir = current_dir:match("(.*)/root/src")
-- Check if we're in a valid project directory
local root_dir = current_dir:match("(.*)/root/src") -- Try to extract the project root
if not root_dir then if not root_dir then
print("Error: src directory not found.") print("Error: src directory not found.")
return return
end end
-- Construct the path to the src directory dynamically
local src_dir = root_dir .. "/root/src" local src_dir = root_dir .. "/root/src"
-- Ensure that the src directory exists
if vim.fn.isdirectory(src_dir) == 0 then
print("Error: src directory not found.")
return
end
local java_file_path = src_dir .. "/" .. class_name .. ".java" local java_file_path = src_dir .. "/" .. class_name .. ".java"
-- Java content template
local java_content = string.format([[ local java_content = string.format([[
public class %s { public class %s {
// New File // New File
} }
]], class_name) ]], class_name)
-- Write the Java class to the file write_file(java_file_path, java_content)
local file = io.open(java_file_path, "w") vim.cmd("edit " .. java_file_path)
if file then print(class_name .. ".java created successfully!")
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)
end end
-- Function to build Java project
local function javabuild() local function javabuild()
-- Get the current directory (where the user is editing)
local current_dir = vim.fn.expand("%:p:h") local current_dir = vim.fn.expand("%:p:h")
-- Try to extract the project root by looking for "root/src" in the path
local project_root = current_dir:match("(.*)/root/src") local project_root = current_dir:match("(.*)/root/src")
if not project_root then if not project_root then
@@ -105,65 +90,46 @@ local function javabuild()
return return
end end
-- Define the source and output directories
local src_dir = project_root .. "/root/src" local src_dir = project_root .. "/root/src"
local out_dir = project_root .. "/root/out" local out_dir = project_root .. "/root/out"
mkdir(out_dir)
-- Ensure that the output directory exists
vim.fn.mkdir(out_dir, "p")
-- Compile all Java files in the src/ directory and output to out/
local compile_command = "javac -d " .. out_dir .. " " .. src_dir .. "/*.java" local compile_command = "javac -d " .. out_dir .. " " .. src_dir .. "/*.java"
local compile_status = vim.fn.system(compile_command) local compile_status = vim.fn.system(compile_command)
-- Check if the compilation was successful
if vim.v.shell_error ~= 0 then if vim.v.shell_error ~= 0 then
print("Error during compilation:\n" .. compile_status) print("Error during compilation:\n" .. compile_status)
return else
print("Compilation successful!")
end end
print("Compilation successful!")
end end
-- Floating terminal for Java execution
local function open_float_terminal(cmd) local function open_float_terminal(cmd)
-- Get the total dimensions of the Neovim window
local ui = vim.api.nvim_list_uis()[1] local ui = vim.api.nvim_list_uis()[1]
local width = math.floor(ui.width * 0.9) -- 90% of the window width local width = math.floor(ui.width * 0.9)
local height = math.floor(ui.height * 0.9) -- 90% of the window height local height = math.floor(ui.height * 0.9)
local row = math.floor(ui.height * 0.05) -- 5% padding top local row = math.floor(ui.height * 0.05)
local col = math.floor(ui.width * 0.05) -- 5% padding left/right local col = math.floor(ui.width * 0.05)
-- Create a new buffer for the floating terminal local buf = vim.api.nvim_create_buf(false, true)
local buf = vim.api.nvim_create_buf(false, true) -- Create a new buffer (no file, no listed) local win = vim.api.nvim_open_win(buf, true, {
-- Floating window options
local opts = {
relative = 'editor', relative = 'editor',
width = width - 10, -- Account for left/right padding width = width - 10,
height = height - 10, -- Account for top/bottom padding height = height - 10,
row = row + 5, -- Shift down for padding row = row + 5,
col = col + 5, -- Shift right for padding col = col + 5,
style = "minimal", -- Removes unnecessary UI elements style = "minimal",
border = "none", -- Removes border for transparency border = "none",
} })
-- Open the floating window vim.api.nvim_win_set_option(win, "winblend", vim.o.pumblend)
local win = vim.api.nvim_open_win(buf, true, opts)
-- Apply transparency and Pmenu styling
vim.api.nvim_win_set_option(win, "winblend", vim.o.pumblend) -- Match `Pmenu` transparency
vim.api.nvim_win_set_option(win, "winhighlight", "Normal:Pmenu,FloatBorder:Pmenu") vim.api.nvim_win_set_option(win, "winhighlight", "Normal:Pmenu,FloatBorder:Pmenu")
-- Enable line numbers in the floating window
vim.api.nvim_win_set_option(win, "number", true)
vim.api.nvim_win_set_option(win, "relativenumber", false) -- Use absolute line numbers
-- Start the interactive terminal
vim.fn.termopen(cmd) vim.fn.termopen(cmd)
-- Automatically enter insert mode for smooth interaction
vim.cmd("startinsert") vim.cmd("startinsert")
-- Map <Esc> in terminal and normal mode to close the floating window -- Keymaps to close the terminal
vim.api.nvim_buf_set_keymap(buf, "t", "<Esc>", "<C-\\><C-n>:q<CR>", { noremap = true, silent = true }) vim.api.nvim_buf_set_keymap(buf, "t", "<Esc>", "<C-\\><C-n>:q<CR>", { noremap = true, silent = true })
vim.api.nvim_buf_set_keymap(buf, "n", "<Esc>", ":q<CR>", { noremap = true, silent = true }) vim.api.nvim_buf_set_keymap(buf, "n", "<Esc>", ":q<CR>", { noremap = true, silent = true })
vim.api.nvim_buf_set_keymap(buf, 'i', '<Esc>', '<Esc>:q!<CR>', { noremap = true, silent = true }) vim.api.nvim_buf_set_keymap(buf, 'i', '<Esc>', '<Esc>:q!<CR>', { noremap = true, silent = true })
@@ -171,11 +137,9 @@ local function open_float_terminal(cmd)
return buf, win return buf, win
end end
-- Function to run Java project in floating terminal
local function javarun_float() local function javarun_float()
-- Get the current directory (where the user is editing)
local current_dir = vim.fn.expand("%:p:h") local current_dir = vim.fn.expand("%:p:h")
-- Try to extract the project root by looking for "root/src" in the path
local project_root = current_dir:match("(.*)/root/src") local project_root = current_dir:match("(.*)/root/src")
if not project_root then if not project_root then
@@ -183,14 +147,12 @@ local function javarun_float()
return return
end end
-- Define the output directory where the .class files are located
local out_dir = project_root .. "/root/out" local out_dir = project_root .. "/root/out"
-- Run the Main class from the output directory in the floating terminal
open_float_terminal("java -cp " .. out_dir .. " Main") open_float_terminal("java -cp " .. out_dir .. " Main")
end end
local function setup() -- Setup function to register commands
function M.setup()
vim.api.nvim_create_autocmd("VimEnter", { vim.api.nvim_create_autocmd("VimEnter", {
group = augroup, group = augroup,
desc = "Register Java project commands", desc = "Register Java project commands",
@@ -204,4 +166,4 @@ local function setup()
}) })
end end
return { setup = setup } return M