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

[Bug] The focus range of Pasted text is wrong #3155

Closed
jdneo opened this issue Jun 15, 2023 · 6 comments
Closed

[Bug] The focus range of Pasted text is wrong #3155

jdneo opened this issue Jun 15, 2023 · 6 comments

Comments

@jdneo
Copy link
Collaborator

jdneo commented Jun 15, 2023

[provide a description of the issue]

Environment

Version: 1.79.1 (user setup)
Commit: 4cb974a7aed77a74c7813bdccd99ee0d04901215
Date: 2023-06-12T16:14:05.102Z
Electron: 22.5.7
Chromium: 108.0.5359.215
Node.js: 16.17.1
V8: 10.8.168.25-electron.0
OS: Windows_NT x64 10.0.19045
Java extension version: 1.19.0

Steps To Reproduce

bug

Current Result

The selected range is wrong.

Expected Result

Should select whole logException

Additional Informations
@jdneo jdneo added the bug label Jun 15, 2023
@LuckyStarry
Copy link

image

每粘贴一次就出现一次。难受,非常难受。

@HerrDerb
Copy link

HerrDerb commented Jun 26, 2023

I don't think it should select the whole logException after paste. This way you keep overriding the pasted text.
Default behaviour should be, that the cursor jumps to the end of the pasted text and nothing beeing selected.

@HerrDerb
Copy link

sorry for bumping, but is anything happening here? It is really annoying, how it works at the moment.

@rgrunber
Copy link
Member

rgrunber commented Jul 25, 2023

I think this is related to our paste event handling, because if I disable it, the problem goes away. As a workaround, there's the setting editor.pasteAs.enabled to false. However, our paste event handling doesn't control offset/range for the content to be pasted. All we control is the text itself. It seems to be the language client that is making some odd requests that might be causing this :

package org.example;
public class Test {
    public void test() {
        String multiLineEscape = "";
        String notComment = "";
    }
}

I'm going to copy multiLineEscape and paste it onto notComment .

The resulting interaction is as follows :

[Trace - 14:30:57] Sending request 'workspace/executeCommand - (2899)'.
Params: {
    "command": "java.edit.handlePasteEvent",
    "arguments": [
        "{\"location\":{\"range\":{\"start\":{\"line\":4,\"character\":15},\"end\":{\"line\":4,\"character\":25}},\"uri\":\"file:///tmp/foo/org/example/Test.java\"},\"text\":\"multiLineEscape\",\"copiedDocumentUri\":\"file:///tmp/foo/org/example/Test.java\",\"formattingOptions\":{\"insertSpaces\":true,\"tabSize\":4}}"
    ]
}


[Info  - 14:30:57] Jul. 25, 2023, 2:30:57 p.m. >> workspace/executeCommand java.edit.handlePasteEvent
[Trace - 14:30:57] Received response 'workspace/executeCommand - (2899)' in 8ms.
Result: {
    "insertText": "multiLineEscape"
}


[Trace - 14:30:57] Sending notification 'textDocument/didChange'.
Params: {
    "textDocument": {
        "uri": "file:///tmp/foo/org/example/Test.java",
        "version": 378
    },
    "contentChanges": [
        {
            "range": {
                "start": {
                    "line": 4,
                    "character": 23
                },
                "end": {
                    "line": 4,
                    "character": 25
                }
            },
            "rangeLength": 2,
            "text": ""
        },
        {
            "range": {
                "start": {
                    "line": 4,
                    "character": 16
                },
                "end": {
                    "line": 4,
                    "character": 22
                }
            },
            "rangeLength": 6,
            "text": "eEscap"
        },
        {
            "range": {
                "start": {
                    "line": 4,
                    "character": 15
                },
                "end": {
                    "line": 4,
                    "character": 15
                }
            },
            "rangeLength": 0,
            "text": "multiLi"
        }
    ]
}

The handlePasteEvent request and response are correct. It matches replacing notComment with multiLineEscape. The problem seems to be after this is returned to provideDocumentPasteEdits, the client comes back with a set of 3 text edits, to transform the selected portion into the target text :

notComment -> notComme -> neEscape -> multiLineEscape . It almost looks like it's doing some kind of edit-distance-like minimization of text changes. As for the selection range, I'm not sure why it does the selection as I don't see us anywhere requesting that. It has to be a result of the way it's modifying the text.

paste-event-handler-selection-range-bug

From what I can tell, the problem also doesn't happen if the text being pasted shares no characters in common with the text being replaced (ie. fastest way is to replace every character)

@slinstaedt
Copy link

slinstaedt commented Sep 15, 2023

Same problem for:

  • VS Code Version: 1.82.2
  • Extension Version: Language Support for Java v1.22.1,
  • JDK Type & Version: OpenJDK Runtime Environment Homebrew (build 20.0.2)
  • OS Type & Version: MacOS 13.5.2 (22G91)

As @HerrDerb mentioned, I would also expect the cursor being at the end of the pasted text without anything being selected as with all text editors, I am aware of.

BTW pasting works this way for other language servers, I have tested. If you open a Java file in "language mode" text, copy&paste works as expected, so the problem must be caused by the Java LS.

@rgrunber
Copy link
Member

I just tried with VS Code 1.82.2 and Language Support for Java v1.26.0 (Jan 2024) and the problem has been gone since then. Here's what I see :

[Trace - 12:04:47] Sending request 'workspace/executeCommand - (173)'.
Params: {
    "command": "java.edit.handlePasteEvent",
    "arguments": [
        "{\"location\":{\"range\":{\"start\":{\"line\":4,\"character\":15},\"end\":{\"line\":4,\"character\":25}},\"uri\":\"file:///tmp/foo/org/example/Test.java\"},\"text\":\"multiLineEscape\",\"copiedDocumentUri\":\"file:///tmp/foo/org/example/Test.java\",\"formattingOptions\":{\"insertSpaces\":true,\"tabSize\":4}}"
    ]
}


[Trace - 12:04:47] Received response 'workspace/executeCommand - (173)' in 3ms.
No result returned.


[Trace - 12:04:47] Sending notification 'textDocument/didChange'.
Params: {
    "textDocument": {
        "uri": "file:///tmp/foo/org/example/Test.java",
        "version": 13
    },
    "contentChanges": [
        {
            "range": {
                "start": {
                    "line": 4,
                    "character": 15
                },
                "end": {
                    "line": 4,
                    "character": 25
                }
            },
            "rangeLength": 10,
            "text": "multiLineEscape"
        }
    ]
}
Screencast.from.2024-06-18.12-07-22.webm

This is due to us fixing our paste event handling implementation to delegate back to VS Code (by returning an empty result) when no relevant changed are made (eg. text to be pasted remains unchanged).

I'm marking this as closed, but feel free to mention if there's something I've missed here, or if someone is still running into this.

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

7 participants