diff --git a/docs/VSCode-Extension-API-Guide.md b/docs/VSCode-Extension-API-Guide.md new file mode 100644 index 0000000..636d3a4 --- /dev/null +++ b/docs/VSCode-Extension-API-Guide.md @@ -0,0 +1,804 @@ +# VS Code Extension API 核心知识点 + +## 目录 +- [1. Extension 生命周期](#1-extension-生命周期) ⭐⭐⭐ +- [2. 激活事件 (Activation Events)](#2-激活事件-activation-events) ⭐⭐ +- [3. 命令系统 (Commands)](#3-命令系统-commands) ⭐⭐ +- [4. Webview API](#4-webview-api) ⭐⭐⭐⭐⭐ **面试重点** +- [5. TreeView 和自定义视图](#5-treeview-和自定义视图) ⭐⭐ +- [6. 文件系统操作](#6-文件系统操作) ⭐⭐⭐ +- [7. 配置和存储](#7-配置和存储) ⭐⭐⭐⭐ **面试重点** +- [8. 消息通知](#8-消息通知) ⭐ +- [9. 语言特性支持](#9-语言特性支持) ⭐ +- [10. 调试和诊断](#10-调试和诊断) ⭐ + +--- + +## 1. Extension 生命周期 ⭐⭐⭐ + +### 1.1 核心函数 🔥必考 + +```typescript +// extension.ts +import * as vscode from 'vscode'; + +// 插件激活时调用(只调用一次) +export function activate(context: vscode.ExtensionContext) { + console.log('Extension is now active!'); + + // 注册命令、视图、事件监听等 + // 使用 context.subscriptions 管理资源 +} + +// 插件停用时调用(清理资源) +export function deactivate() { + console.log('Extension is deactivated'); + // 清理资源、关闭连接等 +} +``` + +### 1.2 ExtensionContext 重要属性 🔥必考 + +```typescript +interface ExtensionContext { + // 插件订阅管理(自动清理) + subscriptions: { dispose(): any }[]; + + // 工作区存储路径 + storageUri: vscode.Uri | undefined; + globalStorageUri: vscode.Uri; + + // 插件路径 + extensionUri: vscode.Uri; + extensionPath: string; + + // 状态存储 + workspaceState: Memento; // 工作区级别 + globalState: Memento; // 全局级别 + secrets: SecretStorage; // 敏感信息存储 + + // 环境变量 + environmentVariableCollection: EnvironmentVariableCollection; +} +``` + +### 1.3 资源管理最佳实践 🔥必考 + +```typescript +export function activate(context: vscode.ExtensionContext) { + // ✅ 推荐:使用 context.subscriptions 自动管理 + context.subscriptions.push( + vscode.commands.registerCommand('extension.command', () => {}) + ); + + // ❌ 不推荐:手动管理容易忘记清理 + const disposable = vscode.commands.registerCommand('extension.command', () => {}); + // 需要在 deactivate 中手动调用 disposable.dispose() +} +``` + +--- + +## 2. 激活事件 (Activation Events) ⭐⭐ + +### 2.1 常用激活事件 📌重要 + +```json +// package.json +{ + "activationEvents": [ + // 启动时激活 + "onStartupFinished", + + // 执行命令时激活 + "onCommand:extension.helloWorld", + + // 打开特定语言文件时激活 + "onLanguage:javascript", + "onLanguage:verilog", + + // 打开特定文件类型时激活 + "onFileSystem:sftp", + + // 打开特定视图时激活 + "onView:myCustomView", + + // 调试时激活 + "onDebug", + + // 打开特定 URI 时激活 + "onUri", + + // Webview 恢复时激活 + "onWebviewPanel:myWebview", + + // 任务执行时激活 + "onTaskType:npm" + ] +} +``` + +### 2.2 延迟激活策略 🔥必考 + +```typescript +// ✅ 推荐:使用 onStartupFinished 延迟激活 +"activationEvents": ["onStartupFinished"] + +// ❌ 不推荐:使用 * 会拖慢启动速度 +"activationEvents": ["*"] +``` + +--- + +## 3. 命令系统 (Commands) + +### 3.1 注册命令 + +```typescript +// 注册简单命令 +const disposable = vscode.commands.registerCommand( + 'extension.helloWorld', + () => { + vscode.window.showInformationMessage('Hello World!'); + } +); +context.subscriptions.push(disposable); + +// 注册带参数的命令 +vscode.commands.registerCommand( + 'extension.openFile', + (filePath: string) => { + vscode.workspace.openTextDocument(filePath).then(doc => { + vscode.window.showTextDocument(doc); + }); + } +); +``` + +### 3.2 执行命令 + +```typescript +// 执行内置命令 +await vscode.commands.executeCommand('workbench.action.files.save'); + +// 执行自定义命令 +await vscode.commands.executeCommand('extension.openFile', '/path/to/file'); + +// 获取所有可用命令 +const commands = await vscode.commands.getCommands(); +``` + +### 3.3 常用内置命令 + +```typescript +// 文件操作 +'workbench.action.files.save' +'workbench.action.files.saveAll' +'workbench.action.closeActiveEditor' + +// 编辑器操作 +'editor.action.formatDocument' +'editor.action.commentLine' +'editor.action.selectAll' + +// 窗口操作 +'workbench.action.toggleSidebarVisibility' +'workbench.action.terminal.new' +'workbench.action.quickOpen' + +// Git 操作 +'git.commit' +'git.push' +'git.pull' +``` + +--- + +## 4. Webview API ⭐⭐⭐⭐⭐ **面试重点** + +### 4.1 创建 Webview Panel 🔥必考 + +```typescript +const panel = vscode.window.createWebviewPanel( + 'myWebview', // viewType(唯一标识) + 'My Webview', // 标题 + vscode.ViewColumn.One, // 显示位置 + { + enableScripts: true, // 启用 JavaScript + retainContextWhenHidden: true, // 隐藏时保留状态 + localResourceRoots: [ // 允许访问的本地资源路径 + vscode.Uri.joinPath(context.extensionUri, 'media') + ] + } +); +``` + +### 4.2 设置 Webview 内容 + +```typescript +panel.webview.html = getWebviewContent(); + +function getWebviewContent() { + return ` + +
+ + +您已成功激活 15 天试用期,让我们开始探索 IC Coder 的强大功能吧!
点击侧边栏的 IC Coder 图标,或使用命令面板搜索 "IC Coder: Open Chat"
+ + + + +IC Coder是一款The Agentic AI Verilog Coding Platform(自主式人工智能 Verilog 编码平台)。我们采用全球顶尖的IC Coder自研芯片设计微调模型,为代码生成提供强大的AI能力支撑。
+ +描述您想要生成的 Verilog 代码或需要帮助的问题,AI 将为您提供专业的解决方案
-使用 "生成 VCD" 命令运行 iverilog 仿真,并通过波形查看器查看仿真结果
-