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

Support rename operations #1250

Closed
albfan opened this issue Dec 31, 2021 · 10 comments
Closed

Support rename operations #1250

albfan opened this issue Dec 31, 2021 · 10 comments
Labels

Comments

@albfan
Copy link
Contributor

albfan commented Dec 31, 2021

I'm curious about this feature:

https://microsoft.github.io/language-server-protocol/specifications/specification-3-17/#resourceChanges

for java files that might change imports secions in other files, but many times, that affects buildsystem, like change a source in a Makefile or meson.build.

I'm unsure if a language server protocol can cross languages and affect other environment in a project like build system

If that sounds interesting I can work on implement it

@mattn
Copy link
Collaborator

mattn commented Dec 31, 2021

What language server can we try this?

@albfan
Copy link
Contributor Author

albfan commented Jan 2, 2022

Still not try it for real: As java class name and file name should match, I think eclipse jdt ls should be a valid example:

https://github.com/eclipse/eclipse.jdt.ls/blob/e9a2af5c2e319566cc79002005218e07cd5a9848/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/ChangeUtil.java#L172

To integrate with vim-lsp I found:

https://github.com/lgranie/vim-lsp-java

If I'm able to find a client that supports rename operations, will push here request/response communication.

@albfan
Copy link
Contributor Author

albfan commented Jan 4, 2022

So finally using vscode and jdtls I as able to see all the communication for a rename:

I create a maven project with:

mvn archetype:generate -B \
-DarchetypeGroupId=org.apache.maven.archetypes \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DarchetypeVersion=1.1 \
-DgroupId=org.albfan \
-DartifactId=Hello \
-Dversion=1.0-SNAPSHOT \
-Dpackage=org.albfan

And change the default src/main/java/org/albfan/App.java App class to Hello class. That causes a need of rename the file to src/main/java/org/albfan/Hello.java as public class names in java needs to match filename.

Project attached with history of changes
hello-maven-project.zip

Communication:

  • Position in App text to rename:
[Trace - 10:51:33] Sending request 'textDocument/prepareRename - (47)'.
Params: {
    "textDocument": {
        "uri": "file:///home/alberto/projects/java/Hello/src/main/java/org/albfan/App.java"
    },
    "position": {
        "line": 6,
        "character": 14
    }
}


[Trace - 10:51:33] Received response 'textDocument/prepareRename - (47)' in 7ms.
Result: {
    "start": {
        "line": 6,
        "character": 13
    },
    "end": {
        "line": 6,
        "character": 16
    }
}
  • Change class name to Hello:
[Trace - 10:51:38] Sending request 'textDocument/rename - (48)'.
Params: {
    "textDocument": {
        "uri": "file:///home/alberto/projects/java/Hello/src/main/java/org/albfan/App.java"
    },
    "position": {
        "line": 6,
        "character": 14
    },
    "newName": "Hello"
}


[Trace - 10:51:38] Received response 'textDocument/rename - (48)' in 166ms.
Result: {
    "changes": {},
    "documentChanges": [
        {
            "textDocument": {
                "version": null,
                "uri": "file:///home/alberto/projects/java/Hello/src/main/java/org/albfan/App.java"
            },
            "edits": [
                {
                    "range": {
                        "start": {
                            "line": 6,
                            "character": 13
                        },
                        "end": {
                            "line": 6,
                            "character": 16
                        }
                    },
                    "newText": "Hello"
                }
            ]
        },
        {
            "oldUri": "file:///home/alberto/projects/java/Hello/src/main/java/org/albfan/App.java",
            "newUri": "file:///home/alberto/projects/java/Hello/src/main/java/org/albfan/Hello.java",
            "kind": "rename"
        }
    ]
}


[Trace - 10:51:39] Sending notification 'textDocument/didChange'.
Params: {
    "textDocument": {
        "uri": "file:///home/alberto/projects/java/Hello/src/main/java/org/albfan/App.java",
        "version": 2
    },
    "contentChanges": [
        {
            "range": {
                "start": {
                    "line": 6,
                    "character": 13
                },
                "end": {
                    "line": 6,
                    "character": 16
                }
            },
            "rangeLength": 3,
            "text": "Hello"
        }
    ]
}

Notice, ls detects that rename needs a rename in file:

