在 VSCode 扩展中构建动态菜单 [英] Build Dynamic Menu in VSCode Extension

查看:33
本文介绍了在 VSCode 扩展中构建动态菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 VSC 插件,在激活时,我想进行 XHR 调用,然后使用该 XHR 的结果填充菜单.似乎没有办法将菜单动态添加到状态栏或动态项目添加到项目列表中.

I'm authoring a VSC plugin that on activation, I would like to make an XHR call and then populate a menu with the results of that XHR. It doesn't seem like there is a way to dynamically add menus to the status bar or dynamic items to the list of items.

推荐答案

你不能那样做.由于其声明式方法,所有命令都必须在 package.json 中预定义.

You can't do that. All commands must be pre-defined in package.json because of its declarative approach.

但是,您可以模仿这种行为.为此,您必须使用 vscode.window.showQuickPick API,添加从 XHR 调用中收到的项目.这种动态方法的一个很好的例子是MDTools 扩展.

You can however, mimic this behavior. To do this you must use the vscode.window.showQuickPick API, adding the items that you received from your XHR call. A good example of this dynamic approach is MDTools extension.

另外,一个示例代码供您开始:

Also, a sample code for you to start:

let items: vscode.QuickPickItem[] = [];
  
for (let index = 0; index < yourHXRResultItems.length; index++) {
  let item = yourHXRResultItems[index];
  items.push({ 
    label: item.name, 
    description: item.moreDetailedInfo});
}

vscode.window.showQuickPick(items).then(selection => {
  // the user canceled the selection
  if (!selection) {
    return;
  }

  // the user selected some item. You could use `selection.name` too
  switch (selection.description) {
    case "onItem": 
      doSomething();
      break;
    case "anotherItem": 
      doSomethingElse();
      break;
    //.....
    default:
      break;
  }
});

这篇关于在 VSCode 扩展中构建动态菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