6

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.

1 Answer 1

8

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

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;
  }
});
3
  • Yeah that's ultimately what I ended up doing.
    – arb
    Mar 27, 2017 at 18:04
  • @alefragnani: can you explain where we should add above code in package.json?
    – Amir
    Sep 7, 2018 at 14:03
  • @Amir You declare commands in contributes\commands section of package.json. You can get more information here: code.visualstudio.com/docs/extensionAPI/…. Hope this helps. Sep 9, 2018 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.