[Trace - 10:51:39] Sending request 'workspace/willRenameFiles - (49)'.
Params: {
    "files": [
        {
            "oldUri": "file:///home/alberto/projects/java/Hello/src/main/java/org/albfan/App.java",
            "newUri": "file:///home/alberto/projects/java/Hello/src/main/java/org/albfan/Hello.java"
        }
    ]
}
  • Omitting language report until 100%:

[Trace - 10:51:39] Received notification 'language/progressReport'.
Params: {
    "task": "Computing rename updates...",
    "status": "100% ",
    "totalWork": 1000,
    "workDone": 1000,
    "complete": true
}


[Trace - 10:51:39] Received response 'workspace/willRenameFiles - (49)' in 68ms.
No result returned.
  • ls communicates old file is closed and new is open:
[Trace - 10:51:39] Sending notification 'textDocument/didClose'.
Params: {
    "textDocument": {
        "uri": "file:///home/alberto/projects/java/Hello/src/main/java/org/albfan/App.java"
    }
}


[Trace - 10:51:39] Sending notification 'textDocument/didOpen'.
Params: {
    "textDocument": {
        "uri": "file:///home/alberto/projects/java/Hello/src/main/java/org/albfan/Hello.java",
        "languageId": "java",
        "version": 1,
        "text": "package org.albfan;\n\n/**\n * Hello world!\n *\n */\npublic class Hello \n{\n    public static void main( String[] args )\n    {\n        System.out.println( \"Hello World!\" );\n    }\n}\n"
    }
}

@albfan
Copy link
Contributor Author

albfan commented Jan 4, 2022

I did the opposite operation but this time for files: So I renamesrc/main/java/org/albfan/Hello.java to src/main/java/org/albfan/App.java, this time ls detects the file rename needs a text change (Hello to App in class name)

[Trace - 11:26:14] Sending request 'workspace/willRenameFiles - (75)'.
Params: {
    "files": [
        {
            "oldUri": "file:///home/alberto/projects/java/Hello/src/main/java/org/albfan/Hello.java",
            "newUri": "file:///home/alberto/projects/java/Hello/src/main/java/org/albfan/App.java"
        }
    ]
}


[Trace - 11:26:14] Received notification 'language/progressReport'.
Params: {
    "task": "Computing rename updates...",
    "status": "0% ",
    "totalWork": 1000,
    "workDone": 0,
    "complete": false
}


[Trace - 11:26:15] Received notification 'language/progressReport'.
Params: {
    "task": "Creating workspace modifications...",
    "subTask": "",
    "status": "100% ",
    "totalWork": 1000,
    "workDone": 1000,
    "complete": true
}

This triggers a document change:

[Trace - 11:26:15] Received response 'workspace/willRenameFiles - (75)' in 36ms.
Result: {
    "changes": {},
    "documentChanges": [
        {
            "textDocument": {
                "version": null,
                "uri": "file:///home/alberto/projects/java/Hello/src/main/java/org/albfan/Hello.java"
            },
            "edits": [
                {
                    "range": {
                        "start": {
                            "line": 6,
                            "character": 13
                        },
                        "end": {
                            "line": 6,
                            "character": 18
                        }
                    },
                    "newText": "App"
                }
            ]
        }
    ]
}

@albfan
Copy link
Contributor Author

albfan commented Jan 4, 2022

So as far as I understand, a rename file operation in general (if language does not need semantic changes due to file rename is:

{
  "id": 1 
  "jsonrpc": "2.0",
  "method": "workspace/willRenameFiles",
  "params": {
    "files": [
        {
            "oldUri": "file:///home/alberto/projects/java/Hello/src/main/java/org/albfan/Hello.java",
            "newUri": "file:///home/alberto/projects/java/Hello/src/main/java/org/albfan/App.java"
        }
    ]
  }
}

an option progress report can be received, and final response shows changes and possible document changes.

@prabirshrestha
Copy link
Owner

Seems like need support for this first. #1069.

@albfan
Copy link
Contributor Author

albfan commented Jan 8, 2022

Will try to collaborate in all those, thanks!

@stale
Copy link

stale bot commented Mar 13, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Mar 13, 2022
@stale stale bot closed this as completed Apr 16, 2022
@mattn
Copy link
Collaborator

mattn commented Apr 16, 2022

#1265 is merged.

@mattn mattn reopened this Apr 16, 2022
@stale stale bot removed the wontfix label Apr 16, 2022
@stale
Copy link

stale bot commented Jun 19, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jun 19, 2022
@stale stale bot closed this as completed Jul 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants