2

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I had read the Activation Events page (I also linked the onCommand event in my post), but the rest of details you provide is never mentioned nor alluded to in the documentation and can't be inferred by the code. I'll mark your answer as the correct one since it explains more clearly the inner workings, although some points remain obscure (such as the time the extension is unloaded after that first loading, whether right after command execution or on closing VS Code).
@atava Once the extension is activated for this session is stays activated unless you disable/uninstall the extension, The extension keeps local storage in the closures of the command functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.