eclipse-jdtls / eclipse.jdt.ls

Java language server
1.71k stars 384 forks source link

result of `textDocument/declaration` is [] #3166

Open chenzhf2019 opened 1 month ago

chenzhf2019 commented 1 month ago

I want to use eclipse.jdt.ls in my java automation program to help me analyze the code, mainly using the 'textDocument/declaration' function, but no matter how I try it will only return [], Although I have implemented the 'textDocument/definition' function, my ls seems to be limited to the current file at the moment, hope you can help me, I really need help, appreciate it.

java -Declipse.application=org.eclipse.jdt.ls.core.id1 -Dosgi.bundles.defaultStartLevel=4 -Declipse.product=org.eclipse.jdt.ls.core.product -Dlog.level=ALL -Xmx10G --add-modules=ALL-SYSTEM --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED  -jar C:\Users\czf\Desktop\jdt-ls\plugins\org.eclipse.equinox.launcher_1.6.800.v20240426-1701.jar -configuration C:\Users\czf\Desktop\jdt-ls\config_win -data C:\Users\czf\Desktop\workspace

image image image

image

rgrunber commented 1 month ago

I tried a similar thing as well and initially textDocument/declaration also returned []. It might be because textDocument/didOpen is required first (though I guess that might technically be considered a bug). Even with didOpen, the following requests never responded so I just need to debug and figure out what's going wrong.

rgrunber commented 1 month ago

I'm able to reproduce and I found the problem. Is the declaration reference in library code ?

You don't need didOpen, it will work without it. All you need to do is make sure to send classFileContentsSupport: true under the extendedClientCapabilities :

{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "initialize",
    "params": {
        "initializationOptions": {
            "workspaceFolders": [
                "file:///home/rgrunber/git/lemminx"
            ],
            "settings": {
                "java": {
                    "autobuild": {
                        "enabled": true
                    }
                }
            },
            "extendedClientCapabilities": {
                "classFileContentsSupport": true
            }
        }
    }
}

For example, I sent :

Content-Length: 255
{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"initializationOptions":{"workspaceFolders":["file:///home/rgrunber/git/lemminx"],"settings":{"java":{"autobuild":{"enabled":true}}},"extendedClientCapabilities":{"classFileContentsSupport": true}}}}

...
...server responds by importing project...
...

Content-Length: 246
{"jsonrpc":"2.0","id":3,"method":"textDocument/declaration","params":{"textDocument":{"uri":"file:///home/rgrunber/git/lemminx/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/XMLLanguageServer.java"},"position":{"line":112,"character":48}}}

and the response I got was :

formatted for legibility

Content-Length: 689
{
    "jsonrpc": "2.0",
    "id": 3,
    "result": [
        {
            "uri": "jdt://contents/org.eclipse.lsp4j-0.20.1.jar/org.eclipse.lsp4j.services/LanguageServer.class?=org.eclipse.lemminx/%5C/home%5C/rgrunber%5C/.m2%5C/repository%5C/org%5C/eclipse%5C/lsp4j%5C/org.eclipse.lsp4j%5C/0.20.1%5C/org.eclipse.lsp4j-0.20.1.jar=/maven.pomderived=/true=/=/maven.groupId=/org.eclipse.lsp4j=/=/maven.artifactId=/org.eclipse.lsp4j=/=/maven.version=/0.20.1=/=/maven.scope=/compile=/=/maven.pomderived=/true=/%3Corg.eclipse.lsp4j.services(LanguageServer.class",
            "range": {
                "start": {
                    "line": 49,
                    "character": 37
                },
                "end": {
                    "line": 49,
                    "character": 47
                }
            }
        }
    ]
}

Additional Note: You might not face this at all, but I did because I was sending data over the gnome-terminal (and because I tried getting didOpen to work). You need to verify that your client does not have a buffer limit. This is because the content of didOpen is quite large and for a large file, if you have a limit of 4096 bytes and your content will be truncated. I was able to use stty -icanon to work around this.