尝试以扩展 VS Code 中的代理模式!

激活事件

激活事件是一组 JSON 声明,您可以在 package.json 扩展清单activationEvents 字段中进行声明。当激活事件发生时,您的扩展将被激活。以下是所有可用激活事件的列表

我们还提供了 package.json 扩展清单中所有字段的参考。

onLanguage

每当打开一个解析为特定语言的文件时,此激活事件都会发出,并且感兴趣的扩展将被激活。

"activationEvents": [
    "onLanguage:python"
]

onLanguage 事件接受一个语言标识符值。

可以在 activationEvents 数组中通过单独的 onLanguage 条目声明多种语言。

"activationEvents": [
    "onLanguage:json",
    "onLanguage:markdown",
    "onLanguage:typescript"
]

注意:从 VS Code 1.74.0 开始,您的扩展贡献的语言不需要相应的 onLanguage 激活事件声明即可激活您的扩展。

此外,如果您的扩展需要在任何语言使用之前激活,您可以使用通用的 onLanguage 激活事件来确保这一点

"activationEvents": [
    "onLanguage"
]

注意:最佳实践是仅在用户需要您的扩展时才激活。如果您的扩展适用于一部分语言,那么列出该子集比在所有语言上都激活对用户更好。

onCommand

每当调用命令时,此激活事件都会发出,并且感兴趣的扩展将被激活

"activationEvents": [
    "onCommand:extension.sayHello"
]

注意:从 VS Code 1.74.0 开始,您的扩展贡献的命令不需要相应的 onCommand 激活事件声明即可激活您的扩展。

onDebug

此激活事件在调试会话开始之前发出,并且感兴趣的扩展将被激活

"activationEvents": [
    "onDebug"
]

这些是四个更细粒度的 onDebug 激活事件

onDebugAdapterProtocolTracker

每当特定类型的调试会话即将启动并且可能需要调试协议跟踪器时,都会发出 onDebugAdapterProtocolTracker

onDebugDynamicConfigurations

此激活事件在调用 DebugConfigurationProviderprovideDebugConfigurations 方法之前发出,以便在用户请求动态调试配置时提供这些配置,例如通过 UI 上的“选择并开始调试”命令。

此激活事件的存在用作扩展贡献动态调试配置的信号。

onDebugInitialConfigurations

此激活事件在调用 DebugConfigurationProviderprovideDebugConfigurations 方法之前发出,以便提供初始调试配置,例如每当需要创建 launch.json 时。

onDebugResolve

onDebugResolve:type 在调用指定类型的 DebugConfigurationProviderresolveDebugConfiguration 方法之前触发。

经验法则: 如果调试扩展的激活是轻量级的,请使用 onDebug。如果是重量级的,请根据 DebugConfigurationProvider 是否实现相应的 provideDebugConfigurations 和/或 resolveDebugConfiguration 方法使用 onDebugInitialConfigurations 和/或 onDebugResolve。有关这些方法的更多详细信息,请参阅使用 DebugConfigurationProvider

workspaceContains

workspaceContains:path 在打开文件夹且该文件夹包含至少一个匹配全局模式的文件时发出,并且感兴趣的扩展将被激活。

"activationEvents": [
    "workspaceContains:**/.editorconfig"
]

onFileSystem

onFileSystem:scheme 在读取特定方案的文件或文件夹时发出,并且感兴趣的扩展将被激活。这通常是 file 方案,但使用自定义文件系统提供程序时,会引入更多方案,例如 ftpssh

"activationEvents": [
    "onFileSystem:sftp"
]

onView

此激活事件在 VS Code 侧边栏中展开指定 ID 的视图时发出,并且感兴趣的扩展将被激活。内置视图不发出激活事件。

当 ID 为 nodeDependencies 的视图可见时,以下激活事件将触发

"activationEvents": [
    "onView:nodeDependencies"
]

注意:从 VS Code 1.74.0 开始,您的扩展贡献的视图不需要相应的 onView 激活事件声明即可激活您的扩展。

onUri

每当打开该扩展的系统范围 Uri 时,此激活事件都会发出,并且感兴趣的扩展将被激活。Uri 方案固定为 vscodevscode-insiders。Uri 权限必须是扩展的标识符。Uri 的其余部分是任意的。

"activationEvents": [
    "onUri"
]

如果 vscode.git 扩展将 onUri 定义为激活事件,则当以下任何 Uri 打开时,它将被激活

  • vscode://vscode.git/init
  • vscode://vscode.git/clone?url=https%3A%2F%2Fgithub.com%2FMicrosoft%2Fvscode-vsce.git
  • vscode-insiders://vscode.git/init(适用于 VS Code Insiders)

onWebviewPanel

当 VS Code 需要恢复具有匹配 viewTypewebview 时,此激活事件会发出,并且感兴趣的扩展将被激活。

例如,下面的 onWebviewPanel 声明

"activationEvents": [
    "onWebviewPanel:catCoding"
]

