eclipse-jdtls / eclipse.jdt.ls

Java language server
1.75k stars 389 forks source link

we can look up the definition, but not the reference #2498

Open a342108611 opened 1 year ago

a342108611 commented 1 year ago

image

We adopted a python client implementation, but found that we could look up the definition but could not find the reference, the same code, we tested the GO language and python language can be found, excuse me, is this a known problem? How can it be fixed?Thank you

rgrunber commented 1 year ago

We do support textDocument/references so it should work. Do you have a sample of what the request looks like that is sent for the textDocument/definition & textDocument/references request ? Even a sample project on which it fails would be helpful.

myname1111 commented 6 months ago

Hello, I too experience this issue. However, I can search for references within my own source code, but not that of 3rd part libraries (.jar files).

Here is an example showing that textDocument/definition works but not textDocument/references:

https://github.com/eclipse-jdtls/eclipse.jdt.ls/assets/75239954/49fbbefa-0229-4aee-8f6e-d52cba406529

I use Neovim with the Astronvim config. Here is my customized config for jdtls:

jdtls = function()
  -- use this function notation to build some variables
  local root_markers = { ".git", "mvnw", "gradlew", "pom.xml", "build.gradle" }
  local root_dir = require("jdtls.setup").find_root(root_markers)

  -- calculate workspace dir
  local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ":p:h:t")
  local workspace_dir = vim.fn.stdpath "data" .. "/site/java/workspace-root/" .. project_name
  os.execute("mkdir " .. workspace_dir)

  -- get the mason install path
  local install_path = require("mason-registry").get_package("jdtls"):get_install_path()

  -- get the current OS
  local os
  if vim.fn.has "macunix" then
    os = "mac"
  elseif vim.fn.has "win32" then
    os = "win"
  else
    os = "linux"
  end

  -- return the server config
  return {
    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:" .. install_path .. "/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(install_path .. "/plugins/org.eclipse.equinox.launcher_*.jar"),
      "-configuration",
      install_path .. "/config_" .. os,
      "-data",
      workspace_dir,
    },
    init_options = {
      extendedClientCapabilities = {
        classFileContentsSupport = true
      },
    },
    root_dir = root_dir,
  }
end

Any ideas on why this happens or how to fix it?

myname1111 commented 3 weeks ago

@a342108611 have you been able to fix this?

TomKrcmar commented 3 days ago

EDIT: My issue turned out to be that my -data directory containing the LSP indexes that JDTLS maintains were out of date. I deleted the index data and re-ingested the files, and it worked.

Old comment:

~I believe I'm experiencing this, too.~

With just these two files, I find all methods and fields in the declarations of Person.java. When I turn around and ask for references to each of them, I expect to see a reference of toStr() in Main.java but instead, I get no references to the method, but 4 incorrect references, 2 to each of the fields firstName and lastName in Main.java. It also seems to be missing references of getName() and getAge() called by toStr() within the same file.

The greyed out argument names in Main.java are just virtual text from intellisense in my editor, not actually in the file.

In the log below, the @x:y-z:w are the start and end of the range for that symbol. I use the start of the range as the location given to /references. Line numbers seem correct assuming zero-indexed. Also the log contains the URIs and ranges of the references returned from the language server.

image

LOG:  Find Symbols Test
LOG:  File: Main.java
LOG:      - Symbol: Main
LOG:          - Symbol: name
LOG:          - Symbol: main(String[])
LOG:  File: Person.java
LOG:      - Symbol: Person
LOG:          - Symbol: firstName
LOG:          - Symbol: lastName
LOG:          - Symbol: age
LOG:          - Symbol: Person(String, String, int)
LOG:          - Symbol: getName()
LOG:          - Symbol: getAge()
LOG:          - Symbol: toStr()
LOG:  Find References Test
LOG:  Sep 6, 2024, 5:03:15 PM Reconciled 2. Took 0 ms
LOG:      - References for Person.java#Person@1:6-1:12
LOG:      - References for Person.java#Person(String, String, int)@6:8-6:14
LOG:      - References for Main.java#Main@1:6-1:10
LOG:      - References for Person.java#toStr()@24:15-24:20
LOG:      - References for Main.java#main(String[])@4:13-4:17
LOG:      - References for Person.java#getAge()@20:15-20:21
LOG:      - References for Person.java#getName()@16:15-16:22
LOG:      - References for Person.java#firstName@2:8-2:17
LOG:          - file:///D:/projects/client/data/jproj/Main.java#@2:8-2:14
LOG:          - file:///D:/projects/client/data/jproj/Main.java#@4:18-4:24
LOG:      - References for Person.java#lastName@3:8-3:16
LOG:          - file:///D:/projects/client/data/jproj/Main.java#@2:8-2:14
LOG:          - file:///D:/projects/client/data/jproj/Main.java#@4:18-4:24
LOG:      - References for Person.java#age@4:5-4:8
LOG:      - References for Main.java#name@2:15-2:19