mfussenegger / nvim-jdtls

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

`jdtls` client only getting attached to first buffer #515

Closed devansh08 closed 1 year ago

devansh08 commented 1 year ago

LSP client configuration

local config = {
    cmd = {
        "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",
        "-Xms1g",
        "--add-modules=ALL-SYSTEM",
        "--add-opens",
        "java.base/java.util=ALL-UNNAMED",
        "--add-opens",
        "java.base/java.lang=ALL-UNNAMED",
        "-javaagent:" .. HOME .. "/.local/share/nvim/mason/packages/jdtls/lombok.jar",
        "-jar",
        vim.fn.glob(
            HOME
                .. "/.local/share/nvim/mason/packages/jdtls/plugins/org.eclipse.equinox.launcher_1.6.400.v20210924-0641.jar"
        ),
        "-configuration",
        HOME .. "/.local/share/nvim/mason/packages/jdtls/config_" .. OS_CONFIG,
        "-data",
        WORKSPACE_DIR,
    },
    root_dir = jdtls_setup.find_root({ ".git", "mvnw", "gradlew", "pom.xml", "build.gradle" }),
    capabilities = capabilities,
    settings = {
        java = {
            eclipse = {
                downloadSources = true,
            },
            configuration = {
                updateBuildConfiguration = "interactive",
                runtimes = {
                    {
                        name = "JavaSE-20",
                        path = JDK_PATH,
                    },
                },
            },
            maven = {
                downloadSources = true,
            },
            implementationsCodeLens = {
                enabled = true,
            },
            referencesCodeLens = {
                enabled = true,
            },
            references = {
                includeDecompiledSources = true,
            },
            inlayHints = {
                parameterNames = {
                    enabled = "all", -- literals, all, none
                },
            },
            format = {
                enabled = false,
            },
        },
        signatureHelp = { enabled = true },
        extendedClientCapabilities = extended_capabilities,
    },
    on_attach = function(client) --, bufnr)
        vim.lsp.codelens.refresh()
        -- require("jdtls").setup_dap({ hotcodereplace = "auto" })
        lsp_defaults.on_attach(client)
        -- local status_ok, jdtls_dap = pcall(require, "jdtls.dap")
        -- if status_ok then
        --  jdtls_dap.setup_dap_main_class_configs()
        -- end
    end,
}

jdtls.start_or_attach(config)

Eclipse.jdt.ls version

v1.24.0

Steps to Reproduce

After opening a maven project, the jdtls client gets attached to the first java file opened in a buffer (confirmed with :LspInfo). The existing LSP and additional features (organize_imports()) work well in that buffer. :LspInfo output of first buffer:

 Language client log: /home/devansh/.local/state/nvim/lsp.log
 Detected filetype:   java

 2 client(s) attached to this buffer: 

 Client: jdtls (id: 1, bufnr: [14])
    filetypes:       
    autostart:       false
    root directory:  /home/devansh/Projects/spring-boot-sample
    cmd:             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 -Xms1g --add-modules=ALL-SYSTEM --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED -javaagent:/home/devansh/.local/share/nvim/mason/packages/jdtls/lombok.jar -jar /home/devansh/.local/share/nvim/mason/packages/jdtls/plugins/org.eclipse.equinox.launcher_1.6.400.v20210924-0641.jar -configuration /home/devansh/.local/share/nvim/mason/packages/jdtls/config_linux -data /home/devansh/.local/share/nvim/jdtls-workspace/spring-boot-sample

 Client: null-ls (id: 2, bufnr: [14, 27])
    filetypes:       luau, lua, yaml, json, css, graphql, less, handlebars, jsonc, markdown.mdx, javascriptreact, javascript, vue, scss, typescript, html, typescriptreact, markdown, java
    autostart:       false
    root directory:  /home/devansh/Projects/spring-boot-sample
    cmd:             <function>

 Configured servers list: lua_ls, jsonls

But on trying to open another java file in a second buffer, the client does not get attached to it. None of the LSP features work here. Everything still works fine in the first buffer, even on closing the buffer and reopening that first file again. :LspInfo output of second buffer:

Language client log: /home/devansh/.local/state/nvim/lsp.log
 Detected filetype:   java

 1 client(s) attached to this buffer: 

 Client: null-ls (id: 2, bufnr: [14, 27])
    filetypes:       luau, lua, yaml, json, css, graphql, less, handlebars, jsonc, markdown.mdx, javascriptreact, javascript, vue, scss, typescript, html, typescriptreact, markdown, java
    autostart:       false
    root directory:  /home/devansh/Projects/spring-boot-sample
    cmd:             <function>

 1 active client(s) not attached to this buffer: 

 Client: jdtls (id: 1, bufnr: [14])
    filetypes:       
    autostart:       false
    root directory:  /home/devansh/Projects/spring-boot-sample
    cmd:             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 -Xms1g --add-modules=ALL-SYSTEM --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED -javaagent:/home/devansh/.local/share/nvim/mason/packages/jdtls/lombok.jar -jar /home/devansh/.local/share/nvim/mason/packages/jdtls/plugins/org.eclipse.equinox.launcher_1.6.400.v20210924-0641.jar -configuration /home/devansh/.local/share/nvim/mason/packages/jdtls/config_linux -data /home/devansh/.local/share/nvim/jdtls-workspace/spring-boot-sample

 Configured servers list: lua_ls, jsonls

Note: nvim-jdtls is setup using lazy.nvim with these options:

lazy = true,
ft = "java",

Expected Result

LSP features should work for any buffer created for a .java file.

Actual Result

Any buffer/file opened after the first does not attach the jdtls client and LSP features do not work in it.

Output of lsp.log for opening 2 buffers (first works perfectly, second doesnt work at all):

[START][2023-06-21 01:48:42] LSP logging initiated
[ERROR][2023-06-21 01:48:42] .../vim/lsp/rpc.lua:734    "rpc"   "java"  "stderr"        "WARNING: Using incubator modules: jdk.incubator.vector, jdk.incubator.concurrent\n"
[WARN][2023-06-21 01:48:46] ...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-06-21 01:48:46] ...lsp/handlers.lua:535    "Jun 21, 2023, 1:48:46 AM Command _java.reloadBundles.command not supported on client"
[START][2023-06-21 01:49:05] LSP logging initiated
[ERROR][2023-06-21 01:49:05] .../vim/lsp/rpc.lua:734    "rpc"   "java"  "stderr"        "WARNING: Using incubator modules: jdk.incubator.concurrent, jdk.incubator.vector\n"
[WARN][2023-06-21 01:49:09] ...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-06-21 01:49:09] ...lsp/handlers.lua:535    "Jun 21, 2023, 1:49:09 AM Command _java.reloadBundles.command not supported on client"

:lua require('jdtls').compile('full') - Outputs Compile successful without any errors.

mfussenegger commented 1 year ago

Make sure jdtls.start_or_attach is called on each buffer. That's why the readme/instructions tell you to put it into a java ftplugin / FileType autocmd.

devansh08 commented 1 year ago

Ah that worked. Did not realize this was the purpose of the ftplugin. Thanks!