You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
vscode.commands.executeCommand doesn't wait for other executeCommand #10534
i want to develop a simple extension that formats the code then save the file.
my extension.js file has the following code
var vscode = require('vscode');
function activate(context) {
var disposable = vscode.commands.registerCommand('extension.sayHello', function () {
vscode.commands.executeCommand("editor.action.format").then(function () {
vscode.commands.executeCommand("workbench.action.files.save");
// i think workbench.action.files.save command doesn't wait for
// editor.action.format' command to complete
})
});
context.subscriptions.push(disposable);
}
exports.activate = activate;
function deactivate() {
}
exports.deactivate = deactivate;
the above code formats the code but doesn't save the file.
if i deliberately delays the workbench.action.files.save using a setTimeout this works
var disposable = vscode.commands.registerCommand('extension.sayHello', function () {
vscode.commands.executeCommand("editor.action.format").then(function () {
setTimeout(function(){
vscode.commands.executeCommand("workbench.action.files.save");
},300);
// this formats the code and saves the file
})
});
is this a bug in vscode.commands.executeCommand or am I doing something wrong
executeCommand returns a promise that signals command completion you must return that from your command (the function passed to registerCommand) to tell the editor that your command is still running. Also don't forget nested executeCommand-calls
thanks for replying but i have already tried that you have suggested . This doesn't work, it only formats the code but doesn't save the file after formatting
how to use vscode.executeFormatDocumentProvider nothing is documented.
so far i have done
var disposable = vscode.commands.registerCommand('extension.sayHello', function () {
var editor = vscode.window.activeTextEditor;
if(editor){
var uri = vscode.Uri.parse(editor.document.fileName);
return vscode.commands.executeCommand("vscode.executeFormatDocumentProvider",uri);
//missing argument formatting option nothing mentioned about it in documentation
}
});
Activity
jrieken commentedon Aug 15, 2016
executeCommandreturns a promise that signals command completion you must return that from your command (the function passed toregisterCommand) to tell the editor that your command is still running. Also don't forget nestedexecuteCommand-callsmanastunga787 commentedon Aug 15, 2016
could you please write down sample code to help me ??
jrieken commentedon Aug 15, 2016
jrieken commentedon Aug 15, 2016
RETURN in lower-case, written in upper-case for readability
manastunga787 commentedon Aug 15, 2016
thanks for replying but i have already tried that you have suggested . This doesn't work, it only formats the code but doesn't save the file after formatting
jrieken commentedon Aug 15, 2016
Unsure,
editor.action.formatis not an api command meaning we might change its semantics and behaviour anytime and without notice - the only promise we make is that it will format. You can use thevscode.executeFormatDocumentProvider(https://code.visualstudio.com/docs/extensionAPI/vscode-api-commands) to format a given document and then apply the text edits and save the document using real API methods (https://code.visualstudio.com/docs/extensionAPI/vscode-api)manastunga787 commentedon Aug 15, 2016
how to use
vscode.executeFormatDocumentProvidernothing is documented.so far i have done
manastunga787 commentedon Aug 15, 2016
getting an error illegal argument resource