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) require("jdtls").start_or_attach(config)

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

@ -5,15 +5,6 @@ local SDKMAN_DIR = os.getenv("SDKMAN_DIR")
local lsp = require("lsp") local lsp = require("lsp")
local jdtls = require("jdtls") 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 bundles = {}
local mason_path = vim.fn.glob(vim.fn.stdpath("data") .. "/mason/") 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-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)") nnoremap("<leader>tm", jdtls.test_nearest_method, bufopts, "Test method (DAP)")
end end
local config = { function M.make_jdtls_config(root_dir, workspace_folder)
flags = { return {
debounce_text_changes = 80, flags = {
allow_incremental_sync = true, debounce_text_changes = 80,
}, allow_incremental_sync = true,
on_attach = on_attach, },
root_dir = root_dir, on_attach = on_attach,
init_options = { root_dir = root_dir,
bundles = bundles, init_options = {
}, bundles = bundles,
},
-- https://github.com/eclipse/eclipse.jdt.ls/wiki/Running-the-JAVA-LS-server-from-the-command-line#initialize-request -- https://github.com/eclipse/eclipse.jdt.ls/wiki/Running-the-JAVA-LS-server-from-the-command-line#initialize-request
settings = { settings = {
java = { java = {
format = { format = {
settings = { settings = {
-- https://github.com/google/styleguide/blob/gh-pages/eclipse-java-google-style.xml -- https://github.com/google/styleguide/blob/gh-pages/eclipse-java-google-style.xml
url = "/.local/share/eclipse/eclipse-java-google-style.xml", url = "/.local/share/eclipse/eclipse-java-google-style.xml",
profile = "GoogleStyle", 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,
}, },
}, contentProvider = { preferred = "fernflower" }, -- Use fernflower to decompile library code
maven = { -- Specify any completion options
downloadSources = true, completion = {
updateSnapshots = true, maxResults = 30,
}, postfix = { enabled = true },
-- On Save Cleanup favoriteStaticMembers = {
cleanup = { "org.hamcrest.MatcherAssert.assertThat",
actionsOnSave = { "org.hamcrest.Matchers.*",
"addOverride", "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
-- How code generation should act implementationsCodeLens = { enabled = true },
codeGeneration = { referenceCodeLens = { enabled = true },
toString = { signatureHelp = { enabled = true },
template = "${object.className}{${member.name()}=${member.value}, ${otherMembers}}", inlayHints = {
parameterNames = { enabled = true },
}, },
hashCodeEquals = { -- Specify any options for organizing imports
useJava7Objects = true, 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 = { maven = {
userSettings = HOME .. "/.m2/settings.xml", downloadSources = true,
updateSnapshots = true,
}, },
runtimes = { -- On Save Cleanup
{ cleanup = {
name = "JavaSE-17", actionsOnSave = {
path = SDKMAN_DIR .. "/candidates/java/17.0.5-amzn", "addOverride",
}, },
{ },
name = "JavaSE-11", -- How code generation should act
path = SDKMAN_DIR .. "/candidates/java/11.0.18-amzn", 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",
}, },
{ runtimes = {
name = "JavaSE-1.8", {
path = SDKMAN_DIR .. "/candidates/java/8.0.362-amzn", 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
-- 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
-- See: https://github.com/eclipse/eclipse.jdt.ls#running-from-the-command-line cmd = {
cmd = { SDKMAN_DIR .. "/candidates/java/17.0.5-amzn/bin/java",
SDKMAN_DIR .. "/candidates/java/17.0.5-amzn/bin/java", "-Declipse.application=org.eclipse.jdt.ls.core.id1",
"-Declipse.application=org.eclipse.jdt.ls.core.id1", "-Dosgi.bundles.defaultStartLevel=4",
"-Dosgi.bundles.defaultStartLevel=4", "-Declipse.product=org.eclipse.jdt.ls.core.product",
"-Declipse.product=org.eclipse.jdt.ls.core.product", "-Dlog.protocol=true",
"-Dlog.protocol=true", "-Dlog.level=ALL",
"-Dlog.level=ALL", "-Xmx4g",
"-Xmx4g", "--add-modules=ALL-SYSTEM",
"--add-modules=ALL-SYSTEM", "--add-opens",
"--add-opens", "java.base/java.util=ALL-UNNAMED",
"java.base/java.util=ALL-UNNAMED", "--add-opens",
"--add-opens", "java.base/java.lang=ALL-UNNAMED",
"java.base/java.lang=ALL-UNNAMED", -- If you use lombok, download the lombok jar and place it in ~/.local/share/eclipse
-- If you use lombok, download the lombok jar and place it in ~/.local/share/eclipse -- "-javaagent:"
-- "-javaagent:" -- .. HOME
-- .. HOME -- .. "/.local/share/eclipse/lombok.jar",
-- .. "/.local/share/eclipse/lombok.jar",
"-jar",
vim.fn.glob(HOME .. "/.local/share/eclipse.jdt.ls/plugins/org.eclipse.equinox.launcher_*.jar"),
"-configuration", "-jar",
HOME .. "/.local/share/eclipse.jdt.ls/config_linux", vim.fn.glob(HOME .. "/.local/share/eclipse.jdt.ls/plugins/org.eclipse.equinox.launcher_*.jar"),
"-data", "-configuration",
workspace_folder, HOME .. "/.local/share/eclipse.jdt.ls/config_linux",
},
}
function M.make_jdtls_config() "-data",
return config workspace_folder,
},
}
end end
return M return M

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

Loading…
Cancel
Save