Skip to content

API Calls with Undo Leave Cursor in Wrong Position #42783

@jpotterm

Description

@jpotterm

Environment

  • VSCode Version: 1.19.3
  • OS Version: macOS 10.13.3
  • Does this issue occur when all extensions are disabled?: Yes

Steps to Reproduce

Create an extension with the following activate function:

export async function activate(context: vscode.ExtensionContext): Promise<void> {
    const editor = vscode.window.activeTextEditor;

    if (!editor) return;

    await editor.edit(function(editBuilder) {
        editBuilder.delete(
            new vscode.Range(
                new vscode.Position(0, 0),
                new vscode.Position(0, 2),
            )
        );
    });

    await vscode.commands.executeCommand('undo');

    await editor.edit(function(editBuilder) {
        editBuilder.delete(
            new vscode.Range(
                new vscode.Position(0, 0),
                new vscode.Position(0, 1),
            )
        );
    });
}

And run it on a text file containing:

|ab

Where | denotes the cursor position. I would expect the result to be |b but the cursor ends up in the wrong position: b|.

Here are the expected results for each of the three steps that the code does:

  1. The first edit should and does give |
  2. Then the undo should and does give |ab
  3. Then the second edit should give |b, but does give b|

Another peculiarity that may be related is that if I undo after running that code I get a TextEditorSelectionChangeEvent with kind: keyboard even though I haven't moved the cursor. That doesn't happen with similar code that leaves the cursor in the expected position.

Metadata

Metadata

Assignees

Labels

bugIssue identified by VS Code Team member as probable bugeditor-coreEditor basic functionalityverifiedVerification succeeded

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions