Igor Simoes
Igor Simoes

Reputation: 21

Receiving Error: Invalid arguments while using TextEditorEdit.insert

I am writing an extension that connects to an API, downloads some text, creates a file, inserts the text to this file and opens on an editor.

For that I am using the code snippet below:

vscode.workspace.openTextDocument(file_uri).then(function(doc){
    vscode.window.showTextDocument(doc).then(function(editor){
        vscode.window.showInformationMessage(editor);
        console.log("Editor..."+editor);
        editor.edit(function(editBuilder){
            editBuilder.insert(0, template_payload);
            //myEditBuilder(editBuilder, template_payload);
        }).then(function(result){vscode.window.showInformationMessage(result);});
        //editor.edit(function(editBuilder){myEditBuilder(editBuilder, template_payload);});
    });
});

When I ran my code I receive the following error and stack trace:

rejected promise not handled within 1 second: Error: Invalid arguments
extensionHostProcess.js:730
stack trace: Error: Invalid arguments
    at new g (/Applications/Visual Studio Code 3.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:329:834)
    at new g (/Applications/Visual Studio Code 3.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:327:254)
    at h.insert (/Applications/Visual Studio Code 3.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:443:123)
    at /Users/igor.simoes/VSCode_extension/appdvelocitytemplateupdater/extension.js:166:18
    at f.edit (/Applications/Visual Studio Code 3.app/Contents/Resources/app/out/vs/workbench/services/extensions/node/extensionHostProcess.js:448:510)
    at /Users/igor.simoes/VSCode_extension/appdvelocitytemplateupdater/extension.js:165:12

On my code line 165 is my editor.edit() command, and it does not seem to be missing arguments.

Any help would be much appreciated.

Upvotes: 0

Views: 510

Answers (1)

Mark
Mark

Reputation: 182211

You have

editBuilder.insert(0, template_payload);

but editBuilder is a TextEditorEdit whose insert function is:

insert(location: Position, value: string): void

this your first argument was 0, that is not a Position which is a (line: number, character: number) so new Position(0,0) is what you wanted.

https://code.visualstudio.com/api/references/vscode-api#TextEditorEdit

Upvotes: 1

Related Questions