I'm trying to write a simple extension for VS code that renames a selection to a given string. The app was bootstrapped with the extension generator: https://code.visualstudio.com/docs/extensions/example-hello-world#_generate-a-new-extension
For this I use this code:
const editor = vscode.window.activeTextEditor;
if (!editor) throw Error;
const position = editor.selection.active
const uri = editor.document.uri
vscode.commands.executeCommand("vscode.executeDocumentRenameProvider", uri, position, "donkey")
.then(edit => {
if (!edit) throw Error;
return vscode.workspace.applyEdit(edit);
});
The command is bound to a keybinding. I launch the debugger with F5(launching an instance of vs code for debugging like in the tutorial: https://code.visualstudio.com/docs/extensions/example-hello-world#_debugging-your-extension ). I then select a bunch of code in a file that I opened in that debugging instance and press my keybinding.
However, in the Debug Console I get "rejected promise not handled within 1 second". No error is thrown and since executeCommand is a Thenable, not a real Promise, I can't call catch() on it.
I tried to wrap the call in a try/catch block but with no success. When i try other to do other stuff, like showing a message with vscode.window.showInformationMessage or prompt the user for input it works and I don't see the error.
I also tried to do the same with a Typescript version of the extension, but I get the same behavior.
I don't see what I am doing wrong, is there something I am missing ?
Thenable.then
takes two arguments: a success continuation and a failure continuation. You can use the failure continuation to make sure rejections are properly handled:
vscode.commands.executeCommand("vscode.executeDocumentRenameProvider", uri, position, "donkey")
.then(edit => {
if (!edit) throw Error;
return vscode.workspace.applyEdit(edit);
})
.then(undefined, err => {
console.error('I am error');
})
This way, if executeCommand
, the previous then
, or applyEdit
fail, the rejection is properly handled
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With