From 38bf9cabaa9fb7d59533b1fef1993a299b87c983 Mon Sep 17 00:00:00 2001 From: salem-karim Date: Fri, 26 Dec 2025 23:34:15 +0100 Subject: [PATCH 1/2] change stylua from Tabs to Spaces and apply formatting --- lua/create_springboot_project.lua | 331 +++++++-------- lua/springboot-nvim/generateclass.lua | 72 ++-- lua/springboot-nvim/init.lua | 224 +++++----- lua/springboot-nvim/ui/springboot_nvim_ui.lua | 395 +++++++++++++----- lua/springboot-nvim/ui/ui_utils.lua | 50 ++- stylua.toml | 2 +- 6 files changed, 631 insertions(+), 443 deletions(-) diff --git a/lua/create_springboot_project.lua b/lua/create_springboot_project.lua index ad5a238..f113162 100644 --- a/lua/create_springboot_project.lua +++ b/lua/create_springboot_project.lua @@ -1,214 +1,215 @@ local function safe_request(url) - local status, request = pcall(function() - return vim.system({ "curl", "-s", url }, { text = true }):wait() - end) + local status, request = pcall(function() + return vim.system({ "curl", "-s", url }, { text = true }):wait() + end) - if not status then - vim.api.nvim_err_writeln("Error making request to " .. url .. ": " .. request) - return nil - end + if not status then + vim.api.nvim_err_writeln("Error making request to " .. url .. ": " .. request) + return nil + end - return request + return request end local function safe_json_decode(data) - local status, decoded = pcall(vim.fn.json_decode, data) + local status, decoded = pcall(vim.fn.json_decode, data) - if not status then - vim.api.nvim_err_writeln("Error decoding JSON: " .. decoded) - return nil - end + if not status then + vim.api.nvim_err_writeln("Error decoding JSON: " .. decoded) + return nil + end - return decoded + return decoded end local function contains(list, element) - for _, value in pairs(list) do - if value == element then - return true - end - end - return false + for _, value in pairs(list) do + if value == element then + return true + end + end + return false end local function list_to_string(list, is_err) - local result = "" - - for i, value in ipairs(list) do - if is_err then - result = result .. "'" .. tostring(value) .. "'" - else - result = result .. tostring(value) - end - if i < #list then - if is_err then - result = result .. " or " - else - result = result .. "/" - end - end - end - return result + local result = "" + + for i, value in ipairs(list) do + if is_err then + result = result .. "'" .. tostring(value) .. "'" + else + result = result .. tostring(value) + end + if i < #list then + if is_err then + result = result .. " or " + else + result = result .. "/" + end + end + end + return result end local function handle_start_springboot_data(data) - local spring_data = {} - for _, value in pairs(data.values) do - table.insert(spring_data, value.id) - end - return spring_data + local spring_data = {} + for _, value in pairs(data.values) do + table.insert(spring_data, value.id) + end + return spring_data end local function get_build_type(data_available) - local build_type_available = list_to_string(data_available, false) - local options_err = list_to_string(data_available, true) - local build_type = vim.fn.input("Enter build type (" .. build_type_available .. "): ", "maven") - if not contains(data_available, build_type) then - print("Invalid build type. Please enter " .. options_err .. ".") - return "" - end - - return build_type + local build_type_available = list_to_string(data_available, false) + local options_err = list_to_string(data_available, true) + local build_type = vim.fn.input("Enter build type (" .. build_type_available .. "): ", "maven") + if not contains(data_available, build_type) then + print("Invalid build type. Please enter " .. options_err .. ".") + return "" + end + + return build_type end local function get_language(data_available) - local language_available = list_to_string(data_available, false) - local options_err = list_to_string(data_available, true) + local language_available = list_to_string(data_available, false) + local options_err = list_to_string(data_available, true) - local language = vim.fn.input("Enter Language (" .. language_available .. "): ", "java") - if not contains(data_available, language) then - print("Invalid language. Please enter " .. options_err .. ".") - return "" - end + local language = vim.fn.input("Enter Language (" .. language_available .. "): ", "java") + if not contains(data_available, language) then + print("Invalid language. Please enter " .. options_err .. ".") + return "" + end - return language + return language end local function get_java_version(data_available) - local version_available = list_to_string(data_available, false) - local options_err = list_to_string(data_available, true) + local version_available = list_to_string(data_available, false) + local options_err = list_to_string(data_available, true) - local java_version = vim.fn.input("Enter Java Version (" .. version_available .. "): ", "21") - if not contains(data_available, java_version) then - print("Invalid Java version. Please enter a valid version " .. options_err .. ".") - return "" - end + local java_version = vim.fn.input("Enter Java Version (" .. version_available .. "): ", "21") + if not contains(data_available, java_version) then + print("Invalid Java version. Please enter a valid version " .. options_err .. ".") + return "" + end - return java_version + return java_version end local function get_boot_version(data_available) - local version_available = list_to_string(data_available, false) - local options_err = list_to_string(data_available, true) + local version_available = list_to_string(data_available, false) + local options_err = list_to_string(data_available, true) - local boot_version = vim.fn.input("Enter Spring Boot Version (" .. version_available .. "): ", data_available[#data_available]) - if not contains(data_available, boot_version) then - print("Invalid Spring Boot version. Please enter a valid version " .. options_err .. ".") - return "" - end + local boot_version = + vim.fn.input("Enter Spring Boot Version (" .. version_available .. "): ", data_available[#data_available]) + if not contains(data_available, boot_version) then + print("Invalid Spring Boot version. Please enter a valid version " .. options_err .. ".") + return "" + end - return boot_version + return boot_version end local function get_packaging(data_available) - local packaging_available = list_to_string(data_available, false) - local options_err = list_to_string(data_available, true) - - local packaging = vim.fn.input("Enter Packaging(" .. packaging_available .. "): ", "jar") - if packaging ~= "jar" and packaging ~= "war" then - print("Invalid packaging. Please enter " .. options_err .. ".") - return "" - end - return packaging + local packaging_available = list_to_string(data_available, false) + local options_err = list_to_string(data_available, true) + + local packaging = vim.fn.input("Enter Packaging(" .. packaging_available .. "): ", "jar") + if packaging ~= "jar" and packaging ~= "war" then + print("Invalid packaging. Please enter " .. options_err .. ".") + return "" + end + return packaging end local function is_nvim_tree_available() - local has_nvim_tree_cmd = vim.fn.exists(":NvimTreeFindFileToggle") == 2 + local has_nvim_tree_cmd = vim.fn.exists(":NvimTreeFindFileToggle") == 2 - local has_nvim_tree_module = pcall(require, "nvim-tree") + local has_nvim_tree_module = pcall(require, "nvim-tree") - return has_nvim_tree_cmd or has_nvim_tree_module + return has_nvim_tree_cmd or has_nvim_tree_module end local function springboot_new_project() - local request = safe_request("https://start.spring.io/metadata/client") - - if not request then - vim.api.nvim_err_writeln("Failed to make a request to the URL.") - return false - end - - local springboot_data = safe_json_decode(request.stdout) - - if not springboot_data then - vim.api.nvim_err_writeln("Failed to decode JSON from the request.") - return false - end - local build_types = { "maven", "gradle" } - local languages = handle_start_springboot_data(springboot_data.language) - local java_versions = handle_start_springboot_data(springboot_data.javaVersion) - local boot_versions = handle_start_springboot_data(springboot_data.bootVersion) - local packagings = handle_start_springboot_data(springboot_data.packaging) - local build_type = get_build_type(build_types) - - if build_type:len() == 0 then - return - end - - local language = get_language(languages) - if language:len() == 0 then - return - end - - local java_version = get_java_version(java_versions) - if java_version:len() == 0 then - return - end - - local boot_version = get_boot_version(boot_versions) - if boot_version:len() == 0 then - return - end - - local packaging = get_packaging(packagings) - if packaging:len() == 0 then - return - end - - local dependencies = vim.fn.input("Enter dependencies (comma separated): ", "devtools,web,data-jpa,h2,thymeleaf") - local group_id = vim.fn.input("Enter Group ID: ", "com.example") - local artifact_id = vim.fn.input("Enter Artifact ID: ", "myproject") - local name = vim.fn.input("Enter project name: ", artifact_id) - local package_name = vim.fn.input("Enter package name: ", group_id .. "." .. artifact_id) - - local command = string.format( - "spring init --boot-version=%s --java-version=%s --build=%s --dependencies=%s --groupId=%s --artifactId=%s --name=%s --package-name=%s %s", - boot_version, - java_version, - build_type, - dependencies, - group_id, - artifact_id, - name, - package_name, - name - ) - - local output = vim.fn.system(command) - if vim.v.shell_error ~= 0 then - print("Erro ao executar: " .. output) - else - print(output) - vim.fn.chdir(name) - local pathJava = vim.fn.system("fd -I java src/main/java") - - vim.cmd("e " .. pathJava) - if is_nvim_tree_available() then - vim.cmd("NvimTreeFindFileToggle") - end - end - - print("Project created successfully!") + local request = safe_request("https://start.spring.io/metadata/client") + + if not request then + vim.api.nvim_err_writeln("Failed to make a request to the URL.") + return false + end + + local springboot_data = safe_json_decode(request.stdout) + + if not springboot_data then + vim.api.nvim_err_writeln("Failed to decode JSON from the request.") + return false + end + local build_types = { "maven", "gradle" } + local languages = handle_start_springboot_data(springboot_data.language) + local java_versions = handle_start_springboot_data(springboot_data.javaVersion) + local boot_versions = handle_start_springboot_data(springboot_data.bootVersion) + local packagings = handle_start_springboot_data(springboot_data.packaging) + local build_type = get_build_type(build_types) + + if build_type:len() == 0 then + return + end + + local language = get_language(languages) + if language:len() == 0 then + return + end + + local java_version = get_java_version(java_versions) + if java_version:len() == 0 then + return + end + + local boot_version = get_boot_version(boot_versions) + if boot_version:len() == 0 then + return + end + + local packaging = get_packaging(packagings) + if packaging:len() == 0 then + return + end + + local dependencies = vim.fn.input("Enter dependencies (comma separated): ", "devtools,web,data-jpa,h2,thymeleaf") + local group_id = vim.fn.input("Enter Group ID: ", "com.example") + local artifact_id = vim.fn.input("Enter Artifact ID: ", "myproject") + local name = vim.fn.input("Enter project name: ", artifact_id) + local package_name = vim.fn.input("Enter package name: ", group_id .. "." .. artifact_id) + + local command = string.format( + "spring init --boot-version=%s --java-version=%s --build=%s --dependencies=%s --groupId=%s --artifactId=%s --name=%s --package-name=%s %s", + boot_version, + java_version, + build_type, + dependencies, + group_id, + artifact_id, + name, + package_name, + name + ) + + local output = vim.fn.system(command) + if vim.v.shell_error ~= 0 then + print("Erro ao executar: " .. output) + else + print(output) + vim.fn.chdir(name) + local pathJava = vim.fn.system("fd -I java src/main/java") + + vim.cmd("e " .. pathJava) + if is_nvim_tree_available() then + vim.cmd("NvimTreeFindFileToggle") + end + end + + print("Project created successfully!") end vim.api.nvim_create_user_command("SpringBootNewProject", springboot_new_project, {}) diff --git a/lua/springboot-nvim/generateclass.lua b/lua/springboot-nvim/generateclass.lua index 2508aca..bf40cd6 100644 --- a/lua/springboot-nvim/generateclass.lua +++ b/lua/springboot-nvim/generateclass.lua @@ -8,13 +8,15 @@ local bufs local class_boiler_plate = "package %s;\n\npublic class %s{\n\n}" local function generate_class() - local file_path = vim.fn.fnamemodify(start_buf, ':p') + local file_path = vim.fn.fnamemodify(start_buf, ":p") -- Search the LAST occurrence of "/java/" in file_path local last_java_idx = nil local search_start = 1 while true do local start_idx, end_idx = file_path:find("/java/", search_start) - if not end_idx then break end + if not end_idx then + break + end last_java_idx = end_idx search_start = start_idx + 1 end @@ -32,27 +34,27 @@ local function generate_class() local package_lines = api.nvim_buf_get_lines(bufs[3], 0, -1, false) local base_package_path = table.concat(package_lines) local package_path = base_package_path:gsub("%.", "/") - if (package_path:sub(-1, -1) ~= '/' and package_path ~= '') then + if package_path:sub(-1, -1) ~= "/" and package_path ~= "" then package_path = package_path .. "/" end - if(class_content ~= '') then + if class_content ~= "" then -- Check the specified package directory to make sure it exists - if(vim.fn.isdirectory(root_path .. '/' .. package_path) == 1) then + if vim.fn.isdirectory(root_path .. "/" .. package_path) == 1 then print("package directory exists") else -- Make the package directory if it does not exist - local command = 'mkdir -p ' .. root_path .. '/' .. package_path + local command = "mkdir -p " .. root_path .. "/" .. package_path os.execute(command) end -- Generate the new java file and inject boiler plate -- Need to strip and trailing periods from the package - if(base_package_path:sub(-1, -1) == '.') then + if base_package_path:sub(-1, -1) == "." then base_package_path = string.sub(base_package_path, 1, -2) end local java_file_content = string.format(class_boiler_plate, base_package_path, class_content) local java_file = io.open(root_path .. "/" .. package_path .. class_content .. ".java", "r") - if(java_file) then + if java_file then print("Class already exists in package") java_file:close() else @@ -61,53 +63,51 @@ local function generate_class() java_file:close() close_generate_class() local path = root_path .. "/" .. package_path .. class_content .. ".java" - vim.cmd('edit ' .. vim.fn.fnameescape(path)) + vim.cmd("edit " .. vim.fn.fnameescape(path)) end else print("Please specify a class name to continue") end - end local function create_package_ui(row, col, width, height, file_path) local package_buf = api.nvim_create_buf(false, true) --api.nvim_buf_set_option(package_buf, 'bufhidden', 'wipe') - api.nvim_buf_set_option(package_buf, 'filetype', 'springbootnvim') - + api.nvim_buf_set_option(package_buf, "filetype", "springbootnvim") + local opts = { - style = 'minimal', - relative = 'editor', + style = "minimal", + relative = "editor", width = width, height = height, row = row, col = col, - zindex = 102 + zindex = 102, } local package = package_text(file_path) - api.nvim_buf_set_lines(package_buf, 0, -1, false, {package}) + api.nvim_buf_set_lines(package_buf, 0, -1, false, { package }) local package_win = api.nvim_open_win(package_buf, true, opts) - + table.insert(windows, package_win) table.insert(bufs, package_buf) return { buf = package_buf, - win = package_win + win = package_win, } - end local function create_ui(bufnr) -- Get the file from where the generate class was called from start_buf = vim.fn.bufname(bufnr) - local file_path = vim.fn.fnamemodify(start_buf, ':p') - local project_root = get_spring_boot_project_root(file_path) - local main_class_dir = find_main_application_class_directory(project_root) + local file_path = vim.fn.fnamemodify(start_buf, ":p") + local project_root = get_spring_boot_project_root(file_path) + local main_class_dir = find_main_application_class_directory(project_root) windows = {} bufs = {} -- Create buffer for popup buf = api.nvim_create_buf(false, true) table.insert(bufs, buf) - api.nvim_buf_set_option(buf, 'bufhidden', 'wipe') + api.nvim_buf_set_option(buf, "bufhidden", "wipe") local border_buf = api.nvim_create_buf(false, true) table.insert(bufs, border_buf) --api.nvim_buf_set_option(border_buf, 'bufhidden', 'wipe') @@ -119,23 +119,23 @@ local function create_ui(bufnr) local col = math.floor((vim.fn.winwidth(0) - width) / 2) local border_opts = { - style = 'minimal', - relative = 'editor', + style = "minimal", + relative = "editor", width = width + 2, height = height + 2, row = row - 1, col = col - 1, - zindex = 99 + zindex = 99, } local opts = { - style = 'minimal', - relative = 'editor', + style = "minimal", + relative = "editor", width = width, height = height, row = row, col = col, - zindex = 100 + zindex = 100, } local outline = draw_border(width, height) @@ -146,22 +146,22 @@ local function create_ui(bufnr) win = api.nvim_open_win(buf, true, opts) table.insert(windows, win) --api.nvim_command('au BufWipeout exe "silent bdelete! "' ..border_buf) - + --api.nvim_win_set_option(win, 'cursorline', true) - - api.nvim_buf_set_lines(buf, 0, -1, false, {center_text("Generate Class")}) + + api.nvim_buf_set_lines(buf, 0, -1, false, { center_text("Generate Class") }) local package_section = draw_package_section() api.nvim_buf_set_lines(buf, 1, -1, false, package_section) local class_section = draw_class_section() api.nvim_buf_set_lines(buf, 5, -1, false, class_section) - api.nvim_buf_set_lines(buf, 8, -1, false, {center_text('Confirm selections with ')}) + api.nvim_buf_set_lines(buf, 8, -1, false, { center_text("Confirm selections with ") }) local package_area = create_package_ui(row + 2, col + 10, 48, 1, main_class_dir) local class_area = create_class_ui(row + 5, col + 10, 25, 1) api.nvim_set_current_win(package_area.win) - local first_line = vim.fn.getline(1,1) + local first_line = vim.fn.getline(1, 1) local first_line_length = string.len(first_line[1]) --api.nvim_feedkeys('a', 'n', true) - api.nvim_win_set_cursor(package_area.win, {1,first_line_length}) + api.nvim_win_set_cursor(package_area.win, { 1, first_line_length }) set_mappings() end @@ -170,5 +170,5 @@ return { close_generate_class = close_generate_class, navigate_to_class = navigate_to_class, navigate_to_package = navigate_to_package, - generate_class = generate_class + generate_class = generate_class, } diff --git a/lua/springboot-nvim/init.lua b/lua/springboot-nvim/init.lua index 83cf337..1b68229 100644 --- a/lua/springboot-nvim/init.lua +++ b/lua/springboot-nvim/init.lua @@ -8,174 +8,174 @@ local jdtls = require("jdtls") local on_compile_result = nil local function incremental_compile() - jdtls.compile("incremental", on_compile_result) + jdtls.compile("incremental", on_compile_result) end local function is_plugin_installed(plugin) - local status, _ = pcall(require, plugin) - return status + local status, _ = pcall(require, plugin) + return status end local function get_spring_boot_project_root() - local current_file = vim.fn.expand("%:p") - if current_file == "" then - print("No file is currently open.") - return nil - end + local current_file = vim.fn.expand("%:p") + if current_file == "" then + print("No file is currently open.") + return nil + end - local root_pattern = { "pom.xml", "build.gradle", "build.gradle.kts", ".git" } + local root_pattern = { "pom.xml", "build.gradle", "build.gradle.kts", ".git" } - local root_dir = lspconfig.util.root_pattern(unpack(root_pattern))(current_file) - if not root_dir then - print("Project root not found.") - return nil - end + local root_dir = lspconfig.util.root_pattern(unpack(root_pattern))(current_file) + if not root_dir then + print("Project root not found.") + return nil + end - return root_dir + return root_dir end local function get_run_command(args) - local project_root = get_spring_boot_project_root() - if not project_root then - return "Unknown" - end - - local maven_file = vim.fn.findfile("pom.xml", project_root) - local gradle_file = vim.fn.findfile("build.gradle", project_root) - local kts_gradle_file = vim.fn.findfile("build.gradle.kts", project_root) - - if maven_file ~= "" then - return string.format( - ':call jobsend(b:terminal_job_id, "cd %s && mvn spring-boot:run %s \\n")', - project_root, - args or "" - ) - elseif gradle_file or kts_gradle_file ~= "" then - return string.format( - ':call jobsend(b:terminal_job_id, "cd %s && ./gradlew bootRun %s \\n")', - project_root, - args or "" - ) - else - print("No build file (pom.xml or build.gradle) found in the project root.") - return "Unknown" - end + local project_root = get_spring_boot_project_root() + if not project_root then + return "Unknown" + end + + local maven_file = vim.fn.findfile("pom.xml", project_root) + local gradle_file = vim.fn.findfile("build.gradle", project_root) + local kts_gradle_file = vim.fn.findfile("build.gradle.kts", project_root) + + if maven_file ~= "" then + return string.format( + ':call jobsend(b:terminal_job_id, "cd %s && mvn spring-boot:run %s \\n")', + project_root, + args or "" + ) + elseif gradle_file or kts_gradle_file ~= "" then + return string.format( + ':call jobsend(b:terminal_job_id, "cd %s && ./gradlew bootRun %s \\n")', + project_root, + args or "" + ) + else + print("No build file (pom.xml or build.gradle) found in the project root.") + return "Unknown" + end end local function boot_run(args) - local project_root = get_spring_boot_project_root() - - if project_root then - vim.cmd("split | terminal") - vim.cmd("resize 15") - vim.cmd("norm G") - local cd_cmd = ':call jobsend(b:terminal_job_id, "cd ' .. project_root .. '\\n")' - vim.cmd(cd_cmd) - local run_cmd = get_run_command(args or "") - vim.cmd(run_cmd) - vim.cmd("wincmd k") - else - print("Not in a Spring Boot project") - end + local project_root = get_spring_boot_project_root() + + if project_root then + vim.cmd("split | terminal") + vim.cmd("resize 15") + vim.cmd("norm G") + local cd_cmd = ':call jobsend(b:terminal_job_id, "cd ' .. project_root .. '\\n")' + vim.cmd(cd_cmd) + local run_cmd = get_run_command(args or "") + vim.cmd(run_cmd) + vim.cmd("wincmd k") + else + print("Not in a Spring Boot project") + end end local function contains_package_info(file_path) - local file = io.open(file_path, "r") - if not file then - return false - end - local current_position = file:seek() - local file_size = file:seek("end") - file:seek("set", current_position) - file:close() - - return file_size > 0 + local file = io.open(file_path, "r") + if not file then + return false + end + local current_position = file:seek() + local file_size = file:seek("end") + file:seek("set", current_position) + file:close() + + return file_size > 0 end local function get_java_package(file_path) - local java_file_path = file_path:match("src/(.-)%.java") - if java_file_path then - local package_path = java_file_path:gsub("/", ".") + local java_file_path = file_path:match("src/(.-)%.java") + if java_file_path then + local package_path = java_file_path:gsub("/", ".") - local t = {} - for str in string.gmatch(package_path, "([^.]+)") do - table.insert(t, str) - end + local t = {} + for str in string.gmatch(package_path, "([^.]+)") do + table.insert(t, str) + end - local package = "" + local package = "" - for i = 3, table.getn(t) - 1 do - package = package .. "." .. t[i] - end + for i = 3, table.getn(t) - 1 do + package = package .. "." .. t[i] + end - return string.sub(package, 2, -1) - else - return nil - end + return string.sub(package, 2, -1) + else + return nil + end end local function check_and_add_package() - local file_path = vim.fn.expand("%:p") - if not contains_package_info(file_path) then - local package_location = get_java_package(file_path) - local package_text = "package " .. package_location .. ";" - local buf = vim.api.nvim_get_current_buf() - vim.api.nvim_buf_set_lines(buf, 0, -1, false, { package_text, "", "" }) - vim.api.nvim_win_set_cursor(0, { 3, 0 }) - end + local file_path = vim.fn.expand("%:p") + if not contains_package_info(file_path) then + local package_location = get_java_package(file_path) + local package_text = "package " .. package_location .. ";" + local buf = vim.api.nvim_get_current_buf() + vim.api.nvim_buf_set_lines(buf, 0, -1, false, { package_text, "", "" }) + vim.api.nvim_win_set_cursor(0, { 3, 0 }) + end end local function fill_package_details() - check_and_add_package() + check_and_add_package() end -- key mapping -- auto commands local function setup(opts) - on_compile_result = opts.on_compile_result + on_compile_result = opts.on_compile_result - vim.api.nvim_exec( - [[ + vim.api.nvim_exec( + [[ augroup JavaAutoCommands autocmd! autocmd BufWritePost *.java lua require('springboot-nvim').incremental_compile() augroup END ]], - false - ) + false + ) - vim.api.nvim_exec( - [[ + vim.api.nvim_exec( + [[ augroup JavaPackageDetails autocmd! autocmd BufReadPost *.java lua require('springboot-nvim').fill_package_details() augroup END ]], - false - ) + false + ) - vim.api.nvim_exec( - [[ + vim.api.nvim_exec( + [[ augroup ClosePluginBuffers autocmd! autocmd FileType springbootnvim autocmd QuitPre * lua require('springboot-nvim').close_ui() augroup END ]], - false - ) + false + ) end return { - setup = setup, - boot_run = boot_run, - incremental_compile = incremental_compile, - fill_package_details = fill_package_details, - foo = foo, - create_ui = generate_class.create_ui, - close_ui = springboot_nvim_ui.close_ui, - generate_class = springboot_nvim_ui.create_generate_class_ui, - generate_record = springboot_nvim_ui.create_generate_record_ui, - generate_interface = springboot_nvim_ui.create_generate_interface_ui, - generate_enum = springboot_nvim_ui.create_generate_enum_ui, + setup = setup, + boot_run = boot_run, + incremental_compile = incremental_compile, + fill_package_details = fill_package_details, + foo = foo, + create_ui = generate_class.create_ui, + close_ui = springboot_nvim_ui.close_ui, + generate_class = springboot_nvim_ui.create_generate_class_ui, + generate_record = springboot_nvim_ui.create_generate_record_ui, + generate_interface = springboot_nvim_ui.create_generate_interface_ui, + generate_enum = springboot_nvim_ui.create_generate_enum_ui, } diff --git a/lua/springboot-nvim/ui/springboot_nvim_ui.lua b/lua/springboot-nvim/ui/springboot_nvim_ui.lua index b41822c..c272be7 100644 --- a/lua/springboot-nvim/ui/springboot_nvim_ui.lua +++ b/lua/springboot-nvim/ui/springboot_nvim_ui.lua @@ -1,21 +1,21 @@ -local ui_utils = require"springboot-nvim.ui.ui_utils" -local utils = require"springboot-nvim.utils" +local ui_utils = require("springboot-nvim.ui.ui_utils") +local utils = require("springboot-nvim.utils") local api = vim.api local components local function close_ui() - if(components ~= nil) then + if components ~= nil then local bufs = components.bufs local wins = components.wins for _, win_id in pairs(wins) do - if(api.nvim_win_is_valid(win_id)) then + if api.nvim_win_is_valid(win_id) then api.nvim_win_close(win_id, true) end end - for _,buf_id in pairs(bufs) do - if(api.nvim_buf_is_valid(buf_id)) then + for _, buf_id in pairs(bufs) do + if api.nvim_buf_is_valid(buf_id) then api.nvim_buf_delete(buf_id, { force = true }) end end @@ -53,56 +53,166 @@ end local function set_mappings(start_buf, bufs, type) --api.nvim_buf_set_keymap(buf, 'n', '', ':lua require("springboot-nvim.generateclass").generate_class()', {nowait=true, noremap=true, silent=true}) - for _,b in pairs(bufs) do - api.nvim_buf_set_keymap(b, 'n', 'k', ':lua require("springboot-nvim.ui.springboot_nvim_ui").navigate_to_package()', {nowait=true, noremap=true, silent=true}) + for _, b in pairs(bufs) do + api.nvim_buf_set_keymap( + b, + "n", + "k", + ':lua require("springboot-nvim.ui.springboot_nvim_ui").navigate_to_package()', + { nowait = true, noremap = true, silent = true } + ) if type == "class" then - api.nvim_buf_set_keymap(b, 'n', 'j', ':lua require("springboot-nvim.ui.springboot_nvim_ui").navigate_to_class()', {nowait=true, noremap=true, silent=true}) - api.nvim_buf_set_keymap(b, 'n', '', ':lua require("springboot-nvim.utils").generate_java_file(' .. start_buf .. ',"' .. type .. '","' .. bufs.package_buf.. '","' .. bufs.class_buf .. '")', {nowait=true, noremap=true, silent=true}) + api.nvim_buf_set_keymap( + b, + "n", + "j", + ':lua require("springboot-nvim.ui.springboot_nvim_ui").navigate_to_class()', + { nowait = true, noremap = true, silent = true } + ) + api.nvim_buf_set_keymap( + b, + "n", + "", + ':lua require("springboot-nvim.utils").generate_java_file(' + .. start_buf + .. ',"' + .. type + .. '","' + .. bufs.package_buf + .. '","' + .. bufs.class_buf + .. '")', + { nowait = true, noremap = true, silent = true } + ) end if type == "record" then - api.nvim_buf_set_keymap(b, 'n', 'j', ':lua require("springboot-nvim.ui.springboot_nvim_ui").navigate_to_record()', {nowait=true, noremap=true, silent=true}) - api.nvim_buf_set_keymap(b, 'n', '', ':lua require("springboot-nvim.utils").generate_java_file(' .. start_buf .. ',"' .. type .. '","' .. bufs.package_buf.. '","' .. bufs.record_buf .. '")', {nowait=true, noremap=true, silent=true}) + api.nvim_buf_set_keymap( + b, + "n", + "j", + ':lua require("springboot-nvim.ui.springboot_nvim_ui").navigate_to_record()', + { nowait = true, noremap = true, silent = true } + ) + api.nvim_buf_set_keymap( + b, + "n", + "", + ':lua require("springboot-nvim.utils").generate_java_file(' + .. start_buf + .. ',"' + .. type + .. '","' + .. bufs.package_buf + .. '","' + .. bufs.record_buf + .. '")', + { nowait = true, noremap = true, silent = true } + ) end - if(type == "interface") then - api.nvim_buf_set_keymap(b, 'n', 'j', ':lua require("springboot-nvim.ui.springboot_nvim_ui").navigate_to_interface()', {nowait=true, noremap=true, silent=true}) - api.nvim_buf_set_keymap(b, 'n', '', ':lua require("springboot-nvim.utils").generate_java_file(' .. start_buf .. ',"' .. type .. '","' .. bufs.package_buf.. '","' .. bufs.interface_buf .. '")', {nowait=true, noremap=true, silent=true}) + if type == "interface" then + api.nvim_buf_set_keymap( + b, + "n", + "j", + ':lua require("springboot-nvim.ui.springboot_nvim_ui").navigate_to_interface()', + { nowait = true, noremap = true, silent = true } + ) + api.nvim_buf_set_keymap( + b, + "n", + "", + ':lua require("springboot-nvim.utils").generate_java_file(' + .. start_buf + .. ',"' + .. type + .. '","' + .. bufs.package_buf + .. '","' + .. bufs.interface_buf + .. '")', + { nowait = true, noremap = true, silent = true } + ) end - if(type == "enum") then - api.nvim_buf_set_keymap(b, 'n', 'j', ':lua require("springboot-nvim.ui.springboot_nvim_ui").navigate_to_enum()', {nowait=true, noremap=true, silent=true}) - api.nvim_buf_set_keymap(b, 'n', '', ':lua require("springboot-nvim.utils").generate_java_file(' .. start_buf .. ',"' .. type .. '","' .. bufs.package_buf.. '","' .. bufs.enum_buf .. '")', {nowait=true, noremap=true, silent=true}) + if type == "enum" then + api.nvim_buf_set_keymap( + b, + "n", + "j", + ':lua require("springboot-nvim.ui.springboot_nvim_ui").navigate_to_enum()', + { nowait = true, noremap = true, silent = true } + ) + api.nvim_buf_set_keymap( + b, + "n", + "", + ':lua require("springboot-nvim.utils").generate_java_file(' + .. start_buf + .. ',"' + .. type + .. '","' + .. bufs.package_buf + .. '","' + .. bufs.enum_buf + .. '")', + { nowait = true, noremap = true, silent = true } + ) end - api.nvim_buf_set_keymap(b, 'n', 'q', ':lua require("springboot-nvim.ui.springboot_nvim_ui").close_ui()', {nowait=true, noremap=true, silent=true}) - api.nvim_buf_set_keymap(b, 'i', '', '', {nowait=true, noremap=true, silent=true}) - api.nvim_buf_set_keymap(b, 'i', '', '', {nowait=true, noremap=true, silent=true}) - api.nvim_buf_set_keymap(b, 'n', '', '', { nowait = true, noremap = true, silent = true }) - api.nvim_buf_set_keymap(b, 'n', '', '', { nowait = true, noremap = true, silent = true }) - api.nvim_buf_set_keymap(b, 'i', '', '', { nowait = true, noremap = true, silent = true }) - api.nvim_buf_set_keymap(b, 'i', '', '', { nowait = true, noremap = true, silent = true }) - + api.nvim_buf_set_keymap( + b, + "n", + "q", + ':lua require("springboot-nvim.ui.springboot_nvim_ui").close_ui()', + { nowait = true, noremap = true, silent = true } + ) + api.nvim_buf_set_keymap(b, "i", "", "", { nowait = true, noremap = true, silent = true }) + api.nvim_buf_set_keymap(b, "i", "", "", { nowait = true, noremap = true, silent = true }) + api.nvim_buf_set_keymap(b, "n", "", "", { nowait = true, noremap = true, silent = true }) + api.nvim_buf_set_keymap(b, "n", "", "", { nowait = true, noremap = true, silent = true }) + api.nvim_buf_set_keymap(b, "i", "", "", { nowait = true, noremap = true, silent = true }) + api.nvim_buf_set_keymap(b, "i", "", "", { nowait = true, noremap = true, silent = true }) end local other_chars = { - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'l', 'n', 'o', 'p', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' + "a", + "b", + "c", + "d", + "e", + "f", + "g", + "h", + "l", + "n", + "o", + "p", + "r", + "s", + "t", + "u", + "v", + "w", + "x", + "y", + "z", } - for _,b in ipairs(bufs) do - for _,v in ipairs(other_chars) do - api.nvim_buf_set_keymap(b, 'n', v, '', { nowait = true, noremap = true, silent = true }) - api.nvim_buf_set_keymap(b, 'n', v:upper(), '', { nowait = true, noremap = true, silent = true }) - api.nvim_buf_set_keymap(b, 'n', '', '', { nowait = true, noremap = true, silent = true }) + for _, b in ipairs(bufs) do + for _, v in ipairs(other_chars) do + api.nvim_buf_set_keymap(b, "n", v, "", { nowait = true, noremap = true, silent = true }) + api.nvim_buf_set_keymap(b, "n", v:upper(), "", { nowait = true, noremap = true, silent = true }) + api.nvim_buf_set_keymap(b, "n", "", "", { nowait = true, noremap = true, silent = true }) end end end local function create_generate_class_ui() - local bufnr = vim.api.nvim_get_current_buf() if components ~= nil then - print('You cannot open multiple generate modals at a time') + print("You cannot open multiple generate modals at a time") return end - local project_root = utils.get_spring_boot_project_root(vim.fn.fnamemodify(bufnr, ':p')) + local project_root = utils.get_spring_boot_project_root(vim.fn.fnamemodify(bufnr, ":p")) local main_class = utils.find_main_application_class_directory(project_root) local width = 60 local height = 9 @@ -110,8 +220,17 @@ local function create_generate_class_ui() local row = math.floor((vim.fn.winheight(0) - height) / 2) local col = math.floor((vim.fn.winwidth(0) - width) / 2) local popup_components = ui_utils.draw_popup(width, height, row, col, "Generate Class") - local package_components = ui_utils.draw_labeled_input(46, 1, row+1, col+1, "Package: ", popup_components.popup_buf, ui_utils.package_text(main_class)) - local class_components = ui_utils.draw_labeled_input(46, 1, row+4, col+1, "Class: ", popup_components.popup_buf, "") + local package_components = ui_utils.draw_labeled_input( + 46, + 1, + row + 1, + col + 1, + "Package: ", + popup_components.popup_buf, + ui_utils.package_text(main_class) + ) + local class_components = + ui_utils.draw_labeled_input(46, 1, row + 4, col + 1, "Class: ", popup_components.popup_buf, "") components = { bufs = { popup_buf = popup_components.popup_buf, @@ -123,34 +242,44 @@ local function create_generate_class_ui() popup_win = popup_components.popup_win, border_win = popup_components.border_win, package_win = package_components.input_win, - class_win = class_components.input_win - } + class_win = class_components.input_win, + }, } - set_mappings(bufnr, components.bufs, 'class') - local edit_package = string.rep(' ', 8) .. "Edit Package: " - local edit_class = "Edit Class: " .. string.rep(' ', 8) - local confirm_class = string.rep(' ', 8) .. "Confirm Class: " - local close_menu = "Close Menu: " .. string.rep(' ', 8) - api.nvim_buf_set_lines(popup_components.popup_buf, row+8, -1, false, {edit_package .. string.rep(' ', 60 - (string.len(edit_package) + string.len(edit_class))) .. edit_class}) - api.nvim_buf_set_lines(popup_components.popup_buf, row+9, -1, false, {confirm_class .. string.rep(' ', 60 - (string.len(confirm_class) + string.len(close_menu))) .. close_menu}) + set_mappings(bufnr, components.bufs, "class") + local edit_package = string.rep(" ", 8) .. "Edit Package: " + local edit_class = "Edit Class: " .. string.rep(" ", 8) + local confirm_class = string.rep(" ", 8) .. "Confirm Class: " + local close_menu = "Close Menu: " .. string.rep(" ", 8) + api.nvim_buf_set_lines( + popup_components.popup_buf, + row + 8, + -1, + false, + { edit_package .. string.rep(" ", 60 - (string.len(edit_package) + string.len(edit_class))) .. edit_class } + ) + api.nvim_buf_set_lines( + popup_components.popup_buf, + row + 9, + -1, + false, + { confirm_class .. string.rep(" ", 60 - (string.len(confirm_class) + string.len(close_menu))) .. close_menu } + ) api.nvim_set_current_win(components.wins.package_win) - local first_line = vim.fn.getline(1,1) + local first_line = vim.fn.getline(1, 1) local first_line_length = string.len(first_line[1]) - api.nvim_feedkeys('a', 'n', true) - api.nvim_win_set_cursor(components.wins.package_win, {1,first_line_length}) - + api.nvim_feedkeys("a", "n", true) + api.nvim_win_set_cursor(components.wins.package_win, { 1, first_line_length }) end local function create_generate_record_ui() - local bufnr = vim.api.nvim_get_current_buf() if components ~= nil then - print('You cannot open multiple generate modals at a time') + print("You cannot open multiple generate modals at a time") return end - local project_root = utils.get_spring_boot_project_root(vim.fn.fnamemodify(bufnr, ':p')) + local project_root = utils.get_spring_boot_project_root(vim.fn.fnamemodify(bufnr, ":p")) local main_class = utils.find_main_application_class_directory(project_root) local width = 60 local height = 9 @@ -158,8 +287,17 @@ local function create_generate_record_ui() local row = math.floor((vim.fn.winheight(0) - height) / 2) local col = math.floor((vim.fn.winwidth(0) - width) / 2) local popup_components = ui_utils.draw_popup(width, height, row, col, "Generate Record") - local package_components = ui_utils.draw_labeled_input(46, 1, row+1, col+1, "Package: ", popup_components.popup_buf, ui_utils.package_text(main_class)) - local record_components = ui_utils.draw_labeled_input(46, 1, row+4, col+1, "Record: ", popup_components.popup_buf, "") + local package_components = ui_utils.draw_labeled_input( + 46, + 1, + row + 1, + col + 1, + "Package: ", + popup_components.popup_buf, + ui_utils.package_text(main_class) + ) + local record_components = + ui_utils.draw_labeled_input(46, 1, row + 4, col + 1, "Record: ", popup_components.popup_buf, "") components = { bufs = { popup_buf = popup_components.popup_buf, @@ -171,32 +309,43 @@ local function create_generate_record_ui() popup_win = popup_components.popup_win, border_win = popup_components.border_win, package_win = package_components.input_win, - record_win = record_components.input_win - } + record_win = record_components.input_win, + }, } - set_mappings(bufnr, components.bufs, 'record') - local edit_package = string.rep(' ', 8) .. "Edit Package: " - local edit_class = "Edit Class: " .. string.rep(' ', 8) - local confirm_class = string.rep(' ', 8) .. "Confirm Record: " - local close_menu = "Close Menu: " .. string.rep(' ', 8) - api.nvim_buf_set_lines(popup_components.popup_buf, row+8, -1, false, {edit_package .. string.rep(' ', 60 - (string.len(edit_package) + string.len(edit_class))) .. edit_class}) - api.nvim_buf_set_lines(popup_components.popup_buf, row+9, -1, false, {confirm_class .. string.rep(' ', 60 - (string.len(confirm_class) + string.len(close_menu))) .. close_menu}) + set_mappings(bufnr, components.bufs, "record") + local edit_package = string.rep(" ", 8) .. "Edit Package: " + local edit_class = "Edit Class: " .. string.rep(" ", 8) + local confirm_class = string.rep(" ", 8) .. "Confirm Record: " + local close_menu = "Close Menu: " .. string.rep(" ", 8) + api.nvim_buf_set_lines( + popup_components.popup_buf, + row + 8, + -1, + false, + { edit_package .. string.rep(" ", 60 - (string.len(edit_package) + string.len(edit_class))) .. edit_class } + ) + api.nvim_buf_set_lines( + popup_components.popup_buf, + row + 9, + -1, + false, + { confirm_class .. string.rep(" ", 60 - (string.len(confirm_class) + string.len(close_menu))) .. close_menu } + ) api.nvim_set_current_win(components.wins.package_win) - local first_line = vim.fn.getline(1,1) + local first_line = vim.fn.getline(1, 1) local first_line_length = string.len(first_line[1]) - api.nvim_feedkeys('a', 'n', true) - api.nvim_win_set_cursor(components.wins.package_win, {1,first_line_length}) - + api.nvim_feedkeys("a", "n", true) + api.nvim_win_set_cursor(components.wins.package_win, { 1, first_line_length }) end local function create_generate_interface_ui() local bufnr = vim.api.nvim_get_current_buf() if components ~= nil then - print('You cannot open multiple generate modals at a time') + print("You cannot open multiple generate modals at a time") return end - local project_root = utils.get_spring_boot_project_root(vim.fn.fnamemodify(bufnr, ':p')) + local project_root = utils.get_spring_boot_project_root(vim.fn.fnamemodify(bufnr, ":p")) local main_class = utils.find_main_application_class_directory(project_root) local width = 60 local height = 9 @@ -204,8 +353,17 @@ local function create_generate_interface_ui() local row = math.floor((vim.fn.winheight(0) - height) / 2) local col = math.floor((vim.fn.winwidth(0) - width) / 2) local popup_components = ui_utils.draw_popup(width, height, row, col, "Generate Interface") - local package_components = ui_utils.draw_labeled_input(46, 1, row+1, col+1, "Package: ", popup_components.popup_buf, ui_utils.package_text(main_class)) - local interface_components = ui_utils.draw_labeled_input(46, 1, row+4, col+1, "Interface: ", popup_components.popup_buf, "") + local package_components = ui_utils.draw_labeled_input( + 46, + 1, + row + 1, + col + 1, + "Package: ", + popup_components.popup_buf, + ui_utils.package_text(main_class) + ) + local interface_components = + ui_utils.draw_labeled_input(46, 1, row + 4, col + 1, "Interface: ", popup_components.popup_buf, "") components = { bufs = { popup_buf = popup_components.popup_buf, @@ -217,32 +375,43 @@ local function create_generate_interface_ui() popup_win = popup_components.popup_win, border_win = popup_components.border_win, package_win = package_components.input_win, - interface_win = interface_components.input_win - } + interface_win = interface_components.input_win, + }, } - set_mappings(bufnr, components.bufs, 'interface') - local edit_package = string.rep(' ', 8) .. "Edit Package: " - local edit_class = "Edit Interface: " .. string.rep(' ', 8) - local confirm_class = string.rep(' ', 8) .. "Confirm Interface: " - local close_menu = "Close Menu: " .. string.rep(' ', 8) - api.nvim_buf_set_lines(popup_components.popup_buf, row+8, -1, false, {edit_package .. string.rep(' ', 60 - (string.len(edit_package) + string.len(edit_class))) .. edit_class}) - api.nvim_buf_set_lines(popup_components.popup_buf, row+9, -1, false, {confirm_class .. string.rep(' ', 60 - (string.len(confirm_class) + string.len(close_menu))) .. close_menu}) + set_mappings(bufnr, components.bufs, "interface") + local edit_package = string.rep(" ", 8) .. "Edit Package: " + local edit_class = "Edit Interface: " .. string.rep(" ", 8) + local confirm_class = string.rep(" ", 8) .. "Confirm Interface: " + local close_menu = "Close Menu: " .. string.rep(" ", 8) + api.nvim_buf_set_lines( + popup_components.popup_buf, + row + 8, + -1, + false, + { edit_package .. string.rep(" ", 60 - (string.len(edit_package) + string.len(edit_class))) .. edit_class } + ) + api.nvim_buf_set_lines( + popup_components.popup_buf, + row + 9, + -1, + false, + { confirm_class .. string.rep(" ", 60 - (string.len(confirm_class) + string.len(close_menu))) .. close_menu } + ) api.nvim_set_current_win(components.wins.package_win) - local first_line = vim.fn.getline(1,1) + local first_line = vim.fn.getline(1, 1) local first_line_length = string.len(first_line[1]) - api.nvim_feedkeys('a', 'n', true) - api.nvim_win_set_cursor(components.wins.package_win, {1,first_line_length}) - + api.nvim_feedkeys("a", "n", true) + api.nvim_win_set_cursor(components.wins.package_win, { 1, first_line_length }) end local function create_generate_enum_ui() local bufnr = vim.api.nvim_get_current_buf() if components ~= nil then - print('You cannot open multiple generate modals at a time') + print("You cannot open multiple generate modals at a time") return end - local project_root = utils.get_spring_boot_project_root(vim.fn.fnamemodify(bufnr, ':p')) + local project_root = utils.get_spring_boot_project_root(vim.fn.fnamemodify(bufnr, ":p")) local main_class = utils.find_main_application_class_directory(project_root) local width = 60 local height = 9 @@ -250,8 +419,17 @@ local function create_generate_enum_ui() local row = math.floor((vim.fn.winheight(0) - height) / 2) local col = math.floor((vim.fn.winwidth(0) - width) / 2) local popup_components = ui_utils.draw_popup(width, height, row, col, "Generate Enum") - local package_components = ui_utils.draw_labeled_input(46, 1, row+1, col+1, "Package: ", popup_components.popup_buf, ui_utils.package_text(main_class)) - local enum_components = ui_utils.draw_labeled_input(46, 1, row+4, col+1, "Enum: ", popup_components.popup_buf, "") + local package_components = ui_utils.draw_labeled_input( + 46, + 1, + row + 1, + col + 1, + "Package: ", + popup_components.popup_buf, + ui_utils.package_text(main_class) + ) + local enum_components = + ui_utils.draw_labeled_input(46, 1, row + 4, col + 1, "Enum: ", popup_components.popup_buf, "") components = { bufs = { popup_buf = popup_components.popup_buf, @@ -263,22 +441,33 @@ local function create_generate_enum_ui() popup_win = popup_components.popup_win, border_win = popup_components.border_win, package_win = package_components.input_win, - enum_win = enum_components.input_win - } + enum_win = enum_components.input_win, + }, } - set_mappings(bufnr, components.bufs, 'enum') - local edit_package = string.rep(' ', 8) .. "Edit Package: " - local edit_class = "Edit Enum: " .. string.rep(' ', 8) - local confirm_class = string.rep(' ', 8) .. "Confirm Enum: " - local close_menu = "Close Menu: " .. string.rep(' ', 8) - api.nvim_buf_set_lines(popup_components.popup_buf, row+8, -1, false, {edit_package .. string.rep(' ', 60 - (string.len(edit_package) + string.len(edit_class))) .. edit_class}) - api.nvim_buf_set_lines(popup_components.popup_buf, row+9, -1, false, {confirm_class .. string.rep(' ', 60 - (string.len(confirm_class) + string.len(close_menu))) .. close_menu}) + set_mappings(bufnr, components.bufs, "enum") + local edit_package = string.rep(" ", 8) .. "Edit Package: " + local edit_class = "Edit Enum: " .. string.rep(" ", 8) + local confirm_class = string.rep(" ", 8) .. "Confirm Enum: " + local close_menu = "Close Menu: " .. string.rep(" ", 8) + api.nvim_buf_set_lines( + popup_components.popup_buf, + row + 8, + -1, + false, + { edit_package .. string.rep(" ", 60 - (string.len(edit_package) + string.len(edit_class))) .. edit_class } + ) + api.nvim_buf_set_lines( + popup_components.popup_buf, + row + 9, + -1, + false, + { confirm_class .. string.rep(" ", 60 - (string.len(confirm_class) + string.len(close_menu))) .. close_menu } + ) api.nvim_set_current_win(components.wins.package_win) - local first_line = vim.fn.getline(1,1) + local first_line = vim.fn.getline(1, 1) local first_line_length = string.len(first_line[1]) - api.nvim_feedkeys('a', 'n', true) - api.nvim_win_set_cursor(components.wins.package_win, {1,first_line_length}) - + api.nvim_feedkeys("a", "n", true) + api.nvim_win_set_cursor(components.wins.package_win, { 1, first_line_length }) end return { @@ -291,5 +480,5 @@ return { navigate_to_class = navigate_to_class, navigate_to_record = navigate_to_record, navigate_to_interface = navigate_to_interface, - navigate_to_enum = navigate_to_enum + navigate_to_enum = navigate_to_enum, } diff --git a/lua/springboot-nvim/ui/ui_utils.lua b/lua/springboot-nvim/ui/ui_utils.lua index e0d59dc..a885939 100644 --- a/lua/springboot-nvim/ui/ui_utils.lua +++ b/lua/springboot-nvim/ui/ui_utils.lua @@ -2,25 +2,25 @@ local api = vim.api local function center_text(str, width) --local width = api.nvim_win_get_width(0) - local shift = math.floor(width/2) - math.floor(string.len(str) / 2) - return string.rep(' ', shift) .. str + local shift = math.floor(width / 2) - math.floor(string.len(str) / 2) + return string.rep(" ", shift) .. str end local function package_text(file_path) local src_index = string.find(file_path, "/src") local base_package_path = string.sub(file_path, src_index + 15) - if(base_package_path) then + if base_package_path then local package_path = base_package_path:gsub("/", ".") - return package_path .. '.' + return package_path .. "." else return nil end end local function draw_border(width, height) - local border_table = { "╭" .. string.rep("─", width) .. "╮"} + local border_table = { "╭" .. string.rep("─", width) .. "╮" } local middle = "│" .. string.rep(" ", width) .. "│" - for i=1, height do + for i = 1, height do table.insert(border_table, middle) end table.insert(border_table, "╰" .. string.rep("─", width) .. "╯") @@ -32,46 +32,45 @@ local function draw_popup(width, height, row, col, header) local popup_buf, border_buf local popup_win, border_win - -- Setup the buffers border_buf = api.nvim_create_buf(false, true) - api.nvim_buf_set_option(border_buf, 'filetype', 'springbootnvim') + api.nvim_buf_set_option(border_buf, "filetype", "springbootnvim") popup_buf = api.nvim_create_buf(false, true) - api.nvim_buf_set_option(popup_buf, 'filetype', 'springbootnvim') + api.nvim_buf_set_option(popup_buf, "filetype", "springbootnvim") local border_opts = { - style = 'minimal', - relative = 'editor', + style = "minimal", + relative = "editor", width = width + 2, height = height + 2, row = row - 1, col = col - 1, - zindex = 99 + zindex = 99, } local opts = { - style = 'minimal', - relative = 'editor', + style = "minimal", + relative = "editor", width = width, height = height, row = row, col = col, - zindex = 100 + zindex = 100, } local outline = draw_border(width, height) api.nvim_buf_set_lines(border_buf, 0, -1, false, outline) border_win = api.nvim_open_win(border_buf, true, border_opts) - - api.nvim_buf_set_lines(popup_buf, 0, -1, false, {center_text(header, width)}) + + api.nvim_buf_set_lines(popup_buf, 0, -1, false, { center_text(header, width) }) popup_win = api.nvim_open_win(popup_buf, true, opts) return { popup_buf = popup_buf, popup_win = popup_win, border_buf = border_buf, - border_win = border_win + border_win = border_win, } end @@ -88,33 +87,32 @@ local function draw_labeled_input(width, height, row, col, label, popup_buf, val -- Create the input window and buffer local input_buf = api.nvim_create_buf(false, true) - api.nvim_buf_set_option(input_buf, 'filetype', 'sprintbootnvim') + api.nvim_buf_set_option(input_buf, "filetype", "sprintbootnvim") local opts = { - style = 'minimal', - relative = 'editor', + style = "minimal", + relative = "editor", width = width, height = height, row = row + 1, col = col + 12, - zindex = 102 + zindex = 102, } - api.nvim_buf_set_lines(input_buf, 0, -1, false, {value}) + api.nvim_buf_set_lines(input_buf, 0, -1, false, { value }) local input_win = api.nvim_open_win(input_buf, true, opts) return { input_buf = input_buf, - input_win = input_win + input_win = input_win, } end - return { draw_border = draw_border, draw_popup = draw_popup, draw_labeled_input = draw_labeled_input, center_text = center_text, - package_text = package_text + package_text = package_text, } diff --git a/stylua.toml b/stylua.toml index 5d9994c..a4ead6c 100644 --- a/stylua.toml +++ b/stylua.toml @@ -1,7 +1,7 @@ syntax = "All" column_width = 120 line_endings = "Unix" -indent_type = "Tabs" +indent_type = "Spaces" indent_width = 4 quote_style = "AutoPreferDouble" call_parentheses = "Always" From 09f4323b58bfa95999aa03077aa000d6804e4d5f Mon Sep 17 00:00:00 2001 From: salem-karim Date: Fri, 26 Dec 2025 23:48:12 +0100 Subject: [PATCH 2/2] changed deprecated api and lua functions for newer ones and switched most global expressions to module(utils) ones & fixed some minor spelling mistakes --- lua/create_springboot_project.lua | 10 ++--- lua/springboot-nvim/generateclass.lua | 45 ++++++++++++------- lua/springboot-nvim/init.lua | 26 +++++------ lua/springboot-nvim/ui/springboot_nvim_ui.lua | 8 ++-- lua/springboot-nvim/ui/ui_utils.lua | 12 ++--- lua/springboot-nvim/utils.lua | 12 +++-- 6 files changed, 64 insertions(+), 49 deletions(-) diff --git a/lua/create_springboot_project.lua b/lua/create_springboot_project.lua index f113162..9cc73d1 100644 --- a/lua/create_springboot_project.lua +++ b/lua/create_springboot_project.lua @@ -4,7 +4,7 @@ local function safe_request(url) end) if not status then - vim.api.nvim_err_writeln("Error making request to " .. url .. ": " .. request) + vim.api.nvim_echo("Error making request to " .. url .. ": " .. request, true, { err = true }) return nil end @@ -15,7 +15,7 @@ local function safe_json_decode(data) local status, decoded = pcall(vim.fn.json_decode, data) if not status then - vim.api.nvim_err_writeln("Error decoding JSON: " .. decoded) + vim.api.nvim_echo("Error decoding JSON: " .. decoded, true, { err = true }) return nil end @@ -135,14 +135,14 @@ local function springboot_new_project() local request = safe_request("https://start.spring.io/metadata/client") if not request then - vim.api.nvim_err_writeln("Failed to make a request to the URL.") + vim.api.nvim_echo({ "Failed to make a request to the URL." }, true, { err = true }) return false end local springboot_data = safe_json_decode(request.stdout) if not springboot_data then - vim.api.nvim_err_writeln("Failed to decode JSON from the request.") + vim.api.nvim_echo({ "Failed to decode JSON from the request." }, true, { err = true }) return false end local build_types = { "maven", "gradle" } @@ -197,7 +197,7 @@ local function springboot_new_project() local output = vim.fn.system(command) if vim.v.shell_error ~= 0 then - print("Erro ao executar: " .. output) + print("Error while executing: " .. output) else print(output) vim.fn.chdir(name) diff --git a/lua/springboot-nvim/generateclass.lua b/lua/springboot-nvim/generateclass.lua index bf40cd6..4c087bd 100644 --- a/lua/springboot-nvim/generateclass.lua +++ b/lua/springboot-nvim/generateclass.lua @@ -5,6 +5,11 @@ local start_buf local windows local bufs +---@diagnostic disable: different-requires +local utils = require("springboot-nvim.utils") +local ui_utils = require("springboot-nvim.ui.ui_utils") +local springboot_nvim_ui = require("springboot-nvim.ui.springboot_nvim_ui") + local class_boiler_plate = "package %s;\n\npublic class %s{\n\n}" local function generate_class() @@ -58,11 +63,17 @@ local function generate_class() print("Class already exists in package") java_file:close() else - java_file = io.open(root_path .. "/" .. package_path .. class_content .. ".java", "w") + local path = root_path .. "/" .. package_path .. class_content .. ".java" + java_file = io.open(path, "w") + + if not java_file then + print("Failed to create Java file") + return + end + java_file:write(java_file_content) java_file:close() - close_generate_class() - local path = root_path .. "/" .. package_path .. class_content .. ".java" + springboot_nvim_ui.close_generate_class() vim.cmd("edit " .. vim.fn.fnameescape(path)) end else @@ -73,7 +84,7 @@ end local function create_package_ui(row, col, width, height, file_path) local package_buf = api.nvim_create_buf(false, true) --api.nvim_buf_set_option(package_buf, 'bufhidden', 'wipe') - api.nvim_buf_set_option(package_buf, "filetype", "springbootnvim") + api.nvim_set_option_value("filetype", "springbootnvim", { buf = package_buf }) local opts = { style = "minimal", @@ -84,7 +95,7 @@ local function create_package_ui(row, col, width, height, file_path) col = col, zindex = 102, } - local package = package_text(file_path) + local package = ui_utils.package_text(file_path) api.nvim_buf_set_lines(package_buf, 0, -1, false, { package }) local package_win = api.nvim_open_win(package_buf, true, opts) @@ -100,14 +111,14 @@ local function create_ui(bufnr) -- Get the file from where the generate class was called from start_buf = vim.fn.bufname(bufnr) local file_path = vim.fn.fnamemodify(start_buf, ":p") - local project_root = get_spring_boot_project_root(file_path) - local main_class_dir = find_main_application_class_directory(project_root) + local project_root = utils.get_spring_boot_project_root(file_path) + local main_class_dir = utils.find_main_application_class_directory(project_root) windows = {} bufs = {} -- Create buffer for popup buf = api.nvim_create_buf(false, true) table.insert(bufs, buf) - api.nvim_buf_set_option(buf, "bufhidden", "wipe") + api.nvim_set_option_value("bufhidden", "wipe", { buf = buf }) local border_buf = api.nvim_create_buf(false, true) table.insert(bufs, border_buf) --api.nvim_buf_set_option(border_buf, 'bufhidden', 'wipe') @@ -138,7 +149,7 @@ local function create_ui(bufnr) zindex = 100, } - local outline = draw_border(width, height) + local outline = ui_utils.sdraw_border(width, height) api.nvim_buf_set_lines(border_buf, 0, -1, false, outline) local border_win = api.nvim_open_win(border_buf, true, border_opts) @@ -149,14 +160,14 @@ local function create_ui(bufnr) --api.nvim_win_set_option(win, 'cursorline', true) - api.nvim_buf_set_lines(buf, 0, -1, false, { center_text("Generate Class") }) - local package_section = draw_package_section() + api.nvim_buf_set_lines(buf, 0, -1, false, { ui_utils.center_text("Generate Class") }) + local package_section = ui_utils.draw_package_section() api.nvim_buf_set_lines(buf, 1, -1, false, package_section) - local class_section = draw_class_section() + local class_section = ui_utils.draw_class_section() api.nvim_buf_set_lines(buf, 5, -1, false, class_section) - api.nvim_buf_set_lines(buf, 8, -1, false, { center_text("Confirm selections with ") }) + api.nvim_buf_set_lines(buf, 8, -1, false, { ui_utils.center_text("Confirm selections with ") }) local package_area = create_package_ui(row + 2, col + 10, 48, 1, main_class_dir) - local class_area = create_class_ui(row + 5, col + 10, 25, 1) + -- local class_area = ui_utils.create_class_ui(row + 5, col + 10, 25, 1) api.nvim_set_current_win(package_area.win) local first_line = vim.fn.getline(1, 1) local first_line_length = string.len(first_line[1]) @@ -167,8 +178,8 @@ end return { create_ui = create_ui, - close_generate_class = close_generate_class, - navigate_to_class = navigate_to_class, - navigate_to_package = navigate_to_package, + close_generate_class = springboot_nvim_ui.close_generate_class, + navigate_to_class = springboot_nvim_ui.navigate_to_class, + navigate_to_package = springboot_nvim_ui.navigate_to_package, generate_class = generate_class, } diff --git a/lua/springboot-nvim/init.lua b/lua/springboot-nvim/init.lua index 1b68229..0258450 100644 --- a/lua/springboot-nvim/init.lua +++ b/lua/springboot-nvim/init.lua @@ -11,10 +11,10 @@ local function incremental_compile() jdtls.compile("incremental", on_compile_result) end -local function is_plugin_installed(plugin) - local status, _ = pcall(require, plugin) - return status -end +-- local function is_plugin_installed(plugin) +-- local status, _ = pcall(require, plugin) +-- return status +-- end local function get_spring_boot_project_root() local current_file = vim.fn.expand("%:p") @@ -25,7 +25,7 @@ local function get_spring_boot_project_root() local root_pattern = { "pom.xml", "build.gradle", "build.gradle.kts", ".git" } - local root_dir = lspconfig.util.root_pattern(unpack(root_pattern))(current_file) + local root_dir = lspconfig.util.root_pattern(table.unpack(root_pattern))(current_file) if not root_dir then print("Project root not found.") return nil @@ -104,7 +104,7 @@ local function get_java_package(file_path) local package = "" - for i = 3, table.getn(t) - 1 do + for i = 3, #t - 1 do package = package .. "." .. t[i] end @@ -135,34 +135,34 @@ end local function setup(opts) on_compile_result = opts.on_compile_result - vim.api.nvim_exec( + vim.api.nvim_exec2( [[ augroup JavaAutoCommands autocmd! autocmd BufWritePost *.java lua require('springboot-nvim').incremental_compile() augroup END ]], - false + { output = true } ) - vim.api.nvim_exec( + vim.api.nvim_exec2( [[ augroup JavaPackageDetails autocmd! autocmd BufReadPost *.java lua require('springboot-nvim').fill_package_details() augroup END ]], - false + { output = false } ) - vim.api.nvim_exec( + vim.api.nvim_exec2( [[ augroup ClosePluginBuffers autocmd! autocmd FileType springbootnvim autocmd QuitPre * lua require('springboot-nvim').close_ui() augroup END ]], - false + { output = false } ) end @@ -171,7 +171,7 @@ return { boot_run = boot_run, incremental_compile = incremental_compile, fill_package_details = fill_package_details, - foo = foo, + -- foo = foo, create_ui = generate_class.create_ui, close_ui = springboot_nvim_ui.close_ui, generate_class = springboot_nvim_ui.create_generate_class_ui, diff --git a/lua/springboot-nvim/ui/springboot_nvim_ui.lua b/lua/springboot-nvim/ui/springboot_nvim_ui.lua index c272be7..41078f9 100644 --- a/lua/springboot-nvim/ui/springboot_nvim_ui.lua +++ b/lua/springboot-nvim/ui/springboot_nvim_ui.lua @@ -212,7 +212,7 @@ local function create_generate_class_ui() return end - local project_root = utils.get_spring_boot_project_root(vim.fn.fnamemodify(bufnr, ":p")) + local project_root = utils.get_spring_boot_project_root(vim.fn.fnamemodify(tostring(bufnr), ":p")) local main_class = utils.find_main_application_class_directory(project_root) local width = 60 local height = 9 @@ -279,7 +279,7 @@ local function create_generate_record_ui() return end - local project_root = utils.get_spring_boot_project_root(vim.fn.fnamemodify(bufnr, ":p")) + local project_root = utils.get_spring_boot_project_root(vim.fn.fnamemodify(tostring(bufnr), ":p")) local main_class = utils.find_main_application_class_directory(project_root) local width = 60 local height = 9 @@ -345,7 +345,7 @@ local function create_generate_interface_ui() return end - local project_root = utils.get_spring_boot_project_root(vim.fn.fnamemodify(bufnr, ":p")) + local project_root = utils.get_spring_boot_project_root(vim.fn.fnamemodify(tostring(bufnr), ":p")) local main_class = utils.find_main_application_class_directory(project_root) local width = 60 local height = 9 @@ -411,7 +411,7 @@ local function create_generate_enum_ui() return end - local project_root = utils.get_spring_boot_project_root(vim.fn.fnamemodify(bufnr, ":p")) + local project_root = utils.get_spring_boot_project_root(vim.fn.fnamemodify(tostring(bufnr), ":p")) local main_class = utils.find_main_application_class_directory(project_root) local width = 60 local height = 9 diff --git a/lua/springboot-nvim/ui/ui_utils.lua b/lua/springboot-nvim/ui/ui_utils.lua index a885939..fb1bbdc 100644 --- a/lua/springboot-nvim/ui/ui_utils.lua +++ b/lua/springboot-nvim/ui/ui_utils.lua @@ -20,7 +20,7 @@ end local function draw_border(width, height) local border_table = { "╭" .. string.rep("─", width) .. "╮" } local middle = "│" .. string.rep(" ", width) .. "│" - for i = 1, height do + for _ = 1, height do table.insert(border_table, middle) end table.insert(border_table, "╰" .. string.rep("─", width) .. "╯") @@ -34,9 +34,9 @@ local function draw_popup(width, height, row, col, header) -- Setup the buffers border_buf = api.nvim_create_buf(false, true) - api.nvim_buf_set_option(border_buf, "filetype", "springbootnvim") + api.nvim_set_option_value("filetype", "springbootnvim", { buf = border_buf }) popup_buf = api.nvim_create_buf(false, true) - api.nvim_buf_set_option(popup_buf, "filetype", "springbootnvim") + api.nvim_set_option_value("filetype", "springbootnvim", { buf = popup_buf }) local border_opts = { style = "minimal", @@ -75,10 +75,10 @@ local function draw_popup(width, height, row, col, header) end local function draw_labeled_input(width, height, row, col, label, popup_buf, value) - -- Generate the lable text and input border + -- Generate the label text and input border local input_border = draw_border(width, height) local label_text = {} - local label_len = string.len(label) + -- local label_len = string.len(label) table.insert(label_text, string.rep(" ", 12) .. input_border[1]) table.insert(label_text, label .. string.rep(" ", (12 - string.len(label))) .. input_border[2]) table.insert(label_text, string.rep(" ", 12) .. input_border[3]) @@ -87,7 +87,7 @@ local function draw_labeled_input(width, height, row, col, label, popup_buf, val -- Create the input window and buffer local input_buf = api.nvim_create_buf(false, true) - api.nvim_buf_set_option(input_buf, "filetype", "sprintbootnvim") + api.nvim_set_option_value("filetype", "sprintbootnvim", { buf = input_buf }) local opts = { style = "minimal", diff --git a/lua/springboot-nvim/utils.lua b/lua/springboot-nvim/utils.lua index 72926e3..903b1ad 100644 --- a/lua/springboot-nvim/utils.lua +++ b/lua/springboot-nvim/utils.lua @@ -8,7 +8,7 @@ local enum_boiler_plate = "package %s;\n\npublic enum %s{\n\n}" local function get_spring_boot_project_root(open_file) local root_pattern = { "pom.xml", "build.gradle", "build.gradle.kts", ".git" } - return lspconfig.util.root_pattern(unpack(root_pattern))(open_file) + return lspconfig.util.root_pattern(table.unpack(root_pattern))(open_file) end local function find_main_application_class_directory(root_path) @@ -39,10 +39,14 @@ local function java_path(full_path) return full_path:match(pattern) .. "/java" end +---@param buf integer +---@param type "class"|"record"|"interface"|"enum" +---@param package_buf integer +---@param class_buf integer local function generate_java_file(buf, type, package_buf, class_buf) - local package_input = vim.api.nvim_buf_get_lines(tonumber(package_buf), 0, -1, false) + local package_input = vim.api.nvim_buf_get_lines(package_buf, 0, -1, false) local package_text = table.concat(package_input) - local class_input = vim.api.nvim_buf_get_lines(tonumber(class_buf), 0, -1, false) + local class_input = vim.api.nvim_buf_get_lines(class_buf, 0, -1, false) local class_text = table.concat(class_input) if class_text ~= "" then local dir = java_path(vim.api.nvim_buf_get_name(buf)) @@ -90,7 +94,7 @@ local function generate_java_file(buf, type, package_buf, class_buf) java_file:write(java_file_content) java_file:close() else - print("an issue occured generating java file") + print("an issue occurred generating java file") end end vim.cmd("q!")