Skip to content

vscode.commands.executeCommand doesn't wait for other executeCommand  #10534

Closed

Description

@manastunga787

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

Activity

  1. added
    *question
    on Aug 15, 2016
  2. jrieken commented on Aug 15, 2016

    @jrieken
    Contributor

    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

  3. manastunga787 commented on Aug 15, 2016

    @manastunga787
    Author

    could you please write down sample code to help me ??

  4. jrieken commented on Aug 15, 2016

    @jrieken
    Contributor
    function activate(context) {   
        var disposable = vscode.commands.registerCommand('extension.sayHello', function () {
            RETURN vscode.commands.executeCommand("editor.action.format").then(function () {
                 RETURN 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);
    }
    
  5. jrieken commented on Aug 15, 2016

    @jrieken
    Contributor

    RETURN in lower-case, written in upper-case for readability

  6. manastunga787 commented on Aug 15, 2016

    @manastunga787
    Author

    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

  7. jrieken commented on Aug 15, 2016

    @jrieken
    Contributor

    Unsure, editor.action.format is 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 the vscode.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)

  8. manastunga787 commented on Aug 15, 2016

    @manastunga787
    Author

    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
    
            }
    
        });
    
  9. manastunga787 commented on Aug 15, 2016

    @manastunga787
    Author

    getting an error illegal argument resource

  10. locked and limited conversation to collaborators on Nov 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    *question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions