local capabilities = vim.lsp.protocol.make_client_capabilities() local path = {} path.data_dir = vim.fn.stdpath('cache') .. '/nvim-jdtls' local data_dir = path.data_dir .. '/' .. vim.fn.fnamemodify(vim.fn.getcwd(), ':p:h:t') local jdtls_install = require('mason-registry') .get_package('jdtls') :get_install_path() path.java_agent = jdtls_install .. '/lombok.jar' path.launcher_jar = vim.fn.glob(jdtls_install .. '/plugins/org.eclipse.equinox.launcher_*.jar') path.platform_config = jdtls_install .. '/config_mac' -- See `:help vim.lsp.start_client` for an overview of the supported `config` options. local config = { -- The command that starts the language server -- See: https://github.com/eclipse/eclipse.jdt.ls#running-from-the-command-line cmd = { -- 💀 -- 'java', -- or '/path/to/java17_or_newer/bin/java' -- depends on if `java` is in your $PATH env variable and if it points to the right version. '/Library/Java/JavaVirtualMachines/temurin-19.jdk/Contents/Home/bin/java', -- '/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/bin/java', -- '/Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home/bin/java', '-Declipse.application=org.eclipse.jdt.ls.core.id1', '-Dosgi.bundles.defaultStartLevel=4', '-Declipse.product=org.eclipse.jdt.ls.core.product', '-Dlog.protocol=true', '-Dlog.level=ALL', '-javaagent:' .. path.java_agent, '-Xmx1g', '--add-modules=ALL-SYSTEM', '--add-opens', 'java.base/java.util=ALL-UNNAMED', '--add-opens', 'java.base/java.lang=ALL-UNNAMED', -- 💀 -- '-jar', '/path/to/jdtls_install_location/plugins/org.eclipse.equinox.launcher_VERSION_NUMBER.jar', '-jar', path.launcher_jar, -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^ -- Must point to the Change this to -- eclipse.jdt.ls installation the actual version -- 💀 -- '-configuration', '/path/to/jdtls_install_location/config_SYSTEM', '-configuration', path.platform_config, -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^ -- Must point to the Change to one of `linux`, `win` or `mac` -- eclipse.jdt.ls installation Depending on your system. -- 💀 -- See `data directory configuration` section in the README -- '-data', '/path/to/unique/per/project/workspace/folder' '-data', data_dir, }, -- 💀 -- This is the default if not provided, you can remove it. Or adjust as needed. -- One dedicated LSP server & client will be started per unique root_dir -- root_dir = require('jdtls.setup').find_root({'.git', 'mvnw', 'gradlew', 'pom.xml', 'build.gradle'}), root_dir = vim.fs.dirname(vim.fs.find({ '.git', 'mvnw', 'gradlew', 'pom.xml', 'build.gradle' }, { upward = true }) [1]), -- Here you can configure eclipse.jdt.ls specific settings -- See https://github.com/eclipse/eclipse.jdt.ls/wiki/Running-the-JAVA-LS-server-from-the-command-line#initialize-request -- for a list of options settings = { java = { home = '/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home', eclipse = { downloadSources = true, }, maven = { downloadSources = true, }, configuration = { updateBuildConfiguration = "interactive", runtimes = { name = 'JavaSE-1.8', path = '/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home', default = true, }, -- runtimes = { -- name = 'JavaSE-19', -- path = '/Library/Java/JavaVirtualMachines/temurin-19.jdk', -- default = true, -- }, }, import = { gradle = { enabled = true, java = { home = '/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home', }, } } } }, -- Language server `initializationOptions` -- You need to extend the `bundles` with paths to jar files -- if you want to use additional eclipse.jdt.ls plugins. -- -- See https://github.com/mfussenegger/nvim-jdtls#java-debug-installation -- -- If you don't plan on using the debugger or other eclipse.jdt.ls plugins you can remove this init_options = { bundles = {} }, on_attach = function(client, bufnr) -- Diagnostic keymaps -- vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, { desc = 'Go to previous diagnostic message' }) -- vim.keymap.set('n', ']d', vim.diagnostic.goto_next, { desc = 'Go to next diagnostic message' }) -- vim.keymap.set('n', 'e', vim.diagnostic.open_float, { desc = 'Open floating diagnostic message' }) -- vim.keymap.set('n', 'q', vim.diagnostic.setloclist, { desc = 'Open diagnostics list' }) -- [[ Configure LSP ]] -- This function gets run when an LSP connects to a particular buffer. -- local on_attach = function(_, bufnr) -- NOTE: Remember that lua is a real programming language, and as such it is possible -- to define small helper and utility functions so you don't have to repeat yourself -- many times. -- -- In this case, we create a function that lets us more easily define mappings specific -- for LSP related items. It sets the mode, buffer and description for us each time. local nmap = function(keys, func, desc) if desc then desc = 'LSP: ' .. desc end vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc }) end nmap('rn', vim.lsp.buf.rename, '[R]e[n]ame') nmap('ca', vim.lsp.buf.code_action, '[C]ode [A]ction') nmap('gd', vim.lsp.buf.definition, '[G]oto [D]efinition') nmap('gr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences') nmap('gI', vim.lsp.buf.implementation, '[G]oto [I]mplementation') nmap('D', vim.lsp.buf.type_definition, 'Type [D]efinition') nmap('ds', require('telescope.builtin').lsp_document_symbols, '[D]ocument [S]ymbols') nmap('ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols') -- See `:help K` for why this keymap nmap('K', vim.lsp.buf.hover, 'Hover Documentation') nmap('', vim.lsp.buf.signature_help, 'Signature Documentation') -- Lesser used LSP functionality nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration') nmap('wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder') nmap('wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder') nmap('wl', function() print(vim.inspect(vim.lsp.buf.list_workspace_folders())) end, '[W]orkspace [L]ist Folders') -- Create a command `:Format` local to the LSP buffer vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_) vim.lsp.buf.format() end, { desc = 'Format current buffer with LSP' }) -- end end } -- This starts a new client & server, -- or attaches to an existing client & server depending on the `root_dir`. require('jdtls').start_or_attach(config) -- local opts = { -- noremap = true, -- silent = true, -- } -- vim.keymap.set('n', 'q', vim.diagnostic.setloclist) -- vim.api.nvim_set_keymap('n', 'gD', 'lua vim.lsp.buf.declaration()', opts) -- vim.api.nvim_set_keymap('n', 'gd', 'lua vim.lsp.buf.definition()', opts) -- vim.api.nvim_set_keymap('n', 'K', 'lua vim.lsp.buf.hover()', opts) -- vim.api.nvim_set_keymap('n', 'gi', 'lua vim.lsp.buf.implementation()', opts) -- vim.api.nvim_set_keymap('n', '', 'lua vim.lsp.buf.signature_help()', opts) -- vim.api.nvim_set_keymap('n', 'wa', 'lua vim.lsp.buf.add_workspace_folder()', opts) -- vim.api.nvim_set_keymap('n', 'wr', 'lua vim.lsp.buf.remove_workspace_folder()', opts) -- vim.api.nvim_set_keymap('n', 'wl', 'lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))', opts) -- vim.api.nvim_set_keymap('n', 'D', 'lua vim.lsp.buf.type_definition()', opts) -- vim.api.nvim_set_keymap('n', 'rn', 'lua vim.lsp.buf.rename()', opts) -- vim.api.nvim_set_keymap('n', 'gr', 'lua vim.lsp.buf.references()', opts) -- vim.api.nvim_set_keymap('n', 'e', 'lua vim.lsp.diagnostic.show_line_diagnostics()', opts) -- vim.api.nvim_set_keymap('n', ']d', 'lua vim.lsp.diagnostic.goto_next', opts) -- vim.api.nvim_set_keymap('n', '[d', 'lua vim.lsp.diagnostic.goto_prev', opts) -- -- vim.api.nvim_set_keymap('n', 'q', 'lua vim.lsp.diagnostic.set_loclist()', opts) -- vim.api.nvim_set_keymap('n', 'f', 'lua vim.lsp.buf.formatting()', opts) -- vim.api.nvim_set_keymap('n', 'lg', 'lua vim.lsp.buf.formatting_sync(nil, 1000)', opts) -- -- vim.api.nvim_set_keymap('n', 'lA', 'lua vim.lsp.buf.code_action()', { silent = true }) -- vim.api.nvim_set_keymap('n', 'lA', 'lua require(\'jdtls\').code_action()', { silent = true })