Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

result of textDocument/declaration is [] #3166

Closed
chenzhf2019 opened this issue May 27, 2024 · 3 comments
Closed

result of textDocument/declaration is [] #3166

chenzhf2019 opened this issue May 27, 2024 · 3 comments

Comments

@chenzhf2019
Copy link

chenzhf2019 commented May 27, 2024

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

@chenzhf2019 chenzhf2019 changed the title result of textDocument/declaration is [] result of textDocument/declaration is [] May 27, 2024
@rgrunber
Copy link
Contributor

rgrunber commented May 29, 2024

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
Copy link
Contributor

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.

@rgrunber
Copy link
Contributor

Going to close this because I was able to reproduceth exact issue and was able to fix by supplying classFileContentsSupport: true in the extended client capabilities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants