From 86ef0a045c44c756b33a93079fb00bbb81c6e217 Mon Sep 17 00:00:00 2001 From: bh Date: Tue, 21 Apr 2026 00:36:43 +0800 Subject: Add Java LSP support --- lua/config/jdtls.lua | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lua/config/lsp.lua | 2 +- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 lua/config/jdtls.lua (limited to 'lua/config') diff --git a/lua/config/jdtls.lua b/lua/config/jdtls.lua new file mode 100644 index 0000000..04f92c4 --- /dev/null +++ b/lua/config/jdtls.lua @@ -0,0 +1,54 @@ +-- ~/.config/nvim/lua/config/jdtls.lua +return function() + local jdtls = require("jdtls") + + local cmp_ok, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp") + local capabilities = vim.lsp.protocol.make_client_capabilities() + if cmp_ok then + capabilities = cmp_nvim_lsp.default_capabilities(capabilities) + end + + local root_dir = require("jdtls.setup").find_root({ ".git", "pom.xml", "build.gradle", "gradlew", ".project", "mvnw" }) + if not root_dir then + root_dir = vim.fn.getcwd() + end + + local project_name = vim.fn.fnamemodify(root_dir, ":p:h:t") + local workspace_dir = vim.fn.stdpath("data") .. "/jdtls-workspace/" .. project_name + + local config = { + cmd = { + "jdtls", + "-data", workspace_dir, + }, + root_dir = root_dir, + capabilities = capabilities, + on_attach = function(client, bufnr) + local opts = { noremap = true, silent = true, buffer = bufnr } + vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts) + vim.keymap.set("n", "K", vim.lsp.buf.hover, opts) + vim.keymap.set("n", "rn", vim.lsp.buf.rename, opts) + vim.keymap.set("n", "gr", vim.lsp.buf.references, opts) + vim.keymap.set("n", "ca", vim.lsp.buf.code_action, opts) + vim.keymap.set("n", "d", vim.diagnostic.open_float, opts) + -- jdtls-specific mappings + vim.keymap.set("n", "oi", jdtls.organize_imports, opts) + end, + settings = { + java = { + signatureHelp = { enabled = true }, + completion = { + favoriteStaticMembers = {}, + }, + sources = { + organizeImports = { + starThreshold = 9999, + staticStarThreshold = 9999, + }, + }, + }, + }, + } + + jdtls.start_or_attach(config) +end diff --git a/lua/config/lsp.lua b/lua/config/lsp.lua index 67e80eb..01ada61 100644 --- a/lua/config/lsp.lua +++ b/lua/config/lsp.lua @@ -52,7 +52,7 @@ return function() rust_analyzer = {}, -- Rust zls = {}, -- Zig hls = {}, -- Haskell - jdtls = {}, -- Java + -- jdtls handled by nvim-jdtls plugin asm_lsp = {}, -- Assembly (if you install it) clojure_lsp = {}, -- Clojure racket_langserver = {}, -- Racket (Scheme dialect) -- cgit v1.2.3