TypeScript Newlines removed/incorrect code generated for getPasteEdits

jxct1oxe  于 6个月前  发布在  TypeScript
关注(0)|答案(3)|浏览(43)

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() {}
lmyy7pcs

lmyy7pcs1#

这不一定是特定于评论的。我认为一个相关的问题是:

function f() {

}

[|function g() {
    
}|]
  1. 选择给定的范围。
  2. 剪切范围的文本。
  3. 将范围的文本粘贴(请求粘贴编辑)
    这会产生以下文件:
function f() {

}
function g() {
    
}

请注意在 g 之前缺少换行符,以及在 g 之后多余的换行符。

e0uiprwp

e0uiprwp2#

请稍等,粘贴编辑是什么?我以前好像没遇到过这个词。

g9icjywg

g9icjywg3#

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 .

相关问题