VS Code 扩展 API 基础 — 速查手册
本参考随课程推进持续更新。当前覆盖:Lesson 0001 涉及的概念及后续问答补充。
package.json 扩展专有字段
| 字段 | 类型 | 说明 |
| publisher | string | Marketplace 发布者 ID,必须与注册时一致 |
| engines.vscode | string | 最低 VS Code 版本要求(SemVer),如 "^1.74.0" |
| activationEvents | string[] | 触发扩展激活的事件列表 |
| main | string | 扩展入口 JS 文件路径(编译产物) |
| contributes | object | 向 VS Code 贡献的功能(命令、配置、菜单等) |
| icon | string | 扩展图标路径(128×128 PNG 推荐) |
| categories | string[] | Marketplace 分类,如 ["Other", "Visualization"] |
| keywords | string[] | Marketplace 搜索关键词 |
SemVer 版本范围语法
"engines"、"dependencies"、"devDependencies" 中的版本号都使用 npm semver range 语法。最常用的是 ^(caret)和 ~(tilde)。
| 写法 | 含义 | 实际覆盖范围 |
1.74.0 | 精确匹配 | 只取 1.74.0 |
^1.74.0 | 锁定 major,兼容 minor/patch | >=1.74.0 <2.0.0 |
~1.74.0 | 锁定 major.minor,兼容 patch | >=1.74.0 <1.75.0 |
>=1.74.0 | 无上限 | 包括 2.0、3.0…… |
1.74.x 或 1.74.* | 通配 patch | >=1.74.0 <1.75.0 |
* 或 x | 任意版本 | 所有版本 |
^ 的规则:锁定最左边非零段
^ 不是简单的"锁定 major"——它锁定的是最左边非零数字所在的段:
| 写法 | 覆盖范围 | 为什么 |
^1.74.0 | >=1.74.0 <2.0.0 | 最左非零是 1(major),锁定它 |
^0.74.0 | >=0.74.0 <0.75.0 | 最左非零是 74(minor),锁定它 |
^0.0.3 | >=0.0.3 <0.0.4 | 最左非零是 3(patch),锁定它 |
为什么 0.x 特别?semver 规定 0.y.z 阶段属于"初始开发",任何 minor 变化都可能是不兼容的。所以 ^0.74.0 只覆盖 0.74.z,不覆盖 0.75.0。
激活事件速查
| 事件 | 触发时机 | 适用场景 |
| * | VS Code 启动时 | 几乎不用(拖慢启动) |
| onStartupFinished | 启动完成后 | 需要在 UI 中始终可见的扩展 |
| onCommand:xxx.yyy | 首次执行命令时 | 大多数扩展(延迟加载) |
| onLanguage:python | 打开特定语言文件时 | 语言工具扩展 |
| onView:viewId | 视图变为可见时 | 侧边栏视图扩展 |
| workspaceContains:pattern | 工作区包含匹配文件时 | 框架特定工具 |
contributes.configuration — 用户配置项
扩展通过 contributes.configuration 向 VS Code 的 settings.json 注入配置项。数据流分三步:扩展声明 → 用户覆盖 → 代码读取。
配置项属性速查
| 属性 | 类型 | 必需 | 说明 |
type | string | 是 | 值类型:"boolean" / "string" / "number" / "object" / "array" |
default | any | 是 | 用户未设置时的默认值 |
description | string | 是 | 纯文本描述,显示在设置面板中 |
markdownDescription | string | 否 | Markdown 格式描述(支持链接、代码块)。与 description 二选一 |
enum | string[] | 否 | 限定可选值,强制下拉选择 |
minimum / maximum | number | 否 | 数值范围约束 |
scope | string | 否 | "window"(默认)/ "resource" / "machine" 等,控制配置同步范围 |
order | number | 否 | 在设置面板中的排序权重(越小越靠前) |
类型决定 UI
type | 设置面板中的 UI | 示例 |
boolean | 复选框 | "claudeContextBar.autoColor": true |
string | 文本框;有 enum 时变为下拉框 | "claudeContextBar.baseColor": "White" |
number | 数字输入框;有 minimum/maximum 时带范围校验 | "claudeContextBar.idleTimeout": 180 |
object | JSON 编辑器 | "claudeContextBar.shortNames": {} |
代码中读取配置
import * as vscode from 'vscode';
// getConfiguration 的参数是配置前缀(package.json 中 properties 的公共前缀)
const config = vscode.workspace.getConfiguration('claudeContextBar');
// 读取单个值——如果用户没设置,返回 default
const threshold = config.get<number>('warningThreshold'); // → 50(默认值)或 60(用户改过)
const color = config.get<string>('baseColor'); // → "White"
const shortNames = config.get<object>('shortNames'); // → {}
// 泛型参数不是必需的,但推荐写上以获得类型提示
const autoColor = config.get('autoColor'); // 也可以
监听配置变化
// 用户在 settings.json 中修改时,扩展可以实时响应
vscode.workspace.onDidChangeConfiguration(event => {
if (event.affectsConfiguration('claudeContextBar.warningThreshold')) {
// 重新读取并更新 UI
const newThreshold = vscode.workspace
.getConfiguration('claudeContextBar')
.get<number>('warningThreshold');
updateStatusBar(newThreshold);
}
});
编译与发布脚本
| 脚本 | 命令 | 用途 |
| compile | tsc -p ./ | TypeScript → JavaScript 编译 |
| watch | tsc -watch -p ./ | 文件变化时自动重新编译 |
| vscode:prepublish | npm run compile | vsce publish 前自动执行 |
| package | vsce package | 打包为 .vsix 文件(本地安装用) |
| publish | vsce publish | 发布到 VS Code Marketplace |
入口文件约定
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
// VS Code 加载扩展时调用
// 在这里:注册命令、创建 UI 元素、设置监听器
// context.subscriptions 用于管理资源的生命周期
}
export function deactivate() {
// VS Code 停用扩展时调用(可选)
// 在这里:清理定时器、关闭连接、释放资源
}
开发依赖清单(claude-context-bar)
| 包 | 用途 |
| @types/vscode | VS Code API 的 TypeScript 类型定义 |
| @types/node | Node.js API 的 TypeScript 类型定义 |
| typescript | TypeScript 编译器 |
claude-context-bar 的依赖非常精简——只有 3 个 devDependencies,没有运行时依赖。VS Code 本身提供了 vscode 模块,不需要安装。
StatusBarItem — 状态栏 UI 原语
状态栏条目是 claude-context-bar 使用的核心 UI 组件。通过 vscode.window.createStatusBarItem() 创建。
创建
const item = vscode.window.createStatusBarItem(
vscode.StatusBarAlignment.Right, // 对齐方向
900 // 优先级
);
对齐方向
| 枚举值 | 位置 | 高优先级 = 更靠…… |
StatusBarAlignment.Left | 左侧 | 左 |
StatusBarAlignment.Right | 右侧 | 左(反直觉!) |
核心属性
| 属性 | 类型 | 说明 |
text | string | 显示文本(支持 emoji) |
tooltip | string | MarkdownString | 悬停提示 |
command | string | Command | 点击触发的命令 |
color | string | ThemeColor | 文字颜色 |
backgroundColor | ThemeColor | 背景颜色(推荐用 ThemeColor 而非硬编码) |
alignment | StatusBarAlignment | 只读,对齐方向 |
priority | number | 只读,优先级 |
方法
| 方法 | 说明 |
show() | 显示在状态栏中(创建后默认隐藏) |
hide() | 隐藏但不销毁,可再次 show |
dispose() | 彻底销毁,释放资源。之后不可再使用 |
ThemeColor — 主题自适应颜色
// 不要硬编码颜色——使用 ThemeColor 让 VS Code 根据主题自动解析
item.backgroundColor = new vscode.ThemeColor('statusBarItem.errorBackground');
item.backgroundColor = new vscode.ThemeColor('statusBarItem.warningBackground');
完整生命周期示例
// 1. 创建(默认隐藏)
const item = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 900);
// 2. 配置
item.text = '$(warning) My Project: 72%';
item.tooltip = new vscode.MarkdownString('**Token Usage**: 144K / 200K');
item.command = { command: 'myExt.showDetails', title: 'Show', arguments: ['arg'] };
item.backgroundColor = new vscode.ThemeColor('statusBarItem.warningBackground');
// 3. 显示
item.show();
// 4. 更新 —— 直接修改属性,VS Code 自动重绘
item.text = '$(error) My Project: 85%';
item.backgroundColor = new vscode.ThemeColor('statusBarItem.errorBackground');
// 5. 销毁
item.dispose();
TypeScript 基础(本课程需要的最低知识)
| 语法 | 示例 | 对应到你熟悉的语言 |
| 接口 | interface SessionInfo { name: string; tokens: number } | 类似 struct / data class |
| 类型注解 | let x: number = 5 | 类似 int x = 5 |
| 联合类型 | string | null | 可为 null 的类型 |
| 泛型 | Map<string, number> | 类似 Map<String, Integer> |
| async/await | const result = await fn() | 异步编程(类似协程) |
| 箭头函数 | (x) => x + 1 | 类似 lambda x: x + 1 |
| 可选链 | obj?.prop?.sub | 安全访问可能为 null 的属性 |