-- more space in the neovim command line for displaying messages
-- use this function notation to build some variables
-- vim.opt_local.shiftwidth = 4
-- vim.opt_local.tabstop = 4
-- vim.opt_local.softtabstop = 4
-- vim.opt_local.ts = 4
-- vim.opt_local.expandtab = true
--
-- local status, jdtls = pcall(require, "jdtls")
-- if not status then
--   return
-- end
--
-- local function capabilities()
--   local status_ok, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp")
--   if status_ok then
--     return cmp_nvim_lsp.default_capabilities()
--   end
--
--   local CAPABILITIES = vim.lsp.protocol.make_client_capabilities()
--   CAPABILITIES.textDocument.completion.completionItem.snippetSupport = true
--   CAPABILITIES.textDocument.completion.completionItem.resolveSupport = {
--     properties = {
--       "documentation",
--       "detail",
--       "additionalTextEdits",
--     },
--   }
--
--   return CAPABILITIES
-- end
--
-- local function directory_exists(path)
--   local f = io.popen("cd " .. path)
--   local ff = f:read("*all")
--
--   if ff:find("ItemNotFoundException") then
--     return false
--   else
--     return true
--   end
-- end
--
-- local root_markers = { ".git", "mvnw", "gradlew", "pom.xml", "build.gradle" }
-- local root_dir = require("jdtls.setup").find_root(root_markers)
--
-- -- calculate workspace dir
-- local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ":p:h:t")
-- local workspace_dir = vim.fn.stdpath("data") .. "/site/java/workspace-root/" .. project_name
-- if directory_exists(workspace_dir) then
-- else
--   os.execute("mkdir " .. workspace_dir)
-- end
-- -- get the mason install path
-- local install_path = require("mason-registry").get_package("jdtls"):get_install_path()
--
-- -- get the current OS
-- local os
-- if vim.fn.has("macunix") then
--   os = "mac"
-- elseif vim.fn.has("win32") then
--   os = "win"
-- else
--   os = "linux"
-- end
--
-- local bundles = {}
-- local mason_path = vim.fn.glob(vim.fn.stdpath("data") .. "/mason/")
-- vim.list_extend(bundles, vim.split(vim.fn.glob(mason_path .. "packages/java-test/extension/server/*.jar"), "\n"))
-- vim.list_extend(
--   bundles,
--   vim.split(
--     vim.fn.glob(
--       vim.fn.stdpath("data")
--         .. "/lazy/java-debug/com.microsoft.java.debug.plugin/target/com.microsoft.java.debug.plugin-*.jar"
--     ),
--     "\n"
--   )
-- )
--
-- local config = {
--   cmd = {
--     "java",
--     "-Declipse.application=org.eclipse.jdt.ls.core.id1",
--     "-Dosgi.bundles.defaultStartLevel=4",
--     "-Declipse.product=org.eclipse.jdt.ls.core.product",
--     "-Dlog.protocol=true",
--     "-Dlog.level=ALL",
--     "-javaagent:" .. install_path .. "/lombok.jar",
--     "-Xms1g",
--     "--add-modules=ALL-SYSTEM",
--     "--add-opens",
--     "java.base/java.util=ALL-UNNAMED",
--     "--add-opens",
--     "java.base/java.lang=ALL-UNNAMED",
--     "-jar",
--     vim.fn.glob(install_path .. "/plugins/org.eclipse.equinox.launcher_*.jar"),
--     "-configuration",
--     install_path .. "/config_" .. os,
--     "-data",
--     workspace_dir,
--   },
--   capabilities = capabilities(),
--   root_dir = root_dir,
--   settings = {
--     java = {},
--   },
--
--   init_options = {
--     bundles = bundles,
--     -- bundles = {
--     -- 	vim.fn.glob(
--     -- 		mason_path .. "packages/java-debug-adapter/extension/server/com.microsoft.java.debug.plugin-*.jar",
--     -- 		"\n"
--     -- 	),
--     -- },
--   },
-- }
--
-- config["on_attach"] = function(client, bufnr)
--   local _, _ = pcall(vim.lsp.codelens.refresh)
--
--   -- valdation if DAP not installed
--   local dap_status, _ = pcall(require, "nvim-dap")
--   if not dap_status then
--     return
--   end
--
--   require("jdtls.dap").setup_dap_main_class_configs()
--   jdtls.setup_dap({ hotcodereplace = "auto" })
--   require("user.lsp.handlers").on_attach(client, bufnr)
-- end
--
-- vim.api.nvim_create_autocmd({ "BufWritePost" }, {
--   pattern = { "*.java" },
--   callback = function()
--     local _, _ = pcall(vim.lsp.codelens.refresh)
--   end,
-- })
--
-- jdtls.start_or_attach(config)
--
-- vim.cmd(
--   [[command! -buffer -nargs=? -complete=custom,v:lua.require'jdtls'._complete_set_runtime JdtSetRuntime lua require('jdtls').set_runtime(<f-args>)]]
-- )

-- local mason_reg = require("mason-registry")
-- for _, pkg in pairs(mason_reg.get_installed_packages()) do
--   for _, type in pairs(pkg.spec.categories) do
--     if type == "LSP" and pkg.spec.name == "jdtls" then
--       local ok, lspconfig = pcall(require, "lspconfig")
--       if not ok then
--         return
--       end
--
--       lspconfig.jdtls.setup({
--         cmd = {
--           "jdtls",
--           "-configuration",
--           vim.fn.expand("$HOME") .. "/.cache/jdtls/config",
--           "-data",
--           vim.fn.expand("$HOME") .. "/.cache/jdtls/workspace",
--         },
--         filetypes = { "java" },
--         root_dir = require("lspconfig.util").root_pattern(
--           -- Single-module projects
--           {
--             "build.xml", -- Ant
--             "pom.xml", -- Maven
--             "settings.gradle", -- Gradle
--             "settings.gradle.kts", -- Gradle
--           },
--           -- Multi-module projects
--           { "build.gradle", "build.gradle.kts" }
--         ) or vim.fn.getcwd(),
--         singe_file_support = true,
--       })
--     end
--   end
-- end