Last updated: October 26, 2023 | Version: 2.1.0

Developing VS Code Extensions

Visual Studio Code (VS Code) is a powerful and flexible platform for building extensions that enhance its functionality. This guide will walk you through the essentials of creating your own VS Code extensions.

Why Create VS Code Extensions?

Extensions allow you to:

Getting Started with Extension Development

The easiest way to start is by using the VS Code Extension Generator. This Yeoman generator scaffolds a new extension project with a basic structure.

1. Install Node.js and npm

If you don't have Node.js and npm installed, download them from nodejs.org.

2. Install Yeoman and the VS Code Extension Generator

Open your terminal or command prompt and run:

npm install -g yo generator-code

3. Generate a New Extension Project

Navigate to the directory where you want to create your extension and run:

yo code

Follow the prompts to configure your new extension. You can choose the type of extension (e.g., TypeScript, JavaScript), its name, identifier, and description.

Understanding the Extension Structure

A typical VS Code extension project includes:

The package.json Manifest

This file is crucial for defining your extension. Key properties include:

The Extension Entry Point

The extension.ts (or extension.js) file contains the core logic. It exports an activate function, which is called when your extension is activated according to the activationEvents in package.json.

// src/extension.ts
import * as vscode from 'vscode';

export function activate(context: vscode.ExtensionContext) {
    console.log('Congratulations, your extension "my-sample-extension" is now active!');

    let disposable = vscode.commands.registerCommand('my-sample-extension.helloWorld', () => {
        vscode.window.showInformationMessage('Hello World from My Sample Extension!');
    });

    context.subscriptions.push(disposable);
}

export function deactivate() {}

Common Extension Contributions

Extensions can contribute various features using the contributes section in package.json:

Running and Debugging Your Extension

VS Code provides excellent support for developing and debugging extensions:

  1. Open your extension project in VS Code.
  2. Press F5 to launch a new VS Code window with your extension loaded. This is the Extension Development Host.
  3. In the Extension Development Host window, you can test your extension's functionality.
  4. Set breakpoints in your extension code and use the debugger in your main VS Code window to step through your code.
Tip: Use the VS Code API documentation and samples to explore the full range of possibilities. The Extension API is extensive and allows for deep integration with the editor.

Packaging and Publishing

Once your extension is ready, you can package it into a .vsix file and publish it to the Visual Studio Code Marketplace.

Resources

Category: Development Tools