Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run an extension command on startup on VSCode?

I built a VSCode extension that consumes an external API. I need it to lunch a command to fetch some data every time a user opens VSCode.

I have read the documentation, but I don't find an answer.

Thank you for your time!

like image 201
Leandro Avatar asked May 11 '19 03:05

Leandro


People also ask

How do I Activate my extension when VSCode is opened?

On the extension's package.json you have the activationEvents setting. This setting establishes WHEN your extension will be activated. You can read about the different options in the official docs, but let me tell you that one of the options is the star operator *. Your extension will activate when VSCode is opened.

How do I run code in Visual Studio Code on macOS?

To do this, from an open terminal or command prompt, navigate to your project folder and type code .: Note: Users on macOS must first run a command ( Shell Command: Install 'code' command in PATH) to add VS Code executable to the PATH environment variable. Read the macOS setup guide for help.

How do I invoke a Visual Studio Code Extension?

Can be invoked using the Command Palette. Can be invoked using a keybinding. Can be invoked through the VS Code UI, such as through the editor title bar. Is intended as an API for other extensions to consume.

How do I programmatically execute a command in Visual Studio Code?

The vscode.commands.executeCommand API programmatically executes a command. This lets you use VS Code's built-in functionality, and build on extensions such as VS Code's built-in Git and Markdown extensions. The editor.action.addCommentLine command, for example, comments the currently selected lines in the active text editor:


2 Answers

I had a misconception about how an extension's life cycle worked. Finally, I've been able to solve my problem. Here is how:

On the extension's package.json you have the activationEvents setting. This setting establishes WHEN your extension will be activated. You can read about the different options in the official docs, but let me tell you that one of the options is the star operator *.

If you set this in you package.json:

"activationEvents": [
    "*"
],

Your extension will activate when VSCode is opened.

What happens when your extension is activated? It will fire the activate function, defined by default in the extension.ts/extension.js file.

There, you can put the code for, for example, launching a specific Command, or creating a specific Tree View.

Hope it helps.

like image 90
Leandro Avatar answered Sep 28 '22 07:09

Leandro


"activationEvents": [ "onStartupFinished" ]

This is much better then "*", it means the same thing besides it won't distract startup of your VS Code

like image 35
David Melkumyan Avatar answered Sep 28 '22 07:09

David Melkumyan