将 vscode 传递给外部函数

Passing vscode to external functions

对于我的 activate() 函数所在的主要入口点:

extension.ts

import * as vscode from "vscode";
import { subscribe } from "./eventListeners.ts";

export function activate(context: vscode.ExtensionContext) {
  vscode.commands.registerCommand("mycommand.activate", () => {});

  subscribe(vscode);

  context.subscriptions.push(t);
}

我已将活动订阅拆分成一个单独的文件:

eventListeners.ts

import * as vscode from "vscode";

type VSCode = typeof vscode;

export function subscribe(vscode: VSCode) {
    ...
}

在单独的文件中我想我会:

  1. 传入vscode实例。 vscode 从 activate() 中读取 vscode.commands.registerCommand 但在执行命令之前不会 运行 其他命令,这一定有一些神奇的事情发生。
  2. 我希望我的代码尽可能的类型安全。这就是为什么我在那里有笨拙的行 type VSCode = typeof vscode;

问题:

  1. 有没有比像我一样传入 vscode 实例更好的方法将事件订阅拆分到一个单独的文件中?
  2. 有没有更好的方法在单独的文件中导入类型?

已经 adding a command as a contribution 使 VSCode 知道该命令并将其显示在命令列表中。

但是如果您唯一的 activation event is when the command is run 那么 activate() 不是 运行,直到您尝试使用该命令。所以命令实际执行的操作直到第一个 运行.

才被注册

我认为以上内容让您感到困惑。您看到命令,所以您认为 vscode.commands.registerCommand() 已经是 运行,因此想知道为什么 activate() 的其他部分没有。但实际上还没有运行。

您可以尝试使用 onStartupFinished 作为激活事件。

关于在eventListeners.ts中使用vscode,只需使用import * as vscode from 'vscode'。没有必要传递它。