Community程式設計與開發github.com

LewisWigmore/Agent-Skills VS Code 擴充功能開發指南

從建立到發布 Marketplace 的 VS Code 擴充功能開發英文指南。涵蓋 Webview 模式、CSP 安全性、TreeView、測試、打包、AI 自訂與疑難排解。改編自 aktsmm/agent-skills,並針對 VS Code 1.74+ API 進行修正。

Agent-Skills VS Code 擴充功能開發指南 是什麼?

此技能提供一個全面的 VS Code 擴充功能開發指南,專為希望建立、開發及發布擴充功能的開發者而設計。它涵蓋了從專案初始化到最終發布的完整生命週期,包括使用 Yeoman 樣板產生器或手動設定的快速入門指引、專案結構最佳實踐、以及打包與發布流程。技能特別強調 VS Code 1.74 以上版本的自動啟用事件偵測機制,以簡化擴充功能的配置。此外,它還包含詳細的參考文件,分別說明 AI 自訂、程式碼審查提示、Webview、TreeView、測試、發布與疑難排解等主題。技能也提供了寶貴的最佳實踐,例如命名一致性(統一套件名稱、設定鍵、指令 ID 與檢視 ID)以及集中式通知處理的參考實作,協助開發者建立高品質且易於維護的擴充功能。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add lewiswigmore/agent-skills

Installed? Explore more 程式設計與開發 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

VS Code 擴充功能指南

建立、開發與發布 VS Code 擴充功能。

使用時機

  • VS Code 擴充功能、擴充功能開發、vscode 外掛
  • 從頭建立新的 VS Code 擴充功能
  • 為擴充功能新增指令、按鍵繫結或設定
  • 發布至 VS Code Marketplace

快速入門

# 建議使用:建立擴充功能樣板
npm install -g yo generator-code
yo code

# 或最小手動設定
mkdir my-extension && cd my-extension
npm init -y && npm install -D typescript @types/vscode

專案結構

my-extension/
├── package.json          # 擴充功能資訊清單(重要)
├── src/extension.ts      # 進入點
├── out/                  # 編譯後的 JS(加入 gitignore)
├── images/icon.png       # Marketplace 用的 128x128 PNG 圖示
└── .vscodeignore         # 從 VSIX 排除的檔案

建置與打包

npm run compile      # 編譯一次
npm run watch        # 監聽模式(按 F5 啟動除錯)
npx @vscode/vsce package   # 建立 .vsix

完成條件

  • 擴充功能啟動無錯誤
  • 所有指令已註冊且運作正常
  • 套件大小 < 5MB(使用 .vscodeignore)
  • README.md 包含 Marketplace/GitHub 連結

快速疑難排解

症狀解決方式
擴充功能未載入檢查 activationEvents(VS Code 1.74 起,系統會自動偵測貢獻的指令/檢視)
指令未找到確認 package.json 與程式碼中的指令 ID 一致
快捷鍵沒作用移除 when 子句,檢查按鍵衝突
主題參考資料
AI 自訂references/ai-customization.md
程式碼審查提示references/code-review-prompts.md
程式碼範例references/code-samples.md
TreeViewreferences/treeview.md
Webviewreferences/webview.md
測試references/testing.md
發布references/publishing.md
疑難排解references/troubleshooting.md

最佳實踐

命名一致性

在發布前統一套件名稱、設定鍵、指令 ID 與檢視 ID:

項目範例
套件名稱copilot-scheduler
設定鍵copilotScheduler.enabled
指令 IDcopilotScheduler.createTask
檢視 IDcopilotSchedulerTasks

集中式通知處理

type NotificationMode = "sound" | "silentToast" | "silentStatus";

function getNotificationMode(): NotificationMode {
  const config = vscode.workspace.getConfiguration("myExtension");
  return config.get<NotificationMode>("notificationMode", "sound");
}

function notifyInfo(message: string, timeoutMs = 4000): void {
  const mode = getNotificationMode();
  switch (mode) {
    case "silentStatus":
      vscode.window.setStatusBarMessage(message, timeoutMs);
      break;
    case "silentToast":
      void vscode.window.withProgress(
        { location: vscode.ProgressLocation.Notification, title: message },
        async () => {},
      );
      break;
    default:
      void vscode.window.showInformationMessage(message);
  }
}

function notifyError(message: string, timeoutMs = 6000): void {
  const mode = getNotificationMode();
  if (mode === "silentStatus") {
    vscode.window.setStatusBarMessage(`⚠ ${message}`, timeoutMs);
    console.error(message);
    return;
  }
  void vscode.window.showErrorMessage(message);
}

提供 notificationMode 設定,讓使用者控制通知的傳遞方式。

相關技能