Update init.lua
This commit is contained in:
+56
-77
@@ -1,39 +1,38 @@
|
|||||||
local M = {}
|
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
|
-- Function to setup the plugin
|
||||||
local function mkdir(path)
|
function M.setup(opts)
|
||||||
os.execute("mkdir -p " .. path)
|
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
|
end
|
||||||
|
|
||||||
-- Helper function for writing to a file
|
-- Create a new Java project
|
||||||
local function write_file(path, content)
|
function M.create_project()
|
||||||
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()
|
|
||||||
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
|
||||||
|
|
||||||
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 src_dir = project_root .. "/root/src"
|
||||||
local out_dir = project_root .. "/root/out"
|
local out_dir = project_root .. "/root/out"
|
||||||
|
|
||||||
mkdir(src_dir)
|
-- Create directories
|
||||||
mkdir(out_dir)
|
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_path = src_dir .. "/Main.java"
|
||||||
local main_java_content = [[
|
local main_java_content = [[
|
||||||
public class Main {
|
public class Main {
|
||||||
@@ -42,17 +41,21 @@ public class Main {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
]]
|
]]
|
||||||
write_file(main_java_path, main_java_content)
|
local file = io.open(main_java_path, "w")
|
||||||
|
if file then
|
||||||
-- Open Main.java in Neovim
|
file:write(main_java_content)
|
||||||
vim.cmd("edit " .. main_java_path)
|
file:close()
|
||||||
print("Java project '" .. project_name .. "' created at " .. project_root)
|
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
|
||||||
|
|
||||||
-- Function to create a new Java file
|
-- Create a new Java file
|
||||||
local function create_new_java_file()
|
function M.create_new_file()
|
||||||
vim.ui.input({ prompt = "Enter class name for new Java file: " }, 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
|
||||||
print("Class creation canceled.")
|
print("Class creation canceled.")
|
||||||
return
|
return
|
||||||
@@ -65,36 +68,39 @@ local function create_new_java_file()
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local src_dir = root_dir .. "/root/src"
|
local java_file_path = root_dir .. "/root/src/" .. class_name .. ".java"
|
||||||
local java_file_path = src_dir .. "/" .. 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)
|
||||||
|
|
||||||
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
|
-- Compile Java files
|
||||||
local function javabuild()
|
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("(.*)/root/src")
|
local project_root = current_dir:match("(.*)/root/src")
|
||||||
|
|
||||||
if not project_root then
|
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
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
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)
|
local compile_status = vim.fn.system(compile_command)
|
||||||
|
|
||||||
if vim.v.shell_error ~= 0 then
|
if vim.v.shell_error ~= 0 then
|
||||||
@@ -104,23 +110,16 @@ local function javabuild()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Floating terminal for Java execution
|
-- Open floating terminal
|
||||||
local function open_float_terminal(cmd)
|
local function open_float_terminal(cmd)
|
||||||
local ui = vim.api.nvim_list_uis()[1]
|
local ui = vim.api.nvim_list_uis()[1]
|
||||||
local width = math.floor(ui.width * 0.9)
|
local width, height = math.floor(ui.width * 0.9), math.floor(ui.height * 0.9)
|
||||||
local height = math.floor(ui.height * 0.9)
|
local row, col = math.floor(ui.height * 0.05), math.floor(ui.width * 0.05)
|
||||||
local row = math.floor(ui.height * 0.05)
|
|
||||||
local col = math.floor(ui.width * 0.05)
|
|
||||||
|
|
||||||
local buf = vim.api.nvim_create_buf(false, true)
|
local buf = vim.api.nvim_create_buf(false, true)
|
||||||
local win = vim.api.nvim_open_win(buf, true, {
|
local win = vim.api.nvim_open_win(buf, true, {
|
||||||
relative = 'editor',
|
relative = "editor", width = width - 10, height = height - 10,
|
||||||
width = width - 10,
|
row = row + 5, col = col + 5, style = "minimal", border = "rounded"
|
||||||
height = height - 10,
|
|
||||||
row = row + 5,
|
|
||||||
col = col + 5,
|
|
||||||
style = "minimal",
|
|
||||||
border = "none",
|
|
||||||
})
|
})
|
||||||
|
|
||||||
vim.api.nvim_win_set_option(win, "winblend", vim.o.pumblend)
|
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.fn.termopen(cmd)
|
||||||
vim.cmd("startinsert")
|
vim.cmd("startinsert")
|
||||||
|
|
||||||
-- 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, 'i', '<Esc>', '<Esc>:q!<CR>', { noremap = true, silent = true })
|
|
||||||
|
|
||||||
return buf, win
|
return buf, win
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Function to run Java project in floating terminal
|
-- Run Java program in floating terminal
|
||||||
local function javarun_float()
|
function M.run()
|
||||||
local current_dir = vim.fn.expand("%:p:h")
|
local current_dir = vim.fn.expand("%:p:h")
|
||||||
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
|
||||||
print("Error: You must be inside the 'src' directory of a project.")
|
print("Error: Must be inside the 'src' directory.")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -151,19 +145,4 @@ local function javarun_float()
|
|||||||
open_float_terminal("java -cp " .. out_dir .. " Main")
|
open_float_terminal("java -cp " .. out_dir .. " Main")
|
||||||
end
|
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
|
return M
|
||||||
|
|||||||
Reference in New Issue
Block a user