redhat-developer / vscode-java

Java Language Support for Visual Studio Code
Eclipse Public License 2.0
2.07k stars 433 forks source link

Code action 'Create method...' does not work #3780

Closed loralei28 closed 4 days ago

loralei28 commented 5 days ago
Environment
Steps To Reproduce
  1. Create a new Java file, for example Test.java
  2. In Test.java file, invoke class and main code snippet to generate code
  3. Type bar();
  4. Click ‘Quick fix..’ in the java file or click 'Show fix' in the error window
  5. Select code action "Create method 'bar()'"
Current Result

”Create method 'bar()'“ does not work and will delete "bar()" 3

rgrunber commented 5 days ago

https://github.com/user-attachments/assets/dda5bdb4-67c2-4b34-b9aa-fcad49d4a91e

Seems to be working for me. @loralei28 , @testforstephen what am I missing here. Also, if you activate Java: Open Java Language Server Log File, are there any errors in that log ? Does the "Extension Host" logs (from the dropdown) of the Output view indicate any errors ?

Could definitely see it being a regression of https://github.com/eclipse-jdtls/eclipse.jdt.ls/pull/3229 though.

datho7561 commented 5 days ago

I reproduced this issue, it seems like it's Windows-specific

datho7561 commented 5 days ago

Here is a sample file:

package functions;

import io.quarkus.funqy.Funq;

/**
 * Your Function class
 */
public class Function {

    /**
     * Use the Quarkus Funqy extension for our function. This function simply echoes its input
     * @param input a Java bean
     * @return a Java bean
     */
    @Funq
    public Output function(Input input) {

        // Add business logic here
        bar();

        return null;
    }

}

Here is a trace of what the server returns for the quickfix:

[Trace - 10:45:24 AM] Received response 'codeAction/resolve - (119)' in 5ms.
Result: {
    "title": "Create method 'bar()'",
    "kind": "quickfix",
    "diagnostics": [
        {
            "range": {
                "start": {
                    "line": 18,
                    "character": 8
                },
                "end": {
                    "line": 18,
                    "character": 11
                }
            },
            "severity": 1,
            "code": "67108964",
            "source": "Java",
            "message": "The method bar() is undefined for the type Function"
        }
    ],
    "edit": {
        "changes": {},
        "documentChanges": [
            {
                "textDocument": {
                    "version": null,
                    "uri": "file:///C:/Users/david/Documents/Programming/TestProjects/quarkus-func/src/main/java/functions/Function.java"
                },
                "edits": [
                    {
                        "snippet": {
                            "value": "${1:bar}();\n\n        return null;\n    }\n\n    ${2|private,public,protected|} ${3:void} ${1:bar}() {\n        // TODO Auto-generated method stub\n        throw new UnsupportedOperationException(\"Unimplemented method 'bar'\");\n    }"
                        },
                        "range": {
                            "start": {
                                "line": 18,
                                "character": 8
                            },
                            "end": {
                                "line": 21,
                                "character": 5
                            }
                        },
                        "newText": ""
                    }
                ]
            }
        ]
    },
    "command": {
        "title": "refresh Diagnostics",
        "command": "java.project.refreshDiagnostics",
        "arguments": [
            "file:///c%3A/Users/david/Documents/Programming/TestProjects/quarkus-func/src/main/java/functions/Function.java",
            "thisFile",
            false,
            true
        ]
    }
}
rgrunber commented 5 days ago

I'm seeing the exact same response, only file URI is different (I'm on Linux). When you have time, i would try to validate whether

