在 VSCode 扩展中构建动态菜单

arb*_*arb 2 visual-studio-code vscode-extensions

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

ale*_*ani 5

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

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

此外,还有一个示例代码供您开始:

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;
  }
});
Run Code Online (Sandbox Code Playgroud)