mirror of
https://github.com/pojokcodeid/nvim-lazy.git
synced 2025-06-21 08:35:48 +02:00
enc: config jdtls independent
This commit is contained in:
parent
e9e0ca3e88
commit
422efe87ce
7 changed files with 239 additions and 195 deletions
|
@ -6,6 +6,7 @@
|
||||||
"alpha-nvim": { "branch": "main", "commit": "bf3c8bb8c02ed3d9644cc5bbc48e2bdc39349cd7" },
|
"alpha-nvim": { "branch": "main", "commit": "bf3c8bb8c02ed3d9644cc5bbc48e2bdc39349cd7" },
|
||||||
"auto-bufferline.nvim": { "branch": "main", "commit": "f5835a4b62bfb6f6c829f8f050e5102ccf230b3e" },
|
"auto-bufferline.nvim": { "branch": "main", "commit": "f5835a4b62bfb6f6c829f8f050e5102ccf230b3e" },
|
||||||
"auto-conform.nvim": { "branch": "main", "commit": "5f0897235e84bf2f8ff9d634e81678d5bfa63ab8" },
|
"auto-conform.nvim": { "branch": "main", "commit": "5f0897235e84bf2f8ff9d634e81678d5bfa63ab8" },
|
||||||
|
"auto-jdtls.nvim": { "branch": "main", "commit": "3851cb9539283972d53ac24a3fd00a9decfdc717" },
|
||||||
"auto-lint.nvim": { "branch": "main", "commit": "aff13cb8eaa055e66e940d43b7d83166397e5413" },
|
"auto-lint.nvim": { "branch": "main", "commit": "aff13cb8eaa055e66e940d43b7d83166397e5413" },
|
||||||
"auto-lsp.nvim": { "branch": "main", "commit": "2a5ade9f78a1c60d747396445d4205352d2491db" },
|
"auto-lsp.nvim": { "branch": "main", "commit": "2a5ade9f78a1c60d747396445d4205352d2491db" },
|
||||||
"auto-lualine.nvim": { "branch": "main", "commit": "fc8557f61dc1da12a8b90167a49420958b991d8c" },
|
"auto-lualine.nvim": { "branch": "main", "commit": "fc8557f61dc1da12a8b90167a49420958b991d8c" },
|
||||||
|
@ -66,7 +67,7 @@
|
||||||
"toggleterm.nvim": { "branch": "main", "commit": "87b2d6a3cab8e2bd9a0255427074285f0365398d" },
|
"toggleterm.nvim": { "branch": "main", "commit": "87b2d6a3cab8e2bd9a0255427074285f0365398d" },
|
||||||
"vim-illuminate": { "branch": "master", "commit": "5eeb7951fc630682c322e88a9bbdae5c224ff0aa" },
|
"vim-illuminate": { "branch": "master", "commit": "5eeb7951fc630682c322e88a9bbdae5c224ff0aa" },
|
||||||
"vim-visual-multi": { "branch": "master", "commit": "a6975e7c1ee157615bbc80fc25e4392f71c344d4" },
|
"vim-visual-multi": { "branch": "master", "commit": "a6975e7c1ee157615bbc80fc25e4392f71c344d4" },
|
||||||
"virt-column.nvim": { "branch": "master", "commit": "b62b4ef0774d19452d4ed18e473e824c7a756f2f" },
|
"virt-column.nvim": { "branch": "master", "commit": "b87e3e0864211a32724a2ebf3be37e24e9e2fa99" },
|
||||||
"volt": { "branch": "main", "commit": "1df6698f7e5d19db578063412d12322e53376f5e" },
|
"volt": { "branch": "main", "commit": "1df6698f7e5d19db578063412d12322e53376f5e" },
|
||||||
"which-key.nvim": { "branch": "main", "commit": "68e37e12913a66b60073906f5d3f14dee0de19f2" },
|
"which-key.nvim": { "branch": "main", "commit": "68e37e12913a66b60073906f5d3f14dee0de19f2" },
|
||||||
"yanky.nvim": { "branch": "main", "commit": "73215b77d22ebb179cef98e7e1235825431d10e4" },
|
"yanky.nvim": { "branch": "main", "commit": "73215b77d22ebb179cef98e7e1235825431d10e4" },
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
local M = {}
|
|
||||||
|
|
||||||
M.setup = function()
|
|
||||||
require("auto-jdtls.utils").attach_jdtls()
|
|
||||||
end
|
|
||||||
|
|
||||||
return M
|
|
8
lua/auto-jdtls2/init.lua
Normal file
8
lua/auto-jdtls2/init.lua
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
M.setup = function(opt)
|
||||||
|
opt = opt or {}
|
||||||
|
require("auto-jdtls.utils").attach_jdtls(opt)
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
|
@ -119,7 +119,8 @@ M.opts = {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
M.attach_jdtls = function()
|
M.attach_jdtls = function(op)
|
||||||
|
M.opts = M.extend_or_override(M.opts, op or {})
|
||||||
local opt = vim.opt
|
local opt = vim.opt
|
||||||
opt.shiftwidth = 4
|
opt.shiftwidth = 4
|
||||||
opt.tabstop = 4
|
opt.tabstop = 4
|
||||||
|
@ -179,8 +180,8 @@ M.attach_jdtls = function()
|
||||||
local client = vim.lsp.get_client_by_id(args.data.client_id)
|
local client = vim.lsp.get_client_by_id(args.data.client_id)
|
||||||
-- stylua: ignore
|
-- stylua: ignore
|
||||||
if client and client.name == "jdtls" then
|
if client and client.name == "jdtls" then
|
||||||
M.jdtls_keymaps()
|
-- M.jdtls_keymaps()
|
||||||
M.lsp_keymaps()
|
-- M.lsp_keymaps()
|
||||||
if M.opts.dap and mason_registry.is_installed("java-debug-adapter") then
|
if M.opts.dap and mason_registry.is_installed("java-debug-adapter") then
|
||||||
-- custom init for Java debugger
|
-- custom init for Java debugger
|
||||||
require("jdtls").setup_dap(M.opts.dap)
|
require("jdtls").setup_dap(M.opts.dap)
|
|
@ -1,39 +1,4 @@
|
||||||
local M = {}
|
local M = {}
|
||||||
local java_filetypes = { "java" }
|
|
||||||
local root_markers = { ".git", "mvnw", "gradlew", "pom.xml", "build.gradle" }
|
|
||||||
|
|
||||||
-- Utility function to extend or override a config table, similar to the way
|
|
||||||
-- that Plugin.opts works.
|
|
||||||
---@param config table
|
|
||||||
---@param custom function | table | nil
|
|
||||||
local function extend_or_override(config, custom, ...)
|
|
||||||
if type(custom) == "function" then
|
|
||||||
config = custom(config, ...) or config
|
|
||||||
elseif custom then
|
|
||||||
config = vim.tbl_deep_extend("force", config, custom) --[[@as table]]
|
|
||||||
end
|
|
||||||
return config
|
|
||||||
end
|
|
||||||
|
|
||||||
local function capabilities()
|
|
||||||
local status_ok, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp")
|
|
||||||
if status_ok then
|
|
||||||
return cmp_nvim_lsp.default_capabilities()
|
|
||||||
end
|
|
||||||
|
|
||||||
local CAPABILITIES = vim.lsp.protocol.make_client_capabilities()
|
|
||||||
CAPABILITIES.textDocument.completion.completionItem.snippetSupport = true
|
|
||||||
CAPABILITIES.textDocument.completion.completionItem.resolveSupport = {
|
|
||||||
properties = {
|
|
||||||
"documentation",
|
|
||||||
"detail",
|
|
||||||
"additionalTextEdits",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
return CAPABILITIES
|
|
||||||
end
|
|
||||||
|
|
||||||
M = {
|
M = {
|
||||||
{
|
{
|
||||||
"williamboman/mason-lspconfig.nvim",
|
"williamboman/mason-lspconfig.nvim",
|
||||||
|
@ -41,6 +6,7 @@ M = {
|
||||||
opts = function(_, opts)
|
opts = function(_, opts)
|
||||||
opts.skip_config = opts.skip_config or {}
|
opts.skip_config = opts.skip_config or {}
|
||||||
vim.list_extend(opts.skip_config, { "jdtls" })
|
vim.list_extend(opts.skip_config, { "jdtls" })
|
||||||
|
vim.keymap.set({ "n", "v" }, "<leader>l", "", { desc = "LSP" })
|
||||||
-- Set vim motion for <Space> + l + h to show code documentation about the code the cursor is currently over if available
|
-- Set vim motion for <Space> + l + h to show code documentation about the code the cursor is currently over if available
|
||||||
vim.keymap.set("n", "<leader>lh", vim.lsp.buf.hover, { desc = "Code Hover Documentation" })
|
vim.keymap.set("n", "<leader>lh", vim.lsp.buf.hover, { desc = "Code Hover Documentation" })
|
||||||
-- Set vim motion for <Space> + l + d to go where the code/variable under the cursor was defined
|
-- Set vim motion for <Space> + l + d to go where the code/variable under the cursor was defined
|
||||||
|
@ -62,116 +28,14 @@ M = {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"mfussenegger/nvim-jdtls",
|
"mfussenegger/nvim-jdtls",
|
||||||
ft = java_filetypes,
|
dependencies = { "pojokcodeid/auto-jdtls.nvim" },
|
||||||
|
ft = { "java" },
|
||||||
enabled = true,
|
enabled = true,
|
||||||
opts = function()
|
-- your opts go here
|
||||||
return {
|
opts = {},
|
||||||
root_dir = require("jdtls.setup").find_root(root_markers),
|
|
||||||
project_name = function()
|
|
||||||
return vim.fn.fnamemodify(vim.fn.getcwd(), ":p:h:t")
|
|
||||||
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,
|
|
||||||
cmd = { vim.fn.exepath("jdtls") },
|
|
||||||
full_cmd = function(opts)
|
|
||||||
local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ":p:h:t")
|
|
||||||
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 = {} },
|
|
||||||
dap_main = {},
|
|
||||||
test = true,
|
|
||||||
settings = {
|
|
||||||
java = {
|
|
||||||
inlayHints = {
|
|
||||||
parameterNames = {
|
|
||||||
enabled = "all",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
end,
|
|
||||||
config = function(_, opts)
|
|
||||||
local opt = vim.opt
|
|
||||||
opt.shiftwidth = 4
|
|
||||||
opt.tabstop = 4
|
|
||||||
opt.softtabstop = 4
|
|
||||||
opt.ts = 4
|
|
||||||
opt.expandtab = true
|
|
||||||
|
|
||||||
local mason_registry = require("mason-registry")
|
|
||||||
local bundles = {} ---@type string[]
|
|
||||||
if opts.dap and mason_registry.is_installed("java-debug-adapter") then
|
|
||||||
local java_dbg_pkg = mason_registry.get_package("java-debug-adapter")
|
|
||||||
local java_dbg_path = java_dbg_pkg:get_install_path()
|
|
||||||
local jar_patterns = {
|
|
||||||
java_dbg_path .. "/extension/server/com.microsoft.java.debug.plugin-*.jar",
|
|
||||||
}
|
|
||||||
-- java-test also depends on java-debug-adapter.
|
|
||||||
if opts.test and mason_registry.is_installed("java-test") then
|
|
||||||
local java_test_pkg = mason_registry.get_package("java-test")
|
|
||||||
local java_test_path = java_test_pkg:get_install_path()
|
|
||||||
vim.list_extend(jar_patterns, {
|
|
||||||
java_test_path .. "/extension/server/*.jar",
|
|
||||||
})
|
|
||||||
end
|
|
||||||
for _, jar_pattern in ipairs(jar_patterns) do
|
|
||||||
for _, bundle in ipairs(vim.split(vim.fn.glob(jar_pattern), "\n")) do
|
|
||||||
table.insert(bundles, bundle)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function attach_jdtls()
|
|
||||||
-- Configuration can be augmented and overridden by opts.jdtls
|
|
||||||
local config = extend_or_override({
|
|
||||||
cmd = opts.full_cmd(opts),
|
|
||||||
root_dir = require("jdtls.setup").find_root(root_markers),
|
|
||||||
init_options = {
|
|
||||||
bundles = bundles,
|
|
||||||
},
|
|
||||||
settings = opts.settings,
|
|
||||||
-- enable CMP capabilities
|
|
||||||
-- capabilities = require("user.lsp.handlers").capabilities or nil,
|
|
||||||
-- capabilities = require("auto-lsp.lsp.handlers").capabilities or nil,
|
|
||||||
capabilities = capabilities() or nil,
|
|
||||||
}, opts.jdtls)
|
|
||||||
|
|
||||||
-- Existing server will be reused if the root_dir matches.
|
|
||||||
require("jdtls").start_or_attach(config)
|
|
||||||
-- not need to require("jdtls.setup").add_commands(), start automatically adds commands
|
|
||||||
end
|
|
||||||
|
|
||||||
vim.api.nvim_create_autocmd("FileType", {
|
|
||||||
pattern = java_filetypes,
|
|
||||||
callback = attach_jdtls,
|
|
||||||
})
|
|
||||||
|
|
||||||
-- Setup keymap and dap after the lsp is fully attached.
|
|
||||||
-- https://github.com/mfussenegger/nvim-jdtls#nvim-dap-configuration
|
|
||||||
-- https://neovim.io/doc/user/lsp.html#LspAttach
|
|
||||||
vim.api.nvim_create_autocmd("LspAttach", {
|
|
||||||
callback = function(args)
|
|
||||||
local client = vim.lsp.get_client_by_id(args.data.client_id)
|
|
||||||
-- stylua: ignore
|
-- stylua: ignore
|
||||||
if client and client.name == "jdtls" then
|
config = function(_, opts)
|
||||||
|
require("auto-jdtls").setup(opts)
|
||||||
-- add keymaps
|
-- add keymaps
|
||||||
vim.keymap.set('n', '<leader>J', "", { desc = "Java" })
|
vim.keymap.set('n', '<leader>J', "", { desc = "Java" })
|
||||||
-- Set a Vim motion to <Space> + <Shift>J + o to organize imports in normal mode
|
-- Set a Vim motion to <Space> + <Shift>J + o to organize imports in normal mode
|
||||||
|
@ -192,22 +56,6 @@ M = {
|
||||||
vim.keymap.set('n', '<leader>JT', "<Cmd> lua require('jdtls').test_class()<CR>", { desc = "Java Test Class" })
|
vim.keymap.set('n', '<leader>JT', "<Cmd> lua require('jdtls').test_class()<CR>", { desc = "Java Test Class" })
|
||||||
-- Set a Vim motion to <Space> + <Shift>J + u to update the project configuration
|
-- Set a Vim motion to <Space> + <Shift>J + u to update the project configuration
|
||||||
vim.keymap.set('n', '<leader>Ju', "<Cmd> JdtUpdateConfig<CR>", { desc = "Java Update Config" })
|
vim.keymap.set('n', '<leader>Ju', "<Cmd> JdtUpdateConfig<CR>", { desc = "Java Update Config" })
|
||||||
if opts.dap and mason_registry.is_installed("java-debug-adapter") then
|
|
||||||
-- custom init for Java debugger
|
|
||||||
require("jdtls").setup_dap(opts.dap)
|
|
||||||
require("jdtls.dap").setup_dap_main_class_configs(opts.dap_main)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- User can set additional keymaps in opts.on_attach
|
|
||||||
if opts.on_attach then
|
|
||||||
opts.on_attach(args)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end,
|
|
||||||
})
|
|
||||||
|
|
||||||
-- Avoid race condition by calling attach the first time, since the autocmd won't fire.
|
|
||||||
attach_jdtls()
|
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -219,6 +67,7 @@ M = {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"williamboman/mason-lspconfig.nvim",
|
"williamboman/mason-lspconfig.nvim",
|
||||||
|
-- stylua: ignore
|
||||||
opts = function(_, opts)
|
opts = function(_, opts)
|
||||||
opts.ensure_installed = opts.ensure_installed or {}
|
opts.ensure_installed = opts.ensure_installed or {}
|
||||||
vim.list_extend(opts.ensure_installed, { "jdtls" })
|
vim.list_extend(opts.ensure_installed, { "jdtls" })
|
||||||
|
|
|
@ -1,10 +1,60 @@
|
||||||
local M = {}
|
local M = {}
|
||||||
|
local java_filetypes = { "java" }
|
||||||
|
local root_markers = { ".git", "mvnw", "gradlew", "pom.xml", "build.gradle" }
|
||||||
|
|
||||||
|
-- Utility function to extend or override a config table, similar to the way
|
||||||
|
-- that Plugin.opts works.
|
||||||
|
---@param config table
|
||||||
|
---@param custom function | table | nil
|
||||||
|
local function extend_or_override(config, custom, ...)
|
||||||
|
if type(custom) == "function" then
|
||||||
|
config = custom(config, ...) or config
|
||||||
|
elseif custom then
|
||||||
|
config = vim.tbl_deep_extend("force", config, custom) --[[@as table]]
|
||||||
|
end
|
||||||
|
return config
|
||||||
|
end
|
||||||
|
|
||||||
|
local function capabilities()
|
||||||
|
local status_ok, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp")
|
||||||
|
if status_ok then
|
||||||
|
return cmp_nvim_lsp.default_capabilities()
|
||||||
|
end
|
||||||
|
|
||||||
|
local CAPABILITIES = vim.lsp.protocol.make_client_capabilities()
|
||||||
|
CAPABILITIES.textDocument.completion.completionItem.snippetSupport = true
|
||||||
|
CAPABILITIES.textDocument.completion.completionItem.resolveSupport = {
|
||||||
|
properties = {
|
||||||
|
"documentation",
|
||||||
|
"detail",
|
||||||
|
"additionalTextEdits",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return CAPABILITIES
|
||||||
|
end
|
||||||
|
|
||||||
M = {
|
M = {
|
||||||
{
|
{
|
||||||
"williamboman/mason-lspconfig.nvim",
|
"williamboman/mason-lspconfig.nvim",
|
||||||
|
-- stylua: ignore
|
||||||
opts = function(_, opts)
|
opts = function(_, opts)
|
||||||
opts.skip_config = opts.skip_config or {}
|
opts.skip_config = opts.skip_config or {}
|
||||||
vim.list_extend(opts.skip_config, { "jdtls" })
|
vim.list_extend(opts.skip_config, { "jdtls" })
|
||||||
|
-- Set vim motion for <Space> + l + h to show code documentation about the code the cursor is currently over if available
|
||||||
|
vim.keymap.set("n", "<leader>lh", vim.lsp.buf.hover, { desc = "Code Hover Documentation" })
|
||||||
|
-- Set vim motion for <Space> + l + d to go where the code/variable under the cursor was defined
|
||||||
|
vim.keymap.set("n", "<leader>ld", vim.lsp.buf.definition, { desc = "Code Goto Definition" })
|
||||||
|
-- Set vim motion for <Space> + l + a for display code action suggestions for code diagnostics in both normal and visual mode
|
||||||
|
vim.keymap.set({ "n", "v" }, "<leader>la", vim.lsp.buf.code_action, { desc = "Code Actions" })
|
||||||
|
-- Set vim motion for <Space> + l + r to display references to the code under the cursor
|
||||||
|
vim.keymap.set("n", "<leader>lr", require("telescope.builtin").lsp_references, { desc = "Code Goto References" })
|
||||||
|
-- Set vim motion for <Space> + l + i to display implementations to the code under the cursor
|
||||||
|
vim.keymap.set("n", "<leader>li", require("telescope.builtin").lsp_implementations, { desc = "Code Goto Implementations" })
|
||||||
|
-- Set a vim motion for <Space> + l + <Shift>R to smartly rename the code under the cursor
|
||||||
|
vim.keymap.set("n", "<leader>lR", vim.lsp.buf.rename, { desc = "Code Rename" })
|
||||||
|
-- Set a vim motion for <Space> + l + <Shift>D to go to where the code/object was declared in the project (class file)
|
||||||
|
vim.keymap.set("n", "<leader>lD", vim.lsp.buf.declaration, { desc = "Code Goto Declaration" })
|
||||||
end,
|
end,
|
||||||
keys = {
|
keys = {
|
||||||
{ "<leader>l", "", desc = "LSP", mode = { "n", "v" } },
|
{ "<leader>l", "", desc = "LSP", mode = { "n", "v" } },
|
||||||
|
@ -12,10 +62,152 @@ M = {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"mfussenegger/nvim-jdtls",
|
"mfussenegger/nvim-jdtls",
|
||||||
ft = { "java" },
|
ft = java_filetypes,
|
||||||
enabled = true,
|
enabled = true,
|
||||||
config = function()
|
opts = function()
|
||||||
require("auto-jdtls").setup()
|
return {
|
||||||
|
root_dir = require("jdtls.setup").find_root(root_markers),
|
||||||
|
project_name = function()
|
||||||
|
return vim.fn.fnamemodify(vim.fn.getcwd(), ":p:h:t")
|
||||||
|
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,
|
||||||
|
cmd = { vim.fn.exepath("jdtls") },
|
||||||
|
full_cmd = function(opts)
|
||||||
|
local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ":p:h:t")
|
||||||
|
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 = {} },
|
||||||
|
dap_main = {},
|
||||||
|
test = true,
|
||||||
|
settings = {
|
||||||
|
java = {
|
||||||
|
inlayHints = {
|
||||||
|
parameterNames = {
|
||||||
|
enabled = "all",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
end,
|
||||||
|
config = function(_, opts)
|
||||||
|
local opt = vim.opt
|
||||||
|
opt.shiftwidth = 4
|
||||||
|
opt.tabstop = 4
|
||||||
|
opt.softtabstop = 4
|
||||||
|
opt.ts = 4
|
||||||
|
opt.expandtab = true
|
||||||
|
|
||||||
|
local mason_registry = require("mason-registry")
|
||||||
|
local bundles = {} ---@type string[]
|
||||||
|
if opts.dap and mason_registry.is_installed("java-debug-adapter") then
|
||||||
|
local java_dbg_pkg = mason_registry.get_package("java-debug-adapter")
|
||||||
|
local java_dbg_path = java_dbg_pkg:get_install_path()
|
||||||
|
local jar_patterns = {
|
||||||
|
java_dbg_path .. "/extension/server/com.microsoft.java.debug.plugin-*.jar",
|
||||||
|
}
|
||||||
|
-- java-test also depends on java-debug-adapter.
|
||||||
|
if opts.test and mason_registry.is_installed("java-test") then
|
||||||
|
local java_test_pkg = mason_registry.get_package("java-test")
|
||||||
|
local java_test_path = java_test_pkg:get_install_path()
|
||||||
|
vim.list_extend(jar_patterns, {
|
||||||
|
java_test_path .. "/extension/server/*.jar",
|
||||||
|
})
|
||||||
|
end
|
||||||
|
for _, jar_pattern in ipairs(jar_patterns) do
|
||||||
|
for _, bundle in ipairs(vim.split(vim.fn.glob(jar_pattern), "\n")) do
|
||||||
|
table.insert(bundles, bundle)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function attach_jdtls()
|
||||||
|
-- Configuration can be augmented and overridden by opts.jdtls
|
||||||
|
local config = extend_or_override({
|
||||||
|
cmd = opts.full_cmd(opts),
|
||||||
|
root_dir = require("jdtls.setup").find_root(root_markers),
|
||||||
|
init_options = {
|
||||||
|
bundles = bundles,
|
||||||
|
},
|
||||||
|
settings = opts.settings,
|
||||||
|
-- enable CMP capabilities
|
||||||
|
-- capabilities = require("user.lsp.handlers").capabilities or nil,
|
||||||
|
-- capabilities = require("auto-lsp.lsp.handlers").capabilities or nil,
|
||||||
|
capabilities = capabilities() or nil,
|
||||||
|
}, opts.jdtls)
|
||||||
|
|
||||||
|
-- Existing server will be reused if the root_dir matches.
|
||||||
|
require("jdtls").start_or_attach(config)
|
||||||
|
-- not need to require("jdtls.setup").add_commands(), start automatically adds commands
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.api.nvim_create_autocmd("FileType", {
|
||||||
|
pattern = java_filetypes,
|
||||||
|
callback = attach_jdtls,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Setup keymap and dap after the lsp is fully attached.
|
||||||
|
-- https://github.com/mfussenegger/nvim-jdtls#nvim-dap-configuration
|
||||||
|
-- https://neovim.io/doc/user/lsp.html#LspAttach
|
||||||
|
vim.api.nvim_create_autocmd("LspAttach", {
|
||||||
|
callback = function(args)
|
||||||
|
local client = vim.lsp.get_client_by_id(args.data.client_id)
|
||||||
|
-- stylua: ignore
|
||||||
|
if client and client.name == "jdtls" then
|
||||||
|
-- add keymaps
|
||||||
|
vim.keymap.set('n', '<leader>J', "", { desc = "Java" })
|
||||||
|
-- Set a Vim motion to <Space> + <Shift>J + o to organize imports in normal mode
|
||||||
|
vim.keymap.set('n', '<leader>Jo', "<Cmd> lua require('jdtls').organize_imports()<CR>", { desc = "Java Organize Imports" })
|
||||||
|
-- Set a Vim motion to <Space> + <Shift>J + v to extract the code under the cursor to a variable
|
||||||
|
vim.keymap.set('n', '<leader>Jv', "<Cmd> lua require('jdtls').extract_variable()<CR>", { desc = "Java Extract Variable" })
|
||||||
|
-- Set a Vim motion to <Space> + <Shift>J + v to extract the code selected in visual mode to a variable
|
||||||
|
vim.keymap.set('v', '<leader>Jv', "<Esc><Cmd> lua require('jdtls').extract_variable(true)<CR>", { desc = "Java Extract Variable" })
|
||||||
|
-- Set a Vim motion to <Space> + <Shift>J + <Shift>C to extract the code under the cursor to a static variable
|
||||||
|
vim.keymap.set('n', '<leader>JC', "<Cmd> lua require('jdtls').extract_constant()<CR>", { desc = "Java Extract Constant" })
|
||||||
|
-- Set a Vim motion to <Space> + <Shift>J + <Shift>C to extract the code selected in visual mode to a static variable
|
||||||
|
vim.keymap.set('v', '<leader>JC', "<Esc><Cmd> lua require('jdtls').extract_constant(true)<CR>", { desc = "Java Extract Constant" })
|
||||||
|
-- Set a Vim motion to <Space> + <Shift>J + t to run the test method currently under the cursor
|
||||||
|
vim.keymap.set('n', '<leader>Jt', "<Cmd> lua require('jdtls').test_nearest_method()<CR>", { desc = "Java Test Method" })
|
||||||
|
-- Set a Vim motion to <Space> + <Shift>J + t to run the test method that is currently selected in visual mode
|
||||||
|
vim.keymap.set('v', '<leader>Jt', "<Esc><Cmd> lua require('jdtls').test_nearest_method(true)<CR>", { desc = "Java Test Method" })
|
||||||
|
-- Set a Vim motion to <Space> + <Shift>J + <Shift>T to run an entire test suite (class)
|
||||||
|
vim.keymap.set('n', '<leader>JT', "<Cmd> lua require('jdtls').test_class()<CR>", { desc = "Java Test Class" })
|
||||||
|
-- Set a Vim motion to <Space> + <Shift>J + u to update the project configuration
|
||||||
|
vim.keymap.set('n', '<leader>Ju', "<Cmd> JdtUpdateConfig<CR>", { desc = "Java Update Config" })
|
||||||
|
if opts.dap and mason_registry.is_installed("java-debug-adapter") then
|
||||||
|
-- custom init for Java debugger
|
||||||
|
require("jdtls").setup_dap(opts.dap)
|
||||||
|
require("jdtls.dap").setup_dap_main_class_configs(opts.dap_main)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- User can set additional keymaps in opts.on_attach
|
||||||
|
if opts.on_attach then
|
||||||
|
opts.on_attach(args)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Avoid race condition by calling attach the first time, since the autocmd won't fire.
|
||||||
|
attach_jdtls()
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -5,9 +5,9 @@ pcode.lang = {
|
||||||
sql = false,
|
sql = false,
|
||||||
deno = false,
|
deno = false,
|
||||||
golang = false,
|
golang = false,
|
||||||
java = false,
|
java = true,
|
||||||
java2 = false,
|
java2 = false,
|
||||||
java3 = true,
|
java3 = false,
|
||||||
javascript = false,
|
javascript = false,
|
||||||
kotlin = false,
|
kotlin = false,
|
||||||
markdown = false,
|
markdown = false,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue