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

Extension API TextEditorEdit.replace only works on primary selection? #5886

Closed
Inori opened this issue Apr 27, 2016 · 4 comments
Closed

Extension API TextEditorEdit.replace only works on primary selection? #5886

Inori opened this issue Apr 27, 2016 · 4 comments
Assignees
Labels
*question Issue represents a question, should be posted to StackOverflow (VS Code)

Comments

@Inori
Copy link
Contributor

Inori commented Apr 27, 2016

  • VSCode Version:1.0.0
  • OS Version:Win7x64

I'm trying to make an extension for vscode.
A function like this:

public insertNumbers() : void
    {
        let selections : vscode.Selection[] = this._textEditor.selections;

        for (var i = 0; i < selections.length; i++) 
        {
            let curSelection : vscode.Selection = selections[i];

            this._textEditor.edit
            (
                function (builder) 
                {
                    builder.replace(curSelection, "test");
                }
            )
        }  
    }

will only replace the text within the first selection:

bug

Even if I convert the 'Selection' to 'Range':

let curRange = new vscode.Range(curSelection.start, curSelection.end);

then use

builder.replace(curRange, "test");

the same thing will happen.

TextEditorEdit.insert doesn't work as expected either.

It seems that the official sample: MDTools has the same problem: #4 with similar code

From the Extension API reference, it says that the primary selection is always at index 0. So the replace api will only work with the primary selection? Then how to make a selection a primary one?
Or this is a bug of vscode extension module?

@SamVerschueren
Copy link
Contributor

I'm not sure if this is the reason why it fails, but you should not call edit for every selection. I suggest to do the looping inside the edit callback.

public insertNumbers(): void {
    const selections: vscode.Selection[] = this._textEditor.selections;

    this._textEditor.edit(builder => {
        for (const selection of selections) {
            builder.replace(selection, 'test');
        }
    });
}

@bpasero
Copy link
Member

bpasero commented Apr 27, 2016

@alexandrudima can you chime in?

@bpasero bpasero added the *question Issue represents a question, should be posted to StackOverflow (VS Code) label Apr 27, 2016
@Inori
Copy link
Contributor Author

Inori commented Apr 28, 2016

@SamVerschueren Wenderful! It is the reason! Thank you very much!
@Tyriar I think the official sample MDTools should also fix this bug. ha ha ha.

@seanmcbreen
Copy link

@Inori - good catch - I fixed up the MDtools example sorry about that.
@SamVerschueren - you were right on the money - embarrassing I remembered to do that in the general method but not in toUpper and toLower :)

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

No branches or pull requests

5 participants