diff options
| author | bh <qn+git@excalibur.computer> | 2026-04-21 00:36:43 +0800 |
|---|---|---|
| committer | bh <qn+git@excalibur.computer> | 2026-04-21 00:36:43 +0800 |
| commit | 86ef0a045c44c756b33a93079fb00bbb81c6e217 (patch) | |
| tree | f19e182e30ea61f49fe5248a2e2203b72dd2830a /lua/config/jdtls.lua | |
| parent | f0ec508a7281617ac47e3ef0cf6ed3438114c152 (diff) | |
Add Java LSP support
Diffstat (limited to 'lua/config/jdtls.lua')
| -rw-r--r-- | lua/config/jdtls.lua | 54 |
1 files changed, 54 insertions, 0 deletions
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", "<leader>rn", vim.lsp.buf.rename, opts) + vim.keymap.set("n", "gr", vim.lsp.buf.references, opts) + vim.keymap.set("n", "<leader>ca", vim.lsp.buf.code_action, opts) + vim.keymap.set("n", "<leader>d", vim.diagnostic.open_float, opts) + -- jdtls-specific mappings + vim.keymap.set("n", "<leader>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 |
