Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is an extension able to contribute menu items dynamically? #15235

Closed
supnate opened this issue Nov 9, 2016 · 7 comments
Closed

Is an extension able to contribute menu items dynamically? #15235

supnate opened this issue Nov 9, 2016 · 7 comments
Assignees
Labels
*question Issue represents a question, should be posted to StackOverflow (VS Code)

Comments

@supnate
Copy link

supnate commented Nov 9, 2016

I want to create an extension which shows custom explorer/context menus for some special project folder structure. Is that possible?

For Sublime plugin I could define is_visible method on a command to decide if the menu item is shown. But I didn't see equivalent mechanism in vscode. The similar one is when clause but it's not powerful enough to do it.

@aeschli aeschli added the *question Issue represents a question, should be posted to StackOverflow (VS Code) label Nov 9, 2016
@jrieken
Copy link
Member

jrieken commented Nov 9, 2016

The similar one is when clause but it's not powerful enough to do it.

That is correct and I would like to know why it's not powerful enough?

@jrieken jrieken closed this as completed Nov 9, 2016
@supnate
Copy link
Author

supnate commented Nov 10, 2016

Hi @jrieken ,
Thanks for the prompt reply, but I didn't get the notification so I didn't reply before you close it:-(
Maybe my question is unclear. Below is what a when property I want:

{
  command: 'myExtension.renameComponent',
  when: (resource) => {
    return isInSpecialProject(resource) && isComponent(resource);
  }
  //...
}

That is, the menus my extension contributes first apply to a project with a special folder structure. Then according to different resource types, show different menus.

Hope this could clarify.

@jrieken
Copy link
Member

jrieken commented Nov 10, 2016

The logic is like: A command is a function with name/id, like myExtension.renameComponent. A menu item or keybinding is a UX gesture to invoke a command. Because the former already is user editable and the latter will be the when-part is like a recommended configuration. Despite the when-part, your command must always be prepared to be run. That means you must structure it into two conditions

  1. in code a hard precondition check without which the command will fail to run
  2. when is a UX configuration when a the developer things is makes sense to show a command

That's the theory. In practice it is often hard to formulate the when clause, because context keys (that part of when to which you compare) aren't nicely documented and because it's little known that extension can actually set their own context keys.

For resources we have the following context keys: resourceScheme, resourceFilename, and resourceLangId. That might be already enough, else you can set your own context keys as described here

@supnate
Copy link
Author

supnate commented Nov 10, 2016

Thank you very much for the detailed introduction.

setContext seems to be very useful for what I want. After going through #10471 I still have below questions:

  1. Could I define context values for different resource (the target file) ? Such as resourceType, it could be 'component', 'page', 'action' in my case.
  2. In which event could I run the function to define the context value? It seems to should be run when I right click the file in explorer.

However I will look at the source code about the mechanism to define resourceScheme, resourceLangId etc. It may help me to understand the logic behind.

@jrieken
Copy link
Member

jrieken commented Nov 10, 2016

The problem you will have is that the explorer isn't represented in the API, e.g. there no explorer selection events etc that would enable you to update those context keys. I might be able out by adding more context keys in the spirit of resourceXYZ if your requests aren't über-specific.

@supnate
Copy link
Author

supnate commented Nov 10, 2016

It's indeed very special even need to scan the content of the file, haha. Thank you anyway.

However, could you make this a feature request? To be simple, you can allow the when property to accept a command, pass the context resource to it and then determine whether to show the menu by the return value of the command.

BTW, I created a Sublime plugin for my purpose by using is_visible property of the menu command.

@jrieken
Copy link
Member

jrieken commented Nov 10, 2016

To be simple, you can allow the when property to accept a command, pass the context resource

The problem is that it must work other way around. A context must update by itself without being asked. Otherwise we would have to wait for those updates/evaluations before handling a keyboard event or before opening a menu. That's what makes it so hard

@vscodebot vscodebot bot locked and limited conversation to collaborators Nov 18, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
*question Issue represents a question, should be posted to StackOverflow (VS Code)
Projects
None yet
Development

No branches or pull requests

3 participants