mfussenegger / nvim-jdtls

Extensions for the built-in LSP support in Neovim for eclipse.jdt.ls
GNU General Public License v3.0
1.05k stars 61 forks source link

Problems occurred when invoking code from plug-in: "org.eclipse.jdt.ls.core". Index 1 out of bounds for length 1 #471

Closed PauloPintor closed 12 months ago

PauloPintor commented 1 year ago

LSP client configuration

local status, jdtls = pcall(require, "jdtls") if not status then return end


-- GENERAL SETTINGS

-- Determine OS local home = os.getenv("HOME") if vim.fn.has("mac") == 1 then WORKSPACE_PATH = home .. "/.cache/jdtls/workspace/" CONFIG = "mac" elseif vim.fn.has("unix") == 1 then WORKSPACE_PATH = home .. "/.cache/jdtls/workspace/" CONFIG = "linux" else print("Unsupported system") end

-- Find root of project local root_markers = { ".git", "mvnw", "gradlew", "pom.xml", "build.gradle" } local root_dir = require("jdtls.setup").find_root(root_markers)

local project_name = vim.fn.fnamemodify(root_dir, ":p:h:t")

local workspace_dir = WORKSPACE_PATH .. project_name

--Make sure DAP is activated by default JAVA_DAP_ACTIVE = true


-- Prepare JAR dependencies

--Debugging local bundles = { vim.fn.glob(home .. "/.config/nvim/jars/java-debug/com.microsoft.java.debug.plugin-*.jar", 1), }

--Testing for _, bundle in ipairs(vim.split(vim.fn.glob(home .. "/.config/nvim/jars/vscode-java-test/server/*.jar", 1), "\n")) do --These two jars are not bundles, therefore don't put them in the table if not vim.endswith(bundle, "com.microsoft.java.test.runner-jar-with-dependencies.jar") and not vim.endswith(bundle, "com.microsoft.java.test.runner.jar") then table.insert(bundles, bundle) end end

--Decompiler for _, bundle in ipairs(vim.split(vim.fn.glob(home .. "/.config/nvim/jars/vscode-java-decompiler/server/*.jar", 1), "\n")) do table.insert(bundles, bundle) end


-- Prepare on_attach and capabilities

-- Highlight symbol under cursor local function lsp_highlight(client, bufnr) if client.supports_method("textDocument/documentHighlight") then vim.api.nvim_create_augroup("lsp_document_highlight", { clear = false, }) vim.api.nvim_clear_autocmds({ buffer = bufnr, group = "lsp_document_highlight", }) vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, { group = "lsp_document_highlight", buffer = bufnr, callback = vim.lsp.buf.document_highlight, }) vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, { group = "lsp_document_highlight", buffer = bufnr, callback = vim.lsp.buf.clear_references, }) end end

local on_attach = function(client, bufnr) if client.name == "jdtls" then require("jdtls.setup").add_commands() require("jdtls").setup_dap({ hotcodereplace = "auto" }) require("jdtls.dap").setup_dap_main_class_configs() require("lsp_signature").on_attach({ bind = true, use_lspsaga = false, floating_window = true, fix_pos = true, hint_enable = false, hi_parameter = "Search", handler_opts = { border = "rounded", }, }) lsp_highlight(client, bufnr) end end

local capabilities = vim.lsp.protocol.make_client_capabilities() capabilities.textDocument.completion.completionItem.snippetSupport = true

local extendedClientCapabilities = jdtls.extendedClientCapabilities extendedClientCapabilities.resolveAdditionalTextEditsSupport = true


-- Server settings

