|
| 1 | +local status, jdtls = pcall(require, "jdtls") |
| 2 | +if not status then |
| 3 | + return |
| 4 | +end |
| 5 | + |
| 6 | +-- Determine OS |
| 7 | +local home = os.getenv("HOME") |
| 8 | +if vim.fn.has("mac") == 1 then |
| 9 | + WORKSPACE_PATH = home .. "/workspace/" |
| 10 | + CONFIG = "mac" |
| 11 | +elseif vim.fn.has("unix") == 1 then |
| 12 | + WORKSPACE_PATH = home .. "/workspace/" |
| 13 | + CONFIG = "linux" |
| 14 | +else |
| 15 | + print("Unsupported system") |
| 16 | +end |
| 17 | + |
| 18 | +-- Find root of project |
| 19 | +local root_markers = { ".git", "mvnw", "gradlew", "pom.xml", "build.gradle" } |
| 20 | +local root_dir = require("jdtls.setup").find_root(root_markers) |
| 21 | +if root_dir == "" then |
| 22 | + return |
| 23 | +end |
| 24 | + |
| 25 | +local extendedClientCapabilities = jdtls.extendedClientCapabilities |
| 26 | +extendedClientCapabilities.resolveAdditionalTextEditsSupport = true |
| 27 | + |
| 28 | +local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ":p:h:t") |
| 29 | + |
| 30 | +local workspace_dir = WORKSPACE_PATH .. project_name |
| 31 | + |
| 32 | +JAVA_DAP_ACTIVE = true |
| 33 | + |
| 34 | +local bundles = { |
| 35 | + vim.fn.glob( |
| 36 | + home .. "/.config/nvim/java-debug/com.microsoft.java.debug.plugin/target/com.microsoft.java.debug.plugin-*.jar" |
| 37 | + ), |
| 38 | +} |
| 39 | + |
| 40 | +vim.list_extend(bundles, vim.split(vim.fn.glob(home .. "/.config/nvim/vscode-java-test/server/*.jar"), "\n")) |
| 41 | + |
| 42 | +-- See `:help vim.lsp.start_client` for an overview of the supported `config` options. |
| 43 | +local config = { |
| 44 | + -- The command that starts the language server |
| 45 | + -- See: https://github.com/eclipse/eclipse.jdt.ls#running-from-the-command-line |
| 46 | + cmd = { |
| 47 | + |
| 48 | + -- 💀 |
| 49 | + "java", -- or '/path/to/java11_or_newer/bin/java' |
| 50 | + -- depends on if `java` is in your $PATH env variable and if it points to the right version. |
| 51 | + |
| 52 | + "-Declipse.application=org.eclipse.jdt.ls.core.id1", |
| 53 | + "-Dosgi.bundles.defaultStartLevel=4", |
| 54 | + "-Declipse.product=org.eclipse.jdt.ls.core.product", |
| 55 | + "-Dlog.protocol=true", |
| 56 | + "-Dlog.level=ALL", |
| 57 | + "-javaagent:" .. home .. "/.local/share/nvim/lsp_servers/jdtls/lombok.jar", |
| 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 | + -- 💀 |
| 66 | + "-jar", |
| 67 | + vim.fn.glob(home .. "/.local/share/nvim/lsp_servers/jdtls/plugins/org.eclipse.equinox.launcher_*.jar"), |
| 68 | + -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^ |
| 69 | + -- Must point to the Change this to |
| 70 | + -- eclipse.jdt.ls installation the actual version |
| 71 | + |
| 72 | + -- 💀 |
| 73 | + "-configuration", |
| 74 | + home .. "/.local/share/nvim/lsp_servers/jdtls/config_" .. CONFIG, |
| 75 | + -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^ |
| 76 | + -- Must point to the Change to one of `linux`, `win` or `mac` |
| 77 | + -- eclipse.jdt.ls installation Depending on your system. |
| 78 | + |
| 79 | + -- 💀 |
| 80 | + -- See `data directory configuration` section in the README |
| 81 | + "-data", |
| 82 | + workspace_dir, |
| 83 | + }, |
| 84 | + |
| 85 | + on_attach = require("akash.lsp.attach").on_attach, |
| 86 | + capabilities = require("akash.lsp.attach").capabilities, |
| 87 | + |
| 88 | + -- 💀 |
| 89 | + -- This is the default if not provided, you can remove it. Or adjust as needed. |
| 90 | + -- One dedicated LSP server & client will be started per unique root_dir |
| 91 | + root_dir = root_dir, |
| 92 | + |
| 93 | + -- Here you can configure eclipse.jdt.ls specific settings |
| 94 | + -- See https://github.com/eclipse/eclipse.jdt.ls/wiki/Running-the-JAVA-LS-server-from-the-command-line#initialize-request |
| 95 | + -- or https://github.com/redhat-developer/vscode-java#supported-vs-code-settings |
| 96 | + -- for a list of options |
| 97 | + settings = { |
| 98 | + java = { |
| 99 | + eclipse = { |
| 100 | + downloadSources = true, |
| 101 | + }, |
| 102 | + configuration = { |
| 103 | + updateBuildConfiguration = "interactive", |
| 104 | + }, |
| 105 | + maven = { |
| 106 | + downloadSources = true, |
| 107 | + }, |
| 108 | + implementationsCodeLens = { |
| 109 | + enabled = true, |
| 110 | + }, |
| 111 | + referencesCodeLens = { |
| 112 | + enabled = true, |
| 113 | + }, |
| 114 | + references = { |
| 115 | + includeDecompiledSources = true, |
| 116 | + }, |
| 117 | + -- Set this to true to use jdtls as your formatter |
| 118 | + format = { |
| 119 | + enabled = false, |
| 120 | + }, |
| 121 | + }, |
| 122 | + signatureHelp = { enabled = true }, |
| 123 | + completion = { |
| 124 | + favoriteStaticMembers = { |
| 125 | + "org.hamcrest.MatcherAssert.assertThat", |
| 126 | + "org.hamcrest.Matchers.*", |
| 127 | + "org.hamcrest.CoreMatchers.*", |
| 128 | + "org.junit.jupiter.api.Assertions.*", |
| 129 | + "java.util.Objects.requireNonNull", |
| 130 | + "java.util.Objects.requireNonNullElse", |
| 131 | + "org.mockito.Mockito.*", |
| 132 | + }, |
| 133 | + }, |
| 134 | + contentProvider = { preferred = "fernflower" }, |
| 135 | + extendedClientCapabilities = extendedClientCapabilities, |
| 136 | + sources = { |
| 137 | + organizeImports = { |
| 138 | + starThreshold = 9999, |
| 139 | + staticStarThreshold = 9999, |
| 140 | + }, |
| 141 | + }, |
| 142 | + codeGeneration = { |
| 143 | + toString = { |
| 144 | + template = "${object.className}{${member.name()}=${member.value}, ${otherMembers}}", |
| 145 | + }, |
| 146 | + useBlocks = true, |
| 147 | + }, |
| 148 | + }, |
| 149 | + |
| 150 | + flags = { |
| 151 | + allow_incremental_sync = true, |
| 152 | + }, |
| 153 | + |
| 154 | + -- Language server `initializationOptions` |
| 155 | + -- You need to extend the `bundles` with paths to jar files |
| 156 | + -- if you want to use additional eclipse.jdt.ls plugins. |
| 157 | + -- |
| 158 | + -- See https://github.com/mfussenegger/nvim-jdtls#java-debug-installation |
| 159 | + -- |
| 160 | + -- If you don't plan on using the debugger or other eclipse.jdt.ls plugins you can remove this |
| 161 | + init_options = { |
| 162 | + -- bundles = {}, |
| 163 | + bundles = bundles, |
| 164 | + }, |
| 165 | +} |
| 166 | + |
| 167 | +vim.api.nvim_create_autocmd({ "BufWritePost" }, { |
| 168 | + pattern = { "*.java" }, |
| 169 | + callback = function() |
| 170 | + vim.lsp.codelens.refresh() |
| 171 | + end, |
| 172 | +}) |
| 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) |
| 177 | + |
| 178 | +vim.cmd( |
| 179 | + "command! -buffer -nargs=? -complete=custom,v:lua.require'jdtls'._complete_compile JdtCompile lua require('jdtls').compile(<f-args>)" |
| 180 | +) |
| 181 | +vim.cmd( |
| 182 | + "command! -buffer -nargs=? -complete=custom,v:lua.require'jdtls'._complete_set_runtime JdtSetRuntime lua require('jdtls').set_runtime(<f-args>)" |
| 183 | +) |
| 184 | +vim.cmd("command! -buffer JdtUpdateConfig lua require('jdtls').update_project_config()") |
| 185 | +vim.cmd("command! -buffer JdtBytecode lua require('jdtls').javap()") |
| 186 | + |
| 187 | +-- Shorten function name |
| 188 | +local keymap = vim.keymap.set |
| 189 | +-- Silent keymap option |
| 190 | +local opts = { silent = true } |
| 191 | + |
| 192 | +keymap("n", "<leader>jo", "<Cmd>lua require'jdtls'.organize_imports()<CR>", opts) |
| 193 | +keymap("n", "<leader>jv", "<Cmd>lua require('jdtls').extract_variable()<CR>", opts) |
| 194 | +keymap("n", "<leader>jc", "<Cmd>lua require('jdtls').extract_constant()<CR>", opts) |
| 195 | +keymap("n", "<leader>jt", "<Cmd>lua require'jdtls'.test_nearest_method()<CR>", opts) |
| 196 | +keymap("n", "<leader>jT", "<Cmd>lua require'jdtls'.test_class()<CR>", opts) |
| 197 | +keymap("n", "<leader>ju", "<Cmd>JdtUpdateConfig<CR>", opts) |
| 198 | + |
| 199 | +keymap("v", "<leader>jv", "<Esc><Cmd>lua require('jdtls').extract_variable(true)<CR>", opts) |
| 200 | +keymap("v", "<leader>jc", "<Esc><Cmd>lua require('jdtls').extract_constant(true)<CR>", opts) |
| 201 | +keymap("v", "<leader>jm", "<Esc><Cmd>lua require('jdtls').extract_method(true)<CR>", opts) |
0 commit comments