refactor(nvim): correctly start java lsp when switching projects

chezmoi
sgoudham 2 years ago
parent b401dbb587
commit 83924bfa13
Signed by: hammy
GPG Key ID: 44E818FD5457EEA4

@ -1,2 +1,6 @@
local config = require("lsp.java").make_jdtls_config()
local root_markers = { "gradlew", "mvnw", ".git", "pom.xml", "build.gradle" }
local root_dir = require("jdtls.setup").find_root(root_markers)
local workspace_folder = os.getenv("HOME") .. "/.local/share/eclipse/" .. vim.fn.fnamemodify(root_dir, ":p:h:t")
local config = require("lsp.java").make_jdtls_config(root_dir, workspace_folder)
require("jdtls").start_or_attach(config)

@ -4,9 +4,6 @@ vim.g.mapleader = " "
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1
vim.lsp.set_log_level("TRACE")
print(vim.lsp.get_log_path())
-- bootstrap & set up lazy
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then

@ -5,15 +5,6 @@ local SDKMAN_DIR = os.getenv("SDKMAN_DIR")
local lsp = require("lsp")
local jdtls = require("jdtls")
local root_markers = { "gradlew", "mvnw", ".git", "pom.xml", "build.gradle" }
local root_dir = require("jdtls.setup").find_root(root_markers)
-- eclipse.jdt.ls stores project specific data within a folder. If you are working
-- with multiple different projects, each project must use a dedicated data directory.
-- This variable is used to configure eclipse to use the directory name of the
-- current project found using the root_marker as the folder for project specific data.
local workspace_folder = HOME .. "/.local/share/eclipse/" .. vim.fn.fnamemodify(root_dir, ":p:h:t")
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"))
@ -58,139 +49,137 @@ local on_attach = function(client, bufnr)
nnoremap("<leader>tm", jdtls.test_nearest_method, bufopts, "Test method (DAP)")
end
local config = {
flags = {
debounce_text_changes = 80,
allow_incremental_sync = true,
},
on_attach = on_attach,
root_dir = root_dir,
init_options = {
bundles = bundles,
},
function M.make_jdtls_config(root_dir, workspace_folder)
return {
flags = {
debounce_text_changes = 80,
allow_incremental_sync = true,
},
on_attach = on_attach,
root_dir = root_dir,
init_options = {
bundles = bundles,
},
-- https://github.com/eclipse/eclipse.jdt.ls/wiki/Running-the-JAVA-LS-server-from-the-command-line#initialize-request
settings = {
java = {
format = {
settings = {
-- https://github.com/google/styleguide/blob/gh-pages/eclipse-java-google-style.xml
url = "/.local/share/eclipse/eclipse-java-google-style.xml",
profile = "GoogleStyle",
},
},
contentProvider = { preferred = "fernflower" }, -- Use fernflower to decompile library code
-- Specify any completion options
completion = {
maxResults = 30,
postfix = { enabled = true },
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.*",
},
},
-- LSP Related
implementationsCodeLens = { enabled = true },
referenceCodeLens = { enabled = true },
signatureHelp = { enabled = true },
inlayHints = {
parameterNames = { enabled = true },
},
-- Specify any options for organizing imports
sources = {
organizeImports = {
starThreshold = 9999,
staticStarThreshold = 9999,
-- https://github.com/eclipse/eclipse.jdt.ls/wiki/Running-the-JAVA-LS-server-from-the-command-line#initialize-request
settings = {
java = {
format = {
settings = {
-- https://github.com/google/styleguide/blob/gh-pages/eclipse-java-google-style.xml
url = "/.local/share/eclipse/eclipse-java-google-style.xml",
profile = "GoogleStyle",
},
},
},
maven = {
downloadSources = true,
updateSnapshots = true,
},
-- On Save Cleanup
cleanup = {
actionsOnSave = {
"addOverride",
contentProvider = { preferred = "fernflower" }, -- Use fernflower to decompile library code
-- Specify any completion options
completion = {
maxResults = 30,
postfix = { enabled = true },
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.*",
},
},
},
-- How code generation should act
codeGeneration = {
toString = {
template = "${object.className}{${member.name()}=${member.value}, ${otherMembers}}",
-- LSP Related
implementationsCodeLens = { enabled = true },
referenceCodeLens = { enabled = true },
signatureHelp = { enabled = true },
inlayHints = {
parameterNames = { enabled = true },
},
hashCodeEquals = {
useJava7Objects = true,
-- Specify any options for organizing imports
sources = {
organizeImports = {
starThreshold = 9999,
staticStarThreshold = 9999,
},
},
useBlocks = true,
},
-- https://github.com/eclipse/eclipse.jdt.ls/wiki/Running-the-JAVA-LS-server-from-the-command-line#initialize-request
-- `interface RuntimeOption`
configuration = {
maven = {
userSettings = HOME .. "/.m2/settings.xml",
downloadSources = true,
updateSnapshots = true,
},
runtimes = {
{
name = "JavaSE-17",
path = SDKMAN_DIR .. "/candidates/java/17.0.5-amzn",
-- On Save Cleanup
cleanup = {
actionsOnSave = {
"addOverride",
},
{
name = "JavaSE-11",
path = SDKMAN_DIR .. "/candidates/java/11.0.18-amzn",
},
-- How code generation should act
codeGeneration = {
toString = {
template = "${object.className}{${member.name()}=${member.value}, ${otherMembers}}",
},
hashCodeEquals = {
useJava7Objects = true,
},
useBlocks = true,
},
-- https://github.com/eclipse/eclipse.jdt.ls/wiki/Running-the-JAVA-LS-server-from-the-command-line#initialize-request
-- `interface RuntimeOption`
configuration = {
maven = {
userSettings = HOME .. "/.m2/settings.xml",
},
{
name = "JavaSE-1.8",
path = SDKMAN_DIR .. "/candidates/java/8.0.362-amzn",
runtimes = {
{
name = "JavaSE-17",
path = SDKMAN_DIR .. "/candidates/java/17.0.5-amzn",
},
{
name = "JavaSE-11",
path = SDKMAN_DIR .. "/candidates/java/11.0.18-amzn",
},
{
name = "JavaSE-1.8",
path = SDKMAN_DIR .. "/candidates/java/8.0.362-amzn",
},
},
},
},
},
},
-- Note that eclipse.jdt.ls must be started with a Java version of 17 or higher
-- See: https://github.com/eclipse/eclipse.jdt.ls#running-from-the-command-line
cmd = {
SDKMAN_DIR .. "/candidates/java/17.0.5-amzn/bin/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",
"-Xmx4g",
"--add-modules=ALL-SYSTEM",
"--add-opens",
"java.base/java.util=ALL-UNNAMED",
"--add-opens",
"java.base/java.lang=ALL-UNNAMED",
-- If you use lombok, download the lombok jar and place it in ~/.local/share/eclipse
-- "-javaagent:"
-- .. HOME
-- .. "/.local/share/eclipse/lombok.jar",
"-jar",
vim.fn.glob(HOME .. "/.local/share/eclipse.jdt.ls/plugins/org.eclipse.equinox.launcher_*.jar"),
-- Note that eclipse.jdt.ls must be started with a Java version of 17 or higher
-- See: https://github.com/eclipse/eclipse.jdt.ls#running-from-the-command-line
cmd = {
SDKMAN_DIR .. "/candidates/java/17.0.5-amzn/bin/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",
"-Xmx4g",
"--add-modules=ALL-SYSTEM",
"--add-opens",
"java.base/java.util=ALL-UNNAMED",
"--add-opens",
"java.base/java.lang=ALL-UNNAMED",
-- If you use lombok, download the lombok jar and place it in ~/.local/share/eclipse
-- "-javaagent:"
-- .. HOME
-- .. "/.local/share/eclipse/lombok.jar",
"-configuration",
HOME .. "/.local/share/eclipse.jdt.ls/config_linux",
"-jar",
vim.fn.glob(HOME .. "/.local/share/eclipse.jdt.ls/plugins/org.eclipse.equinox.launcher_*.jar"),
"-data",
workspace_folder,
},
}
"-configuration",
HOME .. "/.local/share/eclipse.jdt.ls/config_linux",
function M.make_jdtls_config()
return config
"-data",
workspace_folder,
},
}
end
return M

@ -51,6 +51,16 @@ return {
pythonPath = get_python_path,
},
}
dap.configurations.java = {
{
type = "java",
request = "attach",
name = "Debug (Attach) - Remote",
hostName = "127.0.0.1",
port = 5005,
},
}
end,
})

Loading…
Cancel
Save