Skip to content

Faster way to run application

sachin soman edited this page Feb 16, 2021 · 1 revision

Webview Auto refresh command addition

Better was to reload webview

Setup a hotkey.

So go to package.json and add a new command and add a new command to activationEvents

"activationEvents": [
		"onCommand:todo.helloWorld",
		"onCommand:todo.askQuestion",
		"onCommand:todo.welp",
		"onCommand:todo.refresh"     <------
	],

and in commands add this

{
				"command": "todo.refresh",
				"category": "todo",
				"title": "refresh"
}

and in extension.ts include the following command

context.subscriptions.push(

		vscode.commands.registerCommand("vstodo.refresh", () => {
			HelloWorldPanel.kill();
			HelloWorldPanel.createOrShow(context.extensionUri)
		})
	);

so here what we did was that we created a command which will kill our panel and recreate it when we do the refresh command.

Now do npm run watch.

And we can also make it open web tools as well. For that do cmd +shift +p then type devtools and press the cog on right this will open the window shown below. Now right click it to get command ID.

Screenshot_2021-02-15_at_21 53 00

Now to activate devtools via program we can do the executeCommad with the command ID we got. But createShow command doesn't execute immediately so we need to put a delay before we call executeCommmand for devtools

vscode.commands.executeCommand("workbench.action.webview.openDeveloperTools");

So the function refresh now looks like this.

context.subscriptions.push(
		vscode.commands.registerCommand("todo.refresh", () => {
			HelloWorldPanel.kill();
			HelloWorldPanel.createOrShow(context.extensionUri);
			setTimeout(() => {
				vscode.commands.executeCommand("workbench.action.webview.openDeveloperTools");
			}, 500);
		})
	);

but going again to command pallet is time consuming instead we can enable a hotkey. We can do that by again going to command pallet typing refresh command and clicking on cog, then click on hotkey and add the hotkey preferable to use option+r.

This is pretty cool.

Clone this wiki locally