diff --git a/nvim/ftplugin/java.lua b/nvim/ftplugin/java.lua new file mode 100644 index 0000000..9aea2fa --- /dev/null +++ b/nvim/ftplugin/java.lua @@ -0,0 +1,96 @@ +local home = os.getenv("HOME") +local jdtls_path = home .. "/.local/share/nvim/mason/packages/jdtls" +local workspace_dir = home .. "/.cache/jdtls/" .. vim.fn.fnamemodify(vim.fn.getcwd(), ":p:h:t") +local equinox_path = vim.fn.glob(jdtls_path .. "/plugins/org.eclipse.equinox.launcher_*.jar") + +local on_attach = require('lsp-options').on_attach +local capabilities = require('lsp-options').capabilities + +local keymap = vim.keymap + +local opts = { silent = true } +-- ┌ ┐ +-- │ Java │ +-- └ ┘ +local java_on_attach = function(client, bufnr) + on_attach(client, bufnr) + + opts.desc = "Organize Imports" + keymap.set("n", "jo", "lua require('jdtls').organize_imports()", opts) + + opts.desc = "Extract variable" + keymap.set("n", "je", "lua require('jdtls').extract_variable()", opts) + + opts.desc = "Extract constant" + keymap.set("n", "je", "lua require('jdtls').extract_constant()", opts) + + opts.desc = "Test current method" + keymap.set("n", "jtm", "lua require('jdtls').test_nearest_method()", opts) + + opts.desc = "Test current class" + keymap.set("n", "jtc", "lua require('jdtls').test_class()", opts) +end + + +require("jdtls").start_or_attach({ + cmd = { + -- 💀 + "java", -- or '/path/to/java21_or_newer/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", + "-Xmx1g", + "--add-modules=ALL-SYSTEM", + "--add-opens", + "java.base/java.util=ALL-UNNAMED", + "--add-opens", + "java.base/java.lang=ALL-UNNAMED", + + -- 💀 + "-jar", + equinox_path, + + -- 💀 + "-configuration", + jdtls_path .. "/config_linux", + + -- 💀 + -- See `data directory configuration` section in the README + "-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 + -- + -- vim.fs.root requires Neovim 0.10. + -- If you're using an earlier version, use: require('jdtls.setup').find_root({'.git', 'mvnw', 'gradlew'}), + root_dir = vim.fs.root(0, { ".git", "mvnw", "gradlew", "build.xml" }), + + -- 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 = {}, + }, + + -- 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 = vim.split( + vim.fn.glob("$HOME/.local/share/nvim/mason/packages/java-*/extension/server/*.jar", true), + "\n" + ), + }, + on_attach = java_on_attach, + capabilities = capabilities, +})