diff --git a/after/plugin/nvim-java-config.lua b/after/plugin/nvim-java-config.lua index 7562139b..9a8169ca 100644 --- a/after/plugin/nvim-java-config.lua +++ b/after/plugin/nvim-java-config.lua @@ -33,7 +33,6 @@ capabilities.textDocument.foldingRange = { lineFoldingOnly = true, } -require('java').setup() require('lspconfig').jdtls.setup { on_attach = on_attach, capabilities = capabilities, diff --git a/ftplugin/java.lua b/ftplugin/java.lua index ace1b730..9994f2e5 100644 --- a/ftplugin/java.lua +++ b/ftplugin/java.lua @@ -1,111 +1,64 @@ --- Java Language Server configuration. --- Locations: --- 'nvim/ftplugin/java.lua'. --- 'nvim/lang-servers/intellij-java-google-style.xml' - local jdtls_ok, jdtls = pcall(require, 'jdtls') if not jdtls_ok then - vim.notify 'JDTLS not found, install with `:LspInstall jdtls`' + vim.notify 'JDTLS not found, install with `:MasonInstall jdtls`' return end --- See `:help vim.lsp.start_client` for an overview of the supported `config` options. --- local jdtls_path = vim.fn.stdpath 'data' .. '/mason/packages/jdtls' -local jdtls_path = '/Users/oluwatobibello/.local/share/nvim/mason/packages/jdtls' +-- Path setup +local jdtls_path = vim.fn.stdpath('data') .. '/mason/packages/jdtls' local path_to_lsp_server = jdtls_path .. '/config_mac' local path_to_plugins = jdtls_path .. '/plugins/' -local path_to_jar = path_to_plugins .. 'org.eclipse.equinox.launcher_*.jar' -local lombok_path = path_to_plugins .. 'lombok.jar' +local path_to_jar = vim.fn.glob(path_to_plugins .. 'org.eclipse.equinox.launcher_*.jar') +local lombok_path = vim.fn.glob(path_to_plugins .. 'lombok.jar') -local root_markers = { '.git', 'mvnw', 'gradlew', 'pom.xml', 'build.gradle' } -local root_dir = require('jdtls.setup').find_root(root_markers) -if root_dir == '' then - return -end +-- Java home directories +local java_17_home = '/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home' + +-- Root directory detection +local root_dir = require('jdtls.setup').find_root({'.git', 'mvnw', 'gradlew', 'pom.xml', 'build.gradle'}) +if root_dir == '' then return end -local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ':p:h:t') -local workspace_dir = vim.fn.stdpath 'data' .. '/site/java/workspace-root/' .. project_name -os.execute('mkdir ' .. workspace_dir) +-- Workspace setup +local project_name = vim.fn.fnamemodify(root_dir, ':p:h:t') +local workspace_dir = vim.fn.stdpath('data') .. '/site/java/workspace-root/' .. project_name +vim.fn.mkdir(workspace_dir, 'p') -local java_21_home_dir = '/Library/Java/JavaVirtualMachines/openjdk.jdk/Contents/Home/bin/java' -local java_17_home_dir = '/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home' -local java_11_home_dir = '/Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home' +print(workspace_dir) -- Main Config local config = { - -- The command that starts the language server - -- See: https://github.com/eclipse/eclipse.jdt.ls#running-from-the-command-line cmd = { - java_21_home_dir .. '/bin/java', + java_17_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', - '-Xmx1g', '-Dlog.level=ALL', - '--add-modules=ALL-SYSTEM', - '--add-opens', - 'java.base/java.util=ALL-UNNAMED', - '--add-opens', - 'java.base/java.lang=ALL-UNNAMED', - '-javaagent:' .. lombok_path, '-Xms1g', '--add-modules=ALL-SYSTEM', - '--add-opens', - 'java.base/java.util=ALL-UNNAMED', - '--add-opens', - 'java.base/java.lang=ALL-UNNAMED', - - '-jar', - path_to_jar, - '-configuration', - path_to_lsp_server, - '-data', - workspace_dir, + '--add-opens', 'java.base/java.util=ALL-UNNAMED', + '--add-opens', 'java.base/java.lang=ALL-UNNAMED', + '-javaagent:' .. lombok_path, + '-jar', path_to_jar, + '-configuration', path_to_lsp_server, + '-data', workspace_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 = root_dir, - -- 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 = java_21_home_dir, - eclipse = { - downloadSources = true, - }, + eclipse = { downloadSources = true }, + maven = { downloadSources = true }, + implementationsCodeLens = { enabled = true }, + referencesCodeLens = { enabled = true }, + references = { includeDecompiledSources = true }, configuration = { updateBuildConfiguration = 'interactive', runtimes = { - { - name = 'JavaSE-21', - path = java_21_home_dir, - }, - { - name = 'JavaSE-11', - path = java_11_home_dir, - }, - { - name = 'JavaSE-17', - path = java_17_home_dir, - }, - }, - }, - maven = { - downloadSources = true, - }, - implementationsCodeLens = { - enabled = true, - }, - referencesCodeLens = { - enabled = true, - }, - references = { - includeDecompiledSources = true, - }, + { name = 'JavaSE-17', path = java_17_home, default = true } + } + } }, signatureHelp = { enabled = true }, completion = { @@ -117,49 +70,21 @@ local config = { 'java.util.Objects.requireNonNull', 'java.util.Objects.requireNonNullElse', 'org.mockito.Mockito.*', - }, - importOrder = { - 'com', - 'java', - 'jarkata', - 'javax', - 'org', - }, - }, - sources = { - organizeImports = { - starThreshold = 9999, - staticStarThreshold = 9999, - }, - }, - codeGeneration = { - toString = { - template = '${object.className}{${member.name()}=${member.value}, ${otherMembers}}', - }, - useBlocks = true, - }, + } + } }, - flags = { - allow_incremental_sync = true, - }, - init_options = { - bundles = {}, - }, + flags = { allow_incremental_sync = true }, + init_options = { bundles = {} } } -config['on_attach'] = function(_, bufnr) - require('keymaps').map_java_keys(bufnr) - require('lsp_signature').on_attach({ - bind = true, -- This is mandatory, otherwise border config won't get registered. - floating_window_above_cur_line = false, - padding = '', - handler_opts = { - border = 'rounded', - }, - }, bufnr) +-- Keymaps setup +config.on_attach = function(_, bufnr) + -- Your keymaps here + vim.keymap.set('n', 'rn', vim.lsp.buf.rename, { buffer = bufnr }) + vim.keymap.set('n', 'gd', vim.lsp.buf.definition, { buffer = bufnr }) + -- Add other keymaps from your deleted nvim-java-config 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) +-- Start JDTLS +jdtls.start_or_attach(config)