I'm learning VSCode extension development and want to run a command I defined from within the extension.
Here is my code:
const vscode = require('vscode');
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
console.log('Congratulations, your extension "cli-runner-by-blueray453" is now active!');
const disposable = vscode.commands.registerCommand('extension.cli-runner-by-blueray453', function () {
vscode.window.showInformationMessage('Hello World from cli-runner-by-blueray453!');
});
context.subscriptions.push(disposable);
setTimeout(() => {
vscode.commands.executeCommand('extension.cli-runner-by-blueray453');
}, 3000);
}
function deactivate() {
console.log('CLI Runner extension deactivated.');
}
module.exports = {
activate,
deactivate
}
However, After 3 seconds, the command does not display the information message window.
What is the proper way to programmatically trigger a command and see its UI effects during development?
vscode.commands.registerCommandis the right way to deal with the commands. Why would you ever call yoursetTimeoutwithvscode.commands.executeCommandfromactivate?! What makes you thinking it makes any sense? Does your command work in the intended way (F1, and so on)?setTimeout. The purpose ofvscode.commands.executeCommandis to reuse command execution in other methods, for example, in the implementations of other commands. Not to call it from exportedactivatewhen the registration is not yet complete. Why? Why?!