-
-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(java)!: move to
nvim-java
for jdtls
setup
- Loading branch information
Showing
2 changed files
with
29 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,159 +1,46 @@ | ||
return { | ||
{ import = "astrocommunity.pack.xml" }, | ||
{ | ||
"nvim-treesitter/nvim-treesitter", | ||
optional = true, | ||
opts = function(_, opts) | ||
if opts.ensure_installed ~= "all" then | ||
opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed, { "java", "html" }) | ||
opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed, { "java" }) | ||
end | ||
end, | ||
}, | ||
{ | ||
"williamboman/mason-lspconfig.nvim", | ||
optional = true, | ||
opts = function(_, opts) | ||
opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed, { "jdtls", "lemminx" }) | ||
end, | ||
}, | ||
{ | ||
"jay-babu/mason-nvim-dap.nvim", | ||
optional = true, | ||
opts = function(_, opts) | ||
opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed, { "javadbg", "javatest" }) | ||
opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed, { "jdtls" }) | ||
end, | ||
}, | ||
{ | ||
"WhoIsSethDaniel/mason-tool-installer.nvim", | ||
optional = true, | ||
opts = function(_, opts) | ||
opts.ensure_installed = require("astrocore").list_insert_unique( | ||
opts.ensure_installed, | ||
{ "jdtls", "lemminx", "java-debug-adapter", "java-test" } | ||
) | ||
opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed, { "jdtls" }) | ||
end, | ||
}, | ||
{ | ||
"mfussenegger/nvim-jdtls", | ||
ft = { "java" }, | ||
"nvim-java/nvim-java", | ||
lazy = true, | ||
opts = {}, | ||
dependencies = { | ||
"williamboman/mason-lspconfig.nvim", | ||
{ | ||
"AstroNvim/astrolsp", | ||
optional = true, | ||
---@type AstroLSPOpts | ||
opts = { | ||
---@diagnostic disable: missing-fields | ||
handlers = { jdtls = false }, | ||
handlers = { | ||
jdtls = function(server, opts) | ||
require("lazy").load { plugins = { "nvim-java" } } | ||
require("lspconfig")[server].setup(opts) | ||
end, | ||
}, | ||
}, | ||
}, | ||
}, | ||
opts = function(_, opts) | ||
local utils = require "astrocore" | ||
-- use this function notation to build some variables | ||
local root_markers = { ".git", "mvnw", "gradlew", "pom.xml", "build.gradle", ".project" } | ||
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 | ||
vim.fn.mkdir(workspace_dir, "p") | ||
|
||
-- validate operating system | ||
if not (vim.fn.has "mac" == 1 or vim.fn.has "unix" == 1 or vim.fn.has "win32" == 1) then | ||
utils.notify("jdtls: Could not detect valid OS", vim.log.levels.ERROR) | ||
end | ||
|
||
return utils.extend_tbl({ | ||
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:" .. vim.fn.expand "$MASON/share/jdtls/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.expand "$MASON/share/jdtls/plugins/org.eclipse.equinox.launcher.jar", | ||
"-configuration", | ||
vim.fn.expand "$MASON/share/jdtls/config", | ||
"-data", | ||
workspace_dir, | ||
}, | ||
root_dir = root_dir, | ||
settings = { | ||
java = { | ||
eclipse = { downloadSources = true }, | ||
configuration = { updateBuildConfiguration = "interactive" }, | ||
maven = { downloadSources = true }, | ||
implementationsCodeLens = { enabled = true }, | ||
referencesCodeLens = { enabled = true }, | ||
inlayHints = { parameterNames = { enabled = "all" } }, | ||
signatureHelp = { enabled = true }, | ||
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.*", | ||
}, | ||
}, | ||
sources = { | ||
organizeImports = { | ||
starThreshold = 9999, | ||
staticStarThreshold = 9999, | ||
}, | ||
}, | ||
}, | ||
}, | ||
init_options = { | ||
bundles = { | ||
vim.fn.expand "$MASON/share/java-debug-adapter/com.microsoft.java.debug.plugin.jar", | ||
-- unpack remaining bundles | ||
(table.unpack or unpack)(vim.split(vim.fn.glob "$MASON/share/java-test/*.jar", "\n", {})), | ||
}, | ||
}, | ||
handlers = { | ||
["$/progress"] = function() end, -- disable progress updates. | ||
}, | ||
filetypes = { "java" }, | ||
on_attach = function(...) | ||
require("jdtls").setup_dap { hotcodereplace = "auto" } | ||
local astrolsp_avail, astrolsp = pcall(require, "astrolsp") | ||
if astrolsp_avail then astrolsp.on_attach(...) end | ||
end, | ||
}, opts) | ||
end, | ||
config = function(_, opts) | ||
-- setup autocmd on filetype detect java | ||
vim.api.nvim_create_autocmd("Filetype", { | ||
pattern = "java", -- autocmd to start jdtls | ||
callback = function() | ||
if opts.root_dir and opts.root_dir ~= "" then | ||
require("jdtls").start_or_attach(opts) | ||
else | ||
require("astrocore").notify("jdtls: root_dir not found. Please specify a root marker", vim.log.levels.ERROR) | ||
end | ||
end, | ||
}) | ||
-- create autocmd to load main class configs on LspAttach. | ||
-- This ensures that the LSP is fully attached. | ||
-- See https://github.com/mfussenegger/nvim-jdtls#nvim-dap-configuration | ||
vim.api.nvim_create_autocmd("LspAttach", { | ||
pattern = "*.java", | ||
callback = function(args) | ||
local client = vim.lsp.get_client_by_id(args.data.client_id) | ||
-- ensure that only the jdtls client is activated | ||
if client.name == "jdtls" then require("jdtls.dap").setup_dap_main_class_configs() end | ||
end, | ||
}) | ||
end, | ||
}, | ||
} |