本站点文档内容均翻译自code.visualstudio.com,仅供个人学习,如有差异请以官网为准。

扩展解剖学

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

这个Hello World扩展程序做三件事:

  • 注册在命令 激活事件onCommand: helloworld.helloWorld因此,当用户运行时,扩展将被激活Hello World命令。

    注意:VS Code 1.74.0 开始,命令在 命令部分package.json自动激活扩展,而不需要明确的在命令条目在激活事件输入:.

  • 使用贡献命令 贡献点 以执行命令 Hello World在命令面板中可用,并将其绑定到一个命令 IDhelloworld.helloWorld输入:.
  • 使用命令注册命令 VS Code API 绑定一个函数到已注册的命令ID helloworld.helloWorld输入:.

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

一般来说,您的扩展将使用贡献点和 VS Code API 的组合来扩展 VS Code 的功能。 扩展功能概述 主题帮助您找到适合您扩展的贡献点和 VS Code API。

让我们更仔细地看看Hello World查看示例的源代码,了解这些概念如何应用于其中。

扩展文件结构

.
├── .vscode
│   ├── launch.json     // 启动和调试扩展的配置
│   └── tasks.json      // 编译TypeScript的构建任务配置
├── .gitignore          // 忽略构建输出和node_modules
├── README.md           // 描述扩展功能的可读文档
├── src
│   └── extension.ts    // 扩展源代码
├── package.json        // 扩展清单
├── tsconfig.json       // TypeScript配置

您可以阅读更多关于配置文件的信息:

  • launch.json 用于配置 VS Code 调试
  • 任务.json 定义 VS Code 任务
  • tsconfig.json 参考 TypeScript 手册

然而,让我们专注于package.jsonextension.ts,这些是理解的必要条件Hello World扩展。

扩展清单

每个 VS Code 扩展必须有一个package.json作为其扩展清单package.json包含混合的Node.js字段,例如脚本开发依赖项和 VS Code 特定字段,例如出版商激活事件贡献。您可以在扩展清单参考中找到所有 VS Code 特定字段的描述。以下是一些最重要的字段:

  • 名字出版商: VS Code 使用.作为扩展的唯一ID。例如,Hello World示例的ID是vscode-samples.helloworld-sampleVS Code 使用 ID 来唯一标识你的扩展。
  • 主要扩展入口点。
  • 激活事件贡献: 激活事件贡献点数.
  • 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"
  }
}

注意:如果您的扩展针对的是 VS Code 1.74 之前的版本,则必须明确列出onCommand: helloworld.helloWorld激活事件输入:.

扩展条目文件

扩展入口文件导出两个函数,激活停用激活 在您的注册 激活事件 发生时执行。 停用在您的扩展被停用之前,给您一个清理的机会。对于许多扩展,可能不需要明确的清理,而停用该方法可以被移除。然而,如果一个扩展在 VS Code 关闭时或扩展被禁用或卸载时需要执行一个操作,那么这个方法就是用来做这件事的。

VS Code 扩展 API 定义在 @types/vscode 类型定义中。 Visual Studio Code类型定义由该值控制engines.vscode字段在package.jsonVisual Studio Code类型在你的代码中提供IntelliSense、转到定义和其他TypeScript语言特性。

// 模块 'vscode' 包含 VS Code 可扩展性 API
// 导入模块并在您的代码中使用别名 vscode 引用它
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!');
  });

  上下文.订阅.推送(一次性);
}

// 当你的扩展被停用时,此方法会被调用
导出 函数 停用() {}