mirror of
https://github.com/LazyVim/LazyVim.git
synced 2026-07-25 14:01:06 +00:00
refactor(java): move defaults to opts
This commit is contained in:
parent
236029bd3b
commit
d8abb3eb55
1 changed files with 54 additions and 30 deletions
|
|
@ -64,6 +64,49 @@ return {
|
||||||
"mfussenegger/nvim-jdtls",
|
"mfussenegger/nvim-jdtls",
|
||||||
dependencies = { "folke/which-key.nvim" },
|
dependencies = { "folke/which-key.nvim" },
|
||||||
ft = java_filetypes,
|
ft = java_filetypes,
|
||||||
|
opts = function()
|
||||||
|
return {
|
||||||
|
-- How to find the root dir for a given filename. The default comes from
|
||||||
|
-- lspconfig which provides a function specifically for java projects.
|
||||||
|
root_dir = require("lspconfig.server_configurations.jdtls").default_config.root_dir,
|
||||||
|
|
||||||
|
-- How to find the project name for a given root dir.
|
||||||
|
project_name = function(root_dir)
|
||||||
|
return root_dir and vim.fs.basename(root_dir)
|
||||||
|
end,
|
||||||
|
|
||||||
|
-- Where are the config and workspace dirs for a project?
|
||||||
|
jdtls_config_dir = function(project_name)
|
||||||
|
return vim.fn.stdpath("cache") .. "/jdtls/" .. project_name .. "/config"
|
||||||
|
end,
|
||||||
|
jdtls_workspace_dir = function(project_name)
|
||||||
|
return vim.fn.stdpath("cache") .. "/jdtls/" .. project_name .. "/workspace"
|
||||||
|
end,
|
||||||
|
|
||||||
|
-- How to run jdtls. This can be overridden to a full java command-line
|
||||||
|
-- if the Python wrapper script doesn't suffice.
|
||||||
|
cmd = { "jdtls" },
|
||||||
|
full_cmd = function(opts)
|
||||||
|
local fname = vim.api.nvim_buf_get_name(0)
|
||||||
|
local root_dir = opts.root_dir(fname)
|
||||||
|
local project_name = opts.project_name(root_dir)
|
||||||
|
local cmd = vim.deepcopy(opts.cmd)
|
||||||
|
if project_name then
|
||||||
|
vim.list_extend(cmd, {
|
||||||
|
"-configuration",
|
||||||
|
opts.jdtls_config_dir(project_name),
|
||||||
|
"-data",
|
||||||
|
opts.jdtls_workspace_dir(project_name),
|
||||||
|
})
|
||||||
|
end
|
||||||
|
return cmd
|
||||||
|
end,
|
||||||
|
|
||||||
|
-- These depend on nvim-dap, but can additionally be disabled by setting false here.
|
||||||
|
dap = { hotcodereplace = "auto", config_overrides = {} },
|
||||||
|
test = true,
|
||||||
|
}
|
||||||
|
end,
|
||||||
config = function()
|
config = function()
|
||||||
local opts = Util.opts("nvim-jdtls") or {}
|
local opts = Util.opts("nvim-jdtls") or {}
|
||||||
|
|
||||||
|
|
@ -71,14 +114,14 @@ return {
|
||||||
-- if nvim-dap is enabled with java debug/test.
|
-- if nvim-dap is enabled with java debug/test.
|
||||||
local mason_registry = require("mason-registry")
|
local mason_registry = require("mason-registry")
|
||||||
local bundles = {} ---@type string[]
|
local bundles = {} ---@type string[]
|
||||||
if opts.dap ~= false and Util.has("nvim-dap") and mason_registry.is_installed("java-debug-adapter") then
|
if opts.dap and Util.has("nvim-dap") and mason_registry.is_installed("java-debug-adapter") then
|
||||||
local java_dbg_pkg = mason_registry.get_package("java-debug-adapter")
|
local java_dbg_pkg = mason_registry.get_package("java-debug-adapter")
|
||||||
local java_dbg_path = java_dbg_pkg:get_install_path()
|
local java_dbg_path = java_dbg_pkg:get_install_path()
|
||||||
local jar_patterns = {
|
local jar_patterns = {
|
||||||
java_dbg_path .. "/extension/server/com.microsoft.java.debug.plugin-*.jar",
|
java_dbg_path .. "/extension/server/com.microsoft.java.debug.plugin-*.jar",
|
||||||
}
|
}
|
||||||
-- java-test also depends on java-debug-adapter.
|
-- java-test also depends on java-debug-adapter.
|
||||||
if opts.test ~= false and mason_registry.is_installed("java-test") then
|
if opts.test and mason_registry.is_installed("java-test") then
|
||||||
local java_test_pkg = mason_registry.get_package("java-test")
|
local java_test_pkg = mason_registry.get_package("java-test")
|
||||||
local java_test_path = java_test_pkg:get_install_path()
|
local java_test_path = java_test_pkg:get_install_path()
|
||||||
vim.list_extend(jar_patterns, {
|
vim.list_extend(jar_patterns, {
|
||||||
|
|
@ -93,38 +136,21 @@ return {
|
||||||
end
|
end
|
||||||
|
|
||||||
local function attach_jdtls()
|
local function attach_jdtls()
|
||||||
-- The configuration for jdtls contains a useful function to find the java
|
|
||||||
-- project root. This can be overridden by opts.root_dir
|
|
||||||
local find_java_project_root = opts.root_dir
|
|
||||||
or require("lspconfig.server_configurations.jdtls").default_config.root_dir
|
|
||||||
local fname = vim.api.nvim_buf_get_name(0)
|
local fname = vim.api.nvim_buf_get_name(0)
|
||||||
local root_dir = find_java_project_root(fname)
|
|
||||||
local project_name = root_dir and vim.fs.basename(root_dir)
|
|
||||||
local cmd = { "jdtls" }
|
|
||||||
|
|
||||||
if project_name then
|
-- Configuration can be augmented and overridden by opts.jdtls
|
||||||
local jdtls_cache_dir = vim.fn.stdpath("cache") .. "/jdtls/" .. project_name
|
local config = extend_or_override({
|
||||||
vim.list_extend(cmd, {
|
cmd = opts.full_cmd(opts),
|
||||||
"-configuration",
|
root_dir = opts.root_dir(fname),
|
||||||
jdtls_cache_dir .. "/config",
|
|
||||||
"-data",
|
|
||||||
jdtls_cache_dir .. "/workspace",
|
|
||||||
})
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Base configuration can be augmented and overridden by opts.jdtls
|
|
||||||
local base_config = {
|
|
||||||
cmd = cmd,
|
|
||||||
root_dir = root_dir,
|
|
||||||
init_options = {
|
init_options = {
|
||||||
bundles = bundles,
|
bundles = bundles,
|
||||||
},
|
},
|
||||||
-- enable CMP capabilities
|
-- enable CMP capabilities
|
||||||
capabilities = require("cmp_nvim_lsp").default_capabilities(),
|
capabilities = require("cmp_nvim_lsp").default_capabilities(),
|
||||||
}
|
}, opts.jdtls)
|
||||||
|
|
||||||
-- Existing server will be reused if the root_dir matches.
|
-- Existing server will be reused if the root_dir matches.
|
||||||
require("jdtls").start_or_attach(extend_or_override(base_config, opts.jdtls))
|
require("jdtls").start_or_attach(config)
|
||||||
-- not need to require("jdtls.setup").add_commands(), start automatically adds commands
|
-- not need to require("jdtls.setup").add_commands(), start automatically adds commands
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -169,15 +195,13 @@ return {
|
||||||
},
|
},
|
||||||
}, { mode = "v", buffer = args.buf })
|
}, { mode = "v", buffer = args.buf })
|
||||||
|
|
||||||
if opts.dap ~= false and Util.has("nvim-dap") and mason_registry.is_installed("java-debug-adapter") then
|
if opts.dap and Util.has("nvim-dap") and mason_registry.is_installed("java-debug-adapter") then
|
||||||
-- custom init for Java debugger
|
-- custom init for Java debugger
|
||||||
require("jdtls").setup_dap(
|
require("jdtls").setup_dap(opts.dap)
|
||||||
extend_or_override({ hotcodereplace = "auto", config_overrides = {} }, opts.dap)
|
|
||||||
)
|
|
||||||
require("jdtls.dap").setup_dap_main_class_configs()
|
require("jdtls.dap").setup_dap_main_class_configs()
|
||||||
|
|
||||||
-- Java Test require Java debugger to work
|
-- Java Test require Java debugger to work
|
||||||
if opts.test ~= false and mason_registry.is_installed("java-test") then
|
if opts.test and mason_registry.is_installed("java-test") then
|
||||||
-- custom keymaps for Java test runner (not yet compatible with neotest)
|
-- custom keymaps for Java test runner (not yet compatible with neotest)
|
||||||
wk.register({
|
wk.register({
|
||||||
["<leader>t"] = { name = "+test" },
|
["<leader>t"] = { name = "+test" },
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue