|
| 1 | +-- Java Language Server configuration. |
| 2 | +-- Locations: |
| 3 | +-- 'nvim/ftplugin/java.lua'. |
| 4 | +-- 'nvim/lang-servers/intellij-java-google-style.xml' |
| 5 | + |
| 6 | +-- local jdtls_ok, jdtls = pcall(require, 'jdtls') |
| 7 | +-- if not jdtls_ok then |
| 8 | +-- vim.notify 'JDTLS not found, install with `:LspInstall jdtls`' |
| 9 | +-- return |
| 10 | +-- end |
| 11 | + |
| 12 | +local home = os.getenv 'HOME' |
| 13 | +local jdtls = require 'jdtls' |
| 14 | +-- vim.notify 'Home: ' .. home |
| 15 | + |
| 16 | +-- See `:help vim.lsp.start_client` for an overview of the supported `config` options. |
| 17 | +-- local jdtls_path = vim.fn.stdpath 'data' .. '/mason/packages/jdtls' |
| 18 | +local jdtls_path = home .. '/.local/share/nvim/mason/packages/jdtls' |
| 19 | +local path_to_lsp_server = jdtls_path .. '/config_mac' |
| 20 | +local path_to_plugins = jdtls_path .. '/plugins/' |
| 21 | +local path_to_jar = path_to_plugins .. 'org.eclipse.equinox.launcher_1.6.900.v20240613-2009.jar' |
| 22 | +local lombok_path = jdtls_path .. '/lombok.jar' |
| 23 | +local styling = home .. '/.local/share/java/eclipse-java-google-style.xml' |
| 24 | +print(lombok_path) |
| 25 | + |
| 26 | +local root_markers = { '.git', 'mvnw', 'gradlew', 'pom.xml', 'build.gradle' } |
| 27 | +local root_dir = require('jdtls.setup').find_root(root_markers) |
| 28 | +if root_dir == '' then |
| 29 | + return |
| 30 | +end |
| 31 | + |
| 32 | +local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ':p:h:t') |
| 33 | +local workspace_dir = vim.fn.stdpath 'data' .. '/site/java/workspace-root/' .. project_name |
| 34 | +os.execute('mkdir ' .. workspace_dir) |
| 35 | + |
| 36 | +local java_21_home_dir = '/Library/Java/JavaVirtualMachines/openjdk.jdk/Contents/Home' |
| 37 | +local java_17_home_dir = '/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home' |
| 38 | +local java_11_home_dir = '/Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home' |
| 39 | + |
| 40 | +-- Main Config |
| 41 | +local config = { |
| 42 | + -- The command that starts the language server |
| 43 | + -- See: https://github.com/eclipse/eclipse.jdt.ls#running-from-the-command-line |
| 44 | + cmd = { |
| 45 | + java_21_home_dir .. '/bin/java', |
| 46 | + '-Declipse.application=org.eclipse.jdt.ls.core.id1', |
| 47 | + '-Dosgi.bundles.defaultStartLevel=4', |
| 48 | + '-Declipse.product=org.eclipse.jdt.ls.core.product', |
| 49 | + '-Dlog.protocol=true', |
| 50 | + '-Xmx1g', |
| 51 | + '-Dlog.level=ALL', |
| 52 | + '--add-modules=ALL-SYSTEM', |
| 53 | + '--add-opens', |
| 54 | + 'java.base/java.util=ALL-UNNAMED', |
| 55 | + '--add-opens', |
| 56 | + 'java.base/java.lang=ALL-UNNAMED', |
| 57 | + '-javaagent:' .. lombok_path, |
| 58 | + '-Xms1g', |
| 59 | + '--add-modules=ALL-SYSTEM', |
| 60 | + '--add-opens', |
| 61 | + 'java.base/java.util=ALL-UNNAMED', |
| 62 | + '--add-opens', |
| 63 | + 'java.base/java.lang=ALL-UNNAMED', |
| 64 | + |
| 65 | + '-jar', |
| 66 | + path_to_jar, |
| 67 | + '-configuration', |
| 68 | + path_to_lsp_server, |
| 69 | + '-data', |
| 70 | + workspace_dir, |
| 71 | + }, |
| 72 | + |
| 73 | + -- This is the default if not provided, you can remove it. Or adjust as needed. |
| 74 | + -- One dedicated LSP server & client will be started per unique root_dir |
| 75 | + root_dir = root_dir, |
| 76 | + |
| 77 | + settings = { |
| 78 | + java = { |
| 79 | + home = java_21_home_dir, |
| 80 | + eclipse = { |
| 81 | + downloadSources = true, |
| 82 | + }, |
| 83 | + configuration = { |
| 84 | + updateBuildConfiguration = 'interactive', |
| 85 | + runtimes = { |
| 86 | + { |
| 87 | + name = 'JavaSE-21', |
| 88 | + path = java_21_home_dir, |
| 89 | + }, |
| 90 | + { |
| 91 | + name = 'JavaSE-11', |
| 92 | + path = java_11_home_dir, |
| 93 | + }, |
| 94 | + { |
| 95 | + name = 'JavaSE-17', |
| 96 | + path = java_17_home_dir, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + maven = { |
| 101 | + downloadSources = true, |
| 102 | + }, |
| 103 | + implementationsCodeLens = { |
| 104 | + enabled = true, |
| 105 | + }, |
| 106 | + referencesCodeLens = { |
| 107 | + enabled = true, |
| 108 | + }, |
| 109 | + references = { |
| 110 | + includeDecompiledSources = true, |
| 111 | + }, |
| 112 | + format = { |
| 113 | + settings = { |
| 114 | + url = styling, |
| 115 | + profile = 'GoogleStyle', |
| 116 | + }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + signatureHelp = { enabled = true }, |
| 120 | + completion = { |
| 121 | + favoriteStaticMembers = { |
| 122 | + 'org.hamcrest.MatcherAssert.assertThat', |
| 123 | + 'org.hamcrest.Matchers.*', |
| 124 | + 'org.hamcrest.CoreMatchers.*', |
| 125 | + 'org.junit.jupiter.api.Assertions.*', |
| 126 | + 'java.util.Objects.requireNonNull', |
| 127 | + 'java.util.Objects.requireNonNullElse', |
| 128 | + 'org.mockito.Mockito.*', |
| 129 | + }, |
| 130 | + importOrder = { |
| 131 | + 'java', |
| 132 | + 'com', |
| 133 | + 'org', |
| 134 | + 'javax', |
| 135 | + 'jarkata', |
| 136 | + }, |
| 137 | + }, |
| 138 | + sources = { |
| 139 | + organizeImports = { |
| 140 | + starThreshold = 9999, |
| 141 | + staticStarThreshold = 9999, |
| 142 | + }, |
| 143 | + }, |
| 144 | + codeGeneration = { |
| 145 | + toString = { |
| 146 | + template = '${object.className}{${member.name()}=${member.value}, ${otherMembers}}', |
| 147 | + }, |
| 148 | + useBlocks = true, |
| 149 | + }, |
| 150 | + }, |
| 151 | + |
| 152 | + flags = { |
| 153 | + allow_incremental_sync = true, |
| 154 | + }, |
| 155 | + init_options = { |
| 156 | + bundles = { |
| 157 | + vim.fn.glob(lombok_path, 1), |
| 158 | + }, |
| 159 | + }, |
| 160 | +} |
| 161 | + |
| 162 | +-- config['on_attach'] = function(_, bufnr) |
| 163 | +-- require('keymaps').map_java_keys(bufnr) |
| 164 | +-- require('lsp_signature').on_attach({ |
| 165 | +-- bind = true, -- This is mandatory, otherwise border config won't get registered. |
| 166 | +-- floating_window_above_cur_line = false, |
| 167 | +-- padding = '', |
| 168 | +-- handler_opts = { |
| 169 | +-- border = 'rounded', |
| 170 | +-- }, |
| 171 | +-- }, bufnr) |
| 172 | +-- end |
| 173 | + |
| 174 | +-- This starts a new client & server, |
| 175 | +-- or attaches to an existing client & server depending on the `root_dir`. |
| 176 | +require('jdtls').start_or_attach(config) |
0 commit comments