Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

visual-studio-code: Howto executeCommand<T>(command: string, ...rest: any[]): Thenable<T>

I try to figure out how to implement a visual studio code extension. (Based on the "Hello World!" example.)

I want to do the following:

  • Execute an example.cmd file in the editor in a node.js child_process (that works)
  • But prior to that, automatically save the file to activate the latest changes (thats the problem)

At https://code.visualstudio.com/Docs/extensionAPI/vscode-api there is description of visual studio code api.

This is the command I think to be used:

executeCommand<T>(command: string, ...rest: any[]): Thenable<T>

What I need:

The file save is asynchronous. E.g. there may show up a File Save Dialog. The user may save properly or cancel it. I need to wait til end of the user action, then in case of succeess call my command prompt.

In my code:

let disposable = vscode.commands.registerCommand('extension.sayHello', () => {
    ....

    var retval =
     vscode.commands.executeCommand('workbench.action.files.save')
     .then(function(result)
       {
        // It should wait til end of user action,
        // But it never reach here
        myOutputChannel.append('workbench.action.files.save:' + result + '\n');
       });

     // and immediately runs the child process code below
     ....
});

What happens: It runs through the code, don't wait, don't save, process the non existing file, reports an error, exit function. After all the File Save Dialog appears. :(

Can anyone give me a hint whats wrong? Is this a bug of visual studio code? Or what I am doing wrong? I am new to node.js . I guess, I didn't get how to use Thenable<T> properly?

like image 442
caribes42 Avatar asked Sep 28 '26 17:09

caribes42


1 Answers

Here's a short snippet on how to properly save the file:

let editor = vscode.window.activeTextEditor;
if (!editor) {
   return;
}

let doc = editor.document;
await editor.document.save();
like image 136
reflog Avatar answered Sep 30 '26 09:09

reflog