AgentSkillsCN

vscode-extension-guide

从零开始创建 VS Code 扩展,并将其发布到 Marketplace 的指南。当您需要:(1) 创建新的 VS Code 扩展;(2) 向扩展中添加命令、快捷键或设置;(3) 发布到 VS Code Marketplace;(4) 排查扩展激活或打包过程中出现的问题;(5) 构建 TreeView 或 Webview 用户界面;(6) 设置扩展测试环境时,可使用此技能。

SKILL.md
--- frontmatter
name: vscode-extension-guide
description: "Guide for creating VS Code extensions from scratch to Marketplace publication. Use when: (1) Creating a new VS Code extension, (2) Adding commands, keybindings, or settings to an extension, (3) Publishing to VS Code Marketplace, (4) Troubleshooting extension activation or packaging issues, (5) Building TreeView or Webview UI, (6) Setting up extension tests."
license: Complete terms in LICENSE.txt

VS Code Extension Guide

Create, develop, and publish VS Code extensions.

Quick Start

bash
# Scaffold new extension (recommended)
npm install -g yo generator-code
yo code

# Or minimal manual setup
mkdir my-extension && cd my-extension
npm init -y
npm install -D typescript @types/vscode

Project Structure

code
my-extension/
├── package.json          # Extension manifest (CRITICAL)
├── src/extension.ts      # Entry point
├── out/                  # Compiled JS (gitignore)
├── images/icon.png       # 128x128 PNG for Marketplace
├── .vscodeignore         # Exclude files from VSIX
├── .gitignore            # Exclude out/, *.vsix, node_modules/
├── tsconfig.json
└── README.md

package.json Essentials

json
{
  "name": "my-extension",
  "displayName": "My Extension",
  "version": "0.1.0",
  "publisher": "your-publisher-id",
  "engines": { "vscode": "^1.80.0" },
  "activationEvents": ["onStartupFinished"],
  "main": "./out/extension.js",
  "contributes": {
    "commands": [
      {
        "command": "myExt.hello",
        "title": "Hello World",
        "category": "My Extension"
      }
    ]
  }
}

Critical: Without activationEvents, your extension won't load!

Extension Entry Point

typescript
// src/extension.ts
import * as vscode from "vscode";

export function activate(context: vscode.ExtensionContext): void {
  const disposable = vscode.commands.registerCommand("myExt.hello", () => {
    vscode.window.showInformationMessage("Hello!");
  });
  context.subscriptions.push(disposable);
}

export function deactivate(): void {}

Common Patterns

Keybindings

json
"contributes": {
  "keybindings": [{
    "command": "myExt.hello",
    "key": "ctrl+shift+h",
    "mac": "cmd+shift+h"
  }]
}

Avoid "when": "!inputFocus"—it disables shortcuts in editors.

Settings

json
"contributes": {
  "configuration": {
    "title": "My Extension",
    "properties": {
      "myExt.greeting": {
        "type": "string",
        "default": "Hello",
        "description": "Greeting message"
      }
    }
  }
}
typescript
const config = vscode.workspace.getConfiguration("myExt");
const greeting = config.get<string>("greeting", "Hello");

Quick Pick & Status Bar

typescript
// Quick selection dialog
const selected = await vscode.window.showQuickPick(["Option A", "Option B"], {
  placeHolder: "Select an option",
});

// Non-intrusive notification (auto-hide 2s)
vscode.window.setStatusBarMessage("$(check) Done!", 2000);

Building

bash
npm run compile      # Build once
npm run watch        # Watch mode
# Press F5 to launch Extension Development Host

Packaging

bash
npm install -g @vscode/vsce
npx @vscode/vsce package   # Creates .vsix
npx @vscode/vsce ls        # Preview package contents

.vscodeignore (minimize package size):

ignore
**
!package.json
!README.md
!LICENSE
!out/**
!images/icon.png

.gitignore:

ignore
out/
*.vsix
node_modules/
.vscode/

Advanced Features

For complex UI and functionality, see the reference guides:

Quick Troubleshooting

SymptomFix
Extension not loadingAdd activationEvents to package.json
Command not foundEnsure command ID matches in package.json/code
Shortcut not workingRemove when clause, check conflicts
README not on MarketplaceMust be README.md (lowercase)

See references/troubleshooting.md for full list.

README Template for Your Extension

Include these links in your extension's README:

  • Marketplace: https://marketplace.visualstudio.com/items?itemName=<publisher>.<extension>
  • GitHub Releases: https://github.com/<owner>/<repo>/releases
  • Repository: https://github.com/<owner>/<repo>