vscode扩展QuickPick ActiveItems不工作

发布于 2025-01-31 10:00:08 字数 437 浏览 5 评论 0原文

const q = vscode.window.createQuickPick();
q.items = [{ label: "1" }, { label: "2" }];
q.activeItems = [{ label: "2" }];
q.show();

根据我对VSCODE扩展API文档的了解。

/**
  * Active items. This can be read and updated by the extension.
  */
activeItems: readonly T[];

当QuickPick显示时,它应该活跃“ 2”项目。 但是我发现ActiveItems将在show()方法之后重置; 我不知道为什么并且在互联网中找不到同样的问题,

const q = vscode.window.createQuickPick();
q.items = [{ label: "1" }, { label: "2" }];
q.activeItems = [{ label: "2" }];
q.show();

Based on my understand of vscode extension api doc.

/**
  * Active items. This can be read and updated by the extension.
  */
activeItems: readonly T[];

it should active "2" item when quickpick is showing.
But I found activeItems will reset after show() method;
I dont know why and cant find any same issue in the internet,

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

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

发布评论

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

评论(2

美人骨 2025-02-07 10:00:08

看来A bug在这里影响行为

我的解决方案是设置活动性项目< em> 调用表演方法。似乎代码确实参考了比较。

所以

const q = vscode.window.createQuickPick();
const selectedItem = { label: "2" };
q.items = [{ label: "1" }, selectedItem];
q.show();
q.activeItems = [selectedItem];

it seems that a bug is impacting behavior here

my solution was to set the active items after calling the show method. Also it seems the code does reference comparisons.

So

const q = vscode.window.createQuickPick();
const selectedItem = { label: "2" };
q.items = [{ label: "1" }, selectedItem];
q.show();
q.activeItems = [selectedItem];
隔岸观火 2025-02-07 10:00:08

我遇到了同样的问题,我认为我发现了潜在的问题。看起来基于项目的成员资格的基础VSCODE逻辑将使您的分配过滤到ActiveItems,看起来它可能是身份检查而不是结构性检查。

将我的代码更改为类似:

quickPick.items = ...
quickPick.activeItems = quickPick.items.filter((item) => ...);

似乎获得了预期的行为(允许为QuickPick设置初始活动项目)。

因此,在您的情况下,您可以尝试:

const q = vscode.window.createQuickPick();
q.items = [{ label: "1" }, { label: "2" }];
q.activeItems = [q.items[1]];
// Or e.g.:
// q.activeItems = q.items.filter((item) => item.label === "2");
q.show();

我知道这是一年后,但这可能会帮助其他偶然发现这个问题的人:)

I ran into this same issue and I think I've found the underlying problem. It looks like the underlying VSCode logic will filter your assignment to activeItems based on membership in items, and it looks like it may be an identity check rather than a structural check.

Changing my code to be something like:

quickPick.items = ...
quickPick.activeItems = quickPick.items.filter((item) => ...);

seemed to get the expected behavior (allowing an initial active item to be set for the QuickPick).

So in your case you could try:

const q = vscode.window.createQuickPick();
q.items = [{ label: "1" }, { label: "2" }];
q.activeItems = [q.items[1]];
// Or e.g.:
// q.activeItems = q.items.filter((item) => item.label === "2");
q.show();

I know it's a year later but maybe this will help someone else who stumbles upon this issue :)

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