--- yours.txt   2024-09-26 11:17:54.751100419 -0400
+++ mine.txt    2024-09-26 11:23:02.727259008 -0400
@@ -1,4 +1,4 @@
-[Trace - 10:45:24 AM] Received response 'codeAction/resolve - (119)' in 5ms.
+[Trace - 11:22:36] Received response 'codeAction/resolve - (458)' in 39ms.
 Result: {
     "title": "Create method 'bar()'",
     "kind": "quickfix",
@@ -26,7 +26,7 @@
             {
                 "textDocument": {
                     "version": null,
-                    "uri": "file:///C:/Users/david/Documents/Programming/TestProjects/quarkus-func/src/main/java/functions/Function.java"
+                    "uri": "file:///home/rgrunber/sample-projects/my-quarkus-example/src/main/java/functions/Function.java"
                 },
                 "edits": [
                     {
@@ -53,7 +53,7 @@
         "title": "refresh Diagnostics",
         "command": "java.project.refreshDiagnostics",
         "arguments": [
-            "file:///c%3A/Users/david/Documents/Programming/TestProjects/quarkus-func/src/main/java/functions/Function.java",
+            "file:///home/rgrunber/sample-projects/my-quarkus-example/src/main/java/functions/Function.java",
             "thisFile",
             false,
             true

Definitely seems like something on the client side that's treated differently.

Update: Maybe the LSP text document URI is slightly different than the converted to VS Code text document URI ? https://github.com/redhat-developer/vscode-java/blob/29329d8b0d60fd7c99deaedd55408e45d080898a/src/extension.ts#L334 .

testforstephen commented 4 days ago

Looks like snippet text of "codeAction/resolve" response is not respected on Windows, and it still applies newText, which is "" and behaves as delete.

fp024 commented 4 days ago

Hello.

After upgrading the extension to version 1.35.0 in Windows 11 environment, Create local Variable '???' does not work properly.

However, there was no problem in Ubuntu 22.04 environment.

Windows 11 + Ext Ver. 1.35.0 (It became "")

https://github.com/user-attachments/assets/362dac89-a6e0-4e40-9c03-d2595178659c

Ubuntu 22.04 + Ext Ver. 1.35.0

https://github.com/user-attachments/assets/f97e8079-5c60-4487-b43c-3d8184919673

rgrunber commented 4 days ago

@testforstephen , can you try the PR and let me know if it resolves it for you ? It should prevent newText from being populated.

If someone could try out java-win32-x64-1.36.0-179.vsix (on windows) from https://github.com/rgrunber/vscode-java/releases/tag/v1.36.0 and let me know if that fixes things for them, that would help.

fp024 commented 4 days ago

In my Windows 11 environment, installing 1.36.0 did not solve the problem.

image

rgrunber commented 4 days ago

@testforstephen , I think we should just disable the feature ( snippetEditSupport in extendedClientCapabilities ) until there's some kind of fix upstream / we find a proper solution for windows. Could we do a bug-fix release for Monday ?

testforstephen commented 4 days ago

@testforstephen , I think we should just disable the feature ( snippetEditSupport in extendedClientCapabilities ) until there's some kind of fix upstream / we find a proper solution for windows. Could we do a bug-fix release for Monday ?

+1

rgrunber commented 1 day ago

I found the root cause on Windows. It's our code :

https://github.com/redhat-developer/vscode-java/blob/0149fc7323f1a9202dab94fa10eda020d883b459/src/extension.ts#L334

URI from the language server is : file:///C:/Users/user/projects/foo/Test.java URI after converting lsp.codeaction to a vscode.codeaction is : file:///C%3A/Users/user/projects/foo/Test.java

The conversion seems to be done by the asCodeAction conversion call that occurs higher up.

We can probably fix this by calling Uri.parse(..) on the initial Uri we retrieve from the language server.

Could someone try out https://github.com/rgrunber/vscode-java/releases/tag/v1.36.0 . It should contain a new vsix there that should work on Windows.

datho7561 commented 1 day ago

@rgrunber It seems to be working:

https://github.com/user-attachments/assets/7fbd98d7-8cf2-4574-9fb7-f3347b97efa5

There's a bug related to the indentation of the inserted content. At the end of the video I formatted the document manually and the content seems to be correct after that

rgrunber commented 1 day ago

There's a bug related to the indentation of the inserted content. At the end of the video I formatted the document manually and the content seems to be correct after that

This is https://github.com/microsoft/vscode/issues/63129

fp024 commented 1 day ago

The v1.35.1 release yesterday worked well in a Windows environment. Thank you for your support. 👍