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

replace using TextEditorEdit ends with part of the replaced text selected #32058

Closed
ramya-rao-a opened this issue Aug 6, 2017 · 2 comments
Closed
Assignees
Labels
api *question Issue represents a question, should be posted to StackOverflow (VS Code)
Milestone

Comments

@ramya-rao-a
Copy link
Contributor

Have an extension with a command that uses the TextEditorEdit to replace a line of text with a longer line of text.
For eg: the below replaces the first line with a long text.

export function activate(context: vscode.ExtensionContext) {
    context.subscriptions.push(vscode.commands.registerCommand('hello', () => {
        vscode.window.activeTextEditor.edit(editBuilder => {
            editBuilder.replace(new vscode.Range(0,0,1,0), 'This is a long text meant to replace something that was shorter.');
        })
    }));
}

Now place the cursor in the end of the first line of any file and run the above command
Say the first line had text that was 10 chars long.
After running the above command text from the 11th char to the end of the line will be selected.

edit

For extensions that use the TextEditorEdit this would result in text deletion if the user was to press enter after the edits were made

@alexdima
Copy link
Member

@ramya-rao-a

The cursor selection is a tracked range sitting on the text that gets changed via the edit API. There is an internal hidden flag, forceMoveMarkers which can be influenced by the methods being called on the text editor edit builder.

There are two options:
1a. set the position immediately after sending off the edit request:

  • the caveat is that this is slightly incorrect, as if the edit fails the cursor selection will be changed
        vscode.window.activeTextEditor.edit(editBuilder => {
            editBuilder.replace(new vscode.Range(0,0,1,0), 'This is a long text meant to replace something that was shorter.');
        });
        vscode.window.activeTextEditor.position = ...

1b. set the position once the edits are applied:

  • this can also be slightly incorrect, and might suffer from timing issues, though it is IMHO a bit better than 1a.
        vscode.window.activeTextEditor.edit(editBuilder => {
            editBuilder.replace(new vscode.Range(0,0,1,0), 'This is a long text meant to replace something that was shorter.');
        }).then(function() {
             vscode.window.activeTextEditor.position = ...
        });
  1. use delete + insert instead of replace to get forceMoveMarkers to be true.
       vscode.window.activeTextEditor.edit(editBuilder => {
           editBuilder.delete(new vscode.Range(0,0,0,123415));
           editBuilder.insert(new vscode.Position(0,123415), 'This is a long text meant to replace something that was shorter.');
       });

I would try 2, if that fails, then 1b, if that fails, then 1a.

If nothing works for your use-case we might consider adding a new API method on TextEditorEdit to precisely define the resulting selection(s), although 2 is IMHO preferable as the same file can be opened in multiple editors and it would be preferable for all the cursors (not just the one of the current active editor) to behave the same way.

@alexdima alexdima added *question Issue represents a question, should be posted to StackOverflow (VS Code) api labels Aug 14, 2017
@alexdima alexdima added this to the Backlog milestone Aug 14, 2017
@ramya-rao-a
Copy link
Contributor Author

In my case, I turned to use the WorkspaceEdit which worked fine for me.
Created this issue for anyone else facing the same issue.

You can close this, if what you said above is what we want to suggest to extension authors

@rebornix rebornix removed their assignment Aug 15, 2017
@vscodebot vscodebot bot locked and limited conversation to collaborators Dec 8, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api *question Issue represents a question, should be posted to StackOverflow (VS Code)
Projects
None yet
Development

No branches or pull requests

4 participants