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

Newlines removed/incorrect code generated for getPasteEdits #58784

Open
DanielRosenwasser opened this issue Jun 6, 2024 · 3 comments
Open

Newlines removed/incorrect code generated for getPasteEdits #58784

DanielRosenwasser opened this issue Jun 6, 2024 · 3 comments
Assignees
Labels
Bug A bug in TypeScript Needs Investigation This issue needs a team member to investigate its status.

Comments

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Jun 6, 2024

Imagine the following file:

// comment

function f() {}

When I press Ctrl+x on the line containing f, and paste it again, what VS Code does is it creates a buffer with the following contents:

// comment
and then sends **this request for paste edits**
 {
    "file": "d:\\scratch\\copypasta\\src\\file.ts",
    "pastedText": [
        "function f() {}\r\n"
    ],
    "pasteLocations": [
        {
            "start": {
                "line": 2,
                "offset": 1
            },
            "end": {
                "line": 2,
                "offset": 1
            }
        }
    ],
    "copiedFrom": {
        "file": "d:\\scratch\\copypasta\\src\\file.ts",
        "spans": [
            {
                "start": {
                    "line": 3,
                    "offset": 1
                },
                "end": {
                    "line": 3,
                    "offset": 16
                }
            }
        ]
    }
}
and receives **this response**.
{
    "edits": [
        {
            "fileName": "d:/scratch/copypasta/src/file.ts",
            "textChanges": [
                {
                    "start": {
                        "line": 1,
                        "offset": 15
                    },
                    "end": {
                        "line": 1,
                        "offset": 15
                    },
                    "newText": "function f() {}\r\n"
                }
            ]
        }
    ],
    "fixId": "providePostPasteEdits"
}

After the paste, the file no longer contains the original contents, and instead contains invalid text.

// commentfunction f() {}
@DanielRosenwasser
Copy link
Member Author

This is not necessarily specific to comments. I think a related issue is the following:

function f() {

}

[|function g() {
    
}|]
  1. Select the given range.
  2. Cut the text of the range.
  3. Paste the text of the range (requesting paste edits)

This generates the following files:

function f() {

}
function g() {
    
}

Note the missing newline before g and the extra newline after g.

@fatcerberus
Copy link

Wait, what are paste edits? I don’t think I’ve encountered that term before.

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Jun 6, 2024
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 5.6.0 milestone Jun 6, 2024
@DanielRosenwasser DanielRosenwasser added the Bug A bug in TypeScript label Jun 6, 2024
@DanielRosenwasser
Copy link
Member Author

Wait, what are paste edits? I don’t think I’ve encountered that term before.

It's basically a command for editors to request a "smarter" copy/paste that can do things like auto-imports. See #57262 and #50187.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

4 participants