Skip to content

Format selection (and thus format-on-paste) causes destructive edits #50076

Closed

Description

Issue Type: Bug

Create a simple template string in a Javascript or Typescript file as below

const TEST_STRING = `${TEST_VAR_1}, this is some text`; 

Copy any text and paste anywhere after the closing expression bracket "}". All text including the last "}" is deleted.

In my example I've copied the word "TEST_STRING " by double clicking on it. I've tried pasting just before the end back tick

const TEST_STRING = `${TEST_VAR_1; 

If you press undo the expected initial result is shown.

const TEST_STRING = `${TEST_VAR_1}, this is some textTEST_STRING`; 

VS Code version: Code 1.69.2 (3b889b090b5ad5793f524b5d1d39fda662b96a2a, 2022-07-18T16:12:52.460Z)
OS version: Windows_NT x64 10.0.19043
Restricted Mode: No

Extensions: none

Activity

  1. mjbvz commented on Jul 27, 2022

    @mjbvz
    Contributor

    Confirmed with ^4.8.0-dev.20220727. Requires editor.formatOnPaste to be enabled

    Here's the sequence of events for pasting abc:

    [Trace  - 23:33:55.294] <semantic> Sending request: updateOpen (72). Response expected: yes. Current queue length: 0
    Arguments: {
        "changedFiles": [
            {
                "fileName": "/Users/matb/projects/san/index.js",
                "textChanges": [
                    {
                        "newText": "abc",
                        "start": {
                            "line": 1,
                            "offset": 35
                        },
                        "end": {
                            "line": 1,
                            "offset": 35
                        }
                    }
                ]
            }
        ],
        "closedFiles": [],
        "openFiles": []
    }
    [Trace  - 23:33:55.294] <semantic> Response received: updateOpen (72). Request took 0 ms. Success: true 
    Result: true
    [Trace  - 23:33:55.295] <syntax> Response received: updateOpen (28). Request took 1 ms. Success: true 
    Result: true
    [Trace  - 23:33:55.295] <syntax> Sending request: format (29). Response expected: yes. Current queue length: 0
    Arguments: {
        "file": "/Users/matb/projects/san/index.js",
        "line": 1,
        "offset": 35,
        "endLine": 1,
        "endOffset": 38
    }
    [Trace  - 23:33:55.296] <syntax> Response received: format (29). Request took 2 ms. Success: true 
    Result: [
        {
            "start": {
                "line": 1,
                "offset": 34
            },
            "end": {
                "line": 1,
                "offset": 96
            },
            "newText": ""
        }
    ]
    
  2. mjbvz commented on Jul 27, 2022

    @mjbvz
    Contributor

    Seems very similar to #48006

  3. amcasey commented on Jul 27, 2022

    @amcasey
    Member

    Seems very similar to #48006

    +1. Nathan Shively-Sanders (@sandersn) ?

  4. DanielRosenwasser commented on Jul 27, 2022

    @DanielRosenwasser
    Member

    Simpler repro is

    const x = `${0}abc`;

    Select abc and run format selection.

    Note that:

    1. This repro must have a template string with a substitution
    2. The template tail literal must have some string contents.
    3. There MUST be a semicolon at the end of the current statement.
  5. DanielRosenwasser commented on Jul 27, 2022

    @DanielRosenwasser
    Member

    Another similar one

    foo(`${0} `)
    1. This repro must have a template string with a substitution.
    2. The template tail literal must have some string contents.
    3. The template must be in a call.
  6. changed the title [-]Pasting inside Javascript or Typescript template literal/string deletes content[/-] [+]Format selection (and thus format-on-paste) causes destructive edits[/+] on Jul 27, 2022
  7. DanielRosenwasser commented on Jul 28, 2022

    @DanielRosenwasser
    Member

    First appears on typescript@4.6.0-dev.20221130 which is hard to test because VS Code assumes 4.6.* can run with IPC. That means it's one of https://github.com/microsoft/TypeScript/pulls?q=is%3Apr+merged%3A2021-11-29+is%3Aclosed, and likely #46875.

    It's been in the product for over 8 months so I guess I feel weird labeling it "High Priority", but I think we should get it fixed for 4.8

  8. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptDomain: FormatterThe issue relates to the built-in formatterFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions