Update java.lua

This commit is contained in:
DarthMooMancer
2025-03-14 00:36:36 -04:00
parent b2910ac223
commit 52c835a87c
+11 -22
View File
@@ -155,40 +155,29 @@ public class %s {
end) end)
end end
-- Compile Java files
function M.build() function M.build()
local current_dir = vim.fn.expand("%:p:h") local current_dir = vim.fn.expand("%:p:h")
local project_root = current_dir:match("(.*)/src") local project_root = current_dir:match("(.*)/src")
if not project_root then if not project_root then return print("Error: Must be inside the 'src' directory.") end
print("Error: Must be inside the 'src' directory.")
return
end
local src_dir = project_root .. "/src" 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, src_dir)
local compile_status = vim.fn.system(compile_command) local term_buf = M.open_float_terminal(compile_command)
if vim.v.shell_error ~= 0 then local output = vim.fn.system(compile_command)
print("Error during compilation:\n" .. compile_status) local success = vim.v.shell_error == 0
else
print("Compilation successful!") vim.api.nvim_buf_set_lines(term_buf, 0, -1, false, vim.list_extend({ success and "Compilation successful!" or "Error during compilation:" }, vim.split(output, "\n", { trimempty = true })))
end vim.api.nvim_buf_set_option(term_buf, "modifiable", false)
end end
-- Run Java program in floating terminal
function M.run() function M.run()
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: Must be inside the 'src' directory.") end
if not project_root then M.open_float_terminal("java -cp " .. project_root .. "/build" .. " Main")
print("Error: Must be inside the 'src' directory.")
return
end
local out_dir = project_root .. "/build"
M.open_float_terminal("java -cp " .. out_dir .. " Main")
end end
return M return M