Skip to content

Commit 678ad41

Browse files
Jirapat ChookleebJirapat Chookleeb
authored andcommitted
feat: add config JDTLS
1 parent 3338d39 commit 678ad41

File tree

2 files changed

+117
-2
lines changed

2 files changed

+117
-2
lines changed

init.lua

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ require('lazy').setup({
683683
-- But for many setups, the LSP (`ts_ls`) will work just fine
684684
-- ts_ls = {},
685685
--
686-
686+
jdtls = {},
687687
lua_ls = {
688688
-- cmd = { ... },
689689
-- filetypes = { ... },
@@ -722,6 +722,11 @@ require('lazy').setup({
722722
require('mason-lspconfig').setup {
723723
ensure_installed = {}, -- explicitly set to an empty table (Kickstart populates installs via mason-tool-installer)
724724
automatic_installation = false,
725+
automatic_enable = {
726+
exclude = {
727+
'jdtls',
728+
},
729+
},
725730
handlers = {
726731
function(server_name)
727732
local server = servers[server_name] or {}
@@ -777,6 +782,10 @@ require('lazy').setup({
777782
},
778783
},
779784

785+
{
786+
'mfussenegger/nvim-jdtls',
787+
},
788+
780789
{ -- Autocompletion
781790
'saghen/blink.cmp',
782791
event = 'VimEnter',
@@ -1012,5 +1021,13 @@ require('lazy').setup({
10121021
},
10131022
})
10141023

1015-
-- The line beneath this is called `modeline`. See `:help modeline`
1024+
vim.api.nvim_create_autocmd('FileType', {
1025+
pattern = 'java',
1026+
callback = function()
1027+
vim.opt_local.tabstop = 2
1028+
vim.opt_local.shiftwidth = 2
1029+
1030+
require('lsp.jdtls').setup()
1031+
end,
1032+
}) -- The line beneath this is called `modeline`. See `:help modeline`
10161033
-- vim: ts=2 sts=2 sw=2 et

lua/lsp/jdtls.lua

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
local M = {}
2+
3+
function M.setup()
4+
local home = os.getenv 'HOME'
5+
local workspace_path = home .. '/.local/share/nvim/jdtls-workspace/'
6+
local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ':p:h:t')
7+
local workspace_dir = workspace_path .. project_name
8+
9+
local status, jdtls = pcall(require, 'jdtls')
10+
if not status then
11+
vim.notify('jdtls not found!', vim.log.levels.ERROR)
12+
return
13+
end
14+
15+
local extendedClientCapabilities = jdtls.extendedClientCapabilities
16+
17+
-- Root markers for Java project
18+
local root_markers = { '.git', 'mvnw', 'gradlew', 'pom.xml', 'build.gradle' }
19+
20+
local function find_root_dir()
21+
local parent_dir = vim.fn.fnamemodify(vim.fn.getcwd(), ':h')
22+
local root = jdtls.setup.find_root(root_markers, parent_dir)
23+
if not root or root == '' then
24+
root = jdtls.setup.find_root(root_markers)
25+
end
26+
return root or vim.fn.getcwd()
27+
end
28+
29+
-- Java executable (Java 25, macOS)
30+
local java_exe = '/Library/Java/JavaVirtualMachines/temurin-25.jdk/Contents/Home/bin/java'
31+
32+
local config = {
33+
cmd = {
34+
java_exe,
35+
'-Declipse.application=org.eclipse.jdt.ls.core.id1',
36+
'-Dosgi.bundles.defaultStartLevel=4',
37+
'-Declipse.product=org.eclipse.jdt.ls.core.product',
38+
'-Dlog.protocol=true',
39+
'-Dlog.level=ALL',
40+
'-Xmx2g',
41+
'--add-modules=ALL-SYSTEM',
42+
'--add-opens',
43+
'java.base/java.util=ALL-UNNAMED',
44+
'--add-opens',
45+
'java.base/java.lang=ALL-UNNAMED',
46+
'-javaagent:' .. home .. '/.local/share/nvim/mason/packages/jdtls/lombok.jar',
47+
'-jar',
48+
vim.fn.glob(home .. '/.local/share/nvim/mason/packages/jdtls/plugins/org.eclipse.equinox.launcher_*.jar'),
49+
'-configuration',
50+
home .. '/.local/share/nvim/mason/packages/jdtls/config_mac',
51+
'-data',
52+
workspace_dir,
53+
},
54+
55+
root_dir = find_root_dir(),
56+
57+
settings = {
58+
java = {
59+
signatureHelp = { enabled = true },
60+
extendedClientCapabilities = extendedClientCapabilities,
61+
maven = { downloadSources = true },
62+
referencesCodeLens = { enabled = true },
63+
references = { includeDecompiledSources = true },
64+
inlayHints = { parameterNames = { enabled = 'all' } },
65+
format = { enabled = false },
66+
saveActions = { organizeImports = false },
67+
},
68+
},
69+
70+
init_options = {
71+
bundles = {},
72+
},
73+
}
74+
75+
-- Start or attach jdtls
76+
jdtls.start_or_attach(config)
77+
78+
--------------------------------------------------------------------
79+
-- Keymaps
80+
--------------------------------------------------------------------
81+
local opts = { noremap = true, silent = true }
82+
83+
vim.keymap.set('n', '<leader>jo', "<Cmd>lua require'jdtls'.organize_imports()<CR>", { desc = 'Organize Imports' })
84+
vim.keymap.set('n', '<leader>jrv', "<Cmd>lua require('jdtls').extract_variable()<CR>", { desc = 'Extract Variable' })
85+
vim.keymap.set('v', '<leader>jrv', "<Esc><Cmd>lua require('jdtls').extract_variable(true)<CR>", { desc = 'Extract Variable' })
86+
vim.keymap.set('n', '<leader>jrc', "<Cmd>lua require('jdtls').extract_constant()<CR>", { desc = 'Extract Constant' })
87+
vim.keymap.set('v', '<leader>jrc', "<Esc><Cmd>lua require('jdtls').extract_constant(true)<CR>", { desc = 'Extract Constant' })
88+
vim.keymap.set('v', '<leader>jrm', "<Esc><Cmd>lua require('jdtls').extract_method(true)<CR>", { desc = 'Extract Method' })
89+
vim.keymap.set('n', 'nd', vim.lsp.buf.definition, { desc = 'Go to Definition' })
90+
vim.keymap.set('n', 'nD', vim.lsp.buf.declaration, { desc = 'Go to Declaration' })
91+
vim.keymap.set('n', 'nr', vim.lsp.buf.references, { desc = 'References' })
92+
vim.keymap.set('n', 'ni', vim.lsp.buf.implementation, { desc = 'Go to Implementation' })
93+
vim.keymap.set('n', 'K', vim.lsp.buf.hover, { desc = 'Hover Documentation' })
94+
vim.keymap.set('n', '<leader>rn', vim.lsp.buf.rename, { desc = 'Rename Symbol' })
95+
vim.keymap.set('n', '<leader>ca', vim.lsp.buf.code_action, { desc = 'Code Action' })
96+
end
97+
98+
return M

0 commit comments

Comments
 (0)