neovim/ftplugin/java.lua

135 lines
4.2 KiB
Lua

function IsDirectoryExist(dirname)
return os.rename(dirname, dirname) and true or false
end
function Path2Windows(path)
return (string.gsub(path, "/", "\\"))
end
local jdtls = require("jdtls")
local root_markers = { ".git", "mvnw", "gradlew", "pom.xml", "build.gradle" }
local root_dir = require("jdtls.setup").find_root(root_markers)
if root_dir == "" then
return
end
local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ":p:h:t")
local home = os.getenv("HOME")
local jdtls_home = home .. "/.jdtls"
local plugins_home = jdtls_home .. "/plugins"
local workspace = jdtls_home .. "/workspace/" .. project_name
local launcher = plugins_home .. "/org.eclipse.equinox.launcher_1.6.400.v20210924-0641.jar"
local configuration = jdtls_home .. "/config_linux"
if vim.loop.os_uname().sysname == "Windows" then
jdtls_home = Path2Windows(jdtls_home)
plugins_home = Path2Windows(plugins_home)
workspace = Path2Windows(workspace)
launcher = Path2Windows(launcher)
configuration = jdtls_home .. "\\config_win"
end
-- create when workspace not exist
if not IsDirectoryExist(workspace) then
os.execute("mkdir " .. workspace)
end
if vim.loop.os_uname().sysname == "Windows" then
configuration = jdtls_home .. "\\config_win"
end
local config = {
flags = {
debounce_text_changes = 80,
},
on_attach = common_lsp_on_attach,
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",
"-Xmx1g",
"--add-modules=ALL-SYSTEM",
"--add-opens",
"java.base/java.util=ALL-UNNAMED",
"--add-opens",
"java.base/java.lang=ALL-UNNAMED",
"-javaagent:" .. jdtls_home .. "/bin/lombok.jar",
"-Xbootclasspath/a:" .. jdtls_home .. "/bin/lombok.jar",
"-jar",
launcher,
"-configuration",
configuration,
"-data",
workspace,
},
root_dir = root_dir,
settings = {
java = {
maven = {
downloadSources = true,
},
eclipse = {
downloadSources = true,
},
implementationsCodeLens = {
enabled = true,
},
referencesCodeLens = {
enabled = true,
},
references = {
includeDecompiledSources = true,
},
format = {
enabled = true,
settings = {
url = vim.fn.stdpath("config") .. "intellij-java-google-style.xml",
profile = "GoogleStyle",
},
},
signatureHelp = { enabled = true },
contentProvider = { preferred = "fernflower" }, -- Use fernflower to decompile library code
completion = {
favoriteStaticMembers = {
"org.hamcrest.MatcherAssert.assertThat",
"org.hamcrest.Matchers.*",
"org.hamcrest.CoreMatchers.*",
"org.junit.jupiter.api.Assertions.*",
"java.util.Objects.requireNonNull",
"java.util.Objects.requireNonNullElse",
"org.mockito.Mockito.*",
},
filteredTypes = {
"com.sun.*",
"io.micrometer.shaded.*",
"java.awt.*",
"jdk.*",
"sun.*",
},
},
sources = {
organizeImports = {
starThreshold = 9999,
staticStarThreshold = 9999,
},
},
codeGeneration = {
toString = {
template = "${object.className}{${member.name()}=${member.value}, ${otherMembers}}",
},
hashCodeEquals = {
useJava7Objects = true,
},
useBlocks = true,
},
},
},
init_options = {
bundles = {},
},
}
jdtls.start_or_attach(config)