mfussenegger / nvim-jdtls

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

"var cannot be resolve to a type" in maven generated projects #695

Closed Naadiyaar closed 2 weeks ago

Naadiyaar commented 2 weeks ago

LSP client configuration

local jdtls = require('jdtls')
local data_dir = vim.fn.stdpath('cache') .. '/nvim-jdtls' .. '/' ..  vim.fn.fnamemodify(vim.fn.getcwd(), ':p:h:t')

local jdtls_install = require('mason-registry')
    .get_package('jdtls')
    :get_install_path()

local java_agent = jdtls_install .. '/lombok.jar'

local launcher_jar = vim.fn.glob(jdtls_install .. '/plugins/org.eclipse.equinox.launcher_*.jar')

local platform_config = jdtls_install .. '/config_linux'

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',
    '-javaagent:' .. java_agent,
    '-Xmx4g',
    '--add-modules=ALL-SYSTEM',
    '--add-opens', 'java.base/java.util=ALL-UNNAMED',
    '--add-opens', 'java.base/java.lang=ALL-UNNAMED',
    '-jar', launcher_jar,
    '-configuration', platform_config,
    '-data', data_dir
  },

  root_dir = vim.fs.root(0, {'.git', 'mvnw', 'pom.xml', 'gradlew', 'build.gradle'}),

  -- 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
  settings = {
    java = {
      home = '/usr/lib/jvm/java-21-openjdk-21.0.4.0.7-2.fc40.x86_64',
      eclipse = {
        downloadSources = true,
      },
      configuration = {
        updateBuildConfiguration = "interactive",
        runtimes = {
          {
            name = "JavaSE-11",
            path = "/usr/lib/jvm/java-11-openjdk-11.0.24.0.8-2.fc40.x86_64",
          },
          {
            name = "JavaSE-17",
            path = "/usr/lib/jvm/java-17-openjdk-17.0.12.0.7-2.fc40.x86_64",
          },
          {
            name = "JavaSE-21",
            path = "/usr/lib/jvm/java-21-openjdk-21.0.4.0.7-2.fc40.x86_64",
          }
        }
      },
      maven = {
        downloadSources = true,
      },
      implementationsCodeLens = {
        enabled = true,
      },
      referencesCodeLens = {
        enabled = true,
      },
      references = {
        includeDecompiledSources = true,
      },
      signatureHelp = { enabled = true },
      format = {
        enabled = true,
        -- Formatting works by default, but you can refer to a specific file/URL if you choose
        -- settings = {
        --   url = "https://github.com/google/styleguide/blob/gh-pages/intellij-java-google-style.xml",
        --   profile = "GoogleStyle",
        -- },
      },
    },
    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.*",
      },
      importOrder = {
        "java",
        "javax",
        "com",
        "org"
      },
    },
    extendedClientCapabilities = jdtls.extendedClientCapabilities,
    sources = {
      organizeImports = {
        starThreshold = 9999,
        staticStarThreshold = 9999,
      },
    },
    codeGeneration = {
      toString = {
        template = "${object.className}{${member.name()}=${member.value}, ${otherMembers}}",
      },
      useBlocks = true,
    },
  },
  -- Needed for auto-completion with method signatures and placeholders
  capabilities = require('cmp_nvim_lsp').default_capabilities(),
  flags = {
    allow_incremental_sync = true,
  },
    --[[
  init_options = {
    -- References the bundles defined above to support Debugging and Unit Testing
    bundles = bundles
  },
    --]]
}

-- This starts a new client & server, or attaches to an existing client & server based on the `root_dir`.
jdtls.start_or_attach(config)

Eclipse.jdt.ls version

master

Steps to Reproduce

open java file type var following with name and value to be assigned

Expected Result

declare a new variable

Actual Result

LSP complains with error var cannot be resolve to a type Screenshot from 2024-09-21 23-24-40

Worth adding this isn't an issue in gradle projects but all maven projects have this problem.

mfussenegger commented 2 weeks ago

Sounds like the maven configuration doesn't define the language level, or you're missing the runtime entry for the particular java version that's in use in the project.

The reproduction steps don't reproduce

Naadiyaar commented 2 weeks ago

right, sorry for wrong issue. adding source and target to POM fixed it.

  <properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
  </properties>