拡張機能の構造
前回のトピックでは、基本的な拡張機能の実行に成功しました。これは内部でどのように動作するのでしょうか?
Hello World 拡張機能は3つのことを行います。
onCommandアクティベーションイベント:onCommand:helloworld.helloWorldを登録します。これにより、ユーザーがHello Worldコマンドを実行したときに拡張機能がアクティベートされます。注: VS Code 1.74.0 から、
package.jsonのcommandsセクションで宣言されたコマンドは、呼び出されたときに自動的に拡張機能をアクティベートします。activationEventsに明示的なonCommandエントリは必要ありません。contributes.commandsコントリビューションポイント を使用して、コマンドHello Worldをコマンドパレットで利用できるようにし、コマンド IDhelloworld.helloWorldにバインドします。commands.registerCommandVS Code API を使用して、登録されたコマンド IDhelloworld.helloWorldに関数をバインドします。
これらの3つの概念を理解することは、VS Code で拡張機能を記述する上で非常に重要です。
- アクティベーションイベント: 拡張機能がアクティブになるイベント。
- コントリビューションポイント: VS Code を拡張するために、
package.jsonの 拡張機能マニフェスト で行う静的な宣言。 - VS Code API: 拡張機能コードで呼び出すことができる JavaScript API のセット。
一般的に、拡張機能はコントリビューションポイントと VS Code API を組み合わせて、VS Code の機能を拡張します。拡張機能の機能概要 トピックは、拡張機能に適したコントリビューションポイントと VS Code API を見つけるのに役立ちます。
Hello World サンプルのソースコードを詳しく見て、これらの概念がどのように適用されているかを確認しましょう。
拡張機能のファイル構造
.
├── .vscode
│ ├── launch.json // Config for launching and debugging the extension
│ └── tasks.json // Config for build task that compiles TypeScript
├── .gitignore // Ignore build output and node_modules
├── README.md // Readable description of your extension's functionality
├── src
│ └── extension.ts // Extension source code
├── package.json // Extension manifest
├── tsconfig.json // TypeScript configuration
設定ファイルについて詳しく読むことができます。
launch.jsonは VS Code の デバッグ の設定に使用されます。tasks.jsonは VS Code の タスク の定義用です。tsconfig.jsonについては、TypeScript の ハンドブック を参照してください。
ただし、Hello World 拡張機能を理解するために不可欠な package.json と extension.ts に焦点を当てましょう。
拡張機能マニフェスト
各 VS Code 拡張機能には、拡張機能マニフェスト として package.json が必要です。package.json には、scripts や devDependencies などの Node.js フィールドと、publisher、activationEvents、contributes などの VS Code 固有のフィールドが混在しています。すべての VS Code 固有のフィールドの説明は、拡張機能マニフェストリファレンス で確認できます。ここでは、最も重要なフィールドをいくつか紹介します。
nameとpublisher: VS Code は<publisher>.<name>を拡張機能の一意の ID として使用します。たとえば、Hello World サンプルは IDvscode-samples.helloworld-sampleを持っています。VS Code はこの ID を使用して拡張機能を一意に識別します。main: 拡張機能のエントリーポイント。activationEventsとcontributes: アクティベーションイベント と コントリビューションポイント。engines.vscode: これは、拡張機能が依存する VS Code API の最小バージョンを指定します。
{
"name": "helloworld-sample",
"displayName": "helloworld-sample",
"description": "HelloWorld example for VS Code",
"version": "0.0.1",
"publisher": "vscode-samples",
"repository": "https://github.com/microsoft/vscode-extension-samples/helloworld-sample",
"engines": {
"vscode": "^1.51.0"
},
"categories": ["Other"],
"activationEvents": [],
"main": "./out/extension.js",
"contributes": {
"commands": [
{
"command": "helloworld.helloWorld",
"title": "Hello World"
}
]
},
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./"
},
"devDependencies": {
"@types/node": "^8.10.25",
"@types/vscode": "^1.51.0",
"tslint": "^5.16.0",
"typescript": "^3.4.5"
}
}
注: 拡張機能が 1.74 より前の VS Code バージョンを対象としている場合は、
activationEventsにonCommand:helloworld.helloWorldを明示的にリストする必要があります。
拡張機能のエントリーファイル
拡張機能のエントリーファイルは、activate と deactivate の2つの関数をエクスポートします。activate は、登録されたアクティベーションイベントが発生したときに実行されます。deactivate は、拡張機能が無効になる前にクリーンアップする機会を与えます。多くの拡張機能では、明示的なクリーンアップは必要なく、deactivate メソッドは削除できます。ただし、VS Code がシャットダウンするとき、または拡張機能が無効化またはアンインストールされたときに操作を実行する必要がある場合は、このメソッドを使用します。
VS Code 拡張機能 API は、@types/vscode 型定義で宣言されています。vscode 型定義のバージョンは、package.json の engines.vscode フィールドの値によって制御されます。vscode 型は、コード内で IntelliSense、Go to Definition、およびその他の TypeScript 言語機能を提供します。
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "helloworld-sample" is now active!');
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let disposable = vscode.commands.registerCommand('helloworld.helloWorld', () => {
// The code you place here will be executed every time your command is executed
// Display a message box to the user
vscode.window.showInformationMessage('Hello World!');
});
context.subscriptions.push(disposable);
}
// this method is called when your extension is deactivated
export function deactivate() {}