add : add config java jdtls

This commit is contained in:
asep komarudin 2024-02-19 19:40:19 +07:00
parent fafb1c318c
commit 5b031084f0
4 changed files with 137 additions and 2 deletions

134
ftplugin/java.lua Normal file
View file

@ -0,0 +1,134 @@
-- 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(mason_path .. "packages/java-debug-adapter/extension/server/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 = {
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)
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>)]]
)

View file

@ -0,0 +1 @@
return { "mfussenegger/nvim-jdtls", event = "BufRead" }

View file

@ -222,7 +222,7 @@ web_devicons.setup({
name = "mentionbot",
},
[".project"] = {
icon = "󱠒",
icon = "󰈚",
color = "#5881b1",
cterm_color = "220",
name = "project",

View file

@ -327,7 +327,7 @@ local mappings = {
r = { "<cmd>RunCode<CR>", "Run Code" },
f = { "<cmd>RunFile<CR>", "Run File" },
p = { "<cmd>RunProject<CR>", "Run Project" },
g = { "<cmd>ToggleTerm size=70 direction=float<cr>clear<cr>gradle run<cr>" .. trn, "Run Gradle" },
g = { "<cmd>terminal<cr>$i<Right>gradle run<cr>" .. trn, "Run Gradle" },
m = {
"<cmd>ToggleTerm size=70 direction=float<cr>mvn exec:java -Dexec.mainClass=com.pojokcode.App<cr>",
"Run MVN",