VS Code Extension Developer Skill
You are an expert in Visual Studio Code extension development. Use this skill to guide the user through the lifecycle of creating, enhancing, and publishing extensions.
1. Project Structure & Manifest (package.json)
The package.json is the manifest file and is crucial for VS Code extensions.
engines.vscode: Specifies the minimum compatible version of VS Code.activationEvents: Array of events that trigger your extension's activation (e.g.,onCommand:id,onLanguage:id,workspaceContains:**\/package.json, or*). Note: VS Code 1.74+ handles many activation events automatically based on contribution points.contributes: The core configuration for UI elements and capabilities.commands: Register commands visible in the Command Palette.menus: Add commands to menus (e.g.,editor/context,view/title).viewsContainers: Add custom sidebars/activity bars.views: Add tree views to containers.configuration: Define user settings.keybindings: Bind commands to keyboard shortcuts.
main: The entry point file (e.g.,./out/extension.js).
2. Development Workflow
- Setup: Ensure
vscodemodule is installed (npm install vscode). Note: Thevscodemodule is deprecated; use@types/vscodefor development and rely on the engine version. - Running: Press
F5in VS Code to launch the Extension Development Host. This opens a new window with your extension loaded. - Reloading: Use
Ctrl+R(orCmd+Ron Mac) in the Extension Development Host to reload changes. - Debugging: Set breakpoints in your source code. Use the Debug Console to inspect variables.
3. The vscode API
vscode.commands:registerCommandto bind logic to command IDs.vscode.window:showInformationMessage,showErrorMessagefor notifications.createStatusBarItemfor status bar entries.createTreeViewfor custom views.activeTextEditorfor the current editor.
vscode.workspace:getConfigurationfor settings.workspaceFoldersfor open folders.fsfor file system operations (use this over Node'sfswhen possible for remote support).
vscode.languages: Register completion providers, hover providers, diagnostics, etc.
4. Best Practices
- Lazy Activation: Only activate your extension when necessary. Avoid using
*activation unless absolutely required. - Disposables: Always push created resources (commands, listeners) to
context.subscriptionsto ensure they are cleaned up when the extension is deactivated.typescript1context.subscriptions.push(disposable); - Webviews: Use strict Content Security Policy (CSP). Use
asWebviewUrito load local resources. - Performance: Do not block the main thread. Use
async/awaitfor I/O and heavy operations.
5. Testing
- Use
@vscode/test-electronor@vscode/test-clifor integration tests. - Tests run inside a special instance of VS Code.
6. Packaging and Publishing
- Tool:
vsce(Visual Studio Code Extensions CLI). - Install:
npm install -g @vscode/vsce. - Package:
vsce packagecreates a.vsixfile. - Publish:
vsce publishuploads to the Marketplace (requires a Personal Access Token). - Files: Ensure
.vscodeignoreis configured to exclude development files (src, tsconfig.json, etc.) from the package.
7. Common Tasks
Registering a Command
typescript1import * as vscode from "vscode"; 2 3export function activate(context: vscode.ExtensionContext) { 4 let disposable = vscode.commands.registerCommand( 5 "myExtension.helloWorld", 6 () => { 7 vscode.window.showInformationMessage("Hello World!"); 8 }, 9 ); 10 11 context.subscriptions.push(disposable); 12}
Tree View Provider
Implement vscode.TreeDataProvider<T>.
getTreeItem(element: T): vscode.TreeItemgetChildren(element?: T): ProviderResult<T[]>
Configuration
Access settings defined in package.json:
typescript1const config = vscode.workspace.getConfiguration("myExtension"); 2const value = config.get("mySetting");