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

The first java file I open doesn't have codelens #491

Closed gmr458 closed 1 year ago

gmr458 commented 1 year ago

LSP client configuration

local ok, jdtls = pcall(require, "jdtls")
if not ok then
    vim.notify("jdtls could not be loaded")
    return
end

local ok_mason, mason_registry = pcall(require, "mason-registry")
if not ok_mason then
    vim.notify("mason-registry could not be loaded")
    return
end

local jdtls_path = mason_registry.get_package("jdtls"):get_install_path()

local operative_system
if vim.fn.has("linux") then
    operative_system = "linux"
elseif vim.fn.has("win32") then
    operative_system = "win"
elseif vim.fn.has("macunix") then
    operative_system = "mac"
end

local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ":p:h:t")
local workspace_dir = vim.loop.os_homedir() .. "/.cache/jdtls/workspace/" .. project_name

local extendedClientCapabilities = jdtls.extendedClientCapabilities
extendedClientCapabilities.resolveAdditionalTextEditsSupport = true

local java_test_path = mason_registry.get_package("java-test"):get_install_path()
local java_debug_adapter_path = mason_registry.get_package("java-debug-adapter"):get_install_path()
local bundles = {}
vim.list_extend(bundles, vim.split(vim.fn.glob(java_test_path .. "/extension/server/*.jar"), "\n"))
vim.list_extend(
    bundles,
    vim.split(vim.fn.glob(java_debug_adapter_path .. "/extension/server/com.microsoft.java.debug.plugin-*.jar"), "\n")
)

-- 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",
        "-Xmx1g",
        "--add-modules=ALL-SYSTEM",
        "--add-opens",
        "java.base/java.util=ALL-UNNAMED",
        "--add-opens",
        "java.base/java.lang=ALL-UNNAMED",
        "-javaagent:" .. jdtls_path .. "/lombok.jar",

        -- 💀
        "-jar",
        vim.fn.glob(jdtls_path .. "/plugins/org.eclipse.equinox.launcher_*.jar"),
        -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                           ^^^^^^^^^^^^^^
        -- Must point to the                                                         Change this to
        -- eclipse.jdt.ls installation                                               the actual version, with vim.fn.glob() is not necessary

        -- 💀
        "-configuration",
        jdtls_path .. "/config_" .. operative_system,
        -- ^^^^^^^^^^^^^^^^^^^                  ^^^^^^
        -- 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,
    },

    on_attach = require("config.lsp").on_attach,
    capabilities = require("config.lsp").get_capabilities(),

    -- 💀
    -- 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
    root_dir = require("jdtls.setup").find_root({ "build.xml", "pom.xml", "settings.gradle", "settings.gradle.kts" }),

    -- 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 = {
            eclipse = {
                downloadSources = true,
            },
            configuration = {
                updateBuildConfiguration = "interactive",
                -- runtimes = {
                --     {
                --         name = "JavaSE-11",
                --         path = "~/.sdkman/candidates/java/11.0.17-tem",
                --     },
                --     {
                --         name = "JavaSE-18",
                --         path = "~/.sdkman/candidates/java/18.0.2-sem",
                --     },
                -- },
            },
            maven = {
                downloadSources = true,
            },
            referencesCodeLens = {
                enabled = true,
            },
            references = {
                includeDecompiledSources = true,
            },
            inlayHints = {
                parameterNames = {
                    enabled = "all", -- literals, all, none
                },
            },
            format = {
                enabled = true,
            },
        },
        signatureHelp = { enabled = true },
        extendedClientCapabilities = extendedClientCapabilities,
    },

    -- 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 = {
        -- workspace = workspace_dir,
        bundles = bundles,
    },
}
-- This starts a new client & server,
-- or attaches to an existing client & server depending on the `root_dir`.
jdtls.start_or_attach(config)

local _, _ = pcall(vim.lsp.codelens.refresh)
vim.api.nvim_create_autocmd({ "BufWritePost" }, {
    pattern = { "*.java" },
    callback = function()
        local _, _ = pcall(vim.lsp.codelens.refresh)
    end,
})

Eclipse.jdt.ls version

1.23.0

Steps to Reproduce

Open a java file of a project, in the rest of the files codelens works, in the file that I open first it doesn't.

Expected Result

Codelens working on all java files.

Actual Result

lua require('jdtls').compile('full') output: Compile successful

Actual result: Codelens doesn't work on the first file I open in a project.

mfussenegger commented 1 year ago

You probably call vim.lsp.codelens.refresh too early before the project is loaded. This is not a nvim-jdtls issue. codelens are implemented in neovim core, and provided by the language server