将 vscode 传递给外部函数
对于我的主入口点,我的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) {
...
}
在我以为我我以为我的单独文件中将:
- 传递VSCODE实例。当Vscode读取
vscode.commands.registercommand
时,必须有一些魔术,但要从activate(),但在执行命令之前不运行其他命令。 - 我希望我的代码尽可能地使用。这就是为什么我有尴尬的行
type vscode = typeof vscode;
中的type。
问题:
- 是否有更好的方法将事件订阅分为单独的文件,而不是像我一样传递在VSCODE实例中?
- 有没有更好的方法来导入单独文件中的类型?
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:
- 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. - 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:
- Is there a better way to split event subscription out to a separate file than to pass in the vscode instance as I do?
- Is there a better way to import the type in the separate file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
已经添加命令作为贡献命令并将其显示在命令列表中。
但是,如果您的唯一激活事件是在运行命令时然后<代码> 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 ofactivate()
has not. But it hasn't actually been run yet.You can try using
onStartupFinished
as activation event instead.Regarding use of
vscode
ineventListeners.ts
, just useimport * as vscode from 'vscode'
. There's no need to pass it around.