-- 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.

    "-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:" .. home .. "/.local/share/nvim/mason/packages/jdtls/lombok.jar",
    "-Xms1g",
    "--add-modules=ALL-SYSTEM",
    "--add-opens",
    "java.base/java.util=ALL-UNNAMED",
    "--add-opens",
    "java.base/java.lang=ALL-UNNAMED",

    "-jar",
    vim.fn.glob(home .. "/.local/share/nvim/mason/packages/jdtls/plugins/org.eclipse.equinox.launcher_*.jar"),
    -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                       ^^^^^^^^^^^^^^
    -- Must point to the                                                     Change this to
    -- eclipse.jdt.ls installation                                           the actual version

    "-configuration",
    home .. "/.local/share/nvim/mason/packages/jdtls/config_" .. 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",
    workspace_dir,
},
-- 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
-- or https://github.com/redhat-developer/vscode-java#supported-vs-code-settings
-- for a list of options
settings = {
    java = {
        eclipse = {
            downloadSources = true,
        },
        configuration = {
            updateBuildConfiguration = "interactive",
            -- runtimes = {
            --  {
            --      name = "JavaSE-1.8",
            --      path = "/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home",
            --  },
            --  {
            --      name = "JavaSE-1.8",
            --      path = "/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home",
            --  },
            --  {
            --      name = "JavaSE-11",
            --      path = "/opt/homebrew/Cellar/openjdk@11/11.0.18/libexec/openjdk.jdk/Contents/Home",
            --  },
            --  {
            --      name = "JavaSE-19",
            --      path = "/opt/homebrew/Cellar/openjdk/19.0.2/libexec/openjdk.jdk/Contents/Home",
            --  },
            -- },
        },
        maven = {
            downloadSources = true,
        },
        implementationsCodeLens = {
            enabled = false, --Don't automatically show implementations
        },
        referencesCodeLens = {
            enabled = false, --Don't automatically show references
        },
        references = {
            includeDecompiledSources = true,
        },
        format = {
            enabled = false,
            -- settings = {
            --   profile = "asdf"
            -- }
        },
    },
},
signatureHelp = { enabled = true },
completion = {
    favoriteStaticMembers = {
        "org.hamcrest.MatcherAssert.assertThat",
        "org.hamcrest.Matchers.*",
        "org.hamcrest.CoreMatchers.*",
        "org.junit.jupiter.api.Assertions.*",
        "java.util.Objects.requireNonNull",
        "java.util.Objects.requireNonNullElse",
        "org.mockito.Mockito.*",
    },
},
contentProvider = { preferred = "fernflower" },
extendedClientCapabilities = extendedClientCapabilities,
sources = {
    organizeImports = {
        starThreshold = 9999,
        staticStarThreshold = 9999,
    },
},
codeGeneration = {
    toString = {
        template = "${object.className}{${member.name()}=${member.value}, ${otherMembers}}",
    },
    useBlocks = true,
},
flags = {
    allow_incremental_sync = true,
},
-- 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 = bundles,
    extendedClientCapabilities = extendedClientCapabilities,
},
on_attach = on_attach,
capabilities = capabilities,

}

