Optimizing java.lua

This commit is contained in:
DarthMooMancer
2025-03-14 00:50:05 -04:00
parent 595dfee0e7
commit df35fa4f61
2 changed files with 18 additions and 61 deletions
-2
View File
@@ -16,7 +16,6 @@ function M.load_language_module(lang)
return false return false
end end
-- Create a new project by specifying a language
function M.create_project() function M.create_project()
vim.ui.input({ prompt = "Enter language for project: " }, function(lang) 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
@@ -29,7 +28,6 @@ function M.create_project()
end) end)
end end
-- Auto-load language module based on file type
function M.setup() function M.setup()
vim.api.nvim_create_user_command("NewProject", M.create_project, {}) vim.api.nvim_create_user_command("NewProject", M.create_project, {})
vim.keymap.set("n", "<leader>np", ":NewProject<CR>", { silent = true }) vim.keymap.set("n", "<leader>np", ":NewProject<CR>", { silent = true })
+18 -59
View File
@@ -81,25 +81,20 @@ function M.open_float_terminal(cmd)
return buf, win return buf, win
end end
local function write_file(path, content)
local file = assert(io.open(path, "w"), "Error creating file: " .. path)
file:write(content)
file:close()
vim.cmd("edit " .. path)
print(path .. " created successfully!")
end
function M.create_project() function M.create_project()
-- Use vim.ui.input for better handling of 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 return print("Project creation canceled.") end
print("Project creation canceled.")
return
end
-- Paths for the project
local project_root = vim.fn.expand(M.config.project_root) .. "/" .. project_name local project_root = vim.fn.expand(M.config.project_root) .. "/" .. project_name
local src_dir = project_root .. "/src" for _, path in ipairs({ "/src", "/build" }) do vim.fn.mkdir(project_root .. path, "p") end
local build_dir = project_root .. "/build"
-- Create directories
vim.fn.mkdir(src_dir, "p")
vim.fn.mkdir(build_dir, "p")
-- Write the Main.java file
local main_java_path = src_dir .. "/Main.java"
local main_java_content = [[ local main_java_content = [[
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
@@ -108,73 +103,37 @@ public class Main {
} }
]] ]]
-- Create the file and write content write_file(project_root .. "/src/Main.java", main_java_content)
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)
end end
-- Create a new Java file
function M.create_new_file() function M.create_new_file()
vim.ui.input({ prompt = "Enter class name: " }, function(class_name) vim.ui.input({ prompt = "Enter class name: " }, function(class_name)
if not class_name or class_name == "" then if not class_name or class_name == "" then return print("Class creation canceled.") end
print("Class creation canceled.") local root_dir = vim.fn.expand("%:p:h"):match("(.*)/src")
return if not root_dir then return print("Error: src directory not found.") end
end
local current_dir = vim.fn.expand("%:p:h")
local root_dir = current_dir:match("(.*)/src")
if not root_dir then
print("Error: src directory not found.")
return
end
local java_file_path = root_dir .. "/src/" .. class_name .. ".java"
local java_content = string.format([[ local java_content = string.format([[
public class %s { public class %s {
// New File // New File
} }
]], class_name) ]], class_name)
local file = io.open(java_file_path, "w") write_file(root_dir .. "/src/" .. class_name .. ".java", java_content)
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)
end end
function M.build() function M.build()
local current_dir = vim.fn.expand("%:p:h") local project_root = vim.fn.expand("%:p:h"):match("(.*)/src")
local project_root = current_dir:match("(.*)/src") if not project_root then return print("Error: Could not detect project root directory.") end
if not project_root then
print("Error: Could not detect project root directory.")
return
end
local src_dir = project_root .. "/src"
local out_dir = project_root .. "/build" local out_dir = project_root .. "/build"
vim.fn.mkdir(out_dir, "p") vim.fn.mkdir(out_dir, "p")
local compile_command = string.format("javac -d %s %s/*.java", out_dir, src_dir) local compile_command = string.format("javac -d %s %s/*.java", out_dir, project_root .. "/src")
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("Compilation successful!") print("Compilation successful!")
else else
-- Only open the terminal if there's an error
print("Error during compilation. Opening terminal for details...") print("Error during compilation. Opening terminal for details...")
local term_buf = M.open_float_terminal(compile_command) local term_buf = M.open_float_terminal(compile_command)
vim.api.nvim_buf_set_option(term_buf, "modifiable", true) vim.api.nvim_buf_set_option(term_buf, "modifiable", true)