Extension API Overview
Relevant source files
The following files were used as context for generating this wiki page:
- extensions/vscode-api-tests/package.json
- extensions/markdown-language-features/package.json
- extensions/typescript-language-features/package.nls.json
- extensions/github-authentication/package.json
- extensions/npm/package.json
- extensions/markdown-math/package.json
- extensions/git-base/package.json
- extensions/gulp/package.json
- extensions/extension-editing/package.json
The Visual Studio Code Extension API allows developers to add new functionality and integrate external tools directly into the editor. The core of this integration is the package.json extension manifest file, which acts as a declarative entry point. This manifest defines the extension's capabilities, specifies how it gets activated, and lists all the contributions it makes to the VS Code UI and behavior, such as commands, settings, language support, and custom views.
Extensions are designed to be activated lazily to optimize performance. This is controlled by the activationEvents property in the manifest, which defines specific events that trigger the loading of the extension's code. This ensures that resources are only consumed when the extension's functionality is actually needed by the user. The API is extensive, covering everything from simple command registration to complex features like custom editors, notebook renderers, and authentication providers.
The Extension Manifest (package.json)
The package.json file is the heart of every VS Code extension. It contains essential metadata and defines the contract between the extension and the editor.
Core Properties
The manifest starts with standard Node.js package properties, along with some VS Code-specific ones.
| Property | Description | Example | Source |
|---|---|---|---|
name | The identifier for the extension. | markdown-language-features | markdown-language-features/package.json:2 |
displayName | The user-friendly name shown in the UI. | %displayName% (localized) | npm/package.json:4 |
description | A brief description of the extension's purpose. | %description% (localized) | github-authentication/package.json:4 |
publisher | The publisher's identifier. | vscode | git-base/package.json:5 |
version | The version of the extension. | 1.0.0 | markdown-math/package.json:4 |
engines.vscode | The VS Code version compatibility. | ^1.70.0 | markdown-language-features/package.json:10 |
main / browser | The entry point for the extension's code for Node.js and browser environments. | ./out/extension | markdown-language-features/package.json:12-13 |
activationEvents | An array of events that will activate the extension. | ["onLanguage:markdown", "onCommand:markdown.api.render"] | markdown-language-features/package.json:19-25 |
contributes | An object defining all contributions the extension makes to VS Code. | { "commands": [...], "configuration": [...] } | npm/package.json:71 |
Activation Events
Extensions are loaded on-demand to minimize resource usage. The activationEvents property is crucial for defining when an extension should be activated.
This diagram illustrates the extension activation flow.
Common activation events include:
onLanguage:{languageId}: Activates when a file of a specific language is opened.onCommand:{commandId}: Activates when a specific command is invoked.workspaceContains:{filename}: Activates when a folder is opened that contains a file matching the pattern.onTaskType:{taskType}: Activates when a task of a specific type is about to be executed.onWebviewPanel:{viewType}: Activates when a webview panel of a certain type becomes visible.*: Activates on VS Code startup (used for extensions that need to be always active, likegit-base).
Sources: markdown-language-features/package.json:19, npm/package.json:68, git-base/package.json:18
Contribution Points
The contributes section of package.json is where an extension declares its additions to VS Code.
Commands and Menus
Commands are the primary way for extensions to expose functionality. They can be invoked from the Command Palette, keybindings, or menus.
A command contribution includes a command identifier and a title.
{
"command": "npm.runScript",
"title": "%command.run%",
"icon": "$(run)"
}
Sources: npm/package.json:80-84
Commands are then surfaced in the UI by adding them to menus for specific contexts, such as the editor title bar, explorer context menu, or view title.
"menus": {
"view/title": [
{
"command": "npm.refresh",
"when": "view == npm",
"group": "navigation"
}
],
"explorer/context": [
{
"when": "config.npm.enableRunFromFolder && ...",
"command": "npm.runScriptFromFolder",
"group": "2_workspace"
}
]
}
Sources: npm/package.json:138-153
Configuration
Extensions can define configuration settings that users can modify. These are defined under the configuration property and appear in the Settings editor.
| Property | Description |
|---|---|
title | The category title for the settings. |
properties | An object where each key is a setting ID. |
type | The data type of the setting (e.g., string, boolean, object). |
default | The default value for the setting. |
description | A user-friendly description of the setting. |
scope | The scope of the setting (e.g., resource, window, application). |
enum | A list of allowed values for a string type setting. |
Example from the NPM extension:
"configuration": {
"id": "npm",
"type": "object",
"title": "Npm",
"properties": {
"npm.autoDetect": {
"type": "string",
"enum": [ "off", "on" ],
"default": "on",
"scope": "resource",
"description": "%config.npm.autoDetect%"
},
"npm.packageManager": {
"scope": "resource",
"type": "string",
"enum": [ "auto", "npm", "yarn", "pnpm", "bun" ],
"default": "auto",
"description": "%config.npm.packageManager%"
}
}
}
Sources: npm/package.json:155-180, typescript-language-features/package.nls.json:10-11
Language Features
Extensions can enhance the editing experience for various languages by contributing:
- Languages: Defines a new language ID and associates it with file extensions or names.
- Grammars: Provides a TextMate grammar (
.tmLanguage.json) for syntax highlighting. - JSON Validation: Associates a JSON schema with specific file patterns.
This diagram shows how language features are connected.
Authentication
The authentication contribution point allows extensions to provide authentication mechanisms for services like GitHub or Microsoft Azure. An extension registers an authentication provider with a label and an id.
"authentication": [
{
"label": "GitHub",
"id": "github"
}
]
Sources: github-authentication/package.json:29-35
This enables other extensions to request authentication sessions for the specified provider, creating a centralized and secure way to manage user credentials.
Task Definitions
For build systems and task runners, extensions can contribute taskDefinitions. This allows VS Code's task system to understand and auto-detect tasks from files like gulpfile.js or package.json scripts.
A task definition specifies a type and the properties required to identify a task.
"taskDefinitions": [
{
"type": "gulp",
"required": [ "task" ],
"properties": {
"task": {
"type": "string",
"description": "%gulp.taskDefinition.type.description%"
},
"file": {
"type": "string",
"description": "%gulp.taskDefinition.file.description%"
}
}
}
]
Sources: gulp/package.json:37-54
Views and Custom Editors
Extensions can contribute custom views to the side bar, panels, or create custom editors for specific file types.
- Views: The
viewscontribution point allows adding tree views or other custom UI to containers like the Explorer. - Custom Editors: The
customEditorspoint registers an editor for certain file patterns, giving full control over the document's presentation. - Notebooks & Renderers: The
notebookRenderercontribution allows extensions to render cells in notebooks, as seen with Markdown and KaTeX.
Sources: npm/package.json:74-80, markdown-language-features/package.json:302-312, markdown-math/package.json:61-68
API Proposals
For new and experimental features, VS Code uses an API proposal system. Extensions can opt-in to use these unstable APIs by listing them in the enabledApiProposals array in package.json. This allows for testing and feedback before an API is stabilized. The vscode-api-tests extension is a key example of this, enabling a wide range of proposals for testing purposes.
Example proposals enabled for testing:
chatProvidercustomEditorMovenotebookMessagingtunnelsworkspaceTrust
Sources: vscode-api-tests/package.json:7-46
Conclusion
The VS Code Extension API is a powerful and flexible system centered around the declarative package.json manifest. By defining metadata, activation events, and a wide array of contribution points, developers can seamlessly integrate new tools, languages, and workflows into the editor. This declarative-first approach, combined with lazy activation, ensures that VS Code remains fast and responsive while offering a rich and extensible ecosystem.