Home > OS >  Standard flow of execution for a VS Code extension contributing a command
Standard flow of execution for a VS Code extension contributing a command

Time:12-19

Reading the official documentation for creating a VS Code extension contributing a command (see for instance: Extension Entry File, VS Code Api - commands etc) the sample given uses this pattern:

  1. the extension is activated at the invocation of the very command it should define
  2. it defines the code for the command there (in its activate() function)

For more clarity, I'm giving the sample code for the activate() function here:

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
  // Use the console to output diagnostic information (console.log) and errors (console.error)
  // This line of code will only be executed once when your extension is activated
  console.log('Congratulations, your extension "helloworld-sample" is now active!');

  // The command has been defined in the package.json file
  // Now provide the implementation of the command with registerCommand
  // The commandId parameter must match the command field in package.json
  let disposable = vscode.commands.registerCommand('helloworld.helloWorld', () => {
    // The code you place here will be executed every time your command is executed

    // Display a message box to the user
    vscode.window.showInformationMessage('Hello World!');
  });

  context.subscriptions.push(disposable);
}

Now, apart from the fact that this is not the pattern I would have expected from a mere "intuitive" point of view for an extension contributing commands (that one being, perhaps, "an extension defining new commands should be activated at the start of VS Code so that its commands are from there on available etc"), I have a couple of questions, which clearly are only requests for clarification since things are working this way and are even presented as "official":

  • how can the command's code run (the "hello world" message here), if the extension defines it at its very invocation? I assume VS Code is checking commands' handlers after dealing with all activate() functions from extensions;
  • in the comment before the function above, it is stated that the extension is activated the very first time the command is executed, but the doc for the onCommand Activation Event actually states that the extension is invoked any time the command is invoked; that statement would also be contrary to the pattern as I'm understanding it, which is that of "atomically" activating the extension/registering the command at each invocation;
  • I assume the disposable here unregisters the command, so that the handler is freshly associated with the command at each invocation (the documentation isn't clear about this);
  • is this overall pattern (activate the extension when the command is invoked, instantly register the command, then deactivate the extension and unregister the command) meant to improve on memory consumption (e.g. by invoking the extension only when a command is defined versus at the start of VS Code) or for other reasons?

Thanks for any clarification and sorry if I didn't understand the pattern correctly in the first place.

CodePudding user response:

Have you read the package.json file, there it states when the extension should be activated: activationEvents

On startup VSC puts a load_extension function handle in the command table for every extension command defined in package.json.

When you call the command for the first time, the load_extension function is called and the extension is loaded and the command table is updated with the actual command function handle. And the command function is called again, now with the correct function.

This is lazy extension activation, if you don't use the extension this session no need to some work.

  • Related