将 vscode 传递给外部函数

发布于 2025-01-19 22:53:02 字数 929 浏览 4 评论 0原文

对于我的主入口点,我的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);
}

我已经将事件订阅分为saplare文件:

eventlisteners.ts

import * as vscode from "vscode";

type VSCode = typeof vscode;

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

在我以为我我以为我的单独文件中将:

  1. 传递VSCODE实例。当Vscode读取vscode.commands.registercommand时,必须有一些魔术,但要从activate(),但在执行命令之前不运行其他命令。
  2. 我希望我的代码尽可能地使用。这就是为什么我有尴尬的行type vscode = typeof vscode;中的type。

问题:

  1. 是否有更好的方法将事件订阅分为单独的文件,而不是像我一样传递在VSCODE实例中?
  2. 有没有更好的方法来导入单独文件中的类型?

For my main entrypoint where my activate() function resides:

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);
}

I have split out event subscription into a separare file:

eventListeners.ts

import * as vscode from "vscode";

type VSCode = typeof vscode;

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

In the separate file I thought I would:

  1. Pass in the vscode instance. There has to be some magic going on as vscode reads vscode.commands.registerCommand from activate() but does not run the other commands until the command is executed.
  2. I want my code to be as typesafe as possible. That's why I have the awkward line type VSCode = typeof vscode; in there.

Questions:

  1. Is there a better way to split event subscription out to a separate file than to pass in the vscode instance as I do?
  2. Is there a better way to import the type in the separate file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如梦 2025-01-26 22:53:02

已经添加命令作为贡献命令并将其显示在命令列表中。

但是,如果您的唯一激活事件是在运行命令时然后<代码> activate()直到尝试使用命令才能运行。因此,该命令实际上要等到第一次运行才会注册。

我认为以上是使您感到困惑的原因。您会看到该命令,因此您认为已运行vscode.commands.registercommand(),因此想知道为什么activate()>> avice>的其他部分没有。但这实际上还没有运行。

您可以尝试使用

关于在vscode中使用EventListeners.ts,只需将import *用作'vscode'的vscode即可。无需传递它。

Already adding a command as a contribution makes VSCode know about the command and display it in the command list.

But if your sole activation event is when the command is run then activate() isn't run until you try to use the command. So what the command actually does is not registered until the first run.

I think that the above is what confuses you. You see the command, so you think that vscode.commands.registerCommand() has been run, and therefore wonders why the other parts of activate() has not. But it hasn't actually been run yet.

You can try using onStartupFinished as activation event instead.

Regarding use of vscode in eventListeners.ts, just use import * as vscode from 'vscode'. There's no need to pass it around.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文