将导致当 VS Code 需要恢复 viewType 为 catCoding 的 webview 时激活扩展。viewType 在调用 window.createWebviewPanel 中设置,您将需要另一个激活事件(例如,onCommand)来最初激活您的扩展并创建 webview。

onCustomEditor

当 VS Code 需要创建具有匹配 viewType自定义编辑器时,此激活事件会发出,并且感兴趣的扩展将被激活。

例如,下面的 onCustomEditor 声明

"activationEvents": [
    "onCustomEditor:catCustoms.pawDraw"
]

将导致当 VS Code 需要恢复 viewType 为 catCustoms.pawDraw 的自定义编辑器时激活扩展。viewType 在 customEditors 贡献点中设置,并绑定到带有 registerCustomEditorProvider 的提供程序。

注意:从 VS Code 1.74.0 开始,您的扩展贡献的自定义编辑器不需要相应的 onCustomEditor 激活事件声明即可激活您的扩展。

onAuthenticationRequest

当扩展请求具有匹配 providerId 的身份验证会话(通过 authentication.getSession() API)时,此激活事件会发出,并且感兴趣的扩展将被激活。

例如,下面的 onAuthenticationRequest 声明

"activationEvents": [
    "onAuthenticationRequest:github"
]

将导致当 VS Code 需要检索类型为 githubAuthenticationSession 时激活扩展。

注意:从 VS Code 1.74.0 开始,您的扩展贡献的身份验证提供程序不需要相应的 onAuthenticationRequest 激活事件声明即可激活您的扩展。

onStartupFinished

此激活事件在 VS Code 启动一段时间后发出,并且感兴趣的扩展将被激活。这类似于 * 激活事件,但它不会减慢 VS Code 的启动速度。目前,此事件在所有 * 激活的扩展完成激活后发出。

"activationEvents": [
    "onStartupFinished"
]

onTaskType

每当需要列出或解析特定类型的任务时,都会发出 onTaskType:type

"activationEvents": [
    "onTaskType":"npm"
]

注意:从 VS Code 1.76.0 开始,您的扩展贡献的任务不需要相应的 onTaskType 激活事件声明即可激活您的扩展。

onEditSession

当使用给定方案访问编辑会话时,会发出 onEditSession:scheme

"activationEvents": [
    "onEditSession:file"
]

onSearch

当在具有给定方案的文件夹中开始搜索时,会发出 onSearch:scheme

"activationEvents": [
    "onSearch:file"
]

onOpenExternalUri

当外部 URI(例如 http 或 https 链接)被打开时发出的激活事件。

"activationEvents": [
    "onOpenExternalUri"
]

onNotebook

当指定的笔记本文档类型打开时,会发出 onNotebook:type

"activationEvents": [
    "onNotebook:jupyter-notebook",
    "onNotebook:interactive"
]

onRenderer

当使用笔记本输出渲染器时,会发出 onRenderer:id

"activationEvents": [
    "onRenderer:ms-toolsai.jupyter-renderers"
]

onTerminal

当打开具有给定 shell 类型的特定终端时,会发出 onTerminal:shellType

"activationEvents": [
  "onTerminal:bash"
]

onTerminalProfile

当启动特定终端配置文件时,会发出 onTerminalProfile:id

"activationEvents": [
    "onTerminalProfile:terminalTest.terminal-profile"
]

onTerminalShellIntegration

当具有给定 shell 类型的终端激活 shell 集成时,会发出 onTerminalShellIntegration:shellType

"activationEvents": [
    "onTerminalShellIntegration:bash"
]

onWalkthrough

当打开指定的演练时,会发出 onWalkthrough:id

"activationEvents": [
    "onWalkthrough:nodejsWelcome"
]

onIssueReporterOpened

当问题报告器打开时(例如,通过使用帮助:报告问题),此激活事件会发出。

"activationEvents": [
    "onIssueReporterOpened"
]

onChatParticipant

当调用指定的聊天参与者时发出的激活事件。

"activationEvents": [
    "onChatParticipant:my-chat-participant"
]

onLanguageModelTool

当调用指定的语言模型工具时发出的激活事件。

"activationEvents": [
    "onChatParticipant:my-language-model-tool"
]

启动

每当 VS Code 启动时,都会发出 * 激活事件,并且感兴趣的扩展将被激活。

注意: 为了确保出色的用户体验,请仅在您的用例中没有其他激活事件组合有效时才在扩展中使用此激活事件。

"activationEvents": [
    "*"
]

注意: 一个扩展可以侦听多个激活事件,这优于侦听 "*"

注意: 扩展必须从其主模块导出 activate() 函数,并且当任何指定的激活事件发出时,它只会被 VS Code 调用一次。此外,扩展应该从其主模块导出 deactivate() 函数,以便在 VS Code 关闭时执行清理任务。如果清理过程是异步的,扩展必须deactivate() 返回一个 Promise。如果清理同步运行,扩展可以从 deactivate() 返回 undefined