开发环境
-
Nodejs
-
vscode
-
Yeoman&generator-code
为了方便生成插件项目的基础结构,可以使用 Yeoman 以及 VSCode 官方提供的插件生成器。打开终端,输入以下命令全局安装:
npm install -g yo generator-code
插件初始框架
创建插件
yo code生成器会询问以下问题:
- **选择扩展类型**:例如“New Extension (TypeScript)”或者“New Extension (JavaScript)”。- **扩展名称、描述**:根据提示填写想要的扩展名称和描述信息。- **Git 初始化**:是否初始化 Git 仓库。- **包管理器**:选择使用 npm 或 yarn。生成完成后,会得到一个初步搭建好的项目结构。
理解结构
打开生成的项目,会看到一些重要的文件和目录:
-
package.json这个文件定义了扩展的基本信息、依赖以及 VSCode 的激活事件和命令注册。
例如:
{"name": "my-sample-extension","displayName": "My Sample Extension","description": "A simple VSCode extension.","version": "0.0.1","engines": {"vscode": "^1.60.0"},"activationEvents": ["onCommand:extension.helloWorld"],"main": "./out/extension.js","contributes": {"commands": [{"command": "extension.helloWorld","title": "Hello World"}]},"scripts": {"vscode:prepublish": "npm run compile","compile": "tsc -p ./"},"devDependencies": {"typescript": "^4.0.0","vscode": "^1.1.37","@types/node": "^12.0.0"}} -
src/extension.ts****(或extension.js****)这是扩展的入口文件,当扩展被激活时会运行这里的代码。
例如,一个简单的示例:
import * as vscode from 'vscode';export function activate(context: vscode.ExtensionContext) {console.log('Congratulations, your extension "my-sample-extension" is now active!');let disposable = vscode.commands.registerCommand('extension.helloWorld', () => {vscode.window.showInformationMessage('Hello World from your VSCode extension!');});context.subscriptions.push(disposable);}export function deactivate() {}
运行和调试
- 打开项目后,在 VSCode 的调试面板中,可以看到一个
Launch Extension的配置。 - 按下
F5,VSCode 将启动一个新的扩展开发主机(Extension Development Host)。 - 在新的窗口中,通过命令面板(
Ctrl+Shift+P或Cmd+Shift+P)输入注册的命令,比如输入 “Hello World” 来测试扩展是否正常工作。
发布插件
当开发完成并测试好之后,可以考虑将插件发布到 VSCode Marketplace 供其他人使用:
-
安装
vsce工具帮助打包和发布扩展:npm install -g vsce -
在项目根目录下运行打包命令:
vsce package -
根据 官方文档 的指引,完成发布流程。
具体开发
插件启动
VSCode 扩展通过 package.json 中的 activationEvents 数组来定义何时激活扩展。以下是主要的激活事件类型:
常用激活事件
-
- 在 VSCode 启动时立即激活扩展
- 优点:扩展始终可用
- 缺点:会影响 VSCode 的启动性能,不推荐用于生产环境
- onStartupFinished
- 在 VSCode 完成启动后激活扩展
- 比 * 稍晚激活,但对启动性能影响较小
- onCommand
- 当用户执行特定命令时激活扩展
- 例如:onCommand
.helloWorld
- onLanguage
- 当打开特定语言的文件时激活扩展
- 例如:onLanguage
, onLanguage
- onView
- 当特定视图可见时激活扩展
- 例如:onView
其他激活事件
- onUri
- 当打开特定 URI 时激活扩展
- 例如:onUri:https://my-extension.com
- onWebviewPanel
- 当创建特定类型的 webview 面板时激活扩展
- onCustomEditor
- 当打开自定义编辑器时激活扩展
- onDebug
- 当启动调试会话时激活扩展
- onDebugInitialConfigurations
- 当初始化调试配置时激活扩展
- onDebugResolve
- 当解析特定类型的调试配置时激活扩展
- onFileSystem
- 当访问特定文件系统方案时激活扩展
- 例如:onFileSystem
- onTerminalProfile
- 当创建特定终端配置文件时激活扩展
- onAuthenticationRequest
- 当请求特定身份验证提供程序时激活扩展
- onSearch
- 当执行搜索操作时激活扩展
- onTaskType
- 当执行特定类型的任务时激活扩展
- onNotebook
- 当打开特定类型的笔记本时激活扩展
- onTerminal
- 当创建终端时激活扩展
Development Environment
-
Node.js
-
VS Code
-
Yeoman&generator-code
To conveniently generate the basic structure of a plugin project, you can use Yeoman and the official VS Code extension generator. Open a terminal and run the following commands to install globally:
npm install -g yo generator-code
Initial Plugin Framework
Create Plugin
yo codeThe generator will ask the following questions:
- **Choose Extension Type**:For example “New Extension (TypeScript)” or “New Extension (JavaScript)”.- **Extension name and description**:Fill in the extension name and description as prompted.- **Git initialization**:Whether to initialize a Git repository.- **Package manager**:Choose using npm or yarn.After generation completes, you will have a basic project structure scaffolded.
Understanding the Structure
Open the generated project, you will see some important files and directories:
-
package.jsonThis file defines the extension’s basic information, dependencies, as well as VS Code activation events and command registrations.
For example:
{"name": "my-sample-extension","displayName": "My Sample Extension","description": "A simple VS Code extension.","version": "0.0.1","engines": {"vscode": "^1.60.0"},"activationEvents": ["onCommand:extension.helloWorld"],"main": "./out/extension.js","contributes": {"commands": [{"command": "extension.helloWorld","title": "Hello World"}]},"scripts": {"vscode:prepublish": "npm run compile","compile": "tsc -p ./"},"devDependencies": {"typescript": "^4.0.0","vscode": "^1.1.37","@types/node": "^12.0.0"}} -
src/extension.ts(orextension.js)This is the entry point of the extension; this code runs when the extension is activated.
For example, a simple sample:
import * as vscode from 'vscode';export function activate(context: vscode.ExtensionContext) {console.log('Congratulations, your extension "my-sample-extension" is now active!');let disposable = vscode.commands.registerCommand('extension.helloWorld', () => {vscode.window.showInformationMessage('Hello World from your VS Code extension!');});context.subscriptions.push(disposable);}export function deactivate() {}
Run and Debug
- After opening the project, in VS Code’s Debug panel, you will see a configuration named
Launch Extension. - Press
F5; VS Code will start a new Extension Development Host. - In the new window, via the Command Palette (
Ctrl+Shift+PorCmd+Shift+P), run the registered command, for example typing “Hello World” to test whether the extension works properly.
Publish Extension
When development and testing are complete, you may consider publishing the extension to the VS Code Marketplace for others to use:
-
Install the
vscetool to help package and publish extensions:npm install -g vsce -
In the project root, run the packaging command:
vsce package -
Follow the guidance in the official documentation to complete the publishing process. (Official docs: https://code.visualstudio.com/api/working-with-extensions/publishing-extension)
Specific Development
Extension Activation
VS Code extensions are activated according to the activationEvents array in package.json. Here are the main activation event types:
Common Activation Events
-
- Activate the extension immediately when VS Code starts
- Pros: the extension is always available
- Cons: may affect VS Code startup performance; not recommended for production environments
- onStartupFinished
- Activate the extension after VS Code finishes starting up
- Slightly later than the previous, but with less impact on startup performance
- onCommand
- Activate the extension when the user executes a specific command
- For example: onCommand
.helloWorld
- onLanguage
- Activate the extension when opening files of a specific language
- For example: onLanguage
, onLanguage
- onView
- Activate the extension when a specific view becomes visible
- For example: onView
Other Activation Events
- onUri
- Activate the extension when a specific URI is opened
- For example: onUri:https://my-extension.com
- onWebviewPanel
- Activate the extension when creating a specific type of Webview panel
- onCustomEditor
- Activate the extension when opening a custom editor
- onDebug
- Activate the extension when starting a debugging session
- onDebugInitialConfigurations
- Activate the extension when initializing debug configurations
- onDebugResolve
- Activate the extension when resolving a specific type of debug configuration
- onFileSystem
- Activate the extension when accessing a specific filesystem scheme
- For example: onFileSystem
- onTerminalProfile
- Activate the extension when creating a terminal profile
- onAuthenticationRequest
- Activate the extension when requesting authentication from a specific provider
- onSearch
- Activate the extension when performing a search
- onTaskType
- Activate the extension when executing a task of a specific type
- onNotebook
- Activate the extension when opening a notebook of a specific type
- onTerminal
- Activate the extension when a terminal is created
開発環境
-
Nodejs
-
vscode
-
Yeoman&generator-code
为了方便生成插件项目的基础结构,可以使用 Yeoman 以及 VSCode 官方提供的插件生成器。打开终端,输入以下命令全局安装:
npm install -g yo generator-code
プラグインの初期フレームワーク
プラグインの作成
yo codeジェネレータは以下の質問をします:
- **拡張機能のタイプを選択**:例えば“New Extension (TypeScript)”または“New Extension (JavaScript)”。- **拡張機能の名称と説明**:表示に従って、作成したい拡張機能の名称と説明を入力します。- **Git の初期化**:Git リポジトリを初期化するかどうか。- **パッケージマネージャー**:npm または yarn の使用を選択します。生成が完了すると、初期設定済みのプロジェクト構造が得られます。
構造の理解
生成したプロジェクトを開くと、重要なファイルとディレクトリがいくつか表示されます:
-
package.jsonこのファイルには拡張機能の基本情報、依存関係、そして VSCode のアクティベーションイベントとコマンド登録が定義されています。
例えば:
{"name": "my-sample-extension","displayName": "My Sample Extension","description": "A simple VSCode extension.","version": "0.0.1","engines": {"vscode": "^1.60.0"},"activationEvents": ["onCommand:extension.helloWorld"],"main": "./out/extension.js","contributes": {"commands": [{"command": "extension.helloWorld","title": "Hello World"}]},"scripts": {"vscode:prepublish": "npm run compile","compile": "tsc -p ./"},"devDependencies": {"typescript": "^4.0.0","vscode": "^1.1.37","@types/node": "^12.0.0"}} -
src/extension.ts(またはextension.js)これは拡張機能のエントリポイントです。拡張機能がアクティブ化されると、ここのコードが実行されます。
例えば、簡単な例:
import * as vscode from 'vscode';export function activate(context: vscode.ExtensionContext) {console.log('Congratulations, your extension "my-sample-extension" is now active!');let disposable = vscode.commands.registerCommand('extension.helloWorld', () => {vscode.window.showInformationMessage('Hello World from your VSCode extension!');});context.subscriptions.push(disposable);}export function deactivate() {}
実行とデバッグ
- プロジェクトを開くと、VSCode のデバッグパネルに、
Launch Extensionの設定が表示されます。 F5を押すと、VSCode は新しい拡張機能開発ホスト(Extension Development Host)を起動します。- 新しいウィンドウで、コマンドパレット(
Ctrl+Shift+PまたはCmd+Shift+P)を使って、登録済みのコマンドを入力します。例えば “Hello World” と入力して拡張機能が正しく動作するかをテストします。
プラグインの公開
開発が完了し、テストが済んだら、他の人に使ってもらうために VSCode Marketplace に公開することを検討できます:
-
拡張機能のパッケージ化と公開を支援する
vsceツールをインストールします:npm install -g vsce -
プロジェクトのルートディレクトリでパッケージ化コマンドを実行します:
vsce package -
公式ドキュメント の指示に従い、公開手順を完了します。
具体的な開発
プラグインの起動
VSCode の拡張機能は package.json の activationEvents 配列を通じて、いつ有効化するかを定義します。以下は主なアクティベーションイベントのタイプです:
よく使われるアクティベーションイベント
-
- VSCode の起動時にすぐ拡張機能を有効化
- 利点:拡張機能は常に利用可能
- 欠点:VSCode の起動パフォーマンスに影響を与える可能性があるため、本番環境での使用は推奨されません
- onStartupFinished
- VSCode の起動が完了した後に拡張機能を有効化
- より遅く有効化されますが、起動パフォーマンスへの影響は小さいです
- onCommand
- ユーザーが特定のコマンドを実行したときに拡張機能を有効化
- 例えば:onCommand
.helloWorld
- onLanguage
- 特定の言語のファイルを開いたときに拡張機能を有効化
- 例えば:onLanguage
, onLanguage
- onView
- 特定のビューが表示されているときに拡張機能を有効化
- 例えば:onView
その他のアクティベーションイベント
- onUri
- 特定の URI を開くと拡張機能を有効化
- onWebviewPanel
- 特定のタイプの Webview パネルを作成したときに拡張機能を有効化します
- onCustomEditor
- カスタムエディターを開くと拡張機能を有効化します
- onDebug
- デバッグセッションを開始したときに拡張機能を有効化します
- onDebugInitialConfigurations
- デバッグ構成を初期化するときに拡張機能を有効化します
- onDebugResolve
- 特定のタイプのデバッグ構成を解決するときに拡張機能を有効化します
- onFileSystem
- 特定のファイルシステムスキームにアクセスするときに拡張機能を有効化します
- 例:onFileSystem
- onTerminalProfile
- 特定のターミナル設定を作成するときに拡張機能を有効化します
- onAuthenticationRequest
- 特定の認証プロバイダーを要求するときに拡張機能を有効化します
- onSearch
- 検索を実行するときに拡張機能を有効化します
- onTaskType
- 特定のタイプのタスクを実行するときに拡張機能を有効化します
- onNotebook
- 特定のタイプのノートブックを開くときに拡張機能を有効化します
- onTerminal
- ターミナルを作成するときに拡張機能を有効化します
部分信息可能已经过时