vim.api.nvim_create_autocmd({ "BufWritePost" }, { pattern = { "*.java" }, callback = function() vim.lsp.codelens.refresh() 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)

--JDTLS commands vim.cmd( "command! -buffer -nargs=? -complete=custom,v:lua.require'jdtls'._complete_compile JdtCompile lua require('jdtls').compile()" ) vim.cmd( "command! -buffer -nargs=? -complete=custom,v:lua.require'jdtls'._complete_set_runtime JdtSetRuntime lua require('jdtls').set_runtime()" ) vim.cmd("command! -buffer JdtUpdateConfig lua require('jdtls').update_project_config()") vim.cmd("command! -buffer JdtJol lua require('jdtls').jol()") vim.cmd("command! -buffer JdtBytecode lua require('jdtls').javap()") vim.cmd("command! -buffer JdtJshell lua require('jdtls').jshell()")

Eclipse.jdt.ls version

1.22

Steps to Reproduce

When I just enter in a *.java file I get the error of: Could not resolve java executable: Index 1 out of bounds for length 1.

This happens because of this command: require("jdtls.dap").setup_dap_main_class_configs()

So when I try to debug it says: No configuration found for java. You need to add configs to dap.configurations.java (See :h dap-configuration)

If I try your command JdtRefreshDebugConfigs the editor says: E492: Not an editor command: JdtRefreshDebugConfigs

Expected Result

The result should be debugging a .java file

Actual Result

LspLog

[START][2023-04-23 10:55:49] LSP logging initiated [ERROR][2023-04-23 10:55:49] .../vim/lsp/rpc.lua:734 "rpc" "java" "stderr" "WARNING: Using incubator modules: jdk.incubator.foreign, jdk.incubator.vector\n" [WARN][2023-04-23 10:55:51] ...lsp/handlers.lua:137 "The language server jdtls triggers a registerCapability handler despite dynamicRegistration set to false. Report upstream, this warning is harmless" [ERROR][2023-04-23 10:55:51] ...lsp/handlers.lua:535 "23/04/2023, 10:55:51 Command _java.reloadBundles.command not supported on client" [ERROR][2023-04-23 10:55:54] ...lsp/handlers.lua:535 '23/04/2023, 10:55:54 Problems occurred when invoking code from plug-in: "org.eclipse.jdt.ls.core".\nIndex 1 out of bounds for length 1\njava.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1\n\tat java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)\n\tat java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)\n\tat java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)\n\tat java.base/java.util.Objects.checkIndex(Objects.java:359)\n\tat java.base/java.util.ArrayList.get(ArrayList.java:427)\n\tat com.microsoft.java.debug.plugin.internal.ResolveJavaExecutableHandler.resolveJavaExecutable(ResolveJavaExecutableHandler.java:52)\n\tat com.microsoft.java.debug.plugin.internal.JavaDebugDelegateCommandHandler.executeCommand(JavaDebugDelegateCommandHandler.java:92)\n\tat org.eclipse.jdt.ls.core.internal.handlers.WorkspaceExecuteCommandHandler$1.run(WorkspaceExecuteCommandHandler.java:230)\n\tat org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:45)\n\tat org.eclipse.jdt.ls.core.internal.handlers.WorkspaceExecuteCommandHandler.executeCommand(WorkspaceExecuteCommandHandler.java:220)\n\tat org.eclipse.jdt.ls.core.internal.handlers.JDTLanguageServer.lambda$4(JDTLanguageServer.java:584)\n\tat org.eclipse.jdt.ls.core.internal.BaseJDTLanguageServer.lambda$0(BaseJDTLanguageServer.java:87)\n\tat java.base/java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:646)\n\tat java.base/java.util.concurrent.CompletableFuture$Completion.exec(CompletableFuture.java:483)\n\tat java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373)\n\tat java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182)\n\tat java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655)\n\tat java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622)\n\tat java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165)\n' [ERROR][2023-04-23 10:55:54] ...lsp/handlers.lua:535 "23/04/2023, 10:55:54 Error in calling delegate command handler\nIndex 1 out of bounds for length 1\njava.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1\n\tat java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)\n\tat java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)\n\tat java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)\n\tat java.base/java.util.Objects.checkIndex(Objects.java:359)\n\tat java.base/java.util.ArrayList.get(ArrayList.java:427)\n\tat com.microsoft.java.debug.plugin.internal.ResolveJavaExecutableHandler.resolveJavaExecutable(ResolveJavaExecutableHandler.java:52)\n\tat com.microsoft.java.debug.plugin.internal.JavaDebugDelegateCommandHandler.executeCommand(JavaDebugDelegateCommandHandler.java:92)\n\tat org.eclipse.jdt.ls.core.internal.handlers.WorkspaceExecuteCommandHandler$1.run(WorkspaceExecuteCommandHandler.java:230)\n\tat org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:45)\n\tat org.eclipse.jdt.ls.core.internal.handlers.WorkspaceExecuteCommandHandler.executeCommand(WorkspaceExecuteCommandHandler.java:220)\n\tat org.eclipse.jdt.ls.core.internal.handlers.JDTLanguageServer.lambda$4(JDTLanguageServer.java:584)\n\tat org.eclipse.jdt.ls.core.internal.BaseJDTLanguageServer.lambda$0(BaseJDTLanguageServer.java:87)\n\tat java.base/java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:646)\n\tat java.base/java.util.concurrent.CompletableFuture$Completion.exec(CompletableFuture.java:483)\n\tat java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373)\n\tat java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182)\n\tat java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655)\n\tat java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622)\n\tat java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165)\n"

mfussenegger commented 1 year ago

Is this still an issue with recent eclipse.jdt.ls + java-debug & nvim-jdtls?

If so, could you share a project setup where it happens?

95ych commented 1 year ago

Checked with the master repo, yes the bug is reproduced with the above config. Checked on an individual java file.

carschandler commented 1 year ago

@PauloPintor Please wrap all of your logs and blocks of code in triple backticks on a new line on the top and bottom of each section. They are difficult to read as of now. Like this:

``` my code   with code formatting goes in here ```

becomes

my code
  with code formatting
goes in here

You should add "lua" after the top backticks for chunks of lua code and it will add syntax highlighting, i.e.:

```lua local audience = "world" print("Hello " .. audience) ```

local audience = "world"
print("Hello " .. audience)