延伸解剖

在上一个主题中,您能够运行一个基本的扩展。它在幕后是如何工作的?

Hello World扩展做了 3 件事:

理解这三个概念对于在 VS Code 中编写扩展至关重要:

一般来说,您的扩展将使用贡献点和 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手册

不过,让我们重点关注package.jsonextension.ts,它们对于理解扩展至关重要Hello World

扩展清单

每个 VS Code 扩展都必须有一个扩展清单package.json作为其扩展清单。包含package.jsonNode.js 字段(例如scripts和 )devDependencies和 VS Code 特定字段(例如publisheractivationEvents和 )的混合contributes您可以在扩展清单参考中找到所有 VS Code 特定字段的描述。以下是一些最重要的字段:

  • namepublisher:VS Code 用作<publisher>.<name>扩展的唯一 ID。例如,Hello World 示例的 ID 为vscode-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 版本,则必须onCommand:helloworld.helloWorldactivationEvents.

扩展入口文件

扩展入口文件导出两个函数,activate以及deactivate. 当您注册的激活事件activate发生时执行。让您有机会在扩展程序停用之前进行清理。对于许多扩展,可能不需要显式清理,并且可以删除该方法。但是,如果扩展需要在 VS Code 关闭或扩展被禁用或卸载时执行操作,则可以使用此方法。deactivatedeactivate

VS Code 扩展 API 在@types/vscode类型定义中声明。类型定义的版本由中字段vscode的值控制。这些类型为您在代码中提供 IntelliSense、转到定义和其他 TypeScript 语言功能。engines.vscodepackage.jsonvscode

// 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